Skip to content

Instantly share code, notes, and snippets.

@JackFGreen
Last active April 10, 2019 08:53
Show Gist options
  • Select an option

  • Save JackFGreen/fd652c895e92b56b86fd037f7af00a34 to your computer and use it in GitHub Desktop.

Select an option

Save JackFGreen/fd652c895e92b56b86fd037f7af00a34 to your computer and use it in GitHub Desktop.
ci
/**
* build to env
*/
const shell = require('shelljs')
const inquirer = require('inquirer')
const packageInfo = require('../package.json')
const fs = require('fs')
const path = require('path')
const compareVersions = require('compare-versions')
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git')
shell.exit(1)
}
const packagePath = path.resolve(__dirname, '../package.json')
const fileType = 'utf8'
function initInquirer (...prompt) {
return inquirer.prompt([...prompt])
}
const env = {
type: 'list',
name: 'env',
message: 'choose build env',
choices: ['test', 'prod'],
default: 'test'
}
const noconsole = {
type: 'confirm',
name: 'noconsole',
message: 'build noconsole',
default: false
}
const commit = {
type: 'input',
name: 'commit',
message: 'commit message',
validate (v) {
if (!v) return `commit message is required`
return true
}
}
buildInquirer()
async function buildInquirer () {
try {
const res = await initInquirer(env, noconsole, commit)
const build = runBuild(res.noconsole)
shell.exec(build)
shell.exec('git add dist')
shell.exec(`git commit -m "${res.commit}"`)
shell.exec('git log --oneline -10')
if (res.env === 'prod') {
prodInquirer()
} else {
pushInq(push)
}
} catch (err) {
console.log(err)
}
}
function runBuild (noconsole) {
let build = 'yarn build'
return noconsole ? `${build} --noconsole` : build
}
const oldVersion = packageInfo.version
const version = {
type: 'input',
name: 'version',
message: `update version from ${oldVersion} to`,
validate (v) {
if (compareVersions(v, oldVersion) !== 1) return `version must be greater than ${oldVersion}`
return true
}
}
async function prodInquirer () {
try {
commit.message = 'commit without prefix'
const res = await initInquirer(version, commit)
const newVersion = res.version
const pkgText = fs.readFileSync(packagePath, fileType)
const newPkgText = pkgText.replace(/("version": ")(\d+\.\d+\.\d+)/, `$1${newVersion}`)
fs.writeFileSync(packagePath, newPkgText, fileType)
shell.exec('git diff package.json')
shell.exec('git add package.json')
shell.exec(`git commit -m "feat: v${newVersion} ${res.commit}"`)
shell.exec('yarn changelog')
shell.exec('git add CHANGELOG.md')
shell.exec(`git commit -m "docs: changelog"`)
shell.exec('git log --oneline -10')
shell.exec(`git tag -a v${newVersion} -m "${res.commit}"`)
shell.exec('git tag -n')
pushInq(push, pushTag)
} catch (err) {
console.log(err)
}
}
const push = {
type: 'confirm',
name: 'push',
message: 'push all commits'
}
const pushTag = {
type: 'confirm',
name: 'pushTag',
message: 'push all tags'
}
async function pushInq (...inqs) {
try {
const res = await initInquirer(...inqs)
if (res.push) shell.exec('git push')
if (res.pushTag) shell.exec('git push --tag')
} catch (err) {
console.log(err)
}
}
/**
* prettier && standard lint && standard fix
*/
const shell = require('shelljs')
const { getFileRules } = require('./gitModifyFile')
const folders = 'src|build|config|ci'
function prettier () {
const ext = 'js|vue|scss|json'
const fileRules = getFileRules(ext, folders)
shell.exec(`prettier --config ./.prettierrc --write ${fileRules}`)
}
function lint () {
const ext = 'js|vue'
const fileRules = getFileRules(ext, folders)
shell.exec(`standard --plugin html ${fileRules} --fix --verbose | snazzy`)
shell.exec(`standard --plugin html ${fileRules} --verbose | snazzy`)
}
prettier()
lint()
/**
* get modify files
*/
const shell = require('shelljs')
const modifyFiles = shell
.exec('git status -s | cut -c4-', { silent: true })
.stdout.trim()
.split(/\s/g)
function getFilePath (fileName, ext, folders = 'src') {
const reg = new RegExp(`^(${folders})\\/.*\\.(${ext})$`)
if (reg.test(fileName)) return fileName
}
/**
* get modifyFile.`ext` in `folders`
* @param {String} ext js|vue
* @param {String} [folders=src] src|build
*/
function getFileRules (ext, folders) {
const files = []
modifyFiles.forEach(fileName => {
const filePath = getFilePath(fileName, ext, folders)
if (filePath) files.push(filePath)
})
const fileRules = files.join(' ')
if (fileRules.length) {
return fileRules
} else {
console.log(`[Modify Files] No modify files '${ext}' in '${folders}'`)
process.exit()
}
}
exports.getFileRules = getFileRules
{
"scripts": {
"prettier": "prettier --config ./.prettierrc --write 'src/**/*.{js,vue,scss,json}'",
"lint": "standard --plugin html 'src/**/*.{js,vue}' --verbose | snazzy",
"fix": "standard --plugin html 'src/**/*.{js,vue}' --fix --verbose | snazzy",
"format": "yarn prettier && yarn fix && yarn lint",
"commit": "npx git-cz",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"browserslist": "npx browserslist"
},
"devDependencies": {
"@commitlint/cli": "^7.4.0",
"@commitlint/config-angular": "^7.3.1",
"babel-eslint": "^10.0.1",
"conventional-changelog-cli": "^2.0.11",
"cz-conventional-changelog": "^2.1.0",
"eslint-plugin-html": "^3.2.2",
"husky": "^1.3.1",
"prettier": "^1.16.1",
"snazzy": "^8.0.0",
"standard": "^12.0.1"
},
"standard": {
"plugins": [
"html"
],
"parser": "babel-eslint",
"parserOptions": {
"parser": "babel-eslint"
},
"ignore": [
"node_modules/**",
"dist/**",
"static/**",
"test/**"
]
},
"commitlint": {
"extends": [
"@commitlint/config-angular"
]
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "yarn lint"
}
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment