awesome-cordova-plugins/scripts/build/transformers/imports.ts

79 lines
2.5 KiB
TypeScript
Raw Normal View History

import { factory, SourceFile, SyntaxKind, TransformationContext } from 'typescript';
2017-12-28 20:28:44 +08:00
import { getMethodsForDecorator } from '../helpers';
function transformImports(file: SourceFile, ctx: TransformationContext, ngcBuild?: boolean) {
2017-12-28 20:28:44 +08:00
// remove angular imports
if (!ngcBuild) {
// @ts-expect-error
2020-05-16 20:40:49 +08:00
file.statements = (file.statements as any).filter(
(s: any) => !(s.kind === SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@angular/core')
2020-05-16 20:40:49 +08:00
);
2017-12-28 20:28:44 +08:00
}
// find the @awesome-cordova-plugins/core import statement
2017-12-28 20:28:44 +08:00
const importStatement = (file.statements as any).find((s: any) => {
return s.kind === SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@awesome-cordova-plugins/core';
2017-12-28 20:28:44 +08:00
});
// we're only interested in files containing @awesome-cordova-plugins/core import statement
2017-12-28 20:28:44 +08:00
if (!importStatement) return file;
2018-03-23 17:57:54 +08:00
const decorators: string[] = [];
2017-12-28 20:28:44 +08:00
const decoratorRegex: RegExp = /@([a-zA-Z]+)\(/g;
2020-05-16 20:40:49 +08:00
const ignored: string[] = ['Plugin', 'Component', 'Injectable'];
2018-04-20 01:52:26 +08:00
2020-05-16 20:40:49 +08:00
const keep: string[] = ['getPromise', 'checkAvailability'];
2017-12-28 21:40:52 +08:00
2017-12-28 20:28:44 +08:00
let m;
while ((m = decoratorRegex.exec(file.text)) !== null) {
if (m.index === decoratorRegex.lastIndex) {
decoratorRegex.lastIndex++;
}
2017-12-28 21:40:52 +08:00
if (m && m[1] && decorators.indexOf(m[1]) === -1 && ignored.indexOf(m[1]) === -1) decorators.push(m[1]);
2017-12-28 20:28:44 +08:00
}
if (decorators.length) {
let methods = [];
2021-09-28 04:09:05 +08:00
decorators.forEach((d) => (methods = getMethodsForDecorator(d).concat(methods)));
2017-12-28 20:28:44 +08:00
2021-09-28 04:09:05 +08:00
const methodElements = methods.map((m) => factory.createIdentifier(m));
const methodNames = methodElements.map((el) => el.escapedText);
2017-12-28 20:28:44 +08:00
importStatement.importClause.namedBindings.elements = [
factory.createIdentifier('AwesomeCordovaNativePlugin'),
...methodElements,
...importStatement.importClause.namedBindings.elements.filter(
2021-09-28 04:09:05 +08:00
(el) => keep.indexOf(el.name.text) !== -1 && methodNames.indexOf(el.name.text) === -1
2020-05-16 20:40:49 +08:00
),
2017-12-28 20:28:44 +08:00
];
2018-04-10 03:18:30 +08:00
if (ngcBuild) {
importStatement.importClause.namedBindings.elements = importStatement.importClause.namedBindings.elements.map(
2021-09-28 04:09:05 +08:00
(binding) => {
2018-04-20 01:52:26 +08:00
if (binding.escapedText) {
binding.name = {
2020-05-16 20:40:49 +08:00
text: binding.escapedText,
2018-04-20 01:52:26 +08:00
};
}
2018-04-10 03:18:30 +08:00
return binding;
}
);
}
2017-12-28 20:28:44 +08:00
}
return file;
}
export function importsTransformer(ngcBuild?: boolean) {
return (ctx: TransformationContext) => {
2021-09-28 04:09:05 +08:00
return (tsSourceFile) => {
2017-12-28 20:28:44 +08:00
return transformImports(tsSourceFile, ctx, ngcBuild);
2018-03-23 17:57:54 +08:00
};
2017-12-28 20:28:44 +08:00
};
}