UNPKG

ember-codemod-add-component-signatures

Version:
50 lines (49 loc) 2 kB
import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { toEcma, updateJavaScript } from '@codemod-utils/ast-template-tag'; import { doubleColonize, pascalize } from '@codemod-utils/ember'; import { createFiles, } from '@codemod-utils/files'; import { getClassPath } from '../utils/components.js'; import { createRegistry, hasRegistry, renameComponent, } from './create-registries/index.js'; export function createRegistries(context, options) { const { extensionMap } = context; const { projectRoot } = options; const fileMap = new Map(); for (const [componentName, extensions] of extensionMap) { const filePath = getClassPath(componentName, extensions, options); try { let file = readFileSync(join(projectRoot, filePath), 'utf8'); const ecmaFile = toEcma(file); if (hasRegistry(ecmaFile)) { continue; } const data = { entity: { doubleColonizedName: doubleColonize(componentName), name: componentName, pascalizedName: pascalize(componentName), }, }; if (extensions.has('.gts')) { file = updateJavaScript(file, (code) => { code = renameComponent(code, data); code = createRegistry(code, data); return code; }); } else { file = renameComponent(file, data); file = createRegistry(file, data); } fileMap.set(filePath, file); } catch (error) { let message = `WARNING: createRegistries could not update \`${filePath}\`. Please update the file manually.`; if (error instanceof Error) { message += ` (${error.message})`; } console.warn(`${message}\n`); } } createFiles(fileMap, options); }