@bleedingdev/modern-js-create 3.5.0-ultramodern.3 → 3.5.0-ultramodern.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -17
- package/dist/cjs/ultramodern-tooling/commands.cjs +772 -75
- package/dist/cjs/ultramodern-tooling/config.cjs +149 -8
- package/dist/cjs/ultramodern-workspace/add-vertical.cjs +25 -0
- package/dist/cjs/ultramodern-workspace/api.cjs +44 -0
- package/dist/cjs/ultramodern-workspace/app-files.cjs +8 -11
- package/dist/cjs/ultramodern-workspace/backend-federation.cjs +282 -0
- package/dist/cjs/ultramodern-workspace/contracts.cjs +24 -8
- package/dist/cjs/ultramodern-workspace/delivery-unit-sync.cjs +157 -0
- package/dist/cjs/ultramodern-workspace/delivery-unit.cjs +88 -0
- package/dist/cjs/ultramodern-workspace/descriptors.cjs +24 -0
- package/dist/cjs/ultramodern-workspace/module-federation.cjs +161 -50
- package/dist/cjs/ultramodern-workspace/package-json.cjs +26 -43
- package/dist/cjs/ultramodern-workspace/pnpm-workspace-policy-plan.cjs +166 -0
- package/dist/cjs/ultramodern-workspace/tooling-command-catalog.cjs +153 -0
- package/dist/cjs/ultramodern-workspace/versions.cjs +8 -3
- package/dist/cjs/ultramodern-workspace/workspace-script-plan.cjs +175 -0
- package/dist/cjs/ultramodern-workspace/workspace-scripts.cjs +104 -63
- package/dist/cjs/ultramodern-workspace/workspace-validation-contract.cjs +97 -0
- package/dist/cjs/ultramodern-workspace/write-workspace.cjs +11 -3
- package/dist/cjs/ultramodern-workspace/zerops.cjs +100 -0
- package/dist/esm/ultramodern-tooling/commands.js +775 -78
- package/dist/esm/ultramodern-tooling/config.js +144 -9
- package/dist/esm/ultramodern-workspace/add-vertical.js +26 -1
- package/dist/esm/ultramodern-workspace/api.js +43 -2
- package/dist/esm/ultramodern-workspace/app-files.js +8 -11
- package/dist/esm/ultramodern-workspace/backend-federation.js +217 -0
- package/dist/esm/ultramodern-workspace/contracts.js +24 -8
- package/dist/esm/ultramodern-workspace/delivery-unit-sync.js +108 -0
- package/dist/esm/ultramodern-workspace/delivery-unit.js +31 -0
- package/dist/esm/ultramodern-workspace/descriptors.js +13 -1
- package/dist/esm/ultramodern-workspace/module-federation.js +160 -42
- package/dist/esm/ultramodern-workspace/package-json.js +16 -40
- package/dist/esm/ultramodern-workspace/pnpm-workspace-policy-plan.js +113 -0
- package/dist/esm/ultramodern-workspace/tooling-command-catalog.js +100 -0
- package/dist/esm/ultramodern-workspace/versions.js +6 -4
- package/dist/esm/ultramodern-workspace/workspace-script-plan.js +110 -0
- package/dist/esm/ultramodern-workspace/workspace-scripts.js +88 -65
- package/dist/esm/ultramodern-workspace/workspace-validation-contract.js +59 -0
- package/dist/esm/ultramodern-workspace/write-workspace.js +14 -6
- package/dist/esm/ultramodern-workspace/zerops.js +62 -0
- package/dist/esm-node/ultramodern-tooling/commands.js +775 -78
- package/dist/esm-node/ultramodern-tooling/config.js +144 -9
- package/dist/esm-node/ultramodern-workspace/add-vertical.js +26 -1
- package/dist/esm-node/ultramodern-workspace/api.js +43 -2
- package/dist/esm-node/ultramodern-workspace/app-files.js +8 -11
- package/dist/esm-node/ultramodern-workspace/backend-federation.js +218 -0
- package/dist/esm-node/ultramodern-workspace/contracts.js +24 -8
- package/dist/esm-node/ultramodern-workspace/delivery-unit-sync.js +109 -0
- package/dist/esm-node/ultramodern-workspace/delivery-unit.js +32 -0
- package/dist/esm-node/ultramodern-workspace/descriptors.js +13 -1
- package/dist/esm-node/ultramodern-workspace/module-federation.js +160 -42
- package/dist/esm-node/ultramodern-workspace/package-json.js +16 -40
- package/dist/esm-node/ultramodern-workspace/pnpm-workspace-policy-plan.js +114 -0
- package/dist/esm-node/ultramodern-workspace/tooling-command-catalog.js +101 -0
- package/dist/esm-node/ultramodern-workspace/versions.js +6 -4
- package/dist/esm-node/ultramodern-workspace/workspace-script-plan.js +111 -0
- package/dist/esm-node/ultramodern-workspace/workspace-scripts.js +88 -65
- package/dist/esm-node/ultramodern-workspace/workspace-validation-contract.js +60 -0
- package/dist/esm-node/ultramodern-workspace/write-workspace.js +14 -6
- package/dist/esm-node/ultramodern-workspace/zerops.js +63 -0
- package/dist/types/ultramodern-tooling/config.d.ts +6 -0
- package/dist/types/ultramodern-workspace/api.d.ts +1 -0
- package/dist/types/ultramodern-workspace/backend-federation.d.ts +11 -0
- package/dist/types/ultramodern-workspace/contracts.d.ts +1 -1
- package/dist/types/ultramodern-workspace/delivery-unit-sync.d.ts +6 -0
- package/dist/types/ultramodern-workspace/delivery-unit.d.ts +27 -0
- package/dist/types/ultramodern-workspace/descriptors.d.ts +4 -0
- package/dist/types/ultramodern-workspace/module-federation.d.ts +4 -9
- package/dist/types/ultramodern-workspace/package-json.d.ts +0 -1
- package/dist/types/ultramodern-workspace/pnpm-workspace-policy-plan.d.ts +39 -0
- package/dist/types/ultramodern-workspace/tooling-command-catalog.d.ts +18 -0
- package/dist/types/ultramodern-workspace/versions.d.ts +5 -3
- package/dist/types/ultramodern-workspace/workspace-script-plan.d.ts +72 -0
- package/dist/types/ultramodern-workspace/workspace-scripts.d.ts +13 -0
- package/dist/types/ultramodern-workspace/workspace-validation-contract.d.ts +42 -0
- package/dist/types/ultramodern-workspace/zerops.d.ts +2 -0
- package/package.json +3 -3
- package/template-workspace/.gitignore.handlebars +4 -0
- package/template-workspace/README.md.handlebars +18 -15
- package/template-workspace/patches/@module-federation__bridge-react@2.6.0.patch +140 -0
- package/template-workspace/patches/@module-federation__modern-js-v3@2.6.0.patch +57 -0
- package/template-workspace/patches/drizzle-orm-ts7-strict-declarations.patch +452 -0
- package/template-workspace/patches/effect-schema-error-type-id.patch +18 -0
- package/template-workspace/pnpm-workspace.yaml.handlebars +8 -0
- package/template-workspace/scripts/bootstrap-agent-skills.mjs +11 -3
- package/templates/workspace-scripts/check-ultramodern-api-boundaries.mts +40 -2
- package/templates/workspace-scripts/generate-node-backend-federation.mjs +270 -0
- package/templates/workspace-scripts/materialize-zerops-runtime.mjs +448 -0
- package/templates/workspace-scripts/proof-cloudflare-version.mjs +237 -9
- package/templates/workspace-scripts/proof-node-backend-federation.mjs +729 -0
- package/templates/workspace-scripts/ultramodern-cloudflare-proof.mjs +188 -2
- package/templates/workspace-scripts/validate-ultramodern-workspace.mjs.handlebars +434 -18
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
const workspaceRoot = path.resolve(
|
|
8
|
+
process.env.ULTRAMODERN_WORKSPACE_ROOT ?? process.cwd(),
|
|
9
|
+
);
|
|
10
|
+
const args = process.argv.slice(2);
|
|
11
|
+
|
|
12
|
+
function fail(message) {
|
|
13
|
+
console.error(`[ultramodern:zerops] ${message}`);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function readFlag(name) {
|
|
18
|
+
const index = args.indexOf(name);
|
|
19
|
+
if (index === -1) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const value = args[index + 1];
|
|
24
|
+
if (!value || value.startsWith('--')) {
|
|
25
|
+
fail(`${name} requires a value`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function assertRelativePath(label, value) {
|
|
32
|
+
if (path.isAbsolute(value) || value.split(/[\\/]/u).includes('..')) {
|
|
33
|
+
fail(`${label} must be a workspace-relative path`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function assertInsideWorkspace(label, targetPath) {
|
|
38
|
+
const relativePath = path.relative(workspaceRoot, targetPath);
|
|
39
|
+
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
|
40
|
+
fail(`${label} resolved outside the workspace`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function readJson(filePath) {
|
|
45
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function readOptionalJson(filePath) {
|
|
49
|
+
return fs.existsSync(filePath) ? readJson(filePath) : undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function writeJson(filePath, value) {
|
|
53
|
+
fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, 'utf-8');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function run(command, commandArgs, options = {}) {
|
|
57
|
+
const result = spawnSync(command, commandArgs, {
|
|
58
|
+
cwd: options.cwd ?? workspaceRoot,
|
|
59
|
+
env: {
|
|
60
|
+
...process.env,
|
|
61
|
+
MODERNJS_DEPLOY: 'node',
|
|
62
|
+
ULTRAMODERN_ZEPHYR: 'false',
|
|
63
|
+
},
|
|
64
|
+
stdio: 'inherit',
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (result.error) {
|
|
68
|
+
fail(result.error.message);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (result.status !== 0) {
|
|
72
|
+
process.exit(result.status ?? 1);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const sourceSnapshotSkipSegments = new Set([
|
|
77
|
+
'.git',
|
|
78
|
+
'.output',
|
|
79
|
+
'.zerops',
|
|
80
|
+
'dist',
|
|
81
|
+
'node_modules',
|
|
82
|
+
'repos',
|
|
83
|
+
]);
|
|
84
|
+
|
|
85
|
+
function shouldSnapshotSourcePath(relativePath) {
|
|
86
|
+
return !relativePath
|
|
87
|
+
.split(path.sep)
|
|
88
|
+
.some(segment => sourceSnapshotSkipSegments.has(segment));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function listWorkspaceSourceFiles() {
|
|
92
|
+
const files = [];
|
|
93
|
+
const queue = [workspaceRoot];
|
|
94
|
+
while (queue.length > 0) {
|
|
95
|
+
const current = queue.shift();
|
|
96
|
+
for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
|
|
97
|
+
const absolute = path.join(current, entry.name);
|
|
98
|
+
const relativePath = path.relative(workspaceRoot, absolute);
|
|
99
|
+
if (!shouldSnapshotSourcePath(relativePath)) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (entry.isDirectory()) {
|
|
103
|
+
queue.push(absolute);
|
|
104
|
+
} else if (entry.isFile()) {
|
|
105
|
+
files.push(relativePath);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return files.sort();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function snapshotWorkspaceSourceFiles() {
|
|
113
|
+
return new Map(
|
|
114
|
+
listWorkspaceSourceFiles().map(relativePath => [
|
|
115
|
+
relativePath,
|
|
116
|
+
fs.readFileSync(path.join(workspaceRoot, relativePath)),
|
|
117
|
+
]),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function restoreWorkspaceSourceFiles(snapshot) {
|
|
122
|
+
for (const relativePath of listWorkspaceSourceFiles()) {
|
|
123
|
+
if (!snapshot.has(relativePath)) {
|
|
124
|
+
fs.rmSync(path.join(workspaceRoot, relativePath), { force: true });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
for (const [relativePath, content] of snapshot) {
|
|
128
|
+
const absolute = path.join(workspaceRoot, relativePath);
|
|
129
|
+
fs.mkdirSync(path.dirname(absolute), { recursive: true });
|
|
130
|
+
if (
|
|
131
|
+
!fs.existsSync(absolute) ||
|
|
132
|
+
!fs.readFileSync(absolute).equals(content)
|
|
133
|
+
) {
|
|
134
|
+
fs.writeFileSync(absolute, content);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const appId = readFlag('--app');
|
|
140
|
+
const packageName = readFlag('--package');
|
|
141
|
+
const packageDir = readFlag('--package-dir');
|
|
142
|
+
|
|
143
|
+
if (!appId) {
|
|
144
|
+
fail('--app is required');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!packageName) {
|
|
148
|
+
fail('--package is required');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (!packageDir) {
|
|
152
|
+
fail('--package-dir is required');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
assertRelativePath('--package-dir', packageDir);
|
|
156
|
+
|
|
157
|
+
const appRoot = path.resolve(workspaceRoot, packageDir);
|
|
158
|
+
const appOutputDir = path.join(appRoot, '.output');
|
|
159
|
+
const runtimeDir = path.join(workspaceRoot, '.zerops/runtime', appId);
|
|
160
|
+
|
|
161
|
+
assertInsideWorkspace('package directory', appRoot);
|
|
162
|
+
assertInsideWorkspace('runtime directory', runtimeDir);
|
|
163
|
+
|
|
164
|
+
const sourceSnapshot = snapshotWorkspaceSourceFiles();
|
|
165
|
+
try {
|
|
166
|
+
run(process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm', [
|
|
167
|
+
'--filter',
|
|
168
|
+
packageName,
|
|
169
|
+
'run',
|
|
170
|
+
'deploy',
|
|
171
|
+
'--skip-build',
|
|
172
|
+
]);
|
|
173
|
+
} finally {
|
|
174
|
+
restoreWorkspaceSourceFiles(sourceSnapshot);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (!fs.existsSync(appOutputDir)) {
|
|
178
|
+
fail(
|
|
179
|
+
`Modern.js Node deploy did not produce ${path.relative(
|
|
180
|
+
workspaceRoot,
|
|
181
|
+
appOutputDir,
|
|
182
|
+
)}`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
fs.rmSync(runtimeDir, { force: true, recursive: true });
|
|
187
|
+
fs.mkdirSync(path.dirname(runtimeDir), { recursive: true });
|
|
188
|
+
fs.cpSync(appOutputDir, runtimeDir, { recursive: true });
|
|
189
|
+
|
|
190
|
+
const entryPath = path.join(runtimeDir, 'index.js');
|
|
191
|
+
if (!fs.existsSync(entryPath)) {
|
|
192
|
+
fail(
|
|
193
|
+
`Modern.js Node deploy output is missing ${path.relative(
|
|
194
|
+
workspaceRoot,
|
|
195
|
+
entryPath,
|
|
196
|
+
)}`,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const packageJsonPath = path.join(runtimeDir, 'package.json');
|
|
201
|
+
const runtimePackage = fs.existsSync(packageJsonPath)
|
|
202
|
+
? readJson(packageJsonPath)
|
|
203
|
+
: {};
|
|
204
|
+
normalizeRuntimePackageDependencies(runtimePackage);
|
|
205
|
+
|
|
206
|
+
runtimePackage.private = true;
|
|
207
|
+
runtimePackage.name ??= `${appId}-zerops-runtime`;
|
|
208
|
+
runtimePackage.scripts = {
|
|
209
|
+
...(runtimePackage.scripts ?? {}),
|
|
210
|
+
serve: runtimePackage.scripts?.serve ?? 'node index.js',
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
writeJson(packageJsonPath, runtimePackage);
|
|
214
|
+
installRuntimeDependencies(runtimePackage);
|
|
215
|
+
|
|
216
|
+
console.log(
|
|
217
|
+
`[ultramodern:zerops] materialized ${appId} runtime at ${path.relative(
|
|
218
|
+
workspaceRoot,
|
|
219
|
+
runtimeDir,
|
|
220
|
+
)}`,
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
function normalizeRuntimePackageDependencies(packageJson) {
|
|
224
|
+
const compactConfig = readOptionalJson(
|
|
225
|
+
path.join(workspaceRoot, '.modernjs/ultramodern.json'),
|
|
226
|
+
);
|
|
227
|
+
const packageSource = compactConfig?.packageSource;
|
|
228
|
+
const modernPackageVersion = packageSource?.modernPackageVersion;
|
|
229
|
+
const aliasScope = packageSource?.aliasScope;
|
|
230
|
+
const aliasPackageNamePrefix = packageSource?.aliasPackageNamePrefix;
|
|
231
|
+
|
|
232
|
+
if (!modernPackageVersion || !aliasScope || !aliasPackageNamePrefix) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const aliasPrefix = `@${aliasScope}/${aliasPackageNamePrefix}`;
|
|
237
|
+
for (const section of ['dependencies', 'optionalDependencies']) {
|
|
238
|
+
const dependencies = packageJson[section];
|
|
239
|
+
if (
|
|
240
|
+
!dependencies ||
|
|
241
|
+
typeof dependencies !== 'object' ||
|
|
242
|
+
Array.isArray(dependencies)
|
|
243
|
+
) {
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
for (const dependencyName of Object.keys(dependencies)) {
|
|
248
|
+
if (dependencyName.startsWith(aliasPrefix)) {
|
|
249
|
+
const officialPackageName = `@modern-js/${dependencyName.slice(
|
|
250
|
+
aliasPrefix.length,
|
|
251
|
+
)}`;
|
|
252
|
+
dependencies[dependencyName] = modernPackageVersion;
|
|
253
|
+
dependencies[officialPackageName] ??=
|
|
254
|
+
`npm:${dependencyName}@${modernPackageVersion}`;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function installRuntimeDependencies(runtimePackage) {
|
|
261
|
+
const installDir = fs.mkdtempSync(
|
|
262
|
+
path.join(os.tmpdir(), `ultramodern-zerops-${appId}-`),
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
try {
|
|
266
|
+
const workspacePackages = collectWorkspacePackages();
|
|
267
|
+
const installPackage = JSON.parse(JSON.stringify(runtimePackage));
|
|
268
|
+
const localDependencies = removeWorkspaceDependencies(
|
|
269
|
+
installPackage,
|
|
270
|
+
workspacePackages,
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
writeJson(path.join(installDir, 'package.json'), installPackage);
|
|
274
|
+
run(
|
|
275
|
+
process.platform === 'win32' ? 'npm.cmd' : 'npm',
|
|
276
|
+
[
|
|
277
|
+
'install',
|
|
278
|
+
'--omit=dev',
|
|
279
|
+
'--no-audit',
|
|
280
|
+
'--fund=false',
|
|
281
|
+
'--legacy-peer-deps',
|
|
282
|
+
],
|
|
283
|
+
{ cwd: installDir },
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
fs.rmSync(path.join(runtimeDir, 'node_modules'), {
|
|
287
|
+
force: true,
|
|
288
|
+
recursive: true,
|
|
289
|
+
});
|
|
290
|
+
fs.cpSync(
|
|
291
|
+
path.join(installDir, 'node_modules'),
|
|
292
|
+
path.join(runtimeDir, 'node_modules'),
|
|
293
|
+
{ recursive: true },
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
for (const dependency of localDependencies) {
|
|
297
|
+
copyWorkspacePackage(dependency, workspacePackages);
|
|
298
|
+
}
|
|
299
|
+
} finally {
|
|
300
|
+
fs.rmSync(installDir, { force: true, recursive: true });
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function collectWorkspacePackages() {
|
|
305
|
+
const packages = new Map();
|
|
306
|
+
|
|
307
|
+
for (const directory of ['packages', 'apps', 'verticals']) {
|
|
308
|
+
const absoluteDirectory = path.join(workspaceRoot, directory);
|
|
309
|
+
if (!fs.existsSync(absoluteDirectory)) {
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
for (const entry of fs.readdirSync(absoluteDirectory, {
|
|
314
|
+
withFileTypes: true,
|
|
315
|
+
})) {
|
|
316
|
+
if (!entry.isDirectory()) {
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const packageDirectory = path.join(absoluteDirectory, entry.name);
|
|
321
|
+
const packageJsonPath = path.join(packageDirectory, 'package.json');
|
|
322
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const packageJson = readJson(packageJsonPath);
|
|
327
|
+
if (typeof packageJson.name === 'string') {
|
|
328
|
+
packages.set(packageJson.name, packageDirectory);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return packages;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function removeWorkspaceDependencies(packageJson, workspacePackages) {
|
|
337
|
+
const localDependencies = [];
|
|
338
|
+
for (const section of ['dependencies', 'optionalDependencies']) {
|
|
339
|
+
const dependencies = packageJson[section];
|
|
340
|
+
if (
|
|
341
|
+
!dependencies ||
|
|
342
|
+
typeof dependencies !== 'object' ||
|
|
343
|
+
Array.isArray(dependencies)
|
|
344
|
+
) {
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
for (const dependencyName of Object.keys(dependencies)) {
|
|
349
|
+
if (workspacePackages.has(dependencyName)) {
|
|
350
|
+
localDependencies.push(dependencyName);
|
|
351
|
+
delete dependencies[dependencyName];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return localDependencies;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function copyWorkspacePackage(packageName, workspacePackages) {
|
|
360
|
+
const sourceDirectory = workspacePackages.get(packageName);
|
|
361
|
+
if (!sourceDirectory) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const targetDirectory = path.join(
|
|
366
|
+
runtimeDir,
|
|
367
|
+
'node_modules',
|
|
368
|
+
...packageName.split('/'),
|
|
369
|
+
);
|
|
370
|
+
fs.rmSync(targetDirectory, { force: true, recursive: true });
|
|
371
|
+
fs.mkdirSync(path.dirname(targetDirectory), { recursive: true });
|
|
372
|
+
fs.cpSync(sourceDirectory, targetDirectory, {
|
|
373
|
+
filter: sourcePath => !sourcePath.includes(`${path.sep}node_modules`),
|
|
374
|
+
recursive: true,
|
|
375
|
+
});
|
|
376
|
+
makeWorkspacePackageRuntimeSafe(targetDirectory);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function makeWorkspacePackageRuntimeSafe(packageDirectory) {
|
|
380
|
+
const packageJsonPath = path.join(packageDirectory, 'package.json');
|
|
381
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
382
|
+
const packageJson = readJson(packageJsonPath);
|
|
383
|
+
packageJson.exports = rewriteTsExports(packageJson.exports);
|
|
384
|
+
writeJson(packageJsonPath, packageJson);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
for (const tsFile of listTsFiles(packageDirectory)) {
|
|
388
|
+
const jsFile = tsFile.replace(/\.ts$/u, '.js');
|
|
389
|
+
fs.writeFileSync(
|
|
390
|
+
jsFile,
|
|
391
|
+
transpileGeneratedPackageTs(fs.readFileSync(tsFile, 'utf-8')),
|
|
392
|
+
'utf-8',
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function rewriteTsExports(value) {
|
|
398
|
+
if (typeof value === 'string') {
|
|
399
|
+
return value.replace(/\.ts$/u, '.js');
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (Array.isArray(value)) {
|
|
403
|
+
return value.map(rewriteTsExports);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (value && typeof value === 'object') {
|
|
407
|
+
return Object.fromEntries(
|
|
408
|
+
Object.entries(value).map(([key, entry]) => [key, rewriteTsExports(entry)]),
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
return value;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function listTsFiles(directory) {
|
|
416
|
+
const files = [];
|
|
417
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
418
|
+
const entryPath = path.join(directory, entry.name);
|
|
419
|
+
if (entry.isDirectory()) {
|
|
420
|
+
files.push(...listTsFiles(entryPath));
|
|
421
|
+
} else if (entry.isFile() && entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts')) {
|
|
422
|
+
files.push(entryPath);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return files;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function transpileGeneratedPackageTs(source) {
|
|
430
|
+
return source
|
|
431
|
+
.replace(/^\s*import\s+type\s+[^;]+;\s*$/gmu, '')
|
|
432
|
+
.replace(/^\s*export\s+type\s+[^;]+;\s*$/gmu, '')
|
|
433
|
+
.replace(/^\s*type\s+\w+\s*=\s*[^;]+;\s*$/gmu, '')
|
|
434
|
+
.replace(/^\s*interface\s+\w+\s*\{[^}]*\}\s*$/gmsu, '')
|
|
435
|
+
.replace(
|
|
436
|
+
/\b(const|let|var)\s+([A-Za-z_$][\w$]*)\s*:\s*[^=]+=/gu,
|
|
437
|
+
'$1 $2 =',
|
|
438
|
+
)
|
|
439
|
+
.replace(/\(([^)]*)\)\s*:\s*[^=]+=>/gu, (_, params) => `(${stripParameterTypes(params)}) =>`)
|
|
440
|
+
.replace(/\(([^)]*)\)\s*=>/gu, (_, params) => `(${stripParameterTypes(params)}) =>`)
|
|
441
|
+
.replace(/function(\s+\w+\s*)\(([^)]*)\)/gu, (_, name, params) => `function${name}(${stripParameterTypes(params)})`)
|
|
442
|
+
.replace(/\s+as\s+const\b/gu, '')
|
|
443
|
+
.replace(/\s+satisfies\s+[A-Za-z_$][\w$]*(?:<[^>]+>)?/gu, '');
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function stripParameterTypes(params) {
|
|
447
|
+
return params.replace(/([A-Za-z_$][\w$]*)\??:\s*[^,]+/gu, '$1');
|
|
448
|
+
}
|