Skip to content

Commit e8d3e3a

Browse files
authored
Merge pull request #1 from ember-codemods/master
Get the latest code
2 parents e92bb50 + 249d462 commit e8d3e3a

File tree

13 files changed

+494
-32
lines changed

13 files changed

+494
-32
lines changed

bin/ember-component-template-colocation-migrator

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ let options = {
1414
let parsed = nopt(options);
1515
let projectRoot = parsed['project-root'] || process.cwd();
1616

17-
const args = process.argv.slice(2);
17+
const { argv } = require('yargs');
1818

1919
// Allow passing the flag, -fs (flat) or -ns (nested), to specify component structure
20-
let newComponentStructure = 'flat';
20+
const changeToFlatStructure = argv.f && argv.s;
21+
const changeToNestedStructure = argv.n && argv.s;
2122

22-
if (args.includes('-fs')) {
23-
newComponentStructure = 'flat';
23+
let structure = 'flat';
2424

25-
} else if (args.includes('-ns')) {
26-
newComponentStructure = 'nested';
25+
if (changeToFlatStructure) {
26+
structure = 'flat';
27+
28+
} else if (changeToNestedStructure) {
29+
structure = 'nested';
2730

2831
}
2932

30-
let migrator = new Migrator({
31-
projectRoot,
32-
newComponentStructure
33-
});
33+
let migrator = new Migrator({ projectRoot, structure });
3434

3535
migrator.execute().then(function() {
3636
console.log('Codemod finished successfully!');
3737
}).catch(function(error) {
3838
console.error(error.stack);
39-
});
39+
});

lib/migrator.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const { moveFile, removeDirs } = require('./utils/file')
66

