Skip to content

Commit d646bd7

Browse files
committed
build: add build and publish workflow
1 parent f82080b commit d646bd7

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: vscode-extension
2+
on:
3+
workflow_dispatch:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
jobs:
9+
build-and-publish:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: 20.x
16+
- run: npm install
17+
- run: npx vsce package -o packages
18+
- run: npx vsce publish --packagePath packages/*.vsix
19+
if: success() && startsWith(github.ref, 'refs/tags/')
20+
- uses: fnkr/github-action-ghr@v1
21+
if: success() && startsWith(github.ref, 'refs/tags/')
22+
env:
23+
GHR_DELETE: true
24+
GHR_PATH: packages/
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ node_modules
33
/dist
44
/out
55
/.vscode-test
6-
/*.vsix
6+
/packages

.vscodeignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ webpack.config.js
1616
**/*.ts
1717
**/.vscode-test.*
1818
build/**
19+
packages/**

publish-version.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* eslint-disable no-console */
2+
3+
const fs = require('fs')
4+
const childProcess = require('child_process')
5+
6+
const writeFileAndGitAdd = (p, content) => {
7+
fs.writeFileSync(p, content)
8+
if (childProcess.spawnSync('git', ['add', p]).status !== 0) {
9+
throw new Error(`failed to execute git add on ${p}`)
10+
}
11+
}
12+
13+
// check arguments
14+
const version = process.argv[2]
15+
if (!version) {
16+
throw new Error('version not given in argv')
17+
}
18+
if (!/[0-9]+\.[0-9]+\.[0-9]+/.test(version)) {
19+
throw new Error('version illegal')
20+
}
21+
22+
// avoid eslint warnings
23+
;['vscode-extension'].forEach((p) => {
24+
console.info(`Run eslint on ${p}`)
25+
if (
26+
childProcess.spawnSync('npx', ['eslint', '-c', '.eslintrc.js', 'src'], {
27+
cwd: p,
28+
stdio: 'inherit',
29+
}).status !== 0
30+
) {
31+
throw new Error('failed to lint modules (are there eslint warnings or errors?)')
32+
}
33+
})
34+
35+
// check git status
36+
const gitStatusRes = childProcess.spawnSync('git', ['diff', '--name-only'], { encoding: 'utf8' })
37+
if (gitStatusRes.status !== 0 || gitStatusRes.stdout.length > 0) {
38+
throw new Error('failed to check git status (are there uncommitted changes?)')
39+
}
40+
41+
// change npm version
42+
;['package.json'].forEach((p) => {
43+
let content = fs.readFileSync(p, { encoding: 'utf8' })
44+
let oldVersion
45+
const refVersions = []
46+
content = content.replace(/"version": "(.+)"/, (_, v) => {
47+
oldVersion = v
48+
return `"version": "${version}"`
49+
})
50+
if (!oldVersion) {
51+
throw new Error(`version segment not found in ${p}`)
52+
}
53+
console.info(`Update ${p} version from "${oldVersion}" to "${version}"`)
54+
refVersions.forEach(({ mod, v }) => {
55+
console.info(` + dependency ${mod} version from "${v}" to "${version}"`)
56+
})
57+
writeFileAndGitAdd(p, content)
58+
})
59+
60+
// npm test
61+
console.info(`Run npm test`)
62+
if (childProcess.spawnSync('npm', ['test'], { stdio: 'inherit' }).status !== 0) {
63+
throw new Error('failed to run npm test')
64+
}
65+
66+
// add lock files
67+
;['package.json'].forEach((p) => {
68+
if (childProcess.spawnSync('git', ['add', p]).status !== 0) {
69+
throw new Error(`failed to execute git add on ${p}`)
70+
}
71+
})
72+
73+
// git commit
74+
if (
75+
childProcess.spawnSync('git', ['commit', '--message', `version: ${version}`], {
76+
stdio: 'inherit',
77+
}).status !== 0
78+
) {
79+
throw new Error('failed to execute git commit')
80+
}
81+
82+
// add a git tag and push
83+
console.info('Push to git origin')
84+
if (childProcess.spawnSync('git', ['tag', `v${version}`]).status !== 0) {
85+
throw new Error('failed to execute git tag')
86+
}
87+
if (childProcess.spawnSync('git', ['push'], { stdio: 'inherit' }).status !== 0) {
88+
throw new Error('failed to execute git push')
89+
}
90+
if (childProcess.spawnSync('git', ['push', '--tags'], { stdio: 'inherit' }).status !== 0) {
91+
throw new Error('failed to execute git push --tags')
92+
}
93+
94+
console.info('Version updated! Wait the remote actions to build and publish.')

0 commit comments

Comments
 (0)