This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
92 lines (82 loc) · 2.44 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
const fs = require('fs');
const exec = require('child_process').exec;
const nyg = require('nyg');
// Generator configuration
const globs = [
{ base: 'templates/', glob: '.storybook/**/*', template: false },
{ base: 'templates/', glob: 'docs/**/*', template: false },
{ base: 'templates/', glob: 'public/**/*', template: false },
{ base: 'templates/', glob: 'scripts/**/*', template: false },
{ base: 'templates/', glob: 'src/**/*', template: false },
{ base: 'templates/', glob: 'test/**/*', template: false },
{ base: 'templates/', glob: '*', template: false }
];
const generator = nyg(null, globs)
.on('postinstall', onPostInstall)
.run();
/**
* Post Install event
*/
function onPostInstall() {
var done = generator.async();
Promise.all([updateNvmVersion(), updateGeneratedPackageJson(), renameGitIgnore()])
.then(() => {
console.log('App generated');
done();
})
.catch(e => {
console.log(e);
});
}
/**
* Update NVM lock version with the current user sections
*
*/
function updateNvmVersion() {
return new Promise((resolve, reject) => {
exec(`node -v > ${generator.cwd}/.nvmrc`, function(err) {
if (err) return reject(new Error());
resolve();
});
});
}
/**
* Update generated package.json
*
*/
function updateGeneratedPackageJson() {
const packagePath = `${generator.cwd}/package.json`;
const generatedPackageJson = JSON.parse(fs.readFileSync(packagePath));
return _updateNodeJSRequiredVersion(packagePath, generatedPackageJson);
}
/**
* Rename gitignore to .gitignore
*
*/
function renameGitIgnore() {
const gitIgnorePath = `${generator.cwd}/gitignore`;
const generatedGitIgnore = `${generator.cwd}/.gitignore`;
return new Promise((resolve, reject) => {
fs.rename(gitIgnorePath, generatedGitIgnore, function(err) {
if (err) return reject(new Error());
resolve();
});
});
}
/**
* Update node version in engines property inside the package.json
*
* @param {any} packagePath - Package JSON path
* @param {any} packageJson - Package JSON already parsed
* @returns {Promise}
*/
function _updateNodeJSRequiredVersion(packagePath, packageJson) {
const nodeVersion = process.version.replace('v', '');
packageJson['engines'].node = `>=${nodeVersion}`;
return new Promise((resolve, reject) => {
fs.writeFile(packagePath, JSON.stringify(packageJson, null, 2), function(err) {
if (err) return reject(new Error());
resolve();
});
});
}