|
1 |
| -import fs from 'node:fs' |
2 |
| -import path from 'node:path' |
3 |
| - |
4 |
| -// Currently unused as life-cycle scripts do not run on CI |
5 |
| - |
6 | 1 | console.log('Running prepack script')
|
7 |
| - |
8 |
| -/** |
9 |
| - * Files to copy to the dist directory |
10 |
| - * @type {string[]} |
11 |
| - */ |
12 |
| -const FILES_TO_COPY = ['README.md'] |
13 |
| - |
14 |
| -/** |
15 |
| - * Fields to remove from the package.json copy |
16 |
| - * @type {string[]} |
17 |
| - */ |
18 |
| -const FIELDS_TO_REMOVE = [ |
19 |
| - 'devDependencies', |
20 |
| - 'files', |
21 |
| - 'publishConfig', |
22 |
| - 'scripts', |
23 |
| -] |
24 |
| - |
25 |
| -/** |
26 |
| - * Replaces 'dist/' or './dist/' prefix from a file path with './' |
27 |
| - * @param {string} filePath - The file path to process |
28 |
| - * @returns {string} The path without dist prefix |
29 |
| - */ |
30 |
| -function removeDist(filePath) { |
31 |
| - return filePath.replace(/^(\.\/)?dist\//, './') |
32 |
| -} |
33 |
| - |
34 |
| -/** |
35 |
| - * Recursively processes exports object to remove dist prefixes |
36 |
| - * @param {Record<string, any>} exports - The exports object to process |
37 |
| - * @returns {Record<string, any>} The processed exports object |
38 |
| - */ |
39 |
| -function processExports(exports) { |
40 |
| - return Object.fromEntries( |
41 |
| - Object.entries(exports).map(([key, value]) => [ |
42 |
| - key, |
43 |
| - typeof value === 'string' |
44 |
| - ? removeDist(value) |
45 |
| - : typeof value === 'object' && value !== null |
46 |
| - ? processExports(value) |
47 |
| - : value, |
48 |
| - ]), |
49 |
| - ) |
50 |
| -} |
51 |
| - |
52 |
| -console.log('Copying modified package.json') |
53 |
| - |
54 |
| -/** @type {Record<string, any>} */ |
55 |
| -const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')) |
56 |
| - |
57 |
| -const distPackageJson = { ...packageJson } |
58 |
| - |
59 |
| -if (distPackageJson.types) { |
60 |
| - distPackageJson.types = removeDist(distPackageJson.types) |
61 |
| -} |
62 |
| - |
63 |
| -if (distPackageJson.module) { |
64 |
| - distPackageJson.module = removeDist(distPackageJson.module) |
65 |
| -} |
66 |
| - |
67 |
| -if (distPackageJson.exports) { |
68 |
| - distPackageJson.exports = processExports(distPackageJson.exports) |
69 |
| -} |
70 |
| - |
71 |
| -for (const field of FIELDS_TO_REMOVE) { |
72 |
| - delete distPackageJson[field] |
73 |
| -} |
74 |
| - |
75 |
| -if (!fs.existsSync('dist')) { |
76 |
| - fs.mkdirSync('dist', { recursive: true }) |
77 |
| -} |
78 |
| - |
79 |
| -fs.writeFileSync( |
80 |
| - path.join('dist', 'package.json'), |
81 |
| - JSON.stringify(distPackageJson, null, 2), |
82 |
| -) |
83 |
| - |
84 |
| -console.log('Copying other files') |
85 |
| -for (const fileName of FILES_TO_COPY) { |
86 |
| - if (fs.existsSync(fileName)) { |
87 |
| - fs.copyFileSync(fileName, path.join('dist', fileName)) |
88 |
| - console.log(`${fileName}`) |
89 |
| - } else { |
90 |
| - console.log(`${fileName} not found, skipping`) |
91 |
| - } |
92 |
| -} |
93 |
| - |
94 |
| -console.log('prepack complete') |
0 commit comments