-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (178 loc) · 4.31 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
188
#!/usr/bin/env node
/**
* @since 20180510 14:02
* @author vivaxy
*/
import url from 'url';
import path from 'path';
import execa from 'execa';
import fse from 'fs-extra';
import prompts from 'prompts';
import logSymbols from 'log-symbols';
import { createRequire } from 'module';
import gitUrlParse from 'git-url-parse';
import { getCurrentRemoteUrl, getUserName, getUserEmail } from '@vivaxy/git';
import { copyFiles } from './lib/template/copy-files.js';
import { createFiles } from './lib/template/create-files.js';
const require = createRequire(import.meta.url);
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
const pkg = require('./package.json');
function padLeft(value, length = 2, padding = '0') {
if (typeof value !== 'string') {
value = String(value);
}
while (value.length < length) {
value = `${padding}${value}`;
}
return value;
}
(async () => {
const cwd = process.cwd();
logInfo();
await checkDir({ cwd });
const params = await getParams({ cwd });
await generateFiles(params);
const dependencies = [];
const devDependencies = [
// commit hook
'husky',
'lint-staged',
// commit lint
'@commitlint/cli',
'@commitlint/config-conventional',
// code style
'prettier',
// build
'typescript',
// test
'jest',
'@types/jest',
'ts-jest',
// changelog
'standard-version',
];
await installDependencies(dependencies, devDependencies);
console.log('Create N success!');
process.exit(0);
})().catch((ex) => {
console.log(ex);
process.exit(1);
});
function logInfo() {
console.log('Create-n version: ' + pkg.version);
}
async function checkDir({ cwd }) {
const files = await fse.readdir(cwd);
if (files.length !== 0) {
const { override } = await prompts(
[
{
type: 'confirm',
name: 'override',
message: 'Directory not empty, override?',
initial: true,
},
],
{
onCancel: () => {
console.log('Create cancelled.');
process.exit(1);
},
},
);
if (!override) {
console.log('Create cancelled.');
process.exit(0);
}
}
}
async function getParams({ cwd }) {
const { name, license } = await prompts(
[
{
type: 'text',
name: 'name',
message: 'Enter package name',
initial: path.basename(process.cwd()),
},
{
type: 'select',
name: 'license',
message: 'Select a license',
choices: [
{
title: 'GNU GPLv3',
value: 'gpl',
},
{
title: 'MIT License',
value: 'mit',
},
],
},
],
{
onCancel() {
console.log('Create cancelled.');
process.exit(1);
},
},
);
const [username, gitRemoteURL, userEmail] = await Promise.all([
getUserName({ cwd }),
getCurrentRemoteUrl({ cwd }), // => [email protected]:vivaxy/create-n.git
getUserEmail({ cwd }),
]);
if (!gitRemoteURL) {
console.log('Create failed with error git remote URL: ' + gitRemoteURL);
process.exit(1);
}
const {
name: repoName,
owner: repoOwner,
source,
} = gitUrlParse(gitRemoteURL);
const nowDate = new Date();
const year = nowDate.getFullYear();
return {
name,
username,
userEmail,
repoOwner,
repoName,
gitRemoteURL,
hostname: source,
year,
fileHeadersTime: `${year}-${padLeft(nowDate.getMonth() + 1)}-${padLeft(
nowDate.getDate(),
)} ${padLeft(nowDate.getHours())}:${padLeft(
nowDate.getMinutes(),
)}:${padLeft(nowDate.getSeconds())}`,
license,
};
}
async function generateFiles(params) {
console.log(logSymbols.info, 'create files ...');
const srcDir = path.join(dirname, 'template');
await Promise.all([
copyFiles({ srcDir, distDir: process.cwd(), params }),
createFiles({
srcDir,
distDir: process.cwd(),
params,
}),
]);
}
async function installDependencies(dependencies, devDependencies) {
console.log(logSymbols.info, 'npm install ...');
if (dependencies.length) {
await execa('npm', ['install', ...dependencies, '--save'], {
stdio: 'inherit',
});
}
if (devDependencies.length) {
await execa('npm', ['install', ...devDependencies, '--save-dev'], {
stdio: 'inherit',
});
}
}