Skip to content

Commit

Permalink
"Initialize by X-BUILD"
Browse files Browse the repository at this point in the history
  • Loading branch information
codexu committed Jun 30, 2021
1 parent 7a837aa commit e47c1d2
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
node_modules
/test
/bin
/dist
package-lock.json
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"x": "./bin/x-build.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node-dev --respawn --transpile-only src/index.ts",
"test": "rm -rf bin && rm -rf test && tsc --build && sudo npm link && x create test",
"dev": "ts-node-dev --respawn --transpile-only src/x-build.ts",
"build": "rm -rf bin && tsc --build"
},
"repository": {
Expand All @@ -25,6 +25,7 @@
"chalk": "^4.1.0",
"commander": "^6.1.0",
"ejs": "^3.1.6",
"execa": "^5.1.1",
"fs-extra": "^9.0.1",
"prettier": "^2.3.2"
},
Expand Down
60 changes: 31 additions & 29 deletions src/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@
import fs = require('fs-extra');
import chalk = require('chalk');
import path = require('path');
import { spawn } from 'child_process';
// import { spawn } from 'child_process';
import clearConsole from './utils/clearConsole';
import execa = require('execa');
import createTemplate from './createTemplate';
import options from './options';

let startTime, endTime;
let startTime: number, endTime: number;

export default async function (name: string): Promise<void> {
// CLI 模板文件夹路径
const src = path.resolve(__dirname, '../template');
// 目标路径
const dest = path.resolve(process.cwd(), name);
const writeTemplate = createTemplate(dest)
// 获取基础参数
options.name = name;
options.dest = path.resolve(process.cwd(), name);
const writeTemplate = createTemplate(options.dest)
// const execa = createExeca(options.dest)

// startTime = new Date().getTime()
startTime = new Date().getTime()
clearConsole('cyan', `X-BUILD v${options.version}`);
console.log(`> 项目模板生成于目录: ${chalk.yellow(dest)}`);
console.log(`> 项目模板生成于目录: ${chalk.yellow(options.dest)}`);
// 拷贝模板文件
await fs.copy(src, dest);
await fs.copy(src, options.dest);
await writeTemplate('package.json');
await writeTemplate('src/test.ts');

await spawnCmd(dest, null, 'git', ['init'])
await spawnCmd(dest, null, 'git', ['add .'])
await spawnCmd(dest, null, 'git', ['commit -m "Initialize by X-BUILD"'])
await execa('git', ['init']);
await execa('git', ['add', '.']);
await execa('git', ['commit', '-m', '"Initialize by X-BUILD"']);
console.log(`> 成功初始化 Git 仓库`);
console.log(`> 正在自动安装依赖,请稍等...`);
console.log('');
await spawnCmd(dest, [0, 1, 2], 'npm', ['install']);
await execa('npm', ['install']);
clearConsole('cyan', `Mapwhale v${options.version}`);
endTime = new Date().getTime();
const usageTime = (endTime - startTime) / 1000
Expand All @@ -44,20 +46,20 @@ export default async function (name: string): Promise<void> {
* 安装依赖指令
* @param {string} dest 需要执行指令的路径
*/
function spawnCmd(dest: string, stdio = [0, 1, 2], cmd: string, instruction) {
const ls = spawn(cmd, instruction, {
cwd: dest,
stdio: stdio,
shell: true
});
return new Promise((resolve, reject) => {
ls.on('close', (code) => {
if (code === 0) {
resolve(true)
} else {
reject();
}
});
})
// function spawnCmd(dest: string, stdio = [0, 1, 2], cmd: string, instruction) {
// const ls = spawn(cmd, instruction, {
// cwd: dest,
// stdio: stdio,
// shell: true
// });
// return new Promise((resolve, reject) => {
// ls.on('close', (code) => {
// if (code === 0) {
// resolve(true)
// } else {
// reject();
// }
// });
// })

}
// }
20 changes: 17 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
export default {
interface Options {
name: string
version: string
src: string
dest: string
components: {
[key: string]: boolean
}
}

const options: Options = {
name: '',
version: '',
src: '',
dest: '',
components: {
elementPlus: false
elementPlus: false,
}
}
}

export default options
4 changes: 0 additions & 4 deletions src/typings.d.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/utils/createExeca.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import execa = require('execa');

export default function createExeca (root: string) {
return function (cmd: string, args?: string[]): void {
execa(cmd, args, {
cwd: root,
stdio: [2, 2, 2],
});
}
}
8 changes: 4 additions & 4 deletions src/utils/hasDir.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import fs = require('fs');
import { access, constants } from 'fs';
import chalk = require('chalk');

export default function(name: string): Promise<boolean> {
return new Promise ((resolve) => {
fs.access(name, error => {
access(name, constants.F_OK, error => {
if (error) {
resolve(true);
} else {
console.log(chalk.red(
`The ${name} folder already exists in the current directory. Please try to use another project name!`
));
process.exit(1);
} else {
resolve(true);
}
});
});
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"exclude": [
"bin",
"test",
"documents",
"template",
]
Expand Down

0 comments on commit e47c1d2

Please sign in to comment.