ember-codemod-add-component-signatures
Version:
Codemod to add component signatures
41 lines (40 loc) • 1.55 kB
JavaScript
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { updateJavaScript } from '@codemod-utils/ast-template-tag';
import { pascalize } from '@codemod-utils/ember';
import { createFiles, } from '@codemod-utils/files';
import { getClassPath } from '../utils/components.js';
import { createSignature } from './create-signatures/index.js';
export function createSignatures(context, options) {
const { extensionMap } = context;
const { projectRoot } = options;
const fileMap = new Map();
for (const [componentName, extensions] of extensionMap) {
const filePath = getClassPath(componentName, extensions, options);
const data = {
entity: {
pascalizedName: pascalize(componentName),
},
};
try {
let file = readFileSync(join(projectRoot, filePath), 'utf8');
if (extensions.has('.gts')) {
file = updateJavaScript(file, (code) => {
return createSignature(code, data);
});
}
else {
file = createSignature(file, data);
}
fileMap.set(filePath, file);
}
catch (error) {
let message = `WARNING: createSignatures could not update \`${filePath}\`. Please update the file manually.`;
if (error instanceof Error) {
message += ` (${error.message})`;
}
console.warn(`${message}\n`);
}
}
createFiles(fileMap, options);
}