Skip to content

Commit

Permalink
feat: set-project 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
StreakingMan committed Feb 24, 2022
1 parent 39a0756 commit b3cbb72
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 23 deletions.
80 changes: 80 additions & 0 deletions actions/set-project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const { execSync } = require('child_process');
const { batchInstall } = require('../utils/batch-install');
const { fileGenerator } = require('../utils/file-generator');
const inquirer = require('inquirer');

const setProject = async () => {
const {
hasEslintAndPrettier,
hasCommitlintAndLintstagedAndHusky,
hasStandardVersion,
} = await inquirer.prompt([
{
type: 'confirm',
name: 'hasEslintAndPrettier',
message: '是否使用eslint+prettier',
default: true,
},
{
type: 'confirm',
name: 'hasCommitlintAndLintstagedAndHusky',
message: '是否使用commitlint+lintstaged+husky?',
default: true,
},
{
type: 'confirm',
name: 'hasStandardVersion',
message: '是否使用standard-version管理版本号?',
default: true,
},
]);
if (hasEslintAndPrettier) {
batchInstall(
[
'eslint',
'prettier',
'eslint-config-prettier',
'eslint-plugin-prettier',
],
{
dev: true,
}
);
fileGenerator({ templateName: 'prettier' });
fileGenerator({ templateName: 'eslint' });
fileGenerator({ templateName: 'eslintignore' });
}
if (hasCommitlintAndLintstagedAndHusky) {
batchInstall(
[
'@commitlint/cli',
'@commitlint/config-conventional',
'lint-staged',
'husky',
],
{ dev: true }
);
// husky安装与钩子配置
execSync('npx husky install');
execSync('npm set-script prepare "husky install"');
execSync('npx husky add .husky/pre-commit "npx lint-staged"');
execSync(
'npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"'
);
fileGenerator({ templateName: 'commitlint' });
fileGenerator({ templateName: 'lintstaged' });
}
if (hasStandardVersion) {
batchInstall(['standard-version'], { dev: true });
execSync(
'npm set-script release:first "standard-version -- --first-release"'
);
execSync('npm set-script release "standard-version"');
}
fileGenerator({ templateName: 'editor' });
console.log('依赖安装完成,已生成基础配置');
};

module.exports = {
setProject,
};
49 changes: 27 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
#!/usr/bin/env node
const { execSync } = require("child_process");
const { checkGit } = require("./actions/check-git");
const { setGit } = require("./actions/set-git");
const { program } = require("commander");
const fs = require("fs");
const path = require("path");
const { execSync } = require('child_process');
const { checkGit } = require('./actions/check-git');
const { setGit } = require('./actions/set-git');
const { setProject } = require('./actions/set-project');
const { program } = require('commander');
const fs = require('fs');
const path = require('path');
const { version } = JSON.parse(
fs.readFileSync(path.join(__dirname, "./package.json")).toString()
fs.readFileSync(path.join(__dirname, './package.json')).toString()
);
const CLINAME = "skm";
const CLINAME = 'skm';

// 检查版本
const latestVersion = execSync("npm view @tuya-fe/www-cli version");
const [x1, y1, z1] = latestVersion.toString().trim().split(".");
const [x2, y2, z2] = version.split(".");
const latestVersion = execSync('npm view @tuya-fe/www-cli version');
const [x1, y1, z1] = latestVersion.toString().trim().split('.');
const [x2, y2, z2] = version.split('.');
if (!(x1 === x2 && y1 === y2 && z1 <= z2)) {
console.log(`发现新版本${CLINAME}@${version},请更新`);
console.log(`发现新版本${CLINAME}@${version},请更新`);
}

program
.version(`${CLINAME}@${version}`, "-v")
.helpOption("-h", "帮助信息")
.usage("<command> [options]")
.name(CLINAME);
.version(`${CLINAME}@${version}`, '-v')
.helpOption('-h', '帮助信息')
.usage('<command> [options]')
.name(CLINAME);

program
.command("check-git")
.description("查看当前仓库git配置用户名和邮箱")
.action(checkGit);
.command('check-git')
.description('查看当前仓库git配置用户名和邮箱')
.action(checkGit);
program
.command("set-git")
.description("配置当前仓库git配置用户名和邮箱")
.action(setGit);
.command('set-git')
.description('配置当前仓库git配置用户名和邮箱')
.action(setGit);
program
.command('set-project')
.description('安装lint和工程化依赖以及简单配置')
.action(setProject);

