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// Prepares a lite (core) version of zx to publish
16
17import fs from 'node:fs'
18import path from 'node:path'
19import { depseekSync } from 'depseek'
20
21const __dirname = path.dirname(new URL(import.meta.url).pathname)
22const root = path.resolve(__dirname, '..')
23const source = 'package.json'
24const dest = 'package-lite.json'
25const _pkgJson = JSON.parse(fs.readFileSync(path.join(root, source), 'utf-8'))
26
27const files = new Set()
28const entries = new Set(['./core.js', './3rd-party-licenses'])
29
30for (const entry of entries) {
31  if (!fs.existsSync(path.join(root, 'build', entry))) continue
32
33  files.add(entry)
34  const contents = fs.readFileSync(path.join(root, 'build', entry), 'utf-8')
35  const deps = depseekSync(contents)
36  for (const { value: file } of deps) {
37    if (file.startsWith('.')) {
38      entries.add(file)
39      entries.add(file.replace(/\.c?js$/, '.d.ts'))
40    }
41  }
42}
43
44const whitelist = new Set([
45  'name',
46  'version',
47  'description',
48  'type',
49  'main',
50  'types',
51  'typesVersions',
52  'exports',
53  'files',
54  'engines',
55  'optionalDependencies',
56  'publishConfig',
57  'keywords',
58  'repository',
59  'homepage',
60  'author',
61  'license',
62])
63
64const __pkgJson = Object.fromEntries(
65  Object.entries(_pkgJson).filter(([k]) => whitelist.has(k))
66)
67
68const pkgJson = {
69  ...__pkgJson,
70  version: _pkgJson.version + '-lite',
71  exports: {
72    '.': {
73      types: './build/core.d.ts',
74      import: './build/core.js',
75      require: './build/core.cjs',
76      default: './build/core.js',
77    },
78    './package.json': './package.json',
79  },
80  main: './build/core.cjs',
81  types: './build/core.d.ts',
82  typesVersions: {
83    '*': {
84      '.': ['./build/core.d.ts'],
85    },
86  },
87  files: [...files].map((f) => path.join('build', f)).sort(),
88}
89
90fs.writeFileSync(path.resolve(root, dest), JSON.stringify(pkgJson, null, 2))
91
92console.log(`${dest} prepared for npm`)