awesome-cordova-plugins/scripts/tasks/build-esm.ts
2021-09-27 20:26:16 +02:00

38 lines
1.4 KiB
TypeScript

import { readFileSync, readJSONSync, writeFileSync } from 'fs-extra';
import * as path from 'path';
import { PLUGIN_PATHS, ROOT } from '../build/helpers';
import { EMIT_PATH } from '../build/transformers/extract-injectables';
import { generateDeclarations, transpile } from '../build/transpile';
generateDeclarations();
transpile();
const outDirs = PLUGIN_PATHS.map(p =>
p.replace(path.join(ROOT, 'src'), path.join(ROOT, 'dist')).replace(/[\\/]index.ts/, '')
);
const injectableClasses = readJSONSync(EMIT_PATH);
outDirs.forEach(dir => {
const classes = injectableClasses.filter(entry => entry.dirName === dir.split(/[\\/]+/).pop());
let jsFile: string = readFileSync(path.join(dir, 'index.js'), 'utf-8'),
dtsFile: string = readFileSync(path.join(dir, 'index.d.ts'), 'utf-8');
classes.forEach(entry => {
dtsFile = dtsFile.replace(`class ${entry.className} `, 'class ' + entry.className + 'Original ');
dtsFile += `\nexport declare const ${entry.className}: ${entry.className}Original;`;
jsFile = jsFile.replace(
new RegExp(`([\\s\\(])${entry.className}([\\s\\.;\\(,])`, 'g'),
'$1' + entry.className + 'Original$2'
);
jsFile = jsFile.replace(
`export { ${entry.className}Original }`,
`var ${entry.className} = new ${entry.className}Original();\nexport { ${entry.className} }`
);
});
writeFileSync(path.join(dir, 'index.js'), jsFile, 'utf-8');
writeFileSync(path.join(dir, 'index.d.ts'), dtsFile, 'utf-8');
});