This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
99 lines (87 loc) · 2.44 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const path = require('path');
const execa = require('execa');
const ora = require('ora');
const Chalk = require('chalk');
const guessRootPath = require('guess-root-path');
/**
* Take a list of dependencies and filter out any that are already in the package.json
* @param {Array} dependencies
* @param {boolean} isDev
*/
const onlyNewDependencies = (dependencies, isDev) => {
try {
const config = require(path.join(guessRootPath(), 'package.json'));
const deps = Object.keys(config[isDev ? 'devDependencies' : 'dependencies']);
return dependencies.filter(dep => !deps.includes(dep) && dep !== null);
} catch (ex) {
// nothing
}
return dependencies;
};
/**
* Install dependencies (only if needed)
* @param {Array} dependencies
* @param {Array} args
*/
const installDependencies = async (dependencies, args, log) => {
log = log || console.log;
args = args || [];
dependencies = onlyNewDependencies(dependencies, args.includes('--save-dev'));
if (dependencies.length === 0) return;
let spinner = ora(SPINNER).start();
spinner.text = Chalk.gray(
'installing ' +
dependencies.length +
`${args.includes('--save-dev') ? ' development ' : ' '}dependenc${dependencies.length == 1 ? 'y' : 'ies'}`
);
args = ['install', '--silent', '--no-progress'].concat(args).concat(dependencies);
await execa('npm', args);
spinner.stop();
dependencies.forEach(d => {
log(Chalk.yellow(' npm'), d);
});
};
/**
* Look up the current master version of a thing
*/
const getGithubVersion = async repo => {
let spinner = ora(SPINNER).start();
spinner.color = 'white';
spinner.text = Chalk.gray('Fetching latest version of ' + repo);
const p = await fetch(`https://raw.githubusercontent.com/${repo}/master/package.json`).then(r => r.json());
spinner.stop();
return p.version;
};
const SPINNER = {
color: 'yellow',
spinner: {
interval: 80,
frames: [
'⣏⠀⠀',
'⡟⠀⠀',
'⠟⠄⠀',
'⠛⡄⠀',
'⠙⣄⠀',
'⠘⣤⠀',
'⠐⣤⠂',
'⠀⣤⠃',
'⠀⣠⠋',
'⠀⢠⠛',
'⠀⠠⠻',
'⠀⠀⢻',
'⠀⠀⣹',
'⠀⠀⣼',
'⠀⠐⣴',
'⠀⠘⣤',
'⠀⠙⣄',
'⠀⠛⡄',
'⠠⠛⠄',
'⢠⠛⠀',
'⣠⠋⠀',
'⣤⠃⠀',
'⣦⠂⠀',
'⣧⠀⠀'
].map(f => ' ' + f + ' ')
}
};
module.exports = { installDependencies, getGithubVersion, SPINNER };