2020-04-06 17:38:20 +00:00
|
|
|
import shell from 'shelljs';
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
shell.echo('generating imports');
|
|
|
|
const newFiles = new Set();
|
|
|
|
|
|
|
|
if (!fs.existsSync('lib')) {
|
|
|
|
shell.echo('> missing sources');
|
|
|
|
shell.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findModules(dirname: string): string[] {
|
|
|
|
return fs
|
|
|
|
.readdirSync(dirname, { withFileTypes: true })
|
2020-04-12 16:09:36 +00:00
|
|
|
.filter((dirent) => dirent.isDirectory())
|
|
|
|
.map((dirent) => dirent.name)
|
|
|
|
.filter((name) => !name.startsWith('__'))
|
2020-04-06 17:38:20 +00:00
|
|
|
.sort();
|
|
|
|
}
|
|
|
|
async function updateFile(file: string, code: string): Promise<void> {
|
|
|
|
const oldCode = fs.existsSync(file) ? await fs.readFile(file, 'utf8') : null;
|
|
|
|
if (code !== oldCode) {
|
|
|
|
await fs.writeFile(file, code);
|
|
|
|
}
|
|
|
|
newFiles.add(file);
|
|
|
|
}
|
2020-04-07 08:43:25 +00:00
|
|
|
|
|
|
|
async function generate({
|
|
|
|
path,
|
|
|
|
types,
|
|
|
|
map = '',
|
|
|
|
excludes = [],
|
|
|
|
}: {
|
|
|
|
path: string;
|
|
|
|
types: string[];
|
|
|
|
map?: string;
|
|
|
|
excludes?: string[];
|
|
|
|
}): Promise<void> {
|
|
|
|
shell.echo(`> ${path}`);
|
|
|
|
let imports = '';
|
|
|
|
let maps = '';
|
|
|
|
for (const ds of findModules(`lib/${path}`).filter(
|
2020-04-12 16:09:36 +00:00
|
|
|
(n) => !excludes?.includes(n)
|
2020-04-07 08:43:25 +00:00
|
|
|
)) {
|
|
|
|
const name = _.camelCase(ds);
|
|
|
|
imports += `import * as ${name} from './${ds}';\n`;
|
|
|
|
maps += `api.set('${ds}', ${name}${map});\n`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const code = `import { ${types.join(', ')} } from './common';
|
|
|
|
${imports}\n
|
|
|
|
const api = new Map<string, ${types.join(' | ')}>();
|
|
|
|
export default api;
|
|
|
|
${maps}`;
|
|
|
|
|
|
|
|
await updateFile(`lib/${path}/api.generated.ts`, code.replace(/^\s+/gm, ''));
|
|
|
|
}
|
|
|
|
|
2020-04-09 10:47:48 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2020-04-06 17:38:20 +00:00
|
|
|
(async () => {
|
|
|
|
try {
|
2020-04-07 08:43:25 +00:00
|
|
|
// datasources
|
2020-04-06 17:38:20 +00:00
|
|
|
shell.echo('> datasources');
|
|
|
|
let code = `
|
|
|
|
import { Datasource } from './common';
|
|
|
|
const api = new Map<string, Promise<Datasource>>();
|
|
|
|
export default api;
|
|
|
|
`;
|
|
|
|
for (const ds of findModules('lib/datasource')) {
|
|
|
|
code += `api.set('${ds}', import('./${ds}'));\n`;
|
|
|
|
}
|
|
|
|
await updateFile('lib/datasource/api.generated.ts', code);
|
|
|
|
|
2020-04-07 08:43:25 +00:00
|
|
|
// managers
|
|
|
|
await generate({ path: 'manager', types: ['ManagerApi'] });
|
2020-04-06 17:38:20 +00:00
|
|
|
|
2020-04-08 07:14:32 +00:00
|
|
|
// platform
|
|
|
|
await generate({
|
|
|
|
path: 'platform',
|
|
|
|
types: ['Platform'],
|
|
|
|
excludes: ['utils', 'git'],
|
|
|
|
});
|
|
|
|
|
2020-04-07 08:43:25 +00:00
|
|
|
// versioning
|
|
|
|
await generate({
|
|
|
|
path: 'versioning',
|
|
|
|
types: ['VersioningApi', 'VersioningApiConstructor'],
|
|
|
|
map: '.api',
|
|
|
|
});
|
2020-04-06 17:38:20 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
shell
|
|
|
|
.find('lib/**/*.generated.ts')
|
2020-04-12 16:09:36 +00:00
|
|
|
.filter((f) => !newFiles.has(f))
|
|
|
|
.map((file) => fs.remove(file))
|
2020-04-06 17:38:20 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
shell.echo(e.toString());
|
|
|
|
shell.exit(1);
|
|
|
|
}
|
|
|
|
})();
|