Skip to content

Commit

Permalink
build: omit redundant pkgjson fields on npm publishing (#1005)
Browse files Browse the repository at this point in the history
* feat: create script for omit field

* refactor: replace blacklist to whilelist

* feat: scripts

* test: update test
  • Loading branch information
easymikey authored Dec 18, 2024
1 parent 88b8400 commit c8ca866
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- run: npm test
env:
FORCE_COLOR: 3
- run: node scripts/clean-package-json.mjs
- run: echo "//wombat-dressing-room.appspot.com/:_authToken=$AUTH_TOKEN" >> .npmrc
env:
AUTH_TOKEN: ${{ secrets.AUTH_TOKEN }}
Expand Down
49 changes: 49 additions & 0 deletions scripts/clean-package-json.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import fs from 'node:fs'
import path from 'node:path'
import { createRequire } from 'node:module'

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const pkgJsonFile = path.join(__dirname, '../package.json')

const whitelist = new Set([
'name',
'version',
'description',
'type',
'main',
'types',
'typesVersions',
'exports',
'bin',
'man',
'files',
'engines',
'optionalDependencies',
'publishConfig',
'keywords',
'repository',
'homepage',
'author',
'license',
])

const _pkgJson = createRequire(import.meta.url)('../package.json')
const pkgJson = Object.fromEntries(
Object.entries(_pkgJson).filter(([k]) => whitelist.has(k))
)

fs.writeFileSync(pkgJsonFile, JSON.stringify(pkgJson, null, 2))
54 changes: 54 additions & 0 deletions test/it/clean-package-json.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import assert from 'node:assert'
import { tempdir, $, path, fs } from '../../build/index.js'
import { describe, before, after, it } from 'node:test'

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const root = path.resolve(__dirname, '../../')

describe('package.json artifact', () => {
let tmp
let t$

before(async () => {
tmp = tempdir()
t$ = $({ cwd: tmp, quiet: true })

await fs.copy(
path.resolve(root, 'package.json'),
path.resolve(tmp, 'package.json')
)
await fs.copy(
path.resolve(root, 'scripts/clean-package-json.mjs'),
path.resolve(tmp, 'scripts/clean-package-json.mjs')
)
})

after(() => fs.remove(tmp))

it('handle exist properties required for publishing', async () => {
await t$`node scripts/clean-package-json.mjs`
// to throw if manifest is not correct
const pkgJson = JSON.parse(
fs.readFileSync(path.resolve(tmp, 'package.json'))
)

assert.equal(pkgJson.name, 'zx')
assert.equal(pkgJson.description, 'A tool for writing better scripts')
assert.equal(pkgJson.prettier, undefined)
assert.equal(pkgJson.scripts, undefined)
})
})

0 comments on commit c8ca866

Please sign in to comment.