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