-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsync-paths.js
More file actions
58 lines (46 loc) · 1.74 KB
/
Copy pathsync-paths.js
File metadata and controls
58 lines (46 loc) · 1.74 KB
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
import fs from 'fs';
import path from 'path';
import prettier from 'prettier';
import glob from 'tiny-glob';
function readJSON(filepath) {
return JSON.parse(fs.readFileSync(path.resolve(process.cwd(), filepath), 'utf-8'));
}
async function getPackages() {
return glob(`./packages/*/package.json`).then((files) =>
files.map((file) => [readJSON(file).name, path.dirname(file)]).sort(([a], [b]) => a.localeCompare(b)),
);
}
function writeJson(filepath, json) {
const data = prettier.format(JSON.stringify(json, null, 2), {
parser: 'json',
});
fs.writeFileSync(filepath, data, 'utf-8');
}
function writePathsToTsConfig(pkgs) {
const tsConfig = readJSON('./tsconfig.json');
const others = Object.entries(tsConfig.compilerOptions.paths).filter(([k]) => k[0] === '~');
const locals = pkgs.map(([k, v]) => [k, [`${v}/src`]]);
tsConfig.compilerOptions.paths = Object.fromEntries([...others, ...locals]);
writeJson('./tsconfig.json', tsConfig);
}
function writeAliasToExamplePackageJson(pkgs) {
const pkgJson = readJSON('./example/floating-inbox/package.json');
const others = Object.entries(pkgJson.alias).filter(([, v]) => !v.startsWith('../packages/'));
const locals = pkgs.map(([k, v]) => [k, `../${v}`]);
pkgJson.alias = Object.fromEntries([...others, ...locals]);
writeJson('./example/floating-inbox/package.json', pkgJson);
}
function distributeFiles(pkgs, files) {
for (const [_name, dir] of pkgs) {
for (const file of files) {
const src = path.resolve(process.cwd(), file);
const dest = path.resolve(process.cwd(), dir, file);
fs.copyFileSync(src, dest);
}
}
}
getPackages().then((pkgs) => {
writePathsToTsConfig(pkgs);
writeAliasToExamplePackageJson(pkgs);
distributeFiles(pkgs, ['LICENSE']);
});