-
Notifications
You must be signed in to change notification settings - Fork 1
/
plopfile.js
executable file
·108 lines (99 loc) · 2.59 KB
/
plopfile.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
/* eslint-disable no-console */
const { spawn } = require('child_process');
const { resolve } = require('path');
const { platform } = require('os');
const ora = require('ora');
const npmCmd = platform().startsWith('win') ? 'npm.cmd' : 'npm';
const spinner = ora('Installing NPM dependencies');
const spawn$ = (...args) => new Promise((fnResolve, fnReject) => {
const cmd = spawn(...args);
cmd.on('close', fnResolve);
cmd.on('error', fnReject);
});
function camelize(str) {
return str.replace(/\W+(.)/g, (match, chr) => chr.toUpperCase());
}
function lowercase(str) {
if (str && typeof str === 'string') {
return str.toLocaleLowerCase();
}
return str;
}
function getKey(txt) {
return typeof txt === 'string' && txt.match(/[- ]/)
? `'${txt}'`
: txt;
}
module.exports = (plop) => {
// controller generator
plop.setGenerator('module', {
description: 'Create new module',
prompts: [
{
type: 'input',
name: 'name',
message: 'Choose a name',
},
{
type: 'confirm',
name: 'git',
message: 'Init git repository',
},
{
type: 'confirm',
name: 'install',
message: 'Install dependencies',
},
],
actions: [
{
type: 'addMany',
templateFiles: 'plop/module/**/*!(*.hbs)',
destination: 'modules/{{{lowercase name}}}',
skipIfExists: true,
base: 'plop/module',
globOptions: {
dot: true,
},
},
async (answers) => {
if (answers.git !== true) {
return 'Git has been ignored';
}
console.log('Initializing git repository');
try {
await spawn$('git', ['init'], {
cwd: resolve('modules', answers.name),
detached: true,
stdio: 'inherit',
});
} catch (e) {
console.error(e);
}
return 'Git repository has been initialized';
},
async (answers) => {
if (answers.install !== true) {
return 'Dependencies are ignored';
}
spinner.start();
await spawn$(
npmCmd,
['install'],
{
cwd: resolve('modules', answers.name),
detached: true,
stdio: 'ignore',
},
);
spinner.stop();
return 'Dependencies installed successfully';
},
],
});
// Create the camelize helper
plop.setHelper('camelize', camelize);
plop.setHelper('raw-helper', (options) => options.fn());
plop.setHelper('get-key', getKey);
plop.setHelper('lowercase', lowercase);
};