@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
149 lines • 7.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoerceComponentRule = CoerceComponentRule;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const ts_morph_1 = require("@rxap/ts-morph");
const utilities_1 = require("@rxap/utilities");
const workspace_utilities_1 = require("@rxap/workspace-utilities");
const Handlebars = require("handlebars");
const path_1 = require("path");
const ts_morph_transform_1 = require("../ts-morph-transform");
const build_angular_base_path_1 = require("./build-angular-base-path");
const has_component_1 = require("./has-component");
require("colors");
function applyContentHandlebars(options) {
return (entry) => {
const { path, content } = entry;
Handlebars.registerHelper('compile', (context, input) => {
return new Handlebars.SafeString(context(input.hash));
});
Handlebars.registerHelper('indent', (text, spaces) => {
const indent = new Array(spaces + 1).join(' ');
if (text instanceof Handlebars.SafeString) {
text = text.toString();
}
return new Handlebars.SafeString(text.split('\n').map((line, index) => {
return index === 0 ? line : indent + line;
}).join('\n'));
});
Handlebars.registerHelper('propertyValue', (value) => {
if (typeof value === 'string') {
return `'${value}'`;
}
return value;
});
/*
* {{#ifeq variable "string"}}
* ... do this ...
* {{/ifeq}}
*/
Handlebars.registerHelper('ifeq', function (a, b, options) {
if (a == b) {
return options.fn(this);
}
return options.inverse(this);
});
/*
* {{#ifnoteq variable "string"}}
* ... do this ...
* {{/ifnoteq}}
*/
Handlebars.registerHelper('ifnoteq', function (a, b, options) {
if (a != b) {
return options.fn(this);
}
return options.inverse(this);
});
Handlebars.registerHelper('dasherize', (value) => (0, utilities_1.dasherize)(value));
Handlebars.registerHelper('classify', (value) => (0, utilities_1.classify)(value));
Handlebars.registerHelper('decamelize', (value) => (0, utilities_1.decamelize)(value));
Handlebars.registerHelper('camelize', (value) => (0, utilities_1.camelize)(value));
Handlebars.registerHelper('underscore', (value) => (0, utilities_1.underscore)(value));
Handlebars.registerHelper('capitalize', (value) => (0, utilities_1.capitalize)(value));
try {
const template = Handlebars.compile(content.toString());
return {
path,
content: Buffer.from(template(options)),
};
}
catch (e) {
if (e.code === 'ERR_ENCODING_INVALID_ENCODED_DATA') {
return entry;
}
throw e;
}
};
}
function applyHandlebars(options) {
return (0, schematics_1.forEach)((0, schematics_1.when)((path) => path.endsWith('.hbs'), (0, schematics_1.composeFileOperators)([
applyContentHandlebars(options),
// See above for this weird cast.
(0, schematics_1.applyPathTemplate)(options),
(entry) => {
return {
content: entry.content,
path: entry.path.replace(/\.hbs$/, ''),
};
},
])));
}
function CoerceComponentRule(options) {
let { tsMorphTransform, template, project, feature, directory, name, flat, overwrite, handlebars, componentOptions = {}, } = options;
overwrite !== null && overwrite !== void 0 ? overwrite : (overwrite = false);
directory !== null && directory !== void 0 ? directory : (directory = '');
flat !== null && flat !== void 0 ? flat : (flat = directory.endsWith(name));
return tree => {
var _a, _b, _c;
const rules = [];
const basePath = (0, build_angular_base_path_1.BuildAngularBasePath)(tree, options);
const hasComponent = (0, has_component_1.HasComponent)(tree, options);
const componentPath = flat ? '' : name;
const prefix = (0, workspace_utilities_1.GetProjectPrefix)(tree, options.project);
if (!hasComponent) {
rules.push(() => console.log(`Project '${project}' does not have a component '${name}' in the feature '${feature}' in the directory '${directory}', New component will be created.`));
rules.push((0, ts_morph_transform_1.TsMorphAngularProjectTransformRule)(options, (project, [componentSourceFile]) => {
var _a, _b;
(_a = componentOptions.prefix) !== null && _a !== void 0 ? _a : (componentOptions.prefix = prefix);
(_b = componentOptions.changeDetection) !== null && _b !== void 0 ? _b : (componentOptions.changeDetection = 'OnPush');
(0, ts_morph_1.CoerceComponent)(componentSourceFile, name, componentOptions);
}, [
(0, path_1.join)(componentPath, `${name}.component.ts?`),
]));
rules.push(tree => {
(0, workspace_utilities_1.CoerceFile)(tree, (0, path_1.join)(basePath, componentPath, `${name}.component.html`), '');
(0, workspace_utilities_1.CoerceFile)(tree, (0, path_1.join)(basePath, componentPath, `${name}.component.scss`), '');
});
rules.push(() => console.log(`Component is create in folder: ${(0, path_1.join)(basePath, componentPath)}`.blue));
}
if (template && (overwrite === true || (Array.isArray(overwrite) && (overwrite === null || overwrite === void 0 ? void 0 : overwrite.includes('template'))) || !hasComponent)) {
if (handlebars) {
if (handlebars.helpers) {
Handlebars.registerHelper(handlebars.helpers);
}
if (handlebars.partials) {
Handlebars.registerPartial(handlebars.partials);
}
}
(_a = template.url) !== null && _a !== void 0 ? _a : (template.url = './files/component');
const templateOptions = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, core_1.strings), { prefix, scope: `${prefix}` }), options), ((_b = template === null || template === void 0 ? void 0 : template.options) !== null && _b !== void 0 ? _b : {})), { componentName: name });
(_c = templateOptions['prefix']) !== null && _c !== void 0 ? _c : (templateOptions['prefix'] = (0, workspace_utilities_1.GetProjectPrefix)(tree, options.project));
rules.push(() => console.log(`Template '${template.url}' will be used to modify the component.`), () => console.log(`Template options: ${JSON.stringify(templateOptions)}`.grey), (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)(template.url), [
(0, schematics_1.applyTemplates)(templateOptions),
applyHandlebars(templateOptions),
(0, schematics_1.move)(flat ? basePath : (0, path_1.join)(basePath, name)),
]), schematics_1.MergeStrategy.Overwrite));
}
if (tsMorphTransform) {
rules.push((0, ts_morph_transform_1.TsMorphAngularProjectTransformRule)(options, (project, [componentSourceFile]) => {
const componentClass = componentSourceFile.getClassOrThrow(`${(0, utilities_1.classify)(name)}Component`);
tsMorphTransform(project, [componentSourceFile], [componentClass], options);
}, [
(0, path_1.join)(componentPath, `${name}.component.ts`),
]));
}
return (0, schematics_1.chain)(rules);
};
}
//# sourceMappingURL=coerce-component.js.map