This repository has been archived by the owner on Feb 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
125 lines (111 loc) · 3.01 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const chalk = require('chalk');
const commandExists = require('command-exists');
const fs = require('fs-extra');
const logSymbols = require('log-symbols');
const ora = require('ora');
const path = require('path');
const spawn = require('execa');
/**
* Helper to make sure we do not accidentally overwrite something.
*/
const allowedFiles = [
'.DS_Store',
'Thumbs.db',
'.git',
'.gitignore',
'.idea',
'.vscode',
'LICENSE'
];
const hasFileConflicts = dir => {
return !fs.readdirSync(dir)
.every(file => allowedFiles.indexOf(file) >= 0);
};
/**
* Check if we can init presentation in given `dir`, if so
* write a small `package.json` into it.
*/
const prepareDirectory = dir => new Promise((resolve, reject) => {
if (fs.existsSync(dir) && hasFileConflicts(dir)) {
return reject(`${logSymbols.error} Directory ${chalk.italic(dir)} contains files that
could conflict. Please chose another one.`
);
}
const pkg = {
name: path.basename(dir),
version: '1.0.0',
private: true
};
fs.outputJson(path.join(dir, 'package.json'), pkg, err => {
if (err) { resolve(err); return; }
console.log(`${logSymbols.success} Directory created at ${chalk.italic(dir)}`);
resolve(dir);
});
});
/**
* Check if we can use `yarn` instead of `npm` and put together
* installation command.
*/
const prepareInstallCommand = verbose => new Promise((resolve, reject) => {
commandExists('yarn', (err, isAvailable) => {
if (err) {
return reject(chalk.red(err));
}
let cmd, args;
if (isAvailable) {
cmd = 'yarn';
args = ['add', '--dev', '--exact'];
} else {
cmd = 'npm';
args = ['install', '--DE'];
if (verbose) { args.push('--verbose'); }
}
resolve({ cmd, args });
});
});
/**
* Invoke npm/yarn installation for (dev) dependencies.
*/
const installDependencies = (dir, cmd, args, dependencies, verbose) => {
process.chdir(dir);
const child = spawn(cmd, args.concat(dependencies));
if (verbose) {
child.stdout.pipe(process.stdout);
return child;
}
// Only show loading spinner if no other stdout.
const spinner = ora(chalk.dim(`Installing ${chalk.italic(dependencies)}. This may take a while...`));
spinner.start();
return child
.then(() => {
spinner.text = 'Installation complete!';
spinner.succeed();
})
.catch(err => {
// Forward, we only want to stop the spinner.
spinner.text = 'Installation failed!';
spinner.fail();
return Promise.reject(err);
});
};
const run = (dir, { verbose }) => {
const dependencies = 'melodrama-scripts';
return prepareDirectory(dir)
.then(() => prepareInstallCommand(verbose))
.then(({cmd, args}) => installDependencies(dir, cmd, args, dependencies, verbose))
.then(() => {
const bootstrap = path.resolve(
process.cwd(),
'node_modules',
dependencies,
'index.js'
);
return require(bootstrap)(dir, { verbose });
});
};
module.exports = {
run,
prepareDirectory,
prepareInstallCommand,
installDependencies
};