-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-tsconfig.mjs
94 lines (86 loc) · 3.73 KB
/
update-tsconfig.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
import fs from 'fs/promises'
import path from 'path'
const exportOrder = ['types', 'node', 'import', 'require', 'default'];
async function x(type, tsconfigPath, packageConfig)
{
const config = (await import(tsconfigPath, { assert: { type: 'json' } })).default;
config.compilerOptions.outDir = "dist/" + type;
if (config.extends !== `../tsconfig.settings.${type}.json`)
config.extends = `../tsconfig.settings.${type}.json`;
if (config.references)
config.references = config.references.map(ref => ref.path.endsWith(type + '.json') ? ref : { path: `${ref.path}/tsconfig.${type}.json` });
await fs.writeFile(tsconfigPath, JSON.stringify(config, null, 4));
const pkg = (await import(packageConfig, { assert: { type: 'json' } })).default;
if (!pkg.exports)
pkg.exports = { ".": {} };
if (!pkg.exports['.'])
pkg.exports['.'] = {};
switch (type)
{
case 'cjs':
pkg.exports['.'].require = pkg.main;
pkg.main = pkg.exports['.'].require;
pkg.types = pkg.typings = pkg.main.replace(/\.js$/, '.d.ts')
delete pkg.exports['.'].node;
break;
case 'esm':
if (pkg.main)
pkg.module = pkg.main.replace('/cjs', '/esm');
pkg.exports['.'].import = `./dist/${type}/index.js`
pkg.exports['.'].types = pkg.exports['.'].import.replace(/\.js/, '.d.ts');
// pkg.exports['.'].types = pkg.exports['.'].import.replace(/\.js/, '.d.ts');
break;
default:
throw new Error('Not supported type ' + type);
}
pkg.exports['.'].default = pkg.exports['.'].require;
// if (pkg.types && !pkg.exports['.'].types)
pkg.type = 'module';
pkg.exports['.'] = Object.fromEntries(exportOrder.map(k => [k, pkg.exports['.'][k]]));
await fs.writeFile(packageConfig, JSON.stringify(pkg, null, 4));
await fs.mkdir(path.join(path.dirname(tsconfigPath), config.compilerOptions.outDir), { recursive: true });
// if (type == 'esm')
// await fs.writeFile(path.join(path.dirname(tsconfigPath), config.compilerOptions.outDir, "package.json"), JSON.stringify({ type: 'module' }, null, 4))
// else
// await fs.writeFile(path.join(path.dirname(tsconfigPath), config.compilerOptions.outDir, "package.json"), JSON.stringify({ type: 'commonjs' }, null, 4))
}
const cjs = await import('./packages/tsconfig.cjs.json', { assert: { type: 'json' } });
for (var ref of cjs.default.references)
{
const tsconfigPath = path.join(path.dirname(import.meta.url).substring(5), './packages', ref.path);
const packageConfig = path.join(path.dirname(path.join(path.dirname(import.meta.url).substring(5), './packages', ref.path)), './package.json');
try
{
await x('cjs', tsconfigPath, packageConfig);
}
catch (e)
{
if (e instanceof SyntaxError)
{
console.error(e);
break;
}
await fs.copyFile(tsconfigPath.replace('.cjs', ''), tsconfigPath);
await x('cjs', tsconfigPath, packageConfig);
}
}
const esm = await import('./packages/tsconfig.esm.json', { assert: { type: 'json' } });
for (var ref of esm.default.references)
{
const tsconfigPath = path.join(path.dirname(import.meta.url).substring(5), './packages', ref.path);
const packageConfig = path.join(path.dirname(path.join(path.dirname(import.meta.url).substring(5), './packages', ref.path)), './package.json');
try
{
await x('esm', tsconfigPath, packageConfig);
}
catch (e)
{
if (e instanceof SyntaxError)
{
console.error(e);
break;
}
await fs.copyFile(tsconfigPath.replace('.esm', ''), tsconfigPath);
await x('esm', tsconfigPath, packageConfig);
}
}