1
1
import * as fs from 'fs-extra'
2
+ import * as path from 'path'
3
+
2
4
import defaults from 'lodash/defaults'
3
5
import { homedir } from 'os'
4
- import * as path from 'path'
5
6
6
7
let configDir
7
8
if ( process . env . HMD_CLI_CONFIG_DIR ) {
@@ -11,35 +12,114 @@ if (process.env.HMD_CLI_CONFIG_DIR) {
11
12
}
12
13
13
14
const configFilePath = path . join ( configDir , 'config.json' )
14
- const defaultCookiePath = path . join ( homedir ( ) , '.codimd/ cookies.json' )
15
+ const defaultCookiePath = path . join ( homedir ( ) , '.codimd' , ' cookies.json')
15
16
16
17
const defaultConfig = {
17
- cookiePath : defaultCookiePath
18
+ cookiePath : defaultCookiePath ,
19
+ serverUrl : ''
18
20
}
19
21
20
22
const envConfig = {
21
23
cookiePath : process . env . CMD_CLI_COOKIE_PATH ,
22
24
serverUrl : process . env . CMD_CLI_SERVER_URL
23
25
}
24
26
25
- const hasExistingConfig = fs . existsSync ( configFilePath )
26
- const readConfig = hasExistingConfig ? JSON . parse ( fs . readFileSync ( configFilePath , 'utf-8' ) ) : { }
27
+ // look for a readable config file; we can merge it with the env.
28
+ let hasExistingConfigFile = false
29
+ try {
30
+ fs . accessSync ( configFilePath , fs . constants . R_OK )
31
+ hasExistingConfigFile = true
32
+ } catch ( err ) {
33
+ // if we don't have a serverUrl from the environment, we don't have one at all
34
+ // and have to abort
35
+ if ( ! envConfig . serverUrl ) {
36
+ throw new Error ( `
37
+
38
+ Configuration file at ${ configFilePath } not readable. Encountered exception:
39
+
40
+ ${ err }
41
+
42
+ ` )
43
+ }
44
+ }
45
+
46
+ let readConfig = { }
47
+ if ( hasExistingConfigFile ) {
48
+ try {
49
+ readConfig = JSON . parse ( fs . readFileSync ( configFilePath , 'utf-8' ) )
50
+ } catch ( err ) {
51
+ throw new Error ( `
52
+
53
+ Could not read JSON config file at ${ configFilePath } . Encountered exception:
27
54
28
- const defaultServerUrl = 'PLEASE FILL THE SERVER URL'
29
- if ( ! hasExistingConfig ) {
30
- fs . writeFileSync ( configFilePath , JSON . stringify ( { serverUrl : defaultServerUrl } , null , 2 ) , 'utf-8' )
55
+ ${ err }
56
+
57
+ ` )
58
+ }
31
59
}
32
60
33
- const config = defaults ( readConfig , envConfig , defaultConfig )
61
+ // prefer environment config over file config
62
+ const config = defaults ( envConfig , readConfig , defaultConfig )
34
63
35
- if ( ! config . serverUrl || config . serverUrl === defaultServerUrl ) {
64
+ if ( ! config . serverUrl ) {
36
65
throw new Error ( `
37
66
38
- Please specify CodiMD server url either in ${ configFilePath } or by environment varaible.
67
+ Please specify CodiMD server URL either in ${ configFilePath } or by environment variable CMD_CLI_SERVER_URL.
68
+
69
+ You can learn how to configure codimd-cli at https://github.com/hackmdio/codimd-cli
70
+
71
+ ` )
72
+ }
73
+
74
+ const cookieDirPath = path . dirname ( config . cookiePath )
75
+ try {
76
+ fs . mkdirSync ( cookieDirPath )
77
+ } catch ( err ) {
78
+ if ( err . code !== 'EEXIST' ) {
79
+ throw new Error ( `
80
+
81
+ Could not create dir for cookie file at ${ cookieDirPath } . Encountered exception:
82
+
83
+ ${ err }
84
+
85
+ ` )
86
+ }
87
+ // at this point, the directory exists. if the cookie file does not exist,
88
+ // ensure the dir is writable (because we will create the file); otherwise
89
+ // ensure the file itself is writable.
90
+ let hasExistingCookieFile = false
91
+ try {
92
+ fs . existsSync ( config . cookiePath )
93
+ hasExistingConfigFile = true
94
+ } catch ( ignored ) { }
95
+
96
+ if ( hasExistingCookieFile ) {
97
+ try {
98
+ fs . accessSync ( config . cookiePath , fs . constants . W_OK )
99
+ } catch ( err ) {
100
+ throw new Error ( `
39
101
40
- You can learn how to config codimd-cli on https://github.com/hackmdio/codimd-cli
102
+ Cookie file ${ config . cookiePath } is not writable. Encountered exception:
103
+
104
+ ${ err }
41
105
42
106
` )
107
+ }
108
+ } else {
109
+ try {
110
+ fs . accessSync ( cookieDirPath , fs . constants . W_OK )
111
+ } catch ( err ) {
112
+ throw new Error ( `
113
+
114
+ Dir for cookie file at ${ cookieDirPath } is not writable. Encountered exception:
115
+
116
+ ${ err }
117
+
118
+ ` )
119
+ }
120
+ }
43
121
}
44
122
123
+
124
+
45
125
export default config
0 commit comments