Skip to content

Commit

Permalink
feat: support generator argv (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Nov 11, 2023
1 parent 46d79a5 commit 8949ecf
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 10 deletions.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/test-project', '<rootDir>/test/tpl'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
};
38 changes: 30 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const enquirer = require('enquirer');
const { join, relative } = require('path');
const { join, relative, isAbsolute } = require('path');
const { existsSync, remove } = require('fs-extra');
const chalk = require('chalk');
const { CategorySelect } = require('./categorySelect');
Expand Down Expand Up @@ -57,6 +57,19 @@ function formatArgv(argv) {
return [result, leftArgv];
}

// 将 t_template=dd 转换成 { template: 'dd' }
function formatUserArgv(argv) {
const result = {};
for (const key in argv) {
if (key.startsWith('t_')) {
result[key.replace(/^t_/, '')] = argv[key];
} else {
result[key] = argv[key];
}
}
return result;
}

class AddPlugin {
constructor() {
this.cwd = process.cwd();
Expand All @@ -83,7 +96,7 @@ class AddPlugin {
type: 'string',
},
npm: {
usage: 'npm registry',
usage: 'npm client',
type: 'string',
},
registry: {
Expand Down Expand Up @@ -149,8 +162,15 @@ class AddPlugin {
}
this.projectName = projectPath;

const projectDirPath = join(this.cwd, projectPath);
if (existsSync(projectDirPath)) {

// 处理绝对路径
if (isAbsolute(projectPath)) {
this.projectDirPath = projectPath;
} else {
this.projectDirPath = join(this.cwd, projectPath);
}
// 检查是否存在
if (existsSync(this.projectDirPath)) {
const isOverwritten = await new (enquirer).Confirm({
name: 'question',
message: `The name '${projectPath}' already exists, can it be overwritten?`,
Expand All @@ -159,19 +179,21 @@ class AddPlugin {
if (!isOverwritten) {
process.exit();
}
await remove(projectDirPath);
await remove(this.projectDirPath);
}
this.projectDirPath = projectDirPath;
}

async generator() {
const { projectDirPath, template } = this;
let { projectDirPath, template } = this;
if (!template) {
return;
}
let type = 'npm';
if (template[0] === '.' || template[0] === '/') {
type = 'local';
if (!isAbsolute(template)) {
template = join(this.cwd, template);
}
}
const spin = new Spin({
text: 'Downloading Boilerplate...',
Expand All @@ -195,7 +217,7 @@ class AddPlugin {
targetPath: projectDirPath,
});
}
await generator.run();
await generator.run(formatUserArgv(this.argv));
spin.stop();
} catch (e) {
spin.stop();
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"fs-extra": "^8.1.0",
"light-generator": "^1.6.1",
"light-spinner": "^1.0.1",
"@vercel/ncc": "^0.28.0"
"@vercel/ncc": "^0.28.0",
"jest": "29"
},
"engines": {
"node": ">= 10.0.0"
Expand All @@ -29,7 +30,7 @@
],
"scripts": {
"build": "ncc build lib/index.js -o dist",
"test": "npm run test"
"test": "jest"
},
"publishConfig": {
"access": "public"
Expand Down
50 changes: 50 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { remove, existsSync, readFileSync } = require('fs-extra');
const { AddPlugin } = require('../lib/index');
const { join } = require('path');

const testProject = join(__dirname, 'test-project');
describe('test/index.test.js', () => {

beforeAll(async () => {
if (existsSync(testProject)) {
await remove(testProject);
}
});

beforeEach(async () => {
// mock cwd
const spy = jest.spyOn(process, 'cwd');
spy.mockReturnValue(__dirname);
});

afterEach(async () => {
jest.restoreAllMocks();
if (existsSync(testProject)) {
await remove(testProject);
}
})

it('test create with target dir', async () => {
jest.replaceProperty(process, 'argv', ['node', 'create-midway-dev', '--type=koa-v3', testProject]);
await new AddPlugin().run();
expect(existsSync(join(testProject, 'package.json'))).toBeTruthy();
expect(existsSync(join(testProject, 'src'))).toBeTruthy();
})

it('test create with target dir and type', async () => {
jest.replaceProperty(process, 'argv', ['node', 'create-midway-dev', '--template=./tpl', `--target=${testProject}`]);
await new AddPlugin().run();
expect(existsSync(join(testProject, 'package.json'))).toBeTruthy();
expect(existsSync(join(testProject, 'test.js'))).toBeTruthy();
})

it('test create with target dir and absolute template', async () => {
jest.replaceProperty(process, 'argv', ['node', 'create-midway-dev', `-t=${join(__dirname, './tpl')}`, '--bbb=cc', '--t_template=dd', testProject]);
await new AddPlugin().run();
expect(existsSync(join(testProject, 'package.json'))).toBeTruthy();
expect(existsSync(join(testProject, 'test.js'))).toBeTruthy();
expect(readFileSync(join(testProject, 'test.js'), 'utf8')).toMatch('cc');
expect(readFileSync(join(testProject, 'test.js'), 'utf8')).toMatch('dd');
})

});
9 changes: 9 additions & 0 deletions test/tpl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "demo",
"boilerplateConfig": {
"root": ".",
"replaceFile": [
"test.js"
]
}
}
3 changes: 3 additions & 0 deletions test/tpl/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log('abc')
console.log('{{bbb}}')
console.log('{{template}}')

0 comments on commit 8949ecf

Please sign in to comment.