-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
setup-packages.mjs
117 lines (99 loc) · 3.35 KB
/
setup-packages.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
import path from 'node:path';
import fs from 'node:fs/promises';
import { globby } from 'globby';
import prettier from 'prettier';
function readFile(filepath) {
return fs.readFile(filepath, 'utf8');
}
function readJSONFile(filepath) {
return readFile(filepath).then(JSON.parse);
}
function fileExists(filepath) {
return fs.access(filepath).then(
() => true,
() => false,
);
}
async function writeFile(filepath, content) {
if ((await fileExists(filepath)) && (await readFile(filepath)) !== content) {
await fs.writeFile(filepath, content);
}
}
const rootPkg = await readJSONFile(path.join(import.meta.dirname, '../package.json'));
const paths = await globby(['packages/**/package.json', '!**/node_modules']);
paths.forEach(async (pkgPath) => {
const dir = path.dirname(pkgPath);
const prettierJsonOption = await prettier.resolveConfig('tsconfig.json');
const formatJSON = (content) =>
prettier.format(JSON.stringify(content), { ...prettierJsonOption, parser: 'json' });
// Set multi-module system builds exports
const pkg = await readJSONFile(pkgPath);
const isTS = await fileExists(path.join(dir, 'tsconfig.json'));
const hasReadme = await fileExists(path.join(dir, 'README.md'));
// Replicate configs that should always be the same.
pkg.engines = rootPkg.engines;
pkg.author = rootPkg.author;
pkg.license = rootPkg.license;
pkg.repository = rootPkg.repository;
pkg.keywords = [...new Set([...rootPkg.keywords, ...(pkg.keywords ?? [])])];
if (hasReadme) {
const repoPath = dir.split('/').slice(-2).join('/');
pkg.homepage = `https://github.com/SBoudrias/Inquirer.js/blob/master/${repoPath}/README.md`;
}
if (isTS) {
delete pkg.type;
pkg.scripts = pkg.scripts ?? {};
// If the package supports Typescript, then apply the configs.
pkg.exports = {
'.': {
import: {
types: './dist/esm/types/index.d.mts',
default: './dist/esm/index.mjs',
},
require: {
types: './dist/cjs/types/index.d.ts',
default: './dist/cjs/index.js',
},
},
};
pkg.main = pkg.exports['.'].require.default;
pkg.typings = pkg.exports['.'].require.types;
pkg.files = ['dist/**/*'];
pkg.scripts = {
tsc: 'yarn run tsc:esm && yarn run tsc:cjs',
'tsc:esm': 'rm -rf dist/esm && tsc -p ./tsconfig.json',
'tsc:cjs':
'rm -rf dist/cjs && tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs',
attw: 'attw --pack',
};
// Set ESM tsconfig
const esmTsconfig = {
extends: '../../tsconfig.json',
include: ['./src'],
exclude: ['**/*.test.mts'],
compilerOptions: {
lib: ['es2023'],
target: 'es2022',
module: 'NodeNext',
moduleResolution: 'nodenext',
outDir: 'dist/esm',
declarationDir: 'dist/esm/types',
},
};
// Set CJS tsconfig
const cjsTsconfig = {
extends: './tsconfig.json',
compilerOptions: {
lib: ['es2023'],
target: 'es6',
module: 'commonjs',
moduleResolution: 'node10',
outDir: 'dist/cjs',
declarationDir: 'dist/cjs/types',
},
};
writeFile(path.join(dir, 'tsconfig.json'), await formatJSON(esmTsconfig));
writeFile(path.join(dir, 'tsconfig.cjs.json'), await formatJSON(cjsTsconfig));
}
writeFile(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
});