Skip to content

Commit 3539322

Browse files
committed
fix configuration flow
- Checks to ensure the config file is readable (not just exists) - If a `CMD_CLI_SERVER_URL` env var is present, do not create the config file if it does not exist - Check to ensure the cookie dir and/or cookie file is writable
1 parent d6ac3a0 commit 3539322

File tree

1 file changed

+92
-12
lines changed

1 file changed

+92
-12
lines changed

src/config.ts

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as fs from 'fs-extra'
2+
import * as path from 'path'
3+
24
import defaults from 'lodash/defaults'
35
import {homedir} from 'os'
4-
import * as path from 'path'
56

67
let configDir
78
if (process.env.HMD_CLI_CONFIG_DIR) {
@@ -11,35 +12,114 @@ if (process.env.HMD_CLI_CONFIG_DIR) {
1112
}
1213

1314
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')
1516

1617
const defaultConfig = {
17-
cookiePath: defaultCookiePath
18+
cookiePath: defaultCookiePath,
19+
serverUrl: ''
1820
}
1921

2022
const envConfig = {
2123
cookiePath: process.env.CMD_CLI_COOKIE_PATH,
2224
serverUrl: process.env.CMD_CLI_SERVER_URL
2325
}
2426

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:
2754
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+
}
3159
}
3260

33-
const config = defaults(readConfig, envConfig, defaultConfig)
61+
// prefer environment config over file config
62+
const config = defaults(envConfig, readConfig, defaultConfig)
3463

35-
if (!config.serverUrl || config.serverUrl === defaultServerUrl) {
64+
if (!config.serverUrl) {
3665
throw new Error(`
3766
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(`
39101
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}
41105
42106
`)
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+
}
43121
}
44122

123+
124+
45125
export default config

0 commit comments

Comments
 (0)