-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·187 lines (149 loc) · 4.94 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
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
#!/usr/bin/env node
// Note: This Run From Wherever the current process is
const shell = require('shelljs');
const colors = require('colors');
const isFilenameValid = require('valid-filename');
const utilzed = require('utilzed').default;
const fs = require('fs-extra');
const path = require('path');
function getAuthorName() {
let author = '';
author = shell
.exec('npm config get init-author-name', { silent: true })
.stdout.trim();
if (author) return author;
author = shell
.exec('git config --global user.name', { silent: true })
.stdout.trim();
if (author) {
return author;
}
author = shell
.exec('npm config get init-author-email', { silent: true })
.stdout.trim();
if (author) return author;
author = shell
.exec('git config --global user.email', { silent: true })
.stdout.trim();
if (author) return author;
// whoami will be the last fall back
author = shell
.exec('whoami', { silent: true })
.stdout.trim();
return author;
}
async function run() {
const args = process.argv;
const templateNodeVersions = new Map();
templateNodeVersions.set('tslib', '*');
templateNodeVersions.set('express', );
const optionMap = new Map();
optionMap.set('tslib', {
base: 'v10.13.0',
ceil: null
})
optionMap.set('express', {
base: 'v12.19.0',
ceil: null
})
// ['tslib', 'express']
const option = args.find(arg => arg.indexOf('--option') >= 0);
const folder = args.find(arg => arg.indexOf('--folder') >= 0);
// STEP 1: check if necessary arguments present
if (!option || !folder) {
shell.echo(`
${(!option) ? 'option argument is not defined\n' : ''}
${(!folder) ? 'folder argument is not defined' : ''}
`);
shell.exit(1);
}
// STEP 2: check if value for option argument is legit
const optionValue = option.split('=')[1];
if(!optionMap.has(optionValue)) {
shell.echo(`
${optionValue} is not a valid option, select from one of the followings:
${optionMap.keys.reduce((prev, curr) => `${prev}\n\t--option=${curr}`, '')}
`);
shell.exit(1);
}
// STEP 3: check if filename given is legit
const folderValue = folder.split('=')[1];
if(!isFilenameValid(folderValue)) {
shell.echo('Please determine your app name or enter a valid filename');
shell.exit(1);
}
const appDirectory = `${process.cwd()}/${folderValue}`;
// STEP 4: check if directory already exist
shell.cd(process.cwd());
if (shell.test('-d', folderValue)) {
shell.echo(
`${appDirectory} already exist, will not proceed on creating template`
);
shell.exit(1);
}
// STEP 5: check node version
// For express or nextjs has ceil node version requirement
const versionConfig = optionMap.get(optionValue);
if (!shell.which('node')) {
shell.echo(`Please install node min ${versionConfig.base} for this template`);
shell.exit(1);
}
shell.echo('Checking node version compatibility:');
const version = shell.exec('node -v').toString();
const { OPERATORS, isVersionValid } = utilzed.versionCheck;
if (versionConfig.base) {
if (!isVersionValid(version, OPERATORS.MORE_THAN_EQUAL, versionConfig.base)) {
shell.echo(`Please install node min ${versionConfig.base} for this template`);
shell.exit(1);
}
}
if (versionConfig.ceil) {
if (!isVersionValid(version, OPERATORS.LESS_THAN_EQUAL, versionConfig.base)) {
shell.echo(`Please install node <= ${versionConfig.ceil} for this template`);
shell.exit(1);
}
}
// STEP 6: Copying the template
shell.mkdir(folderValue);
// 6.1 Copy base code
fs.copySync(
path.resolve(__dirname, `./templates/${optionValue}`),
appDirectory,
{
overwrite: true,
}
);
// 6.2 Replacing files with placeholder
const files = ['package.json', 'LICENSE', 'CHANGELOG.md', 'README.md'];
const author = getAuthorName();
const date = new Date();
const dateString = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
files.forEach((file) => {
const path = `${appDirectory}/placeholder/${file}`;
const hasFile = fs.pathExistsSync(path);
shell.echo(`${path}`);
shell.echo(`contain file: ${hasFile}`);
if (hasFile){
// const data = fs.readFileSync(`./placeholder/${optionValue}/${file}`, 'utf8');
const data = fs.readFileSync(`${path}`, 'utf8');
var result = data.replace(/__LIB__/g, folderValue)
.replace(/__AUTHOR__/g, author)
.replace(/__YEAR__/g, date.getFullYear())
.replace(/__DATE__/g, dateString);
fs.writeFileSync(`${appDirectory}/${file}`, result, 'utf8');
shell.echo(`create file complete: ${path}`);
}
});
shell.cd(folderValue);
shell.rm('-rf', 'placeholder');
if (!shell.which('pnpm')) {
shell.exec('npm install -g pnpm')
}
if (!shell.which('np')) {
shell.exec('npm install -g np')
}
shell.exec('pnpm i');
shell.echo(`${folderValue} library setup`);
shell.exit(0);
}
run();