program.showHelpAfterError(`${CLINAME} -h 查看帮助`);
program.addHelpCommand(false);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
"devDependencies": {
"@commitlint/cli": "^16.2.1",
"@commitlint/config-conventional": "^16.2.1",
"ejs": "^3.1.6",
"eslint": "7",
"eslint-config-prettier": "^8.4.0",
"eslint-plugin-prettier": "^4.0.0",
"fs-extra": "^10.0.1",
"husky": "^7.0.4",
"lint-staged": "^12.3.4",
"prettier": "^2.5.1",
Expand Down
9 changes: 9 additions & 0 deletions templates/commitlint.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* commitlint config
* @ref http://commitlint.js.org/
* @desc generated at <%- generatedAt %> by streakingman-cli@<%- version %>
*/

module.exports = {
extends: ['@commitlint/config-conventional'],
};
15 changes: 15 additions & 0 deletions templates/editor.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# https://editorconfig.org/
# generated at <%- generatedAt %> by streakingman-cli@<%- version %>
# 该配置文件是给webstorm使用的
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
19 changes: 19 additions & 0 deletions templates/eslint.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* eslint config
* @ref http://eslint.cn/
* @desc generated at <%- generatedAt %> by streakingman-cli@<%- version %>
*/

module.exports = {
parserOptions: {
ecmaVersion: 11,
},
env: {
es6: true,
},
plugins: ['eslint-plugin-prettier'],
extends: ['plugin:prettier/recommended'],
rules: {
'prettier/prettier': 'error',
},
};
6 changes: 6 additions & 0 deletions templates/eslintignore.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# generated at <%- generatedAt %> by streakingman-cli@<%- version %>

!.eslintrc.js
!.prettierrc.js
!.commitlintrc.js
!.lintstagedrc.js
14 changes: 14 additions & 0 deletions templates/lintstaged.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* lint-staged config
* @ref https://www.npmjs.com/package/lint-staged
* @desc generated at <%- generatedAt %> by streakingman-cli@<%- version %>
*/

module.exports = {
'*.{[tj]s,[tj]sx,[cm]js}': [
'eslint --fix'
],
'*.json': [
'prettier --write'
]
};
12 changes: 12 additions & 0 deletions templates/prettier.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* prettier config
* @ref https://prettier.io/
* @desc generated at <%- generatedAt %> by streakingman-cli@<%- version %>
*/

module.exports = {
tabWidth: 4,
singleQuote: true,
htmlWhitespaceSensitivity: 'css',
endOfLine: 'lf'
}
16 changes: 16 additions & 0 deletions utils/batch-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { execSync } = require('child_process');

const batchInstall = (deps, { dev }) => {
for (const dep of deps) {
console.log(`🚓 正在安装 ${dep} ...`);
try {
execSync(`yarn add ${dep} ${dev ? '--dev' : ''}`, { stdio: [2] });
} catch (e) {
console.log(`❌ ${dep} 安装失败:${e}`);
}
}
};

module.exports = {
batchInstall,
};
40 changes: 40 additions & 0 deletions utils/file-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { version } = require('../package.json');
const { execSync } = require('child_process');
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const ejs = require('ejs');

const generatorTemplateFileMap = {
commitlint: '.commitlintrc.js',
editor: '.editorconfig',
eslint: '.eslintrc.js',
eslintignore: '.eslintignore',
lintstaged: '.lintstagedrc.js',
prettier: '.prettierrc.js',
};

const fileGenerator = ({ templateName }) => {
const cwd = process.cwd();
const file = path.join(
path.join(__dirname, '../templates'),
`${templateName}.ejs`
);
const template = fs.readFileSync(file, 'utf8');
const data = {
generatedAt: new Date().toLocaleString(),
version,
};
const filename = generatorTemplateFileMap[templateName];

fse.outputFileSync(path.join(cwd, filename), ejs.render(template, data));

// 格式化生成配置文件
if (filename.endsWith('.js')) {
execSync(`prettier --write ${path.join(cwd, filename)}`);
}
};

module.exports = {
fileGenerator,
};
31 changes: 30 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ astral-regex@^2.0.0:
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31"
integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==

async@0.9.x:
version "0.9.2"
resolved "https://registry.yarnpkg.com/async/-/async-0.9.2.tgz#aea74d5e61c1f899613bf64bda66d4c78f2fd17d"
integrity sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0=

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
Expand Down Expand Up @@ -893,6 +898,13 @@ eastasianwidth@^0.2.0:
resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==

ejs@^3.1.6:
version "3.1.6"
resolved "https://registry.yarnpkg.com/ejs/-/ejs-3.1.6.tgz#5bfd0a0689743bb5268b3550cceeebbc1702822a"
integrity sha512-9lt9Zse4hPucPkoP7FHDF0LQAlGyF9JVpnClFLFH3aSSbxmyoqINRpp/9wePWJTUl4KOQwRL72Iw3InHPDkoGw==
dependencies:
jake "^10.6.1"

emoji-regex@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
Expand Down Expand Up @@ -1116,6 +1128,13 @@ file-entry-cache@^6.0.1:
dependencies:
flat-cache "^3.0.4"

filelist@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/filelist/-/filelist-1.0.2.tgz#80202f21462d4d1c2e214119b1807c1bc0380e5b"
integrity sha512-z7O0IS8Plc39rTCq6i6iHxk43duYOn8uFJiWSewIq0Bww1RNybVHSCjahmcC87ZqAm4OTvFzlzeGu3XAzG1ctQ==
dependencies:
minimatch "^3.0.4"

fill-range@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40"
Expand Down Expand Up @@ -1173,7 +1192,7 @@ fs-access@^1.0.1:
dependencies:
null-check "^1.0.0"

fs-extra@^10.0.0:
fs-extra@^10.0.0, fs-extra@^10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.0.1.tgz#27de43b4320e833f6867cc044bfce29fdf0ef3b8"
integrity sha512-NbdoVMZso2Lsrn/QwLXOy6rm0ufY2zEOKCDzJR/0kBsb0E6qed0P3iYK+Ath3BfvXEeu4JhEtXLgILx5psUfag==
Expand Down Expand Up @@ -1499,6 +1518,16 @@ isexe@^2.0.0:
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=

jake@^10.6.1:
version "10.8.2"
resolved "https://registry.yarnpkg.com/jake/-/jake-10.8.2.tgz#ebc9de8558160a66d82d0eadc6a2e58fbc500a7b"
integrity sha512-eLpKyrfG3mzvGE2Du8VoPbeSkRry093+tyNjdYaBbJS9v17knImYGNXQCUV0gLxQtF82m3E8iRb/wdSQZLoq7A==
dependencies:
async "0.9.x"
chalk "^2.4.2"
filelist "^1.0.1"
minimatch "^3.0.4"

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down

0 comments on commit b3cbb72

Please sign in to comment.