forked from open-wc/create
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerator.js
139 lines (123 loc) · 4.06 KB
/
Generator.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* eslint-disable no-console, import/no-cycle */
import prompts from 'prompts';
import path from 'path';
import { spawn } from 'child_process';
import {
copyTemplates,
copyTemplate,
copyTemplateJsonInto,
installNpm,
writeFilesToDisk,
optionsToCommand,
} from './core.js';
/**
* Options for the generator
* @typedef {object} GeneratorOptions
* @property {string} [tagName] the dash-case tag name
* @property {string} [destinationPath='auto'] path to output to. default value 'auto' will output to current working directory
* @property {'scaffold'} [type='scaffold'] path to output to. default value 'auto' will output to current working directory
* @property {'true'|'false'} [writeToDisk] whether to write to disk
* @property {'yarn'|'npm'|'false'} [installDependencies] whether and with which tool to install dependencies
*/
/**
* dash-case to PascalCase
* @param {string} tagName dash-case tag name
* @return {string} PascalCase class name
*/
function getClassName(tagName) {
return tagName
.split('-')
.reduce((previous, part) => previous + part.charAt(0).toUpperCase() + part.slice(1), '');
}
class Generator {
constructor() {
/**
* @type {GeneratorOptions}
*/
this.options = {
destinationPath: 'auto',
};
this.templateData = {};
this.wantsNpmInstall = true;
this.wantsWriteToDisk = true;
this.wantsRecreateInfo = true;
this.generatorName = '@open-wc';
}
execute() {
if (this.options.tagName) {
const { tagName } = this.options;
const className = getClassName(tagName);
this.templateData = { ...this.templateData, tagName, className };
if (this.options.destinationPath === 'auto') {
this.options.destinationPath = process.cwd();
if (this.options.type === 'scaffold') {
this.options.destinationPath = path.join(process.cwd(), tagName);
}
}
}
}
destinationPath(destination = '') {
return path.join(this.options.destinationPath, destination);
}
copyTemplate(from, to, ejsOptions = {}) {
copyTemplate(from, to, this.templateData, ejsOptions);
}
copyTemplateJsonInto(from, to, options = { mode: 'merge' }, ejsOptions = {}) {
copyTemplateJsonInto(from, to, this.templateData, options, ejsOptions);
}
async copyTemplates(from, to = this.destinationPath(), ejsOptions = {}) {
return copyTemplates(from, to, this.templateData, ejsOptions);
}
async end() {
if (this.wantsWriteToDisk) {
this.options.writeToDisk = await writeFilesToDisk();
}
if (this.wantsNpmInstall) {
const answers = await prompts(
[
{
type: 'select',
name: 'installDependencies',
message: 'Do you want to install dependencies?',
choices: [
{ title: 'No', value: 'false' },
{ title: 'Yes, with yarn', value: 'yarn' },
{ title: 'Yes, with npm', value: 'npm' },
],
},
],
{
onCancel: () => {
process.exit();
},
},
);
this.options.installDependencies = answers.installDependencies;
const { installDependencies } = this.options;
if (installDependencies === 'yarn' || installDependencies === 'npm') {
await installNpm(this.options.destinationPath, installDependencies);
await new Promise(resolve => {
const install = spawn(installDependencies, ['run', 'analyze'], {
cwd: this.options.destinationPath,
shell: true,
});
install.stdout.on('data', data => {
console.log(`${data}`.trim());
});
install.stderr.on('data', data => {
console.log(`analyze: ${data}`);
});
install.on('close', () => {
resolve();
});
});
}
}
if (this.wantsRecreateInfo) {
console.log('');
console.log('If you want to rerun this exact same generator you can do so by executing:');
console.log(optionsToCommand(this.options, this.generatorName));
}
}
}
export default Generator;