mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2026-05-20 00:06:24 +08:00
refactor(build): use typescript es6 imports
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { unlinkSync, writeJSONSync } from 'fs-extra';
|
||||
import { resolve } from 'path';
|
||||
import * as ts from 'typescript';
|
||||
import { ClassDeclaration, SyntaxKind, TransformationContext, visitEachChild } from 'typescript';
|
||||
|
||||
import { hasDecorator, ROOT } from '../helpers';
|
||||
|
||||
@@ -22,13 +22,13 @@ export const EMIT_PATH = resolve(ROOT, 'injectable-classes.json');
|
||||
* window['IonicNative'] object.
|
||||
*/
|
||||
export function extractInjectables() {
|
||||
return (ctx: ts.TransformationContext) => {
|
||||
return (ctx: TransformationContext) => {
|
||||
return tsSourceFile => {
|
||||
if (tsSourceFile.fileName.indexOf('src/@awesome-cordova-plugins/plugins') > -1) {
|
||||
ts.visitEachChild(
|
||||
visitEachChild(
|
||||
tsSourceFile,
|
||||
node => {
|
||||
if (node.kind !== ts.SyntaxKind.ClassDeclaration) {
|
||||
if (node.kind !== SyntaxKind.ClassDeclaration) {
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ export function extractInjectables() {
|
||||
if (isInjectable) {
|
||||
injectableClasses.push({
|
||||
file: tsSourceFile.path,
|
||||
className: (node as ts.ClassDeclaration).name.text,
|
||||
className: (node as ClassDeclaration).name.text,
|
||||
dirName: tsSourceFile.path.split(/[\\\/]+/).reverse()[1],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
import * as ts from 'typescript';
|
||||
import { createIdentifier, SourceFile, SyntaxKind, TransformationContext } from 'typescript';
|
||||
|
||||
import { getMethodsForDecorator } from '../helpers';
|
||||
|
||||
function transformImports(file: ts.SourceFile, ctx: ts.TransformationContext, ngcBuild?: boolean) {
|
||||
function transformImports(file: SourceFile, ctx: TransformationContext, ngcBuild?: boolean) {
|
||||
// remove angular imports
|
||||
if (!ngcBuild) {
|
||||
// @ts-expect-error
|
||||
file.statements = (file.statements as any).filter(
|
||||
(s: any) => !(s.kind === ts.SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@angular/core')
|
||||
(s: any) => !(s.kind === SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@angular/core')
|
||||
);
|
||||
}
|
||||
|
||||
// find the @awesome-cordova-plugins/core import statement
|
||||
const importStatement = (file.statements as any).find((s: any) => {
|
||||
return s.kind === ts.SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@awesome-cordova-plugins/core';
|
||||
return s.kind === SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@awesome-cordova-plugins/core';
|
||||
});
|
||||
|
||||
// we're only interested in files containing @awesome-cordova-plugins/core import statement
|
||||
@@ -40,11 +41,11 @@ function transformImports(file: ts.SourceFile, ctx: ts.TransformationContext, ng
|
||||
|
||||
decorators.forEach(d => (methods = getMethodsForDecorator(d).concat(methods)));
|
||||
|
||||
const methodElements = methods.map(m => ts.createIdentifier(m));
|
||||
const methodElements = methods.map(m => createIdentifier(m));
|
||||
const methodNames = methodElements.map(el => el.escapedText);
|
||||
|
||||
importStatement.importClause.namedBindings.elements = [
|
||||
ts.createIdentifier('AwesomeCordovaNativePlugin'),
|
||||
createIdentifier('AwesomeCordovaNativePlugin'),
|
||||
...methodElements,
|
||||
...importStatement.importClause.namedBindings.elements.filter(
|
||||
el => keep.indexOf(el.name.text) !== -1 && methodNames.indexOf(el.name.text) === -1
|
||||
@@ -69,7 +70,7 @@ function transformImports(file: ts.SourceFile, ctx: ts.TransformationContext, ng
|
||||
}
|
||||
|
||||
export function importsTransformer(ngcBuild?: boolean) {
|
||||
return (ctx: ts.TransformationContext) => {
|
||||
return (ctx: TransformationContext) => {
|
||||
return tsSourceFile => {
|
||||
return transformImports(tsSourceFile, ctx, ngcBuild);
|
||||
};
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import * as ts from 'typescript';
|
||||
import { ClassDeclaration, createConstructor, SyntaxKind } from 'typescript';
|
||||
|
||||
import { transformMethod } from './methods';
|
||||
import { transformProperty } from './properties';
|
||||
|
||||
export function transformMembers(cls: ts.ClassDeclaration) {
|
||||
export function transformMembers(cls: ClassDeclaration) {
|
||||
const propertyIndices: number[] = [];
|
||||
|
||||
const members = cls.members.map((member: any, index: number) => {
|
||||
@@ -10,13 +11,13 @@ export function transformMembers(cls: ts.ClassDeclaration) {
|
||||
if (!member.decorators || !member.decorators.length) return member;
|
||||
|
||||
switch (member.kind) {
|
||||
case ts.SyntaxKind.MethodDeclaration:
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
return transformMethod(member);
|
||||
case ts.SyntaxKind.PropertyDeclaration:
|
||||
case SyntaxKind.PropertyDeclaration:
|
||||
propertyIndices.push(index);
|
||||
return member;
|
||||
case ts.SyntaxKind.Constructor:
|
||||
return ts.createConstructor(undefined, undefined, member.parameters, member.body);
|
||||
case SyntaxKind.Constructor:
|
||||
return createConstructor(undefined, undefined, member.parameters, member.body);
|
||||
default:
|
||||
return member; // in case anything gets here by accident...
|
||||
}
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
import * as ts from 'typescript';
|
||||
import {
|
||||
createBinary,
|
||||
createBlock,
|
||||
createCall,
|
||||
createIdentifier,
|
||||
createIf,
|
||||
createImmediatelyInvokedArrowFunction,
|
||||
createLiteral,
|
||||
createMethod,
|
||||
createReturn,
|
||||
createThis,
|
||||
createTrue,
|
||||
Expression,
|
||||
MethodDeclaration,
|
||||
SyntaxKind,
|
||||
} from 'typescript';
|
||||
|
||||
import { Logger } from '../../logger';
|
||||
import {
|
||||
convertValueToLiteral,
|
||||
@@ -8,7 +24,7 @@ import {
|
||||
getMethodsForDecorator,
|
||||
} from '../helpers';
|
||||
|
||||
export function transformMethod(method: ts.MethodDeclaration) {
|
||||
export function transformMethod(method: MethodDeclaration) {
|
||||
if (!method) return;
|
||||
|
||||
const decorator = getDecorator(method),
|
||||
@@ -16,7 +32,7 @@ export function transformMethod(method: ts.MethodDeclaration) {
|
||||
decoratorArgs = getDecoratorArgs(decorator);
|
||||
|
||||
try {
|
||||
return ts.createMethod(
|
||||
return createMethod(
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
@@ -25,7 +41,7 @@ export function transformMethod(method: ts.MethodDeclaration) {
|
||||
method.typeParameters,
|
||||
method.parameters,
|
||||
method.type,
|
||||
ts.createBlock([ts.createReturn(getMethodBlock(method, decoratorName, decoratorArgs))])
|
||||
createBlock([createReturn(getMethodBlock(method, decoratorName, decoratorArgs))])
|
||||
);
|
||||
} catch (e) {
|
||||
Logger.error('Error transforming method: ' + (method.name as any).text);
|
||||
@@ -33,30 +49,30 @@ export function transformMethod(method: ts.MethodDeclaration) {
|
||||
}
|
||||
}
|
||||
|
||||
function getMethodBlock(method: ts.MethodDeclaration, decoratorName: string, decoratorArgs: any): ts.Expression {
|
||||
function getMethodBlock(method: MethodDeclaration, decoratorName: string, decoratorArgs: any): Expression {
|
||||
const decoratorMethod = getMethodsForDecorator(decoratorName)[0];
|
||||
|
||||
switch (decoratorName) {
|
||||
case 'CordovaCheck':
|
||||
case 'InstanceCheck':
|
||||
// TODO remove function wrapper
|
||||
return ts.createImmediatelyInvokedArrowFunction([
|
||||
ts.createIf(
|
||||
ts.createBinary(
|
||||
ts.createCall(ts.createIdentifier(decoratorMethod), undefined, [ts.createThis()]),
|
||||
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
||||
ts.createTrue()
|
||||
return createImmediatelyInvokedArrowFunction([
|
||||
createIf(
|
||||
createBinary(
|
||||
createCall(createIdentifier(decoratorMethod), undefined, [createThis()]),
|
||||
SyntaxKind.EqualsEqualsEqualsToken,
|
||||
createTrue()
|
||||
),
|
||||
method.body
|
||||
),
|
||||
]);
|
||||
|
||||
default:
|
||||
return ts.createCall(ts.createIdentifier(decoratorMethod), undefined, [
|
||||
ts.createThis(),
|
||||
ts.createLiteral(decoratorArgs?.methodName || (method.name as any).text),
|
||||
return createCall(createIdentifier(decoratorMethod), undefined, [
|
||||
createThis(),
|
||||
createLiteral(decoratorArgs?.methodName || (method.name as any).text),
|
||||
convertValueToLiteral(decoratorArgs),
|
||||
ts.createIdentifier('arguments'),
|
||||
createIdentifier('arguments'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
import * as ts from 'typescript';
|
||||
import {
|
||||
createClassDeclaration,
|
||||
createIdentifier,
|
||||
createProperty,
|
||||
createToken,
|
||||
SourceFile,
|
||||
SyntaxKind,
|
||||
TransformationContext,
|
||||
TransformerFactory,
|
||||
visitEachChild,
|
||||
} from 'typescript';
|
||||
|
||||
import { Logger } from '../../logger';
|
||||
import { convertValueToLiteral, getDecorator, getDecoratorArgs, getDecoratorName } from '../helpers';
|
||||
@@ -16,10 +26,10 @@ function transformClass(cls: any, ngcBuild?: boolean) {
|
||||
// add plugin decorator args as static properties of the plugin's class
|
||||
for (const prop in pluginDecoratorArgs) {
|
||||
pluginStatics.push(
|
||||
ts.createProperty(
|
||||
createProperty(
|
||||
undefined,
|
||||
[ts.createToken(ts.SyntaxKind.StaticKeyword)],
|
||||
ts.createIdentifier(prop),
|
||||
[createToken(SyntaxKind.StaticKeyword)],
|
||||
createIdentifier(prop),
|
||||
undefined,
|
||||
undefined,
|
||||
convertValueToLiteral(pluginDecoratorArgs[prop])
|
||||
@@ -28,11 +38,11 @@ function transformClass(cls: any, ngcBuild?: boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
cls = ts.createClassDeclaration(
|
||||
cls = 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)],
|
||||
[createToken(SyntaxKind.ExportKeyword)],
|
||||
cls.name,
|
||||
cls.typeParameters,
|
||||
cls.heritageClauses,
|
||||
@@ -43,14 +53,14 @@ function transformClass(cls: any, ngcBuild?: boolean) {
|
||||
return cls;
|
||||
}
|
||||
|
||||
function transformClasses(file: ts.SourceFile, ctx: ts.TransformationContext, ngcBuild?: boolean) {
|
||||
function transformClasses(file: SourceFile, ctx: TransformationContext, ngcBuild?: boolean) {
|
||||
Logger.silly('Transforming file: ' + file.fileName);
|
||||
return ts.visitEachChild(
|
||||
return visitEachChild(
|
||||
file,
|
||||
node => {
|
||||
if (
|
||||
node.kind !== ts.SyntaxKind.ClassDeclaration ||
|
||||
(node.modifiers && node.modifiers.find(v => v.kind === ts.SyntaxKind.DeclareKeyword))
|
||||
node.kind !== SyntaxKind.ClassDeclaration ||
|
||||
(node.modifiers && node.modifiers.find(v => v.kind === SyntaxKind.DeclareKeyword))
|
||||
) {
|
||||
return node;
|
||||
}
|
||||
@@ -60,11 +70,12 @@ function transformClasses(file: ts.SourceFile, ctx: ts.TransformationContext, ng
|
||||
);
|
||||
}
|
||||
|
||||
export function pluginClassTransformer(ngcBuild?: boolean): ts.TransformerFactory<ts.SourceFile> {
|
||||
return (ctx: ts.TransformationContext) => {
|
||||
export function pluginClassTransformer(ngcBuild?: boolean): TransformerFactory<SourceFile> {
|
||||
return (ctx: TransformationContext) => {
|
||||
return tsSourceFile => {
|
||||
if (tsSourceFile.fileName.indexOf('src/@awesome-cordova-plugins/plugins') > -1)
|
||||
if (tsSourceFile.fileName.indexOf('src/@awesome-cordova-plugins/plugins') > -1) {
|
||||
return transformClasses(tsSourceFile, ctx, ngcBuild);
|
||||
}
|
||||
return tsSourceFile;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,9 +1,21 @@
|
||||
import * as ts from 'typescript';
|
||||
import {
|
||||
createBlock,
|
||||
createCall,
|
||||
createGetAccessor,
|
||||
createIdentifier,
|
||||
createLiteral,
|
||||
createParameter,
|
||||
createReturn,
|
||||
createSetAccessor,
|
||||
createStatement,
|
||||
createThis,
|
||||
PropertyDeclaration,
|
||||
} from 'typescript';
|
||||
|
||||
import { getDecorator, getDecoratorName } from '../helpers';
|
||||
|
||||
export function transformProperty(members: any[], index: number) {
|
||||
const property = members[index] as ts.PropertyDeclaration,
|
||||
const property = members[index] as PropertyDeclaration,
|
||||
decorator = getDecorator(property),
|
||||
decoratorName = getDecoratorName(decorator);
|
||||
|
||||
@@ -22,33 +34,33 @@ export function transformProperty(members: any[], index: number) {
|
||||
return property;
|
||||
}
|
||||
|
||||
const getter = ts.createGetAccessor(
|
||||
const getter = createGetAccessor(
|
||||
undefined,
|
||||
undefined,
|
||||
property.name,
|
||||
undefined,
|
||||
property.type,
|
||||
ts.createBlock([
|
||||
ts.createReturn(
|
||||
ts.createCall(ts.createIdentifier(type + 'PropertyGet'), undefined, [
|
||||
ts.createThis(),
|
||||
ts.createLiteral((property.name as any).text),
|
||||
createBlock([
|
||||
createReturn(
|
||||
createCall(createIdentifier(type + 'PropertyGet'), undefined, [
|
||||
createThis(),
|
||||
createLiteral((property.name as any).text),
|
||||
])
|
||||
),
|
||||
])
|
||||
);
|
||||
|
||||
const setter = ts.createSetAccessor(
|
||||
const setter = createSetAccessor(
|
||||
undefined,
|
||||
undefined,
|
||||
property.name,
|
||||
[ts.createParameter(undefined, undefined, undefined, 'value', undefined, property.type)],
|
||||
ts.createBlock([
|
||||
ts.createStatement(
|
||||
ts.createCall(ts.createIdentifier(type + 'PropertySet'), undefined, [
|
||||
ts.createThis(),
|
||||
ts.createLiteral((property.name as any).text),
|
||||
ts.createIdentifier('value'),
|
||||
[createParameter(undefined, undefined, undefined, 'value', undefined, property.type)],
|
||||
createBlock([
|
||||
createStatement(
|
||||
createCall(createIdentifier(type + 'PropertySet'), undefined, [
|
||||
createThis(),
|
||||
createLiteral((property.name as any).text),
|
||||
createIdentifier('value'),
|
||||
])
|
||||
),
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user