-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39a0756
commit b3cbb72
Showing
12 changed files
with
270 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters