Files
awesome-cordova-plugins/scripts/build/transformers/extract-injectables.ts
T
Daniel Sogl 120e0f6d23 refactor: replace build dependencies with native Node.js APIs
Replace fs-extra/rimraf with native node:fs, lodash with
structuredClone/spread, ts-node with tsx, minimist with node:util
parseArgs, winston with console logger, async-promise-queue with
native Promise concurrency. Use promisify for child_process.exec.
Normalize all imports to use node: protocol. Extract Jest config to
jest.config.ts and replace ts-jest with @swc/jest.
2026-03-21 15:16:55 -07:00

60 lines
1.6 KiB
TypeScript

import { unlinkSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { ClassDeclaration, SyntaxKind, TransformationContext, visitEachChild } from 'typescript';
import { hasDecorator, ROOT } from '../helpers';
export interface InjectableClassEntry {
file: string;
className: string;
dirName: string;
}
const injectableClasses: InjectableClassEntry[] = [];
export const EMIT_PATH = resolve(ROOT, 'injectable-classes.json');
/**
* This transformer extracts all the injectable classes
* so we can use all the names later on when we compile
* an es5 bundle.
*
* Every injectable class will end up in the
* window['IonicNative'] object.
*/
export function extractInjectables() {
return (ctx: TransformationContext) => {
return (tsSourceFile) => {
if (tsSourceFile.fileName.indexOf('src/@awesome-cordova-plugins/plugins') > -1) {
visitEachChild(
tsSourceFile,
(node) => {
if (node.kind !== SyntaxKind.ClassDeclaration) {
return node;
}
const isInjectable: boolean = hasDecorator('Injectable', node);
if (isInjectable) {
injectableClasses.push({
file: tsSourceFile.path,
className: (node as ClassDeclaration).name.text,
dirName: tsSourceFile.path.split(/[\\\/]+/).reverse()[1],
});
}
},
ctx
);
}
return tsSourceFile;
};
};
}
export function emitInjectableClasses() {
writeFileSync(EMIT_PATH, JSON.stringify(injectableClasses, null, 2));
}
export function cleanEmittedData() {
unlinkSync(EMIT_PATH);
}