Skip to content

Commit

Permalink
feat: support develop script (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lzzzs authored Dec 20, 2023
1 parent f6b147d commit 99fc77a
Show file tree
Hide file tree
Showing 24 changed files with 657 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-trains-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pv-script": patch
---

support develop script
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## template
```json
%name%
%time%
%version%
%type%
```
155 changes: 152 additions & 3 deletions bin/pv-script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,154 @@
function two() {
return 1 + 2
import { Command } from 'commander';
import prompts from 'prompts';
import chalk from 'chalk';
import { log } from 'console';
import { spawn } from 'child_process';

var name = "pv-script";
var version = "0.0.0";
var description = "A project git/version script";

function getCurrentTime() {
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const currentMonth = padZero(currentDate.getMonth() + 1);
const currentDay = padZero(currentDate.getDate());
return `${currentYear}-${currentMonth}-${currentDay}`;
}
function padZero(number) {
return number < 10 ? `0${number}` : number;
}

function validateTemplate(str) {
// 定义模板正则表达式
const templateRegex = /^([^%]*%[^%]*%[^%]*)*$|^([^%]*)$/;
return templateRegex.test(str);
}
function replaceVariables(str, variables) {
return str.replace(/%([^%]+)%/g, (match, variableName) => {
return variables.hasOwnProperty(variableName) ? variables[variableName] : match;
});
}

const switchMasterCommand = (main) => `git switch ${main}`;
const pullMasterCommand = 'git pull';
const checkoutCommand = (branchName) => `git checkout -b ${branchName}`;

const questions = [
{
type: 'select',
name: 'type',
message: '请选择要切换的类型分支',
choices: [
{ title: 'feature', description: '功能分支', value: 'feature' },
{ title: 'hotfix', description: '修复分支', value: 'hotfix' },
],
},
{
type: 'text',
name: 'branch',
message: '请填写分支名称',
validate: value => value.length > 0 ? true : '分支名称不能为空',
},
];
async function developAction(options) {
const { template, main } = options;
if (!validateTemplate(template)) {
log(chalk.bgRedBright('模板名称不合法'));
return;
}
const { type, branch: name } = await prompts(questions);
// 用户强制退出的情况
if (type === undefined || name === undefined)
return;
// develop 只有三个变量 type、name、time
const branchName = replaceVariables(template, { type, name, time: getCurrentTime() });
const switchProcess = spawn(switchMasterCommand(main), {
stdio: 'inherit',
shell: true,
});
switchProcess.on('close', (switchCode) => {
if (switchCode !== 0) {
log(chalk.bgRedBright('切换 master 分支失败'));
return;
}
log(chalk.green('🎉 切换 master 分支成功'));
const pullProcess = spawn(pullMasterCommand, {
stdio: 'inherit',
shell: true,
});
pullProcess.on('close', (pullCode) => {
if (pullCode !== 0) {
log(chalk.bgRedBright('拉取 master 代码失败'));
return;
}
log(chalk.green('🎉 拉取 master 代码成功'));
spawn(checkoutCommand(branchName), {
stdio: 'inherit',
shell: true,
}).on('close', (checkoutCode) => {
if (checkoutCode !== 0) {
log(chalk.bgRedBright('创建分支失败'));
return;
}
log(chalk.green('🎉 分支切换成功'));
});
});
});
}

const DEFAULT_MAIN_BRANCH = 'master';
const DEFAULT_RELEASE_BRANCH = 'release/%version%-%name%';

function createDevelopCommand() {
return {
name: "develop",
description: "develop",
action: developAction,
options: [
{
flags: "-t, --template <template>",
description: "set template name",
defaultValue: DEFAULT_RELEASE_BRANCH
},
{
flags: "-m, --main <branch>",
description: "set main branch name",
defaultValue: DEFAULT_MAIN_BRANCH
}
]
};
}

var createCommandFunctions = [
createDevelopCommand
];

function registerCommand (program) {
createCommandFunctions.forEach(fn => {
const { name, description, action, options } = fn();
const command = program.command(name).description(description).action(action);
options?.forEach(({ flags, description, defaultValue }) => {
command.option(flags, description, defaultValue);
});
});
program.command('test')
.description('Split a string into substrings and display as an array')
.argument('<string>', 'string to split')
.option('--first', 'display just the first substring', () => {
console.log(6);
})
.option('-s, --separator <char>', 'separator character', ',')
.action((str, options) => {
const limit = options.first ? 1 : undefined;
console.log(str.split(options.separator, limit));
});
}

console.log(two())
const program = new Command();
program
.name(name)
.description(description)
.version(version);
registerCommand(program);
program.parse();
6 changes: 5 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import antfu from '@antfu/eslint-config'

export default antfu()
export default antfu({
rules: {
"no-prototype-builtins": "off"
}
})
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pv-script",
"type": "module",
"version": "0.0.0",
"description": "a project git/version script",
"description": "A project git/version script",
"author": "Lzzzs",
"license": "MIT",
"keywords": [
Expand All @@ -25,6 +25,7 @@
"homepage": "https://github.com/Lzzzs/pv-script",
"bugs": "https://github.com/Lzzzs/pv-script/issues",
"scripts": {
"dev": "pnpm build && node bin/index.js",
"build": "rollup -c",
"prepare": "husky install",
"test": "vitest run",
Expand All @@ -46,6 +47,9 @@
"@antfu/eslint-config": "^2.3.4",
"@changesets/cli": "^2.27.1",
"@commitlint/config-conventional": "^18.4.3",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-json": "^6.0.1",
"@types/prompts": "^2.4.9",
"chalk": "^5.3.0",
"commander": "^11.1.0",
"commitizen": "^4.3.0",
Expand All @@ -56,6 +60,9 @@
"lint-staged": "^15.2.0",
"prompts": "^2.4.2",
"rollup": "^4.6.1",
"rollup-plugin-typescript2": "^0.36.0",
"tslib": "^2.6.2",
"typescript": "^5.3.2",
"vitest": "^1.0.1"
},
"lint-staged": {
Expand Down
Loading

0 comments on commit 99fc77a

Please sign in to comment.