-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathutils.js
51 lines (46 loc) · 1.9 KB
/
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
/* */
import { execSync } from 'child_process'
import appRootDir from 'app-root-dir'
import { resolve as resolvePath } from 'path'
import { readFileSync } from 'fs'
export function removeEmpty(x) {
return x.filter(y => y != null)
}
// This is a higher order function that accepts a boolean condition and will
// return a function allowing you to provide if/else values that should be
// resolved based on the boolean condition.
//
// That sounds complicated, but it isn't really. See the examples below. :)
//
// For example, say that we have a "isDev" boolean flag had a value of `true`,
// and we would like to create a webpack loader based on this value being true.
// Then when we used this function like so:
// const ifDev = ifElse(isDev);
// ifDev('foo'); // => 'foo'
//
// You can also set an "else" value. In the below case the "isDev" flag is false.
// const ifDev = ifElse(isDev);
// ifDev('foo', 'bar'); // => 'bar'
//
// The "else" value is optional, in which case a null value would be returned.
//
// This is really handy for doing inline value resolution within or webpack
// configuration. Then we simply use one of our other utility functions (e.g.
// removeEmpty) to remove all the nulls from our objects/arrays.
//
// As an additional feature: if you pass a function as the "then"/"or" value
// then this function will only be interpretted after the ifElse has run. This
// can be handy for values that require some complex initialization process.
// e.g. ifDev(() => 'lazy', 'not lazy');
export function ifElse(condition) {
return function ifElseResolver(then, or) {
const execIfFuc = x => typeof x === 'function' ? x() : x
return condition ? execIfFuc(then) : or
}
}
export function getPackageJson() {
return JSON.parse(readFileSync(resolvePath(appRootDir.get(), './package.json'), 'utf-8'))
}
export function exec(command) {
execSync(command, { stdio: 'inherit', cwd: appRootDir.get() })
}