Skip to content

Commit bdd2b87

Browse files
author
Robert Jackson
committed
yarn lint --fix
1 parent ef4acb0 commit bdd2b87

File tree

19 files changed

+361
-373
lines changed

19 files changed

+361
-373
lines changed

lib/migrator.js

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
const fs = require('fs');
44
const path = require('path');
5-
const glob = require("glob");
6-
const { getLayoutNameTemplates, getPartialTemplates, getImportedTemplates } = require('./utils/templates')
7-
const { moveFile, removeDirs } = require('./utils/file')
5+
const glob = require('glob');
6+
const {
7+
getLayoutNameTemplates,
8+
getPartialTemplates,
9+
getImportedTemplates,
10+
} = require('./utils/templates');
11+
const { moveFile, removeDirs } = require('./utils/file');
812
const { transform: dropLayoutBinding } = require('./utils/rewrite-imports');
913
const { template } = require('jscodeshift');
1014

@@ -59,30 +63,36 @@ module.exports = class Migrator {
5963
}
6064

6165
findComponentTemplates() {
62-
return glob.sync(`{${this.appComponentTemplatesDir},${this.addonComponentTemplatesDir}}/**/*.hbs`);
66+
return glob.sync(
67+
`{${this.appComponentTemplatesDir},${this.addonComponentTemplatesDir}}/**/*.hbs`
68+
);
6369
}
6470

6571
findComponentClasses() {
6672
return glob.sync(`{${this.appBackingClassesDir},${this.addonBackingClassesDir}}/**/*.{js,ts}`);
6773
}
6874

6975
findAllTemplates() {
70-
return glob.sync(`{${this.appBackingClassesDir},${this.appTemplatesDir},${this.addonBackingClassesDir},${this.addonTemplatesDir}}/**/*.hbs`);
76+
return glob.sync(
77+
`{${this.appBackingClassesDir},${this.appTemplatesDir},${this.addonBackingClassesDir},${this.addonTemplatesDir}}/**/*.hbs`
78+
);
7179
}
7280

7381
/**
7482
* @param {string[]} templateFilePaths
7583
* @param {string[]} componentsWithLayoutName
7684
*/
7785
skipTemplatesUsedAsLayoutName(templateFilePaths, componentsWithLayoutName) {
78-
console.info(`\nChecking if any component templates are used as templates of other components using \`layoutName\``);
86+
console.info(
87+
`\nChecking if any component templates are used as templates of other components using \`layoutName\``
88+
);
7989

8090
if (componentsWithLayoutName.length) {
81-
componentsWithLayoutName.sort().forEach(component => {
91+
componentsWithLayoutName.sort().forEach((component) => {
8292
console.info(`❌ Did not move '${component}' due to usage as "layoutName" in a component`);
8393
});
8494

85-
templateFilePaths = templateFilePaths.filter(templateFilePath => {
95+
templateFilePaths = templateFilePaths.filter((templateFilePath) => {
8696
// Extract '/(app|addon)/templates/components/nested1/nested-component.hbs'
8797
const filePathFromApp = templateFilePath.slice(this.projectRoot.length);
8898

@@ -102,19 +112,19 @@ module.exports = class Migrator {
102112
}
103113

104114
/**
105-
* @param {string[]} templateFilePaths
115+
* @param {string[]} templateFilePaths
106116
*/
107117
skipTemplatesUsedAsPartial(templateFilePaths) {
108118
console.info(`\nChecking if any component templates are used as partials`);
109119

110120
const componentsWithPartial = getPartialTemplates(this.findAllTemplates());
111121

112122
if (componentsWithPartial.length) {
113-
componentsWithPartial.sort().forEach(component => {
123+
componentsWithPartial.sort().forEach((component) => {
114124
console.info(`❌ Did not move '${component}' due to usage as a "partial"`);
115125
});
116126

117-
templateFilePaths = templateFilePaths.filter(templateFilePath => {
127+
templateFilePaths = templateFilePaths.filter((templateFilePath) => {
118128
// Extract '/(app|addon)/templates/components/nested1/nested-component.hbs'
119129
let filePathFromApp = templateFilePath.slice(this.projectRoot.length);
120130

@@ -150,10 +160,8 @@ module.exports = class Migrator {
150160
* @param {string[]} templateFilePaths
151161
* @returns {string[]}
152162
*/
153-
skipTemplatesUsedInMultipleBackingClasses(templateFilePaths) {
154-
console.info(
155-
'\nChecking if any component templates are used in multiple backing classes'
156-
);
163+
skipTemplatesUsedInMultipleBackingClasses(templateFilePaths) {
164+
console.info('\nChecking if any component templates are used in multiple backing classes');
157165

158166
const componentFilePaths = this.findComponentClasses();
159167
const componentsImportingTemplates = getImportedTemplates(
@@ -168,12 +176,9 @@ module.exports = class Migrator {
168176

169177
// Map the imported template name back to the template file paths as we
170178
// have them on
171-
const importedTemplate = importedTemplates[0]
172-
.replace(/.*\/templates/gi, "templates");
179+
const importedTemplate = importedTemplates[0].replace(/.*\/templates/gi, 'templates');
173180

174-
const template = templateFilePaths.find(
175-
path => path.includes(importedTemplate)
176-
)
181+
const template = templateFilePaths.find((path) => path.includes(importedTemplate));
177182

178183
// If we've previously put this in the "allowed" bucket, we now know it
179184
// *shouldn't* be allowed, because it's used in another
@@ -194,8 +199,9 @@ module.exports = class Migrator {
194199
}
195200

196201
for (let [templatePath, importingModules] of reusedTemplates.entries()) {
197-
const importingModuleLocalPaths =
198-
importingModules.map(path => path.replace(/.*\/(app|addon)/, ""));
202+
const importingModuleLocalPaths = importingModules.map((path) =>
203+
path.replace(/.*\/(app|addon)/, '')
204+
);
199205

200206
const message =
201207
`❌ Did not move '${templatePath}' because it was imported by ` +
@@ -205,24 +211,27 @@ module.exports = class Migrator {
205211
console.info(message);
206212
}
207213

208-
const nonImported = templateFilePaths
209-
.filter(path => !allowed.has(path) && !reusedTemplates.has(path));
214+
const nonImported = templateFilePaths.filter(
215+
(path) => !allowed.has(path) && !reusedTemplates.has(path)
216+
);
210217

211218
return Array.from(allowed.keys()).concat(nonImported);
212219
}
213220

214221
/**
215-
* @param {string[]} templateFilePaths
222+
* @param {string[]} templateFilePaths
216223
*/
217224
changeComponentStructureToFlat(templateFilePaths) {
218-
templateFilePaths.forEach(templateFilePath => {
225+
templateFilePaths.forEach((templateFilePath) => {
219226
// Extract '/(app|addon)/templates/components/nested1/nested-component.hbs'
220227
const filePathFromApp = templateFilePath.slice(this.projectRoot.length);
221228

222229
const type = typeFromFilePath(filePathFromApp);
223230

224231
// Extract '/nested1/nested-component.hbs'
225-
const filePathFromAppTemplatesComponents = filePathFromApp.slice(`${type}/templates/components/`.length);
232+
const filePathFromAppTemplatesComponents = filePathFromApp.slice(
233+
`${type}/templates/components/`.length
234+
);
226235

227236
// '[APP_PATH]/(app|addon)/components/nested1/nested-component.hbs'
228237
const root = type === 'app' ? this.appRoot : this.addonRoot;
@@ -232,19 +241,21 @@ module.exports = class Migrator {
232241
}
233242

234243
/**
235-
* @param {string[]} templateFilePaths
244+
* @param {string[]} templateFilePaths
236245
*/
237246
changeComponentStructureToNested(templateFilePaths) {
238247
const classFilePaths = this.findComponentClasses();
239248

240-
templateFilePaths.forEach(templateFilePath => {
249+
templateFilePaths.forEach((templateFilePath) => {
241250
// Extract '/(app|addon)/templates/components/nested1/nested-component.hbs'
242251
const filePathFromProject = templateFilePath.slice(this.projectRoot.length);
243252

244253
const type = typeFromFilePath(filePathFromProject);
245254

246255
// Extract '/nested1/nested-component.hbs'
247-
const filePathFromAppTemplatesComponents = filePathFromProject.slice(`${type}/templates/components/`.length);
256+
const filePathFromAppTemplatesComponents = filePathFromProject.slice(
257+
`${type}/templates/components/`.length
258+
);
248259
const fileExtension = path.extname(filePathFromAppTemplatesComponents);
249260

250261
// Extract '/nested1/nested-component'
@@ -258,42 +269,39 @@ module.exports = class Migrator {
258269
// Build '[APP_PATH]/(app|addon)/components/nested1/nested-component/index.js'
259270
const classFilePath = {
260271
js: path.join(root, 'components', `${targetPath}.js`),
261-
ts: path.join(root, 'components', `${targetPath}.ts`)
272+
ts: path.join(root, 'components', `${targetPath}.ts`),
262273
};
263274

264275
if (classFilePaths.includes(classFilePath.js)) {
265276
const newClassFilePath = path.join(root, 'components', targetPath, 'index.js');
266277
moveFile(classFilePath.js, newClassFilePath);
267-
268278
} else if (classFilePaths.includes(classFilePath.ts)) {
269279
const newClassFilePath = path.join(root, 'components', targetPath, 'index.ts');
270280
moveFile(classFilePath.ts, newClassFilePath);
271-
272281
}
273282
});
274283
}
275284

276285
/**
277-
* @param {string[]} templateFilePaths
286+
* @param {string[]} templateFilePaths
278287
*/
279288
removeLayoutBinding(templateFilePaths) {
280-
templateFilePaths.forEach(templateFilePath => {
289+
templateFilePaths.forEach((templateFilePath) => {
281290
// Extract '/(app|addon)/templates/components/nested1/nested-component.hbs'
282-
const filePathFromProject = templateFilePath
283-
.slice(this.projectRoot.length);
284-
291+
const filePathFromProject = templateFilePath.slice(this.projectRoot.length);
292+
285293
const backingJsClassProjectPath = filePathFromProject
286294
.replace('templates/components', 'components')
287295
.replace('.hbs', '.js');
288-
296+
289297
const backingTsClassProjectPath = filePathFromProject
290298
.replace('templates/components', 'components')
291299
.replace('.hbs', '.ts');
292-
293-
const backingClassPaths =
294-
[backingJsClassProjectPath, backingTsClassProjectPath].map(
295-
backingClassPath => path.join(this.projectRoot, backingClassPath)
296-
);
300+
301+
const backingClassPaths = [
302+
backingJsClassProjectPath,
303+
backingTsClassProjectPath,
304+
].map((backingClassPath) => path.join(this.projectRoot, backingClassPath));
297305

298306
backingClassPaths.forEach((path) => {
299307
if (fs.existsSync(path)) {
@@ -303,7 +311,7 @@ module.exports = class Migrator {
303311
fs.writeFileSync(path, updatedContent);
304312
}
305313
}
306-
})
314+
});
307315
});
308316
}
309317

@@ -320,7 +328,7 @@ module.exports = class Migrator {
320328
const removeOnlyEmptyDirectories = Boolean(
321329
templatesWithLayoutName.length + componentsImportingTemplates.length
322330
);
323-
331+
324332
await removeDirs(this.appComponentTemplatesDir, removeOnlyEmptyDirectories);
325333
await removeDirs(this.addonComponentTemplatesDir, removeOnlyEmptyDirectories);
326334
}
@@ -330,28 +338,29 @@ module.exports = class Migrator {
330338
const componentsWithLayoutName = getLayoutNameTemplates(classFilePaths);
331339

332340
let templateFilePaths = this.findComponentTemplates();
333-
templateFilePaths = this.skipTemplatesUsedAsLayoutName(templateFilePaths, componentsWithLayoutName);
341+
templateFilePaths = this.skipTemplatesUsedAsLayoutName(
342+
templateFilePaths,
343+
componentsWithLayoutName
344+
);
334345
templateFilePaths = this.skipTemplatesUsedAsPartial(templateFilePaths);
335346
templateFilePaths = this.skipTemplatesUsedInMultipleBackingClasses(templateFilePaths);
336347

337348
this.removeLayoutBinding(templateFilePaths);
338349

339350
if (this.structure === 'flat') {
340351
this.changeComponentStructureToFlat(templateFilePaths);
341-
342352
} else if (this.structure === 'nested') {
343353
this.changeComponentStructureToNested(templateFilePaths);
344-
345354
}
346355

347356
// Clean up
348357
await this.removeEmptyClassicComponentDirectories();
349358
}
350-
}
359+
};
351360

352361
/**
353362
* Determine whether the file path represents an app or an addon
354-
* @param {string} filePathFromApp
363+
* @param {string} filePathFromApp
355364
* @return {'app' | 'addon'}
356365
*/
357366
function typeFromFilePath(filePathFromApp) {
@@ -362,4 +371,4 @@ function typeFromFilePath(filePathFromApp) {
362371
} else {
363372
throw new Error(`invalid file path: ${filePathFromApp}`);
364373
}
365-
}
374+
}

lib/utils/file.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const fse = require("fs-extra");
2-
const path = require("path");
3-
const removeDirectories = require("remove-empty-directories");
1+
const fse = require('fs-extra');
2+
const path = require('path');
3+
const removeDirectories = require('remove-empty-directories');
44

55
function moveFile(sourceFilePath, targetFilePath) {
66
let targetFileDirectory = path.dirname(targetFilePath);

lib/utils/js-parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ const options = {
2323
'dynamicImport',
2424
'nullishCoalescingOperator',
2525
'optionalChaining',
26-
'decorators-legacy'
26+
'decorators-legacy',
2727
],
2828
};
2929

3030
module.exports = {
3131
parse(code) {
3232
return babylon.parse(code, options);
3333
},
34-
};
34+
};

0 commit comments

Comments
 (0)