-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathsubpath-workaround.mjs
56 lines (44 loc) · 1.98 KB
/
subpath-workaround.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
#!/usr/bin/env zx
import 'zx/globals';
import fs from 'fs';
const loadJSON = path => JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));
const writeJSON = (path, contents) =>
fs.writeFileSync(new URL(path, import.meta.url), JSON.stringify(contents, null, 2));
const pkgJsonPlaceholder = name => ({
main: `../dist/${name}.js`,
});
const pkgJsonBarrelPlaceholder = name => ({
main: `../dist/${name}/index.js`,
});
async function run() {
const pkgName = argv._[0];
console.log(`Loading package.json for ${pkgName}`);
const pkgFile = loadJSON(`../packages/${pkgName}/package.json`);
const subpathHelperFile = await import(`../packages/${pkgName}/subpaths.mjs`);
console.log(
`Found ${subpathHelperFile.subpathNames.length} subpaths and ${subpathHelperFile.subpathFoldersBarrel.length} subpath barrels`,
);
const allFilesNames = [...subpathHelperFile.subpathNames, ...subpathHelperFile.subpathFoldersBarrel, 'dist'];
const hasAllSubpathsInFiles = pkgFile.files.every(name => allFilesNames.includes(name));
if (!hasAllSubpathsInFiles) {
throw new Error('Not all subpaths from the package.json "files" array are in the subpaths.mjs');
}
// Create directories for each subpath name using the pkgJsonPlaceholder
subpathHelperFile.subpathNames.forEach(name => {
const dir = new URL(`../packages/${pkgName}/${name}`, import.meta.url);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
writeJSON(`../packages/${pkgName}/${name}/package.json`, pkgJsonPlaceholder(name));
});
// Create directories for each subpath barrel file using the pkgJsonBarrelPlaceholder
subpathHelperFile.subpathFoldersBarrel.forEach(name => {
const dir = new URL(`../packages/${pkgName}/${name}`, import.meta.url);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
writeJSON(`../packages/${pkgName}/${name}/package.json`, pkgJsonBarrelPlaceholder(name));
});
console.log('Successfully created subpath directories with placeholder files');
}
await run();