main
 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// Optimizes package.json for npm publishing
16
17import fs from 'node:fs'
18import path from 'node:path'
19
20const __dirname = path.dirname(new URL(import.meta.url).pathname)
21const root = path.resolve(__dirname, '..')
22const source = 'package.json'
23const dest = 'package-main.json'
24const _pkgJson = JSON.parse(fs.readFileSync(path.join(root, source), 'utf-8'))
25
26const whitelist = new Set([
27  'name',
28  'version',
29  'description',
30  'type',
31  'main',
32  'types',
33  'typesVersions',
34  'exports',
35  'bin',
36  'man',
37  'files',
38  'engines',
39  'optionalDependencies',
40  'publishConfig',
41  'keywords',
42  'repository',
43  'homepage',
44  'author',
45  'license',
46])
47
48const pkgJson = Object.fromEntries(
49  Object.entries(_pkgJson).filter(([k]) => whitelist.has(k))
50)
51
52fs.writeFileSync(path.resolve(root, dest), JSON.stringify(pkgJson, null, 2))
53
54console.log(`${dest} prepared for npm`)