77
module.exports = class Migrator {
88
constructor(options) {
9-
const { projectRoot, newComponentStructure } = options;
9+
const { projectRoot, structure } = options;
1010

1111
this.projectRoot = projectRoot;
12-
this.newComponentStructure = newComponentStructure;
12+
this.structure = structure;
1313
}
1414

1515
findClassicComponentTemplates() {
@@ -21,7 +21,7 @@ module.exports = class Migrator {
2121

2222
findClassicComponentClasses() {
2323
const classFolderPath = path.join(this.projectRoot, 'app/components');
24-
const classFilePaths = glob.sync(`${classFolderPath}/**/*.js`);
24+
const classFilePaths = glob.sync(`${classFolderPath}/**/*.{js,ts}`);
2525

2626
return classFilePaths;
2727
}
@@ -75,6 +75,15 @@ module.exports = class Migrator {
7575
// Extract '/app/templates/components/nested1/nested-component.hbs'
7676
let filePathFromApp = templateFilePath.slice(this.projectRoot.length);
7777

78+
/*
79+
When Ember sees `{{partial "foo"}}`, it will look for the template in
80+
two locations:
81+
82+
- `app/templates/foo.hbs`
83+
- `app/templates/-foo.hbs`
84+
85+
If `filePathFromApp` matches the latter pattern, we remove the hyphen.
86+
*/
7887
if (/\/\-[\w\-]+\.hbs/.test(filePathFromApp)) {
7988
filePathFromApp = filePathFromApp.replace('/-', '/');
8089
}
@@ -125,11 +134,19 @@ module.exports = class Migrator {
125134
moveFile(templateFilePath, newTemplateFilePath);
126135

127136
// Build '[APP_PATH]/app/components/nested1/nested-component/index.js'
128-
const classFilePath = path.join(this.projectRoot, 'app/components', `${targetPath}.js`);
137+
const classFilePath = {
138+
js: path.join(this.projectRoot, 'app/components', `${targetPath}.js`),
139+
ts: path.join(this.projectRoot, 'app/components', `${targetPath}.ts`)
140+
};
129141

130-
if (classFilePaths.includes(classFilePath)) {
142+
if (classFilePaths.includes(classFilePath.js)) {
131143
const newClassFilePath = path.join(this.projectRoot, 'app/components', targetPath, 'index.js');
132-
moveFile(classFilePath, newClassFilePath);
144+
moveFile(classFilePath.js, newClassFilePath);
145+
146+
} else if (classFilePaths.includes(classFilePath.ts)) {
147+
const newClassFilePath = path.join(this.projectRoot, 'app/components', targetPath, 'index.ts');
148+
moveFile(classFilePath.ts, newClassFilePath);
149+
133150
}
134151
});
135152
}
@@ -149,10 +166,10 @@ module.exports = class Migrator {
149166
templateFilePaths = this.skipTemplatesUsedAsLayoutName(templateFilePaths);
150167
templateFilePaths = this.skipTemplatesUsedAsPartial(templateFilePaths);
151168

152-
if (this.newComponentStructure === 'flat') {
169+
if (this.structure === 'flat') {
153170
this.changeComponentStructureToFlat(templateFilePaths);
154171

155-
} else if (this.newComponentStructure === 'nested') {
172+
} else if (this.structure === 'nested') {
156173
this.changeComponentStructureToNested(templateFilePaths);
157174

158175
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"fs-extra": "^7.0.1",
2929
"glob": "^7.1.4",
3030
"nopt": "^4.0.1",
31-
"remove-empty-directories": "^0.0.1"
31+
"remove-empty-directories": "^0.0.1",
32+
"yargs": "^15.3.1"
3233
},
3334
"repository": {
3435
"type": "git",

test/fixtures/classic-to-flat/example-1/input.js renamed to test/fixtures/classic-to-flat/example-js/input.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,15 @@ module.exports = {
6666

6767
// A partial template
6868
'partials': {
69-
'partials-template.hbs': '{{!-- partials-template.hbs --}}',
69+
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
70+
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
71+
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}',
72+
7073
'with-partial.hbs': [
7174
'{{!-- with-partial.hbs --}}',
72-
'{{partial "components/partials/partials-template"}}'
75+
'{{partial "components/partials/partial-one-template"}}',
76+
'{{partial "components/partials/partial-two-template"}}',
77+
'{{partial "components/partials/partial-three-template"}}'
7378
].join('\n')
7479
}
7580
}

test/fixtures/classic-to-flat/example-1/output.js renamed to test/fixtures/classic-to-flat/example-js/output.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ module.exports = {
4444
'partials': {
4545
'with-partial.hbs': [
4646
'{{!-- with-partial.hbs --}}',
47-
'{{partial "components/partials/partials-template"}}'
47+
'{{partial "components/partials/partial-one-template"}}',
48+
'{{partial "components/partials/partial-two-template"}}',
49+
'{{partial "components/partials/partial-three-template"}}'
4850
].join('\n')
4951
}
5052
},
@@ -60,7 +62,9 @@ module.exports = {
6062

6163
// A partial template
6264
'partials': {
63-
'partials-template.hbs': '{{!-- partials-template.hbs --}}',
65+
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
66+
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
67+
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}'
6468
}
6569
}
6670
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
module.exports = {
2+
app: {
3+
'app.js': '// app',
4+
5+
components: {
6+
// A standalone component
7+
'top-level-component.ts': '// top-level-component.ts',
8+
9+
// A nested component
10+
'parent-component.ts': '// parent-component.ts',
11+
'parent-component': {
12+
'child-component.ts': '// parent-component/child-component.ts',
13+
'child-component': {
14+
'grandchild-component.ts': '// parent-component/child-component/grandchild-component.ts'
15+
}
16+
},
17+
18+
// Another nested component
19+
nested1: {
20+
'nested-component.ts': '// nested1/nested-component.ts',
21+
nested2: {
22+
'super-nested-component.ts': '// nested1/nested2/super-nested-component.ts'
23+
}
24+
},
25+
26+
// A component with layoutName
27+
'layout-name': {
28+
'has-layout-name.ts': [
29+
'// top-level-component.ts',
30+
'Component.extend({ layoutName: "components/layout-name/layout-name-template" });'
31+
].join('\n')
32+
}
33+
},
34+
35+
templates: {
36+
'application.hbs': '{{outlet}}',
37+
38+
components: {
39+
// A standalone component
40+
'top-level-component.hbs': '{{!-- top-level-component.hbs --}}',
41+
42+
// A template-only component
43+
'template-only-component.hbs': '{{!-- template-only-component.hbs --}}',
44+
45+
// A nested component
46+
'parent-component.hbs': '{{!-- parent-component.hbs --}}',
47+
'parent-component': {
48+
'child-component.hbs': '{{!-- parent-component/child-component.hbs --}}',
49+
'child-component': {
50+
'grandchild-component.hbs': '{{!-- parent-component/child-component/grandchild-component.hbs --}}'
51+
}
52+
},
53+
54+
// Another nested component
55+
nested1: {
56+
'nested-component.hbs': '{{!-- nested1/nested-component.hbs --}}',
57+
nested2: {
58+
'super-nested-component.hbs': '{{!-- nested1/nested2/super-nested-component.hbs --}}'
59+
}
60+
},
61+
62+
// A component with layoutName
63+
'layout-name': {
64+
'layout-name-template.hbs': '{{!-- layout-name-template.hbs --}}'
65+
},
66+
67+
// A partial template
68+
'partials': {
69+
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
70+
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
71+
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}',
72+
73+
'with-partial.hbs': [
74+
'{{!-- with-partial.hbs --}}',
75+
'{{partial "components/partials/partial-one-template"}}',
76+
'{{partial "components/partials/partial-two-template"}}',
77+
'{{partial "components/partials/partial-three-template"}}'
78+
].join('\n')
79+
}
80+
}
81+
}
82+
}
83+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module.exports = {
2+
app: {
3+
'app.js': '// app',
4+
5+
components: {
6+
// A standalone component
7+
'top-level-component.hbs': '{{!-- top-level-component.hbs --}}',
8+
'top-level-component.ts': '// top-level-component.ts',
9+
10+
// A template-only component
11+
'template-only-component.hbs': '{{!-- template-only-component.hbs --}}',
12+
13+
// A nested component
14+
'parent-component.hbs': '{{!-- parent-component.hbs --}}',
15+
'parent-component.ts': '// parent-component.ts',
16+
'parent-component': {
17+
'child-component.hbs': '{{!-- parent-component/child-component.hbs --}}',
18+
'child-component.ts': '// parent-component/child-component.ts',
19+
'child-component': {
20+
'grandchild-component.hbs': '{{!-- parent-component/child-component/grandchild-component.hbs --}}',
21+
'grandchild-component.ts': '// parent-component/child-component/grandchild-component.ts'
22+
}
23+
},
24+
25+
// Another nested component
26+
nested1: {
27+
'nested-component.hbs': '{{!-- nested1/nested-component.hbs --}}',
28+
'nested-component.ts': '// nested1/nested-component.ts',
29+
nested2: {
30+
'super-nested-component.hbs': '{{!-- nested1/nested2/super-nested-component.hbs --}}',
31+
'super-nested-component.ts': '// nested1/nested2/super-nested-component.ts'
32+
}
33+
},
34+
35+
// A component with layoutName
36+
'layout-name': {
37+
'has-layout-name.ts': [
38+
'// top-level-component.ts',
39+
'Component.extend({ layoutName: "components/layout-name/layout-name-template" });'
40+
].join('\n')
41+
},
42+
43+
// A component with partial
44+
'partials': {
45+
'with-partial.hbs': [
46+
'{{!-- with-partial.hbs --}}',
47+
'{{partial "components/partials/partial-one-template"}}',
48+
'{{partial "components/partials/partial-two-template"}}',
49+
'{{partial "components/partials/partial-three-template"}}'
50+
].join('\n')
51+
}
52+
},
53+
54+
templates: {
55+
'application.hbs': '{{outlet}}',
56+
57+
components: {
58+
// A component with layoutName
59+
'layout-name': {
60+
'layout-name-template.hbs': '{{!-- layout-name-template.hbs --}}'
61+
},
62+
63+
// A partial template
64+
'partials': {
65+
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
66+
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
67+
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}'
68+
}
69+
}
70+
}
71+
},
72+
};

test/fixtures/classic-to-nested/example-1/input.js renamed to test/fixtures/classic-to-nested/example-js/input.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,15 @@ module.exports = {
6666

6767
// A partial template
6868
'partials': {
69-
'partials-template.hbs': '{{!-- partials-template.hbs --}}',
69+
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
70+
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
71+
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}',
72+
7073
'with-partial.hbs': [
7174
'{{!-- with-partial.hbs --}}',
72-
'{{partial "components/partials/partials-template"}}'
75+
'{{partial "components/partials/partial-one-template"}}',
76+
'{{partial "components/partials/partial-two-template"}}',
77+
'{{partial "components/partials/partial-three-template"}}'
7378
].join('\n')
7479
}
7580
}

test/fixtures/classic-to-nested/example-1/output.js renamed to test/fixtures/classic-to-nested/example-js/output.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ module.exports = {
5555
'with-partial': {
5656
'index.hbs': [
5757
'{{!-- with-partial.hbs --}}',
58-
'{{partial "components/partials/partials-template"}}'
58+
'{{partial "components/partials/partial-one-template"}}',
59+
'{{partial "components/partials/partial-two-template"}}',
60+
'{{partial "components/partials/partial-three-template"}}'
5961
].join('\n')
6062
}
6163
}
@@ -72,7 +74,9 @@ module.exports = {
7274

7375
// A partial template
7476
'partials': {
75-
'partials-template.hbs': '{{!-- partials-template.hbs --}}',
77+
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
78+
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
79+
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}'
7680
}
7781
}
7882
}

0 commit comments

Comments
 (0)