Skip to content

Commit c1417f7

Browse files
committed
feat: support generator argv
1 parent 46d79a5 commit c1417f7

File tree

6 files changed

+100
-10
lines changed

6 files changed

+100
-10
lines changed

jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
testPathIgnorePatterns: ['<rootDir>/test/test-project', '<rootDir>/test/tpl'],
4+
coveragePathIgnorePatterns: ['<rootDir>/test/'],
5+
};

lib/index.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const enquirer = require('enquirer');
2-
const { join, relative } = require('path');
2+
const { join, relative, isAbsolute } = require('path');
33
const { existsSync, remove } = require('fs-extra');
44
const chalk = require('chalk');
55
const { CategorySelect } = require('./categorySelect');
@@ -57,6 +57,19 @@ function formatArgv(argv) {
5757
return [result, leftArgv];
5858
}
5959

60+
// 将 t_template=dd 转换成 { template: 'dd' }
61+
function formatUserArgv(argv) {
62+
const result = {};
63+
for (const key in argv) {
64+
if (key.startsWith('t_')) {
65+
result[key.replace(/^t_/, '')] = argv[key];
66+
} else {
67+
result[key] = argv[key];
68+
}
69+
}
70+
return result;
71+
}
72+
6073
class AddPlugin {
6174
constructor() {
6275
this.cwd = process.cwd();
@@ -83,7 +96,7 @@ class AddPlugin {
8396
type: 'string',
8497
},
8598
npm: {
86-
usage: 'npm registry',
99+
usage: 'npm client',
87100
type: 'string',
88101
},
89102
registry: {
@@ -149,8 +162,15 @@ class AddPlugin {
149162
}
150163
this.projectName = projectPath;
151164

152-
const projectDirPath = join(this.cwd, projectPath);
153-
if (existsSync(projectDirPath)) {
165+
166+
// 处理绝对路径
167+
if (isAbsolute(projectPath)) {
168+
this.projectDirPath = projectPath;
169+
} else {
170+
this.projectDirPath = join(this.cwd, projectPath);
171+
}
172+
// 检查是否存在
173+
if (existsSync(this.projectDirPath)) {
154174
const isOverwritten = await new (enquirer).Confirm({
155175
name: 'question',
156176
message: `The name '${projectPath}' already exists, can it be overwritten?`,
@@ -159,19 +179,21 @@ class AddPlugin {
159179
if (!isOverwritten) {
160180
process.exit();
161181
}
162-
await remove(projectDirPath);
182+
await remove(this.projectDirPath);
163183
}
164-
this.projectDirPath = projectDirPath;
165184
}
166185

167186
async generator() {
168-
const { projectDirPath, template } = this;
187+
let { projectDirPath, template } = this;
169188
if (!template) {
170189
return;
171190
}
172191
let type = 'npm';
173192
if (template[0] === '.' || template[0] === '/') {
174193
type = 'local';
194+
if (!isAbsolute(template)) {
195+
template = join(this.cwd, template);
196+
}
175197
}
176198
const spin = new Spin({
177199
text: 'Downloading Boilerplate...',
@@ -195,7 +217,7 @@ class AddPlugin {
195217
targetPath: projectDirPath,
196218
});
197219
}
198-
await generator.run();
220+
await generator.run(formatUserArgv(this.argv));
199221
spin.stop();
200222
} catch (e) {
201223
spin.stop();

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"fs-extra": "^8.1.0",
1919
"light-generator": "^1.6.1",
2020
"light-spinner": "^1.0.1",
21-
"@vercel/ncc": "^0.28.0"
21+
"@vercel/ncc": "^0.28.0",
22+
"jest": "29"
2223
},
2324
"engines": {
2425
"node": ">= 10.0.0"
@@ -29,7 +30,7 @@
2930
],
3031
"scripts": {
3132
"build": "ncc build lib/index.js -o dist",
32-
"test": "npm run test"
33+
"test": "jest"
3334
},
3435
"publishConfig": {
3536
"access": "public"

test/index.test.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const { remove, existsSync, readFileSync } = require('fs-extra');
2+
const { AddPlugin } = require('../lib/index');
3+
const { join } = require('path');
4+
5+
const testProject = join(__dirname, 'test-project');
6+
describe('test/index.test.js', () => {
7+
8+
beforeAll(async () => {
9+
if (existsSync(testProject)) {
10+
await remove(testProject);
11+
}
12+
});
13+
14+
beforeEach(async () => {
15+
// mock cwd
16+
const spy = jest.spyOn(process, 'cwd');
17+
spy.mockReturnValue(__dirname);
18+
});
19+
20+
afterEach(async () => {
21+
jest.restoreAllMocks();
22+
if (existsSync(testProject)) {
23+
await remove(testProject);
24+
}
25+
})
26+
27+
it('test create with target dir', async () => {
28+
jest.replaceProperty(process, 'argv', ['node', 'create-midway-dev', '--type=koa-v3', testProject]);
29+
await new AddPlugin().run();
30+
expect(existsSync(join(testProject, 'package.json'))).toBeTruthy();
31+
expect(existsSync(join(testProject, 'src'))).toBeTruthy();
32+
})
33+
34+
it('test create with target dir and type', async () => {
35+
jest.replaceProperty(process, 'argv', ['node', 'create-midway-dev', '--template=./tpl', `--target=${testProject}`]);
36+
await new AddPlugin().run();
37+
expect(existsSync(join(testProject, 'package.json'))).toBeTruthy();
38+
expect(existsSync(join(testProject, 'test.js'))).toBeTruthy();
39+
})
40+
41+
it('test create with target dir and absolute template', async () => {
42+
jest.replaceProperty(process, 'argv', ['node', 'create-midway-dev', `-t=${join(__dirname, './tpl')}`, '--bbb=cc', '--t_template=dd', testProject]);
43+
await new AddPlugin().run();
44+
expect(existsSync(join(testProject, 'package.json'))).toBeTruthy();
45+
expect(existsSync(join(testProject, 'test.js'))).toBeTruthy();
46+
expect(readFileSync(join(testProject, 'test.js'), 'utf8')).toMatch('cc');
47+
expect(readFileSync(join(testProject, 'test.js'), 'utf8')).toMatch('dd');
48+
})
49+
50+
});

test/tpl/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "demo",
3+
"boilerplateConfig": {
4+
"root": ".",
5+
"replaceFile": [
6+
"test.js"
7+
]
8+
}
9+
}

test/tpl/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
console.log('abc')
2+
console.log('{{bbb}}')
3+
console.log('{{template}}')

0 commit comments

Comments
 (0)