Skip to content

Commit c8ca866

Browse files
authored
build: omit redundant pkgjson fields on npm publishing (google#1005)
* feat: create script for omit field * refactor: replace blacklist to whilelist * feat: scripts * test: update test
1 parent 88b8400 commit c8ca866

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

.github/workflows/npm-publish.yml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
- run: npm test
2424
env:
2525
FORCE_COLOR: 3
26+
- run: node scripts/clean-package-json.mjs
2627
- run: echo "//wombat-dressing-room.appspot.com/:_authToken=$AUTH_TOKEN" >> .npmrc
2728
env:
2829
AUTH_TOKEN: ${{ secrets.AUTH_TOKEN }}

scripts/clean-package-json.mjs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import fs from 'node:fs'
16+
import path from 'node:path'
17+
import { createRequire } from 'node:module'
18+
19+
const __dirname = path.dirname(new URL(import.meta.url).pathname)
20+
const pkgJsonFile = path.join(__dirname, '../package.json')
21+
22+
const whitelist = new Set([
23+
'name',
24+
'version',
25+
'description',
26+
'type',
27+
'main',
28+
'types',
29+
'typesVersions',
30+
'exports',
31+
'bin',
32+
'man',
33+
'files',
34+
'engines',
35+
'optionalDependencies',
36+
'publishConfig',
37+
'keywords',
38+
'repository',
39+
'homepage',
40+
'author',
41+
'license',
42+
])
43+
44+
const _pkgJson = createRequire(import.meta.url)('../package.json')
45+
const pkgJson = Object.fromEntries(
46+
Object.entries(_pkgJson).filter(([k]) => whitelist.has(k))
47+
)
48+
49+
fs.writeFileSync(pkgJsonFile, JSON.stringify(pkgJson, null, 2))

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

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import assert from 'node:assert'
16+
import { tempdir, $, path, fs } from '../../build/index.js'
17+
import { describe, before, after, it } from 'node:test'
18+
19+
const __dirname = path.dirname(new URL(import.meta.url).pathname)
20+
const root = path.resolve(__dirname, '../../')
21+
22+
describe('package.json artifact', () => {
23+
let tmp
24+
let t$
25+
26+
before(async () => {
27+
tmp = tempdir()
28+
t$ = $({ cwd: tmp, quiet: true })
29+
30+
await fs.copy(
31+
path.resolve(root, 'package.json'),
32+
path.resolve(tmp, 'package.json')
33+
)
34+
await fs.copy(
35+
path.resolve(root, 'scripts/clean-package-json.mjs'),
36+
path.resolve(tmp, 'scripts/clean-package-json.mjs')
37+
)
38+
})
39+
40+
after(() => fs.remove(tmp))
41+
42+
it('handle exist properties required for publishing', async () => {
43+
await t$`node scripts/clean-package-json.mjs`
44+
// to throw if manifest is not correct
45+
const pkgJson = JSON.parse(
46+
fs.readFileSync(path.resolve(tmp, 'package.json'))
47+
)
48+
49+
assert.equal(pkgJson.name, 'zx')
50+
assert.equal(pkgJson.description, 'A tool for writing better scripts')
51+
assert.equal(pkgJson.prettier, undefined)
52+
assert.equal(pkgJson.scripts, undefined)
53+
})
54+
})

0 commit comments

Comments
 (0)