Skip to content

Commit b0a8448

Browse files
committed
Split fs code from the transform() function
1 parent 10b32f9 commit b0a8448

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const debug = require('debug')('tagless-ember-components-codemod');
44
const chalk = require('chalk');
55

66
const SilentError = require('./silent-error');
7-
const { transform } = require('./transform');
7+
const { transformPath } = require('./transform');
88

99
async function run() {
1010
let path = await pkgDir();
@@ -24,7 +24,7 @@ async function runForPath(path, options = {}) {
2424

2525
for (let path of paths) {
2626
try {
27-
let tagName = transform(path);
27+
let tagName = transformPath(path);
2828
if (tagName) {
2929
log(chalk.green(`${chalk.dim(path)}: <${tagName}>...</${tagName}>`));
3030
} else {

lib/transform.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs');
22
const path = require('path');
33
const templateRecast = require('ember-template-recast');
44
const j = require('jscodeshift').withParser('ts');
5-
const debug = require('debug')('tagless-ember-components-codemod');
5+
const _debug = require('debug')('tagless-ember-components-codemod');
66

77
const SilentError = require('./silent-error');
88

@@ -46,17 +46,31 @@ const EVENT_HANDLER_METHODS = [
4646
'drop',
4747
];
4848

49-
function transform(componentPath) {
50-
let source = fs.readFileSync(componentPath, 'utf8');
49+
function transformPath(componentPath) {
50+
let debug = (fmt, ...args) => _debug(`${componentPath}: ${fmt}`, ...args);
5151

5252
let templatePath = guessTemplatePath(componentPath);
5353
if (!fs.existsSync(templatePath)) {
5454
throw new SilentError(`Could not find template at ${templatePath}`);
5555
}
56-
debug(`${componentPath}: templatePath: %o`, templatePath);
56+
debug('templatePath: %o', templatePath);
5757

58+
let source = fs.readFileSync(componentPath, 'utf8');
5859
let template = fs.readFileSync(templatePath, 'utf8');
5960

61+
let result = transform(source, template, { debug });
62+
63+
if (result.tagName) {
64+
fs.writeFileSync(componentPath, result.source, 'utf8');
65+
fs.writeFileSync(templatePath, result.template, 'utf8');
66+
}
67+
68+
return result.tagName;
69+
}
70+
71+
function transform(source, template, options = {}) {
72+
let debug = options.debug || _debug;
73+
6074
let root = j(source);
6175

6276
// find `export default Component.extend({ ... });` AST node
@@ -95,11 +109,11 @@ function transform(componentPath) {
95109

96110
// skip tagless components (silent)
97111
if (tagName === '') {
98-
debug(`${componentPath}: tagName: %o -> skip`, tagName);
99-
return;
112+
debug('tagName: %o -> skip', tagName);
113+
return { source, template };
100114
}
101115

102-
debug(`${componentPath}: tagName: %o`, tagName);
116+
debug('tagName: %o', tagName);
103117

104118
// skip components that use `this.element`
105119
let thisElementPaths = j(objectArg).find(j.MemberExpression, {
@@ -120,16 +134,16 @@ function transform(componentPath) {
120134

121135
// analyze `elementId`, `attributeBindings`, `classNames` and `classNameBindings`
122136
let elementId = findElementId(properties);
123-
debug(`${componentPath}: elementId: %o`, elementId);
137+
debug('elementId: %o', elementId);
124138

125139
let attributeBindings = findAttributeBindings(properties);
126-
debug(`${componentPath}: attributeBindings: %o`, attributeBindings);
140+
debug('attributeBindings: %o', attributeBindings);
127141

128142
let classNames = findClassNames(properties);
129-
debug(`${componentPath}: classNames: %o`, classNames);
143+
debug('classNames: %o', classNames);
130144

131145
let classNameBindings = findClassNameBindings(properties);
132-
debug(`${componentPath}: classNameBindings: %o`, classNameBindings);
146+
debug('classNameBindings: %o', classNameBindings);
133147

134148
let templateAST = templateRecast.parse(template);
135149

@@ -158,7 +172,7 @@ function transform(componentPath) {
158172
)
159173
.remove();
160174

161-
fs.writeFileSync(componentPath, root.toSource(), 'utf8');
175+
let newSource = root.toSource();
162176

163177
// wrap existing template with root element
164178
let classNodes = [];
@@ -200,9 +214,9 @@ function transform(componentPath) {
200214
}),
201215
];
202216

203-
fs.writeFileSync(templatePath, templateRecast.print(templateAST), 'utf8');
217+
let newTemplate = templateRecast.print(templateAST);
204218

205-
return tagName;
219+
return { source: newSource, template: newTemplate, tagName };
206220
}
207221

208222
function isProperty(path, name) {
@@ -301,6 +315,7 @@ function guessTemplatePath(componentPath) {
301315

302316
module.exports = {
303317
transform,
318+
transformPath,
304319
findTagName,
305320
findElementId,
306321
findAttributeBindings,

0 commit comments

Comments
 (0)