@rxap/schematics-ts-morph
Version:
This package provides utilities for manipulating TypeScript code using ts-morph, particularly for Angular and NestJS projects. It offers functions to add, coerce, and modify code elements like classes, methods, decorators, and imports. The package also in
71 lines • 3.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoerceNavigationTableActionRule = CoerceNavigationTableActionRule;
const ts_morph_1 = require("@rxap/ts-morph");
const ts_morph_2 = require("ts-morph");
const coerce_table_action_1 = require("./coerce-table-action");
function extractAllProperties(route) {
const match = route.match(/\{\{([^}]+)}}/g);
if (!match) {
return [];
}
return Array.from(match).map(m => m.replace('{{', '').replace('}}', ''));
}
function buildDynamicRoute(route) {
const properties = extractAllProperties(route);
if (!properties.length) {
return route;
}
for (const property of properties) {
route = route.replace(`{{${property}}}`, `\${${property}}`);
}
return route;
}
function CoerceNavigationTableActionRule(options) {
let { tsMorphTransform, tableName, type, route, relativeTo, } = options;
tsMorphTransform !== null && tsMorphTransform !== void 0 ? tsMorphTransform : (tsMorphTransform = () => ({}));
route !== null && route !== void 0 ? route : (route = '{{uuid}}');
return (0, coerce_table_action_1.CoerceTableActionRule)(Object.assign(Object.assign({}, options), { tsMorphTransform: (project, sourceFile, classDeclaration) => {
(0, ts_morph_1.CoerceImports)(sourceFile, {
namedImports: ['Router'],
moduleSpecifier: '@angular/router',
});
(0, ts_morph_1.CoerceDependencyInjection)(sourceFile, {
injectionToken: 'Router',
parameterName: 'router',
scope: ts_morph_2.Scope.Private,
module: ts_morph_1.Module.ANGULAR,
});
if (relativeTo) {
(0, ts_morph_1.CoerceDependencyInjection)(sourceFile, {
injectionToken: 'ActivatedRoute',
parameterName: 'route',
scope: ts_morph_2.Scope.Private,
module: ts_morph_1.Module.ANGULAR,
});
(0, ts_morph_1.CoerceImports)(sourceFile, {
namedImports: ['ActivatedRoute'],
moduleSpecifier: '@angular/router',
});
}
const properties = extractAllProperties(route);
const statements = [];
statements.push(`console.log(\`action row type: ${type}\`, parameters);`);
let routeValue = route;
if (properties.length) {
statements.push(`const { ${properties.join(', ')} } = parameters;`);
for (const property of properties) {
statements.push(`if (!${property}) { throw new Error('The table action ${type} is called with a row object that does not have the property ${property}.'); }`);
}
routeValue = buildDynamicRoute(route);
}
if (relativeTo) {
statements.push(`return this.router.navigate([ \`${routeValue}\` ], { relativeTo: this.route } );`);
}
else {
statements.push(`return this.router.navigate([ \`${routeValue}\` ]);`);
}
return Object.assign({ statements, scope: ts_morph_2.Scope.Public, returnType: 'Promise<any>' }, tsMorphTransform(project, sourceFile, classDeclaration));
} }));
}
//# sourceMappingURL=coerce-navigation-table-action.js.map