FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hackmd-cli/src/utils.ts at develop · hackmdio/hackmd-cli · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
hackmdio
/
hackmd-cli
Public
Notifications
You must be signed in to change notification settings
Fork
12
Star
166
Code
Issues
19
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
hackmd-cli
/
src
/
utils.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (50 loc) · 1.75 KB
Breadcrumbs
hackmd-cli
/
src
/
utils.ts
Copy path
File metadata and controls
64 lines (50 loc) · 1.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import
type
{
ApiFolderOrder
}
from
'@hackmd/api'
import
fs
from
'fs-extra'
import
{
homedir
,
tmpdir
}
from
'node:os'
import
path
from
'node:path'
export
function
getConfigFilePath
(
)
{
const
configDir
=
process
.
env
.
HMD_CLI_CONFIG_DIR
?
process
.
env
.
HMD_CLI_CONFIG_DIR
||
''
:
path
.
join
(
homedir
(
)
,
'.hackmd'
)
const
configPath
=
path
.
join
(
configDir
,
'config.json'
)
if
(
!
fs
.
existsSync
(
configDir
)
)
{
fs
.
ensureFileSync
(
configPath
)
fs
.
writeFileSync
(
configPath
,
JSON
.
stringify
(
{
}
)
)
}
return
configPath
}
export
function
setAccessTokenConfig
(
token
:
string
)
{
const
configFilePath
=
getConfigFilePath
(
)
const
configContent
=
fs
.
readFileSync
(
configFilePath
,
'utf8'
)
const
newConfigFile
=
JSON
.
parse
(
configContent
)
newConfigFile
.
accessToken
=
token
fs
.
writeFile
(
configFilePath
,
JSON
.
stringify
(
newConfigFile
,
null
,
2
)
,
err
=>
{
if
(
err
)
{
throw
err
}
}
)
}
export
function
safeStdinRead
(
)
{
let
result
try
{
result
=
fs
.
readFileSync
(
process
.
stdin
.
fd
)
.
toString
(
)
}
catch
{
}
return
result
}
// generate temporary markdown file in /tmp directory
export
function
temporaryMD
(
)
{
const
tmpDir
=
tmpdir
(
)
const
filename
=
`temp_
${
Math
.
random
(
)
.
toString
(
36
)
.
slice
(
2
)
}
.md`
const
filePath
=
path
.
join
(
tmpDir
,
filename
)
return
filePath
}
export
function
parseFolderOrder
(
order
:
string
)
:
ApiFolderOrder
{
const
parsed
=
JSON
.
parse
(
order
)
if
(
!
parsed
||
typeof
parsed
!==
'object'
||
Array
.
isArray
(
parsed
)
)
{
throw
new
Error
(
'Folder order must be a JSON object'
)
}
for
(
const
[
key
,
value
]
of
Object
.
entries
(
parsed
)
)
{
if
(
!
Array
.
isArray
(
value
)
||
!
value
.
every
(
item
=>
typeof
item
===
'string'
)
)
{
throw
new
Error
(
`Folder order entry "
${
key
}
" must be an array of folder ids`
)
}
}
return
parsed
as
ApiFolderOrder
}
Back
|
FazBrowse Home
|
New Git URL