Skip to content

Commit f534b56

Browse files
committed
Created changeComponentStructureToNested method
1 parent 161e9fa commit f534b56

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

lib/migrator.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,34 @@ module.exports = class Migrator {
106106
});
107107
}
108108

109+
changeComponentStructureToNested(templateFilePaths) {
110+
const classFilePaths = this.findClassicComponentClasses();
111+
112+
templateFilePaths.forEach(templateFilePath => {
113+
// Extract '/app/templates/components/nested1/nested-component.hbs'
114+
const filePathFromApp = templateFilePath.slice(this.projectRoot.length);
115+
116+
// Extract '/nested1/nested-component.hbs'
117+
const filePathFromAppTemplatesComponents = filePathFromApp.slice('app/templates/components/'.length);
118+
const fileExtension = path.extname(filePathFromAppTemplatesComponents);
119+
120+
// Extract '/nested1/nested-component'
121+
const targetPath = filePathFromAppTemplatesComponents.slice(0, -fileExtension.length);
122+
123+
// Build '[APP_PATH]/app/components/nested1/nested-component/index.hbs'
124+
const newTemplateFilePath = path.join(this.projectRoot, 'app/components', targetPath, 'index.hbs');
125+
moveFile(templateFilePath, newTemplateFilePath);
126+
127+
// Build '[APP_PATH]/app/components/nested1/nested-component/index.js'
128+
const classFilePath = path.join(this.projectRoot, 'app/components', `${targetPath}.js`);
129+
130+
if (classFilePaths.includes(classFilePath)) {
131+
const newClassFilePath = path.join(this.projectRoot, 'app/components', targetPath, 'index.js');
132+
moveFile(classFilePath, newClassFilePath);
133+
}
134+
});
135+
}
136+
109137
async removeEmptyClassicComponentDirectories() {
110138
const templateFolderPath = path.join(this.projectRoot, 'app/templates/components');
111139

@@ -123,6 +151,10 @@ module.exports = class Migrator {
123151

124152
if (this.newComponentStructure === 'flat') {
125153
this.changeComponentStructureToFlat(templateFilePaths);
154+
155+
} else if (this.newComponentStructure === 'nested') {
156+
this.changeComponentStructureToNested(templateFilePaths);
157+
126158
}
127159

128160
// Clean up

test/migrator-test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,53 @@ describe("Migrator", function() {
5757
});
5858
});
5959
});
60+
61+
62+
describe('newComponentStructure = nested', function() {
63+
var tmpPath = "tmp/process-files";
64+
var fixturesPath = path.resolve(__dirname, "fixtures/classic-to-nested");
65+
66+
beforeEach(function() {
67+
fse.mkdirsSync(tmpPath);
68+
});
69+
70+
afterEach(function() {
71+
fse.removeSync(tmpPath);
72+
});
73+
74+
var entries = fse.readdirSync(fixturesPath);
75+
76+
entries.forEach(async function(entry) {
77+
it(`should migrate ${entry} fixture properly`, async function() {
78+
var fixturePath = path.join(fixturesPath, entry);
79+
var input = require(fixturePath + "/input");
80+
var expected = require(fixturePath + "/output");
81+
var migratorConfig = {};
82+
try {
83+
migratorConfig = require(fixturePath + "/config");
84+
} catch (e) {
85+
// fixture uses default config...
86+
}
87+
88+
fixturify.writeSync(tmpPath, input);
89+
90+
var migratorOptions = Object.assign(
91+
{
92+
projectRoot: tmpPath,
93+
newComponentStructure: 'nested'
94+
},
95+
migratorConfig
96+
);
97+
98+
var migrator = new Migrator(migratorOptions);
99+
await migrator.execute();
100+
101+
var actual = fixturify.readSync(tmpPath);
102+
assertDiff.deepEqual(actual, expected, "the codemod should work as expected");
103+
104+
await migrator.execute();
105+
assertDiff.deepEqual(actual, expected, "the codemod should be idempotent");
106+
});
107+
});
108+
});
60109
});

0 commit comments

Comments
 (0)