mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2026-04-13 00:00:10 +08:00
Merge in v5 code
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import * as ts from 'typescript';
|
||||
import { convertValueToLiteral, getDecorator, getDecoratorArgs, getDecoratorName } from '../helpers';
|
||||
import { transformMembers } from './members';
|
||||
|
||||
function transformClass(cls: any, ngcBuild?: boolean) {
|
||||
|
||||
console.time('~ transformClass: ' + cls.name.text);
|
||||
|
||||
const pluginStatics = [];
|
||||
const dec: any = getDecorator(cls);
|
||||
|
||||
if (dec) {
|
||||
const pluginDecoratorArgs = getDecoratorArgs(dec);
|
||||
|
||||
// add plugin decorator args as static properties of the plugin's class
|
||||
for (let prop in pluginDecoratorArgs) {
|
||||
pluginStatics.push(ts.createProperty(
|
||||
undefined,
|
||||
[ts.createToken(ts.SyntaxKind.StaticKeyword)],
|
||||
ts.createIdentifier(prop),
|
||||
undefined,
|
||||
undefined,
|
||||
convertValueToLiteral(pluginDecoratorArgs[prop])
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
cls = ts.createClassDeclaration(
|
||||
ngcBuild && cls.decorators && cls.decorators.length? cls.decorators.filter(d => getDecoratorName(d) === 'Injectable') : undefined, // remove Plugin and Injectable decorators
|
||||
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
|
||||
cls.name,
|
||||
cls.typeParameters,
|
||||
cls.heritageClauses,
|
||||
[
|
||||
...transformMembers(cls),
|
||||
...pluginStatics
|
||||
]
|
||||
);
|
||||
|
||||
console.timeEnd('~ transformClass: ' + cls.name.text);
|
||||
return cls;
|
||||
}
|
||||
|
||||
function transformClasses(file: ts.SourceFile, ctx: ts.TransformationContext, ngcBuild?: boolean) {
|
||||
// console.log('Transforming file: ' + file.fileName);
|
||||
return ts.visitEachChild(file, node => {
|
||||
if (node.kind !== ts.SyntaxKind.ClassDeclaration) {
|
||||
return node;
|
||||
}
|
||||
|
||||
return transformClass(node, ngcBuild);
|
||||
}, ctx);
|
||||
}
|
||||
|
||||
export function pluginClassTransformer(ngcBuild?: boolean): ts.TransformerFactory<ts.SourceFile> {
|
||||
return (ctx: ts.TransformationContext) => {
|
||||
return tsSourceFile => {
|
||||
if (tsSourceFile.fileName.indexOf('src/plugins') > -1)
|
||||
return transformClasses(tsSourceFile, ctx, ngcBuild);
|
||||
|
||||
return tsSourceFile;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user