-
Notifications
You must be signed in to change notification settings - Fork 6
/
fed-mini-modules.js
64 lines (54 loc) · 1.66 KB
/
fed-mini-modules.js
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
const fse = require('fs-extra');
const { globSync } = require('glob');
const path = require('path');
const root = process.cwd();
const sourceFiles = globSync(`${root}/src/*/`)
.map((name) => name.replace(/\/$/, ''));
const indexTypings = globSync(`${root}/src/index.d.ts`);
async function copyTypings(files, dest) {
const cmds = [];
files.forEach((file) => {
const fileName = file.split('/').pop();
cmds.push(fse.copyFile(file, `${dest}/${fileName}`));
});
return Promise.all(cmds);
}
async function createPackage(file) {
const fileName = file.split('/').pop();
const esmSource = globSync(`${root}/esm/${fileName}/**/index.js`)[0];
/**
* Prevent creating package.json for directories with no JS files (like CSS directories)
*/
if (!esmSource) {
return;
}
const destFile = `${path.resolve(root, file.split('/src/').pop())}/package.json`;
const esmRelative = path.relative(file.replace('/src', ''), esmSource);
const content = {
main: 'index.js',
module: esmRelative,
};
const typings = globSync(`${root}/src/${fileName}/*.d.ts`);
const cmds = [];
content.typings = 'index.d.ts';
cmds.push(copyTypings(typings, `${root}/${fileName}`));
cmds.push(fse.writeJSON(destFile, content));
return Promise.all(cmds);
}
async function generatePackages(files) {
const cmds = files.map((file) => createPackage(file));
return Promise.all(cmds);
}
async function run(files) {
try {
await generatePackages(files);
if (indexTypings.length === 1) {
copyTypings(indexTypings, root);
}
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
}
}
run(sourceFiles);