UNPKG

ember-codemod-css-modules

Version:

Codemod to replace ember-component-css with ember-css-modules (compatible with embroider-css-modules)

29 lines (28 loc) 991 B
import { readFileSync, writeFileSync, existsSync } from 'node:fs'; import { AST } from '@codemod-utils/ast-template'; const expressionsToReplace = ['this.styleNamespace', 'this.componentCssClassName']; const replaceWith = 'this.styles.component'; function replaceHbsUsage(filePath) { if (!existsSync(filePath)) return false; let didReplace = false; const contents = readFileSync(filePath).toString(); const traverse = AST.traverse(); const ast = traverse(contents, { PathExpression(path) { if (!expressionsToReplace.includes(path.original)) return; path.original = replaceWith; didReplace = true; }, }); writeFileSync(filePath, AST.print(ast)); return didReplace; } export function updateTemplates(context, options) { for (const entry of context) { if (replaceHbsUsage(`${options.projectRoot}/${entry.hbsPath}`)) { entry.hasHbsUsage = true; } } }