-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprepack.mjs
executable file
·220 lines (181 loc) · 6.07 KB
/
prepack.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// @ts-check
/*
* Used as a pre-publishing step.
*/
import * as fs from 'node:fs/promises';
import process from 'node:process';
import arg from 'arg';
import { consola } from 'consola';
import { execa } from 'execa';
import { entries, mapValues } from 'remeda';
import color from './colors.mjs';
import { FAIL, IN_PROGRESS, SUCCESS } from './icons.mjs';
import { Frog } from './log.mjs';
import { progress } from './progress.mjs';
import { verifyPublishTag } from './verify-publish-tag.mjs';
const cwd = new URL(`file:${process.cwd()}/`);
/**
* @param {*} value
* @param {(path: string, value: string) => string} transform
* @param {string=} path
* @returns {*}
*/
function deepMapStrings(value, transform, path = '') {
if (value === undefined || value === null) {
return value;
}
if (Array.isArray(value)) {
return value;
}
if (typeof value === 'object') {
return Object.fromEntries(
Object.entries(value).map(([key, val]) => [
key,
deepMapStrings(val, transform, `${path}.${key}`),
]),
);
}
if (typeof value === 'string') {
return transform(path, value);
}
return value;
}
async function transformPackageJSON() {
const packageJsonUrl = new URL('./package.json', cwd);
const distPackageJsonUrl = new URL('./dist/package.json', cwd);
const packageJson = JSON.parse(await fs.readFile(packageJsonUrl, 'utf-8'));
let distPackageJson = structuredClone(packageJson);
// Replacing `exports`, `main`, and `types` with `publishConfig.*`
if (distPackageJson.publishConfig?.main) {
distPackageJson.main = distPackageJson.publishConfig.main;
}
if (distPackageJson.publishConfig?.types) {
distPackageJson.types = distPackageJson.publishConfig.types;
}
if (distPackageJson.publishConfig?.exports) {
distPackageJson.exports = distPackageJson.publishConfig.exports;
}
distPackageJson.publishConfig = undefined;
// Altering paths in the package.json
distPackageJson = deepMapStrings(distPackageJson, (_path, value) => {
if (value.startsWith('./dist/')) {
return value.replace(/^\.\/dist/, '.');
}
if (value.startsWith('./src/')) {
return value.replace(/^\.\/src/, '.');
}
return value;
});
// Erroring out on any wildcard dependencies
for (const [moduleKey, versionSpec] of [
...entries(distPackageJson.dependencies ?? {}),
]) {
if (versionSpec === '*' || versionSpec === 'workspace:*') {
throw new Error(
`Cannot depend on a module with a wildcard version. (${moduleKey}: ${versionSpec})`,
);
}
}
distPackageJson.private = false;
distPackageJson.scripts = {};
// Removing dev dependencies.
distPackageJson.devDependencies = undefined;
// Removing workspace specifiers in dependencies.
distPackageJson.dependencies = mapValues(
distPackageJson.dependencies ?? {},
(/** @type {string} */ value) => value.replace(/^workspace:/, ''),
);
await fs.writeFile(
distPackageJsonUrl,
JSON.stringify(distPackageJson, undefined, 2),
'utf-8',
);
}
async function transformReadme() {
const readmeUrl = new URL('./README.md', cwd);
const distReadmeUrl = new URL('./dist/README.md', cwd);
let readme = await fs.readFile(readmeUrl, 'utf-8');
// npmjs.com does not handle multiple logos well, remove the dark mode only one.
readme = readme.replace(/!.*#gh-dark-mode-only\)/, '');
await fs.writeFile(distReadmeUrl, readme, 'utf-8');
}
/**
* @typedef {'in_progress' | 'success' | 'fail'} TaskStatus
*/
/** @type {Record<TaskStatus, string>} */
const ICON = {
in_progress: IN_PROGRESS,
success: SUCCESS,
fail: FAIL,
};
async function main() {
consola.start('Preparing the package for publishing');
console.log('');
const args = arg({ '--skip-publish-tag-check': Boolean });
if (!args['--skip-publish-tag-check']) {
verifyPublishTag();
}
/** @type {PromiseSettledResult<*>[]} */
const results = await progress('', async (update) => {
/** @typedef {'biome' | 'build' | 'spec' | 'types'} TaskName */
/** @type {Record<TaskName, TaskStatus>} */
const status = {
biome: 'in_progress',
build: 'in_progress',
spec: 'in_progress',
types: 'in_progress',
};
const taskString = (/** @type {TaskName} */ name) =>
`${{ in_progress: color.Magenta, success: color.Green, fail: color.Red }[status[name]]}${ICON[status[name]]}${color.Reset} ${name}`;
const updateMsg = () =>
update(
`${color.BgBrightMagenta}${color.Black}${Frog} working on tasks...${color.Reset} ${taskString('biome')}, ${taskString('build')}, ${taskString('spec')}, ${taskString('types')}`,
);
/**
* @template {Promise<*>} T
* @param {TaskName} name
* @param {T} promise
* @returns {T}
*/
const withStatusUpdate = (name, promise) =>
/** @type {T} */ (
promise
.then((result) => {
status[name] = 'success';
updateMsg();
return result;
})
.catch((err) => {
status[name] = 'fail';
updateMsg();
err.taskName = name;
throw err;
})
);
const $ = execa({ all: true });
const results = await Promise.allSettled([
withStatusUpdate('biome', $`biome check .`),
withStatusUpdate('build', $`pnpm build`),
withStatusUpdate('spec', $`pnpm -w test:spec`),
withStatusUpdate('types', $`pnpm -w test:types`),
]);
update(
`${color.BgBrightMagenta}${color.Black}${Frog} finished!${color.Reset} ${taskString('biome')}, ${taskString('build')}, ${taskString('spec')}, ${taskString('types')}`,
);
return results;
});
console.log('');
const rejected = /** @type {PromiseRejectedResult[]} */ (
results.filter((result) => result.status === 'rejected')
);
for (const rej of rejected) {
consola.error(`Task '${rej.reason.taskName}' failed\n`, rej.reason.stderr);
}
if (rejected.length > 0) {
process.exit(1);
}
consola.start('Transforming miscellaneous files...');
await Promise.all([transformPackageJSON(), transformReadme()]);
consola.success('Package prepared!');
}
export default main;