awesome-cordova-plugins/scripts/build/helpers.ts

149 lines
4.2 KiB
TypeScript
Raw Normal View History

import { readdirSync } from 'fs-extra';
2017-12-28 20:28:44 +08:00
import { camelCase, clone } from 'lodash';
2021-09-28 02:32:20 +08:00
import { join, resolve } from 'path';
import {
ArrayLiteralExpression,
createArrayLiteral,
createLiteral,
createNumericLiteral,
createObjectLiteral,
createPropertyAssignment,
Decorator,
Expression,
Node,
ObjectLiteralElementLike,
ObjectLiteralExpression,
SyntaxKind,
} from 'typescript';
2017-12-29 12:15:34 +08:00
import { Logger } from '../logger';
2017-12-28 20:28:44 +08:00
2021-09-28 02:32:20 +08:00
export const ROOT = resolve(__dirname, '../../');
2018-03-23 17:57:54 +08:00
// tslint:disable-next-line:no-var-requires
2021-09-28 02:32:20 +08:00
export const TS_CONFIG = clone(require(resolve(ROOT, 'tsconfig.json')));
2017-12-28 20:28:44 +08:00
export const COMPILER_OPTIONS = TS_CONFIG.compilerOptions;
2021-09-28 02:32:20 +08:00
export const PLUGINS_ROOT = join(ROOT, 'src/@awesome-cordova-plugins/plugins/');
export const PLUGIN_PATHS = readdirSync(PLUGINS_ROOT).map(d => join(PLUGINS_ROOT, d, 'index.ts'));
2017-12-28 20:28:44 +08:00
export function getDecorator(node: Node, index = 0): Decorator {
2018-09-18 00:23:18 +08:00
if (node.decorators && node.decorators[index]) {
2017-12-28 20:28:44 +08:00
return node.decorators[index];
2018-09-18 00:23:18 +08:00
}
2017-12-28 20:28:44 +08:00
}
export function hasDecorator(decoratorName: string, node: Node): boolean {
2020-05-16 20:40:49 +08:00
return (
node.decorators &&
node.decorators.length &&
node.decorators.findIndex(d => getDecoratorName(d) === decoratorName) > -1
);
2017-12-28 20:28:44 +08:00
}
export function getDecoratorName(decorator: any) {
return decorator.expression.expression.text;
}
export function getRawDecoratorArgs(decorator: any): any[] {
if (decorator.expression.arguments.length === 0) return [];
return decorator.expression.arguments[0].properties;
}
export function getDecoratorArgs(decorator: any) {
const properties: any[] = getRawDecoratorArgs(decorator);
const args = {};
properties.forEach(prop => {
let val: number | boolean;
2017-12-28 20:28:44 +08:00
switch (prop.initializer.kind) {
case SyntaxKind.StringLiteral:
case SyntaxKind.Identifier:
2017-12-28 20:28:44 +08:00
val = prop.initializer.text;
break;
case SyntaxKind.ArrayLiteralExpression:
2018-09-18 00:23:18 +08:00
val = prop.initializer.elements.map((e: any) => e.text);
2017-12-28 20:28:44 +08:00
break;
case SyntaxKind.TrueKeyword:
2017-12-28 20:28:44 +08:00
val = true;
break;
case SyntaxKind.FalseKeyword:
2017-12-28 20:28:44 +08:00
val = false;
break;
case SyntaxKind.NumericLiteral:
2017-12-28 20:28:44 +08:00
val = Number(prop.initializer.text);
break;
default:
2017-12-29 12:43:29 +08:00
Logger.debug('Unexpected property value type: ' + prop.initializer.kind);
2018-03-23 17:57:54 +08:00
throw new Error('Unexpected property value type << helpers.ts >>');
2017-12-28 20:28:44 +08:00
}
args[prop.name.text] = val;
});
return args;
}
/**
* FROM STENCIL
* Convert a js value into typescript AST
* @param val array, object, string, boolean, or number
* @returns Typescript Object Literal, Array Literal, String Literal, Boolean Literal, Numeric Literal
*/
export function convertValueToLiteral(val: any) {
if (Array.isArray(val)) {
return arrayToArrayLiteral(val);
}
if (typeof val === 'object') {
return objectToObjectLiteral(val);
}
if (typeof val === 'number') {
return createNumericLiteral(String(val));
2017-12-28 20:28:44 +08:00
}
return createLiteral(val);
2017-12-28 20:28:44 +08:00
}
/**
* FROM STENCIL
* Convert a js object into typescript AST
* @param obj key value object
* @returns Typescript Object Literal Expression
*/
function objectToObjectLiteral(obj: { [key: string]: any }): ObjectLiteralExpression {
const newProperties: ObjectLiteralElementLike[] = Object.keys(obj).map((key: string): ObjectLiteralElementLike => {
return createPropertyAssignment(createLiteral(key), convertValueToLiteral(obj[key]) as Expression);
});
2017-12-28 20:28:44 +08:00
return createObjectLiteral(newProperties);
2017-12-28 20:28:44 +08:00
}
/**
* FROM STENCIL
* Convert a js array into typescript AST
* @param list array
* @returns Typescript Array Literal Expression
*/
function arrayToArrayLiteral(list: any[]): ArrayLiteralExpression {
2017-12-28 20:28:44 +08:00
const newList: any[] = list.map(convertValueToLiteral);
return createArrayLiteral(newList);
2017-12-28 20:28:44 +08:00
}
export function getMethodsForDecorator(decoratorName: string) {
switch (decoratorName) {
2020-05-16 20:40:49 +08:00
case 'CordovaProperty':
return ['cordovaPropertyGet', 'cordovaPropertySet'];
case 'InstanceProperty':
return ['instancePropertyGet', 'instancePropertySet'];
case 'CordovaCheck':
return ['checkAvailability'];
case 'InstanceCheck':
return ['instanceAvailability'];
2017-12-28 20:28:44 +08:00
}
return [camelCase(decoratorName)];
}