-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenAngularLibrary.js
executable file
·212 lines (184 loc) · 6.81 KB
/
genAngularLibrary.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env node
'use strict';
const cmd = require('node-cmd');
const readline = require('readline');
const fs = require('fs');
const chalk = require('chalk');
const ora = require('ora');
const figlet = require('figlet');
let spinner;
const fileUtils = (function () {
function _readTemplateFile(fileName) {
return fs.readFileSync(`${__dirname}/template-files/${fileName}`, 'utf-8');
}
function _copyTemplateFileToDestination(fileName, data, options, copyToLib) {
fs.writeFileSync(`./${options.libName}/${fileName}`, data, 'utf-8');
if (copyToLib) {
fs.writeFileSync(`./${options.libName}/projects/${options.libName}/${fileName}`, data, 'utf-8');
}
logSteps(`Adding ${fileName}\` file.`);
}
function _copyTemplateFile(fileName, options, copyToLib) {
const data = fileUtils.readTemplateFile(fileName);
fileUtils.copyTemplateFileToDestination(fileName, data, options, copyToLib);
}
return {
readTemplateFile: _readTemplateFile,
copyTemplateFile: _copyTemplateFile,
copyTemplateFileToDestination: _copyTemplateFileToDestination,
};
})();
const optionsUtils = (function () {
function _getFullName(options) {
return options.fullName.toLowerCase()
.split(' ')
.map((s) => s.charAt(0).toUpperCase() + s.substring(1))
.join(' ');
}
return {
getFullName: _getFullName
};
})();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function question(questionText, defaultValue) {
return new Promise((resolve) => {
rl.question(`${chalk.hex('#98c379').bold('❯ ')} ${questionText}: `, (answer) => {
resolve(answer || defaultValue);
})
})
}
function createAngularWorkspace(options) {
logSteps(`Creating Angular Workspace`);
cmd.get(
`
ng new ${options.libName} --create-application=false
`,
() => generateAngularLibrary(options)
);
}
function generateAngularLibrary(options) {
logSteps(`Creating Angular Library`);
cmd.get(
`
cd ${options.libName}
ng generate library ${options.libName} --prefix=${options.libPrefix}`,
() => generateAngularApplicationExample(options)
);
}
function generateAngularApplicationExample(options) {
logSteps(`Creating Angular application example`);
cmd.get(
`
cd ${options.libName}
ng generate application ${options.libName}-example --style=${options.exampleStyle}
ng config schematics.@schematics/angular:component.styleext ${options.exampleStyle}
`,
() => installAdditionalNpmPackages(options)
);
}
function installAdditionalNpmPackages(options) {
logSteps(`Installing additional npm packages: prettier, tslint-config-prettier, husky, lint-staged`);
cmd.get(
`
cd ${options.libName}
npm i --D --E prettier
npm i --D tslint-config-prettier husky lint-staged
`,
() => {
createLicenseFile(options);
fileUtils.copyTemplateFile('.prettierrc', options, false);
fileUtils.copyTemplateFile('CONTRIBUTING.md', options, true);
createReadmeFile(options);
spinner.succeed();
console.log(chalk.hex('#1ec537').bold(`\n\r Library ${options.libName} created successfully. 💪`));
figlet(options.libName, function (err, data) {
if (err) {
console.log('Something went wrong...');
console.dir(err);
return;
}
console.log(data)
});
editJsonFile(`./${options.libName}/package.json`, (jsonObject) => {
const libName = options.libName;
jsonObject.scripts = {
"ng": "ng",
"all:build": "npm run example:build && npm run lib:build",
"start": "ng serve --open",
"lib:build": `ng build ${libName}`,
"lib:lint": `ng lint ${libName} --fix`,
"lib:publish-2-npm": `./dist/${libName} npm publish`
};
jsonObject.husky = {hooks: {"pre-commit": "lint-staged"}};
jsonObject["lint-staged"] = {
"projects/**/*.ts": [
"prettier --write",
"git add"
]
};
return jsonObject;
});
editJsonFile(`./${options.libName}/tslint.json`, (jsonObject) => {
if (Array.isArray(jsonObject.extends)) {
jsonObject.extends = [...jsonObject.extends];
} else {
jsonObject.extends = [jsonObject.extends];
}
jsonObject.extends.push("tslint-config-prettier");
return jsonObject;
});
}
);
}
function createLicenseFile(options) {
const fullName = optionsUtils.getFullName(options);
const fileName = 'LICENSE'
const data = fileUtils.readTemplateFile(fileName);
let newValue = data.replace('[year]', new Date().getFullYear());
newValue = newValue.replace('[fullname]', fullName);
fileUtils.copyTemplateFileToDestination(fileName, newValue, options, true);
}
function createReadmeFile(options) {
const fullName = optionsUtils.getFullName(options);
const fileName = 'README.md'
const data = fileUtils.readTemplateFile(fileName);
let newValue = data.replace('[year]', new Date().getFullYear());
newValue = newValue.replace('[fullname]', fullName);
fileUtils.copyTemplateFileToDestination(fileName, newValue, options, true)
}
function editJsonFile(pathToFile, cb) {
let jsonObject = JSON.parse(fs.readFileSync(pathToFile, 'utf8'));
jsonObject = cb(jsonObject);
fs.writeFileSync(pathToFile, JSON.stringify(jsonObject, null, 2));
}
function logBlue(text) {
console.log(chalk.hex("#61AFEF").bold(text));
}
function logYellow(text) {
console.log(chalk.hex("#FCE546").bold(text));
}
function logSteps(text) {
if (spinner) {
spinner.succeed();
}
spinner = ora(`${chalk.hex('#98c379')(text)}`).start();
}
async function questions() {
const options = {};
options.libName = await question('Enter library name');
options.fullName = await question('Enter your full name');
options.libPrefix = await question('Enter library prefix', 'ngx');
options.exampleStyle = await question('Enter style: (css|scss|sass|less|styl)', 'scss');
rl.close();
return options;
}
const main = async () => {
logBlue('Hi, welcome to Angular Library Generator 🚀 \n\r');
const options = await questions();
logYellow('\n\r Start generating your library. (it might take some time)');
createAngularWorkspace(options);
};
main();