-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
76 lines (65 loc) · 1.69 KB
/
helpers.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const chalk = require("chalk")
function findConfig() {
const configPath = locateConfig()
if (undefined === configPath) {
return {}
}
return require(configPath)
}
function changeToRoot() {
const configLocation = locateConfig()
let runningDir = process.cwd()
if (undefined !== configLocation) {
const {
parse
} = require('path')
const {
dir
} = parse(configLocation)
if (dir !== runningDir) {
process.chdir(dir)
runningDir = dir
}
logNotice(`Running from ${runningDir}`)
return;
}
logError("I couldn't find an Elf config file!")
logNotice("If you would like to generate a config file in the current directly, run `elf make-config`")
}
function locateConfig() {
const findUp = require("find-up")
return findUp.sync('elf.config.js')
}
function logNotice(msg) {
console.log(chalk.blue(`ℹ️ ${msg}`))
}
function logError(msg) {
console.log(chalk.bgRed.white(`⚠️ Oh No!`))
console.error(msg)
}
function filePath(path) {
return chalk.bgGreen.black(` ${path} `)
}
module.exports = {
logError,
logProgress: msg => {
console.log(chalk.green(msg))
},
logSuccess: msg => {
console.log(``)
console.log(chalk.blue(`✅ ${msg}`))
},
logNotice,
filePath,
makeRelative: path => {
return path.replace(`${process.cwd()}/`, '')
},
makeSafe: (string, replace = '_') => {
return string.replace(/[^a-z0-9_]/gi, replace).toLowerCase()
},
getSubcommand: yargs => {
return yargs.getContext().commands.join(' ')
},
findConfig,
changeToRoot
}