Skip to content

Commit

Permalink
feat(script): add version command
Browse files Browse the repository at this point in the history
  • Loading branch information
liuweiGL committed Jul 2, 2021
1 parent 092be19 commit eb50103
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 32 deletions.
19 changes: 1 addition & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,8 @@
"name": "root",
"private": true,
"license": "MIT",
"engines": {
"pnpm": ">=6",
"npm": ">=7"
},
"workspaces": [
"packages/*"
],
"scripts": {
"dev": "npm run dev --workspace=vite-plugin-mkcert",
"build": "npm run build --workspace=vite-plugin-mkcert",
"preinstall": "npx only-allow pnpm",
"preversion": "npm run build",
"version": "npm version -ws --git-tag-version",
"postversion": "npm run changelog --workspace=script",
"version:alpha": "npm run version prerelease --preid alpha",
"version:patch": "npm run version patch",
"version:minor": "npm run version minor",
"version:major": "npm run version major",
"postpublish": "npm run release --workspace=script"
"preinstall": "npx only-allow pnpm"
},
"devDependencies": {
"@types/debug": "^4.1.6",
Expand Down
15 changes: 14 additions & 1 deletion packages/script/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
"version": "1.1.0",
"private": true,
"scripts": {
"version:alpha": "ts-node src/version.ts prepatch",
"version:patch": "ts-node src/version.ts patch",
"version:minor": "ts-node src/version.ts minor",
"version:major": "ts-node src/version.ts major",
"changelog": "ts-node src/index.ts changelog && git add -A",
"release": "ts-node src/index.ts release"
"github": "ts-node src/index.ts release",
"release:alpha": "pnpm version:alpha && pnpm changelog",
"release:patch": "pnpm version:patch && pnpm changelog",
"release:minor": "pnpm version:minor && pnpm changelog",
"release:major": "pnpm version:major && pnpm changelog",
"prepublishOnly": "pnpm build --filter vite-plugin-mkcert"
},
"devDependencies": {
"execa": "^5.1.1",
Expand All @@ -14,5 +23,9 @@
"semver": "^7.3.5",
"string-similarity": "^4.0.4",
"ts-node": "^10.0.0"
},
"dependencies": {
"@types/fs-extra": "^9.0.11",
"@types/semver": "^7.3.6"
}
}
3 changes: 3 additions & 0 deletions packages/script/src/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "1.1.0"
}
27 changes: 15 additions & 12 deletions packages/script/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ghRelease from 'gh-release'
import fs from 'fs-extra'
import path from 'path'
import moment from 'moment'
import { compareTwoStrings } from 'string-similarity'
import {
Expand All @@ -10,8 +9,12 @@ import {
getCurrentBranch,
getGithubToken,
getSortableAllTags,
getTaggedTime,
getTaggedTime
} from './git'
import { resolveRoot } from './util'
import config from './config.json'

const { version: latestVersion } = config

const ReleaseTitle = 'vite-plugin-mkcert release 🚀'

Expand All @@ -26,7 +29,7 @@ const CommitGroupBy: Array<[string, string[]]> = [
[':rocket: Improve Performance', ['perf']],
[':hammer_and_wrench: Update Workflow Scripts', ['build']],
[':construction: Add/Update Test Cases', ['test']],
[':blush: Other Changes', ['chore']],
[':blush: Other Changes', ['chore']]
]

const isPublishMessage = (str: string) => {
Expand All @@ -37,7 +40,7 @@ const isPublishMessage = (str: string) => {
const getCurrentChanges = (from = lastTag(), to = 'HEAD') => {
const summarys = []
return listCommits(from, to).filter(({ summary }) => {
if (summarys.some((target) => compareTwoStrings(target, summary) > 0.5))
if (summarys.some(target => compareTwoStrings(target, summary) > 0.5))
return false
if (isPublishMessage(summary)) return false
summarys.push(summary)
Expand All @@ -49,12 +52,12 @@ const getGroupChanges = (from = lastTag(), to = 'HEAD') => {
const changes = getCurrentChanges(from, to)
const results: Array<[string, string[]]> = CommitGroupBy.map(([group]) => [
group,
[],
[]
])
changes.forEach(({ summary, author, sha }) => {
for (const [group, value] of CommitGroupBy) {
if (value.some((target) => new RegExp(target).test(summary))) {
results.forEach((item) => {
if (value.some(target => new RegExp(target).test(summary))) {
results.forEach(item => {
if (item[0] === group) {
item[1].push(
`[${summary}](${GithubRepo}/commit/${sha}) :point_right: ( [${author}](https://github.com/${author}) )`
Expand All @@ -71,7 +74,7 @@ const getGroupChanges = (from = lastTag(), to = 'HEAD') => {

const createChangelog = (from = lastTag(), to = 'HEAD') => {
const isHead = to === 'HEAD'
const headVersion = isHead ? to : to
const headVersion = isHead ? latestVersion : to
const changes = getGroupChanges(from, to)
const nowDate = isHead
? moment().format('YYYY-MM-DD')
Expand All @@ -81,7 +84,7 @@ const createChangelog = (from = lastTag(), to = 'HEAD') => {
return `
### ${group}
${contents
.map((content) => {
.map(content => {
return `
1. ${content}
`
Expand Down Expand Up @@ -121,8 +124,8 @@ const createReleaseNote = () => {
repo: 'vite-plugin-mkcert',
endpoint: 'https://api.github.com',
auth: {
token,
},
token
}
},
(err: unknown, response: unknown) => {
if (err) {
Expand Down Expand Up @@ -150,7 +153,7 @@ ${tags
})
.join('')}
`
fs.writeFileSync(path.resolve(__dirname, '../../../CHANGELOG.md'), file, 'utf8')
fs.writeFileSync(resolveRoot('CHANGELOG.md'), file, 'utf8')
}

if (process.argv.includes('release')) {
Expand Down
5 changes: 5 additions & 0 deletions packages/script/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import path from 'path'

export const resolveRoot = (...paths: string[]) => {
return path.resolve(__dirname, '../../../', ...paths)
}
75 changes: 75 additions & 0 deletions packages/script/src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import fs from 'fs-extra'
import path from 'path'
import semver from 'semver'
import execa from 'execa'
import { resolveRoot } from './util'
import config from './config.json'

const { version: currentVersion } = config

type Options = {
releaseType: semver.ReleaseType
preid?: string
}

const getNewVersion = ({ releaseType, preid }: Options) => {
return semver.inc(currentVersion, releaseType, preid)
}

const updatePackage = async (version: string) => {
const packagesPath = resolveRoot('packages')
const packages = fs.opendirSync(packagesPath)

for await (const item of packages) {
if (item.isDirectory()) {
const pkgPath = path.resolve(packagesPath, item.name, 'package.json')
const pkgObj = fs.readJSONSync(pkgPath)

fs.writeJsonSync(
pkgPath,
{
...pkgObj,
version
},
{
spaces: 2
}
)
}
}
}

const updateConfig = (version: string) => {
const configPath = path.resolve(__dirname, 'config.json')
const configObj = fs.readJSONSync(configPath)

fs.writeJsonSync(
configPath,
{
...configObj,
version
},
{
spaces: 2
}
)
}

const tagGit = (version: string) => {
execa.sync('git', ['add', '-A'])
execa.sync('git', ['commit', '-m', `chore: publish ${version}`])
execa.sync('git', ['tag', version])
}

;(() => {
const [, , releaseType = 'minor', preid = 'alpha'] = process.argv

const version = getNewVersion({
releaseType: releaseType as semver.ReleaseType,
preid
})

updatePackage(version)
updateConfig(version)
tagGit(version)
})()
1 change: 1 addition & 0 deletions packages/script/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"module": "commonjs",
"target": "ESNext",
Expand Down
20 changes: 19 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eb50103

Please sign in to comment.