Skip to content

Commit 03b9ba5

Browse files
authored
ci: refactor build scripts, reduce jsr token permissions (#1320)
* ci: refactor build scripts, reduce jsr token permissions * test: update fixtures * test(size): update zx-lite paths
1 parent e2eb47b commit 03b9ba5

File tree

9 files changed

+106
-75
lines changed

9 files changed

+106
-75
lines changed

.github/workflows/dev-publish.yml

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ jobs:
2121
env:
2222
FORCE_COLOR: 3
2323
- run: |
24-
node scripts/build-jsr.mjs
25-
node scripts/prepublish-clean.mjs
24+
npm run build:jsr
25+
npm run build:lite
26+
npm run build:pkgjson
2627
- uses: actions/upload-artifact@v4
2728
with:
2829
name: build-${{ github.run_id }}
2930
path: |
3031
build
3132
jsr.json
3233
package.json
33-
node_modules/depseek
34+
package-lite.json
3435
retention-days: 1
3536

3637
npm-publish:
@@ -83,11 +84,33 @@ jobs:
8384
8485
- name: pushing lite snapshot to ${{ env.GOOGLE_NPM_REGISTRY }}
8586
run: |
86-
cat <<< $(jq '.name="zx"' package.json) > package.json
87-
node scripts/prepublish-lite.mjs
87+
mv package-lite.json package.json
8888
cat <<< $(jq '.version="${{ env.ZX_LITE_DEV_VERSION }}"' package.json) > package.json
8989
npm publish --provenance --access=public --no-git-tag-version --tag dev --registry https://${{ env.GOOGLE_NPM_REGISTRY }}
9090
91+
jsr-publish:
92+
needs: build
93+
runs-on: ubuntu-latest
94+
permissions:
95+
contents: read
96+
id-token: write
97+
steps:
98+
- uses: actions/checkout@v4
99+
with:
100+
persist-credentials: false
101+
- uses: actions/setup-node@v4
102+
with:
103+
node-version: 22
104+
cache: 'npm'
105+
- uses: actions/download-artifact@v4
106+
with:
107+
name: build-${{ github.run_id }}
108+
- name: Get zx version info
109+
run: |
110+
echo SHA_SHORT=$(git rev-parse --short HEAD) >> $GITHUB_ENV
111+
echo ZX_VERSION=$(jq -r '.version' package.json) >> $GITHUB_ENV
112+
- run: |
113+
echo ZX_DEV_VERSION="${{ env.ZX_VERSION }}-dev.${{ env.SHA_SHORT }}" >> $GITHUB_ENV
91114
- name: pushing to jsr.io
92115
run: |
93116
cat <<< $(jq '.version="${{ env.ZX_DEV_VERSION }}"' jsr.json) > jsr.json

.github/workflows/publish.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ jobs:
2323
env:
2424
FORCE_COLOR: 3
2525
- run: |
26-
node scripts/build-jsr.mjs
27-
node scripts/prepublish-clean.mjs
26+
npm run build:jsr
27+
npm run build:lite
28+
npm run build:pkgjson
2829
- uses: actions/upload-artifact@v4
2930
with:
3031
name: build-${{ github.run_id }}
3132
path: |
3233
build
3334
package.json
35+
package-lite.json
3436
jsr.json
35-
node_modules/depseek
3637
retention-days: 1
3738

3839
npm-publish:
@@ -77,10 +78,26 @@ jobs:
7778
7879
- name: pushing lite snapshot to ${{ env.GOOGLE_NPM_REGISTRY }}
7980
run: |
80-
cat <<< $(jq '.name="zx"' package.json) > package.json
81-
node scripts/prepublish-lite.mjs
81+
mv package-lite.json package.json
8282
npm publish --provenance --access=public --no-git-tag-version --tag lite --registry https://${{ env.GOOGLE_NPM_REGISTRY }}
8383
84+
jsr-publish:
85+
needs: build
86+
runs-on: ubuntu-latest
87+
permissions:
88+
contents: read
89+
id-token: write
90+
steps:
91+
- uses: actions/checkout@v4
92+
with:
93+
persist-credentials: false
94+
- uses: actions/setup-node@v4
95+
with:
96+
node-version: 22
97+
cache: 'npm'
98+
- uses: actions/download-artifact@v4
99+
with:
100+
name: build-${{ github.run_id }}
84101
- name: pushing to jsr.io
85102
run: npx jsr publish --allow-dirty
86103

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ temp
99
test/fixtures/ts-project/build/
1010
jsr.json
1111
.npmrc
12+
package-lite.json

.size-limit.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
"path": [
55
"build/3rd-party-licenses",
66
"build/core.cjs",
7-
"build/core.js",
87
"build/core.d.ts",
8+
"build/core.js",
99
"build/deno.js",
10+
"build/error.d.ts",
1011
"build/esblib.cjs",
12+
"build/internals.cjs",
13+
"build/log.d.ts",
1114
"build/util.cjs",
1215
"build/util.d.ts",
1316
"build/vendor-core.cjs",
1417
"build/vendor-core.d.ts",
1518
"README.md",
1619
"LICENSE"
1720
],
18-
"limit": "124.85 kB",
21+
"limit": "127.60 kB",
1922
"brotli": false,
2023
"gzip": false
2124
},

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
"build:dts": "tsc --project tsconfig.json && node scripts/build-dts.mjs",
7979
"build:dcr": "docker build -f ./dcr/Dockerfile . -t zx",
8080
"build:jsr": "node scripts/build-jsr.mjs",
81+
"build:lite": "node scripts/build-lite.mjs",
82+
"build:pkgjson": "node scripts/build-pkgjson.mjs",
8183
"postbuild": "node scripts/build-clean.mjs",
8284
"docs:dev": "vitepress dev docs",
8385
"docs:build": "vitepress build docs",
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,32 @@ for (const entry of entries) {
4040
}
4141
}
4242

43+
const whitelist = new Set([
44+
'name',
45+
'version',
46+
'description',
47+
'type',
48+
'main',
49+
'types',
50+
'typesVersions',
51+
'exports',
52+
'files',
53+
'engines',
54+
'optionalDependencies',
55+
'publishConfig',
56+
'keywords',
57+
'repository',
58+
'homepage',
59+
'author',
60+
'license',
61+
])
62+
63+
const __pkgJson = Object.fromEntries(
64+
Object.entries(_pkgJson).filter(([k]) => whitelist.has(k))
65+
)
66+
4367
const pkgJson = {
44-
..._pkgJson,
68+
...__pkgJson,
4569
version: _pkgJson.version + '-lite',
4670
exports: {
4771
'.': {
@@ -59,10 +83,9 @@ const pkgJson = {
5983
'.': ['./build/core.d.ts'],
6084
},
6185
},
62-
man: undefined,
6386
files: [...files].map((f) => path.join('build', f)).sort(),
6487
}
6588

66-
fs.writeFileSync(pkgJsonFile, JSON.stringify(pkgJson, null, 2))
89+
fs.writeFileSync('package-lite.json', JSON.stringify(pkgJson, null, 2))
6790

68-
console.log('package.json prepared for zx-lite publishing')
91+
console.log('package-lite.json prepared for zx-lite publishing')

test/it/build-npm.test.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('npm artifact', () => {
5151
])
5252

5353
// pack / unpack
54-
await $`node scripts/prepublish-clean.mjs`
54+
await $`npm run build:pkgjson`
5555
const pack = await $`npm pack`
5656
await $`tar xf ${pack}`
5757
await $`rm ${pack}`.nothrow()
@@ -62,11 +62,22 @@ describe('npm artifact', () => {
6262
assert.match(stderr, /hello/)
6363

6464
// contents
65+
const pkgJson = await fs.readJson(
66+
path.resolve(tmp, 'package/package.json')
67+
)
6568
const files = await glob('**/*', {
6669
cwd: path.resolve(tmp, 'package'),
6770
absolute: false,
6871
onlyFiles: true,
6972
})
73+
74+
assert.equal(pkgJson.name, 'zx')
75+
assert.equal(pkgJson.description, 'A tool for writing better scripts')
76+
assert.equal(pkgJson.devDependencies, undefined)
77+
assert.equal(pkgJson.prettier, undefined)
78+
assert.equal(pkgJson.scripts, undefined)
79+
assert.equal(pkgJson.volta, undefined)
80+
7081
assert.deepEqual(
7182
files.sort(),
7283
[
@@ -126,8 +137,8 @@ describe('npm artifact', () => {
126137
])
127138

128139
// prepare package.json for lite
129-
await $`node scripts/prepublish-lite.mjs`
130-
await $`node scripts/prepublish-clean.mjs`
140+
await $`npm run build:lite`
141+
await $`mv package-lite.json package.json`
131142

132143
// pack / unpack
133144
const pack = await $`npm pack`
@@ -140,18 +151,24 @@ describe('npm artifact', () => {
140151
assert.match(stderr, /hello/)
141152

142153
// contents
154+
const pkgJson = await fs.readJson(
155+
path.resolve(tmp, 'package/package.json')
156+
)
143157
const files = await glob('**/*', {
144158
cwd: path.resolve(tmp, 'package'),
145159
absolute: false,
146160
onlyFiles: true,
147161
})
162+
163+
assert.equal(pkgJson.name, 'zx')
164+
assert.equal(pkgJson.devDependencies, undefined)
165+
assert.equal(pkgJson.bin, undefined)
148166
assert.deepEqual(
149167
files.sort(),
150168
[
151169
'LICENSE',
152170
'README.md',
153171
'build/3rd-party-licenses',
154-
'build/cli.js',
155172
'build/core.cjs',
156173
'build/core.d.ts',
157174
'build/core.js',

test/it/clean-package-json.test.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)