-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·101 lines (82 loc) · 3.07 KB
/
Copy pathcli.js
File metadata and controls
executable file
·101 lines (82 loc) · 3.07 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
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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const ts = require('typescript');
const getArg = (argName) => {
const argIndex = process.argv.indexOf(argName);
return argIndex !== -1 ? process.argv[argIndex + 1] : null;
};
const outDirArg = getArg('--outputDir');
const outputDir = outDirArg
? path.resolve('./', outDirArg)
: path.resolve(__dirname, '../../@types/__federated_types/');
const findFederationConfigs = (base, files, result) => {
files = files || fs.readdirSync(base);
result = result || [];
files.forEach((file) => {
const newBase = path.join(base, file);
if (fs.statSync(newBase).isDirectory()) {
result = findFederationConfigs(newBase, fs.readdirSync(newBase), result);
} else {
if (file === 'federation.config.json') {
result.push(require(path.resolve('./', newBase)));
}
}
});
return result;
};
const configs = findFederationConfigs('./');
if (configs.length !== 1) {
console.error(`ERROR: Found ${configs.length} federation configs`);
process.exit(1);
}
const federationConfig = configs[0];
const compileFiles = Object.values(federationConfig.exposes);
const outFile = path.resolve(outputDir, `${federationConfig.name}.d.ts`);
try {
// write the typings file
const program = ts.createProgram(compileFiles, {
outFile,
declaration: true,
emitDeclarationOnly: true,
skipLibCheck: true,
jsx: 'react',
esModuleInterop: true,
});
program.emit();
typing = fs.readFileSync(outFile, { encoding: 'utf8', flag: 'r' });
console.log('writing typing file:', outFile);
fs.writeFileSync(
outFile,
typing.replace(/declare module \"/g, `declare module "${federationConfig.name}/`)
);
// if we are writing to the node_modules/@types directory, add a package.json file
if (outputDir.includes('node_modules/@types')) {
const packageJsonPath = path.resolve(outputDir, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
console.log('writing package.json:', packageJsonPath);
fs.copyFileSync(path.resolve(__dirname, 'typings.package.tmpl.json'), packageJsonPath);
} else {
console.log(packageJsonPath, 'already exists');
}
} else {
console.log('not writing to node modules, dont need a package.json');
}
// write/update the index.d.ts file
const indexPath = path.resolve(outputDir, 'index.d.ts');
const importStatement = `export * from './${federationConfig.name}';`;
if (!fs.existsSync(indexPath)) {
console.log('creating index.d.ts file');
fs.writeFileSync(indexPath, `${importStatement}\n`);
} else {
console.log('updating index.d.ts file');
const contents = fs.readFileSync(indexPath);
if (!contents.includes(importStatement)) {
fs.writeFileSync(indexPath, `${contents}${importStatement}\n`);
}
}
console.log('Success!');
} catch (e) {
console.error(`ERROR:`, e);
process.exit(1);
}