-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
32 lines (27 loc) · 1.13 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
/**
* Utility Methods
*
* Credit: Kent C. Dodds
*
* @see https://github.com/kentcdodds/kcd-scripts/blob/main/src/utils.js
*/
const fs = require('fs');
const path = require('path');
const readPkgUp = require('read-pkg-up');
const arrify = require('arrify');
const has = require('lodash.has');
const { packageJson: pkg, path: pkgPath } = readPkgUp.sync({
cwd: fs.realpathSync(process.cwd()),
});
const appDirectory = path.dirname(pkgPath);
const fromRoot = (...p) => path.join(appDirectory, ...p);
const hasFile = (...p) => fs.existsSync(fromRoot(...p));
const ifFile = (files, t, f) => (arrify(files).some(file => hasFile(file)) ? t : f);
const hasPkgProp = props => arrify(props).some(prop => has(pkg, prop));
const hasPkgSubProp = pkgProp => props => hasPkgProp(arrify(props).map(p => `${pkgProp}.${p}`));
const hasDep = hasPkgSubProp('dependencies');
const hasDevDep = hasPkgSubProp('devDependencies');
const hasPeerDep = hasPkgSubProp('peerDependencies');
const hasAnyDep = args => [hasDep, hasDevDep, hasPeerDep].some(fn => fn(args));
const ifAnyDep = (deps, t, f) => (hasAnyDep(arrify(deps)) ? t : f);
module.exports = { ifAnyDep, ifFile };