forked from DavidWells/markdown-magic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli-utils.js
55 lines (49 loc) 路 1.71 KB
/
cli-utils.js
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
const resolve = require('path').resolve
const loadJSConfig = getAttemptModuleRequireFn(function onFail(configPath, requirePath) {
console.log(`Unable to find JS config at "${configPath}". Attempted to require as "${requirePath}"`)
// log.error({
// message: colors.red(`Unable to find JS config at "${configPath}". Attempted to require as "${requirePath}"`),
// ref: 'unable-to-find-config',
// })
return undefined
})
/**
* Determines the proper require path for a module. If the path starts with `.` then it is resolved with process.cwd()
* @param {String} moduleName The module path
* @return {String} the module path to require
*/
function getModuleRequirePath(moduleName) {
return moduleName[0] === '.' ? resolve(process.cwd(), moduleName) : moduleName
}
function getAttemptModuleRequireFn(onFail) {
return function attemptModuleRequire(moduleName) {
const requirePath = getModuleRequirePath(moduleName)
try {
return requireDefaultFromModule(requirePath)
} catch (e) {
if (e.constructor.name === 'SyntaxError') {
throw e
}
return onFail(moduleName, requirePath)
}
}
}
/**
* Requires the given module and returns the `default` if it's an `__esModule`
* @param {String} modulePath The module to require
* @return {*} The required module (or it's `default` if it's an `__esModule`)
*/
function requireDefaultFromModule(modulePath) {
/* eslint global-require:0,import/no-dynamic-require:0 */
const mod = require(modulePath)
if (mod.__esModule) {
return mod.default
} else {
return mod
}
}
function loadConfig(configPath) {
// potentially load other types of config files
return loadJSConfig(configPath)
}
module.exports.loadConfig = loadConfig