Skip to content

Commit

Permalink
Added support for moving component classes written in TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
ijlee2 committed Apr 28, 2020
1 parent da8cd91 commit 23766e2
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = class Migrator {

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

return classFilePaths;
}
Expand Down Expand Up @@ -134,11 +134,19 @@ module.exports = class Migrator {
moveFile(templateFilePath, newTemplateFilePath);

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

if (classFilePaths.includes(classFilePath)) {
if (classFilePaths.includes(classFilePath.js)) {
const newClassFilePath = path.join(this.projectRoot, 'app/components', targetPath, 'index.js');
moveFile(classFilePath, newClassFilePath);
moveFile(classFilePath.js, newClassFilePath);

} else if (classFilePaths.includes(classFilePath.ts)) {
const newClassFilePath = path.join(this.projectRoot, 'app/components', targetPath, 'index.ts');
moveFile(classFilePath.ts, newClassFilePath);

}
});
}
Expand Down
83 changes: 83 additions & 0 deletions test/fixtures/classic-to-flat/example-ts/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module.exports = {
app: {
'app.js': '// app',

components: {
// A standalone component
'top-level-component.ts': '// top-level-component.ts',

// A nested component
'parent-component.ts': '// parent-component.ts',
'parent-component': {
'child-component.ts': '// parent-component/child-component.ts',
'child-component': {
'grandchild-component.ts': '// parent-component/child-component/grandchild-component.ts'
}
},

// Another nested component
nested1: {
'nested-component.ts': '// nested1/nested-component.ts',
nested2: {
'super-nested-component.ts': '// nested1/nested2/super-nested-component.ts'
}
},

// A component with layoutName
'layout-name': {
'has-layout-name.ts': [
'// top-level-component.ts',
'Component.extend({ layoutName: "components/layout-name/layout-name-template" });'
].join('\n')
}
},

templates: {
'application.hbs': '{{outlet}}',

components: {
// A standalone component
'top-level-component.hbs': '{{!-- top-level-component.hbs --}}',

// A template-only component
'template-only-component.hbs': '{{!-- template-only-component.hbs --}}',

// A nested component
'parent-component.hbs': '{{!-- parent-component.hbs --}}',
'parent-component': {
'child-component.hbs': '{{!-- parent-component/child-component.hbs --}}',
'child-component': {
'grandchild-component.hbs': '{{!-- parent-component/child-component/grandchild-component.hbs --}}'
}
},

// Another nested component
nested1: {
'nested-component.hbs': '{{!-- nested1/nested-component.hbs --}}',
nested2: {
'super-nested-component.hbs': '{{!-- nested1/nested2/super-nested-component.hbs --}}'
}
},

// A component with layoutName
'layout-name': {
'layout-name-template.hbs': '{{!-- layout-name-template.hbs --}}'
},

// A partial template
'partials': {
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}',

'with-partial.hbs': [
'{{!-- with-partial.hbs --}}',
'{{partial "components/partials/partial-one-template"}}',
'{{partial "components/partials/partial-two-template"}}',
'{{partial "components/partials/partial-three-template"}}'
].join('\n')
}
}
}
}
};
72 changes: 72 additions & 0 deletions test/fixtures/classic-to-flat/example-ts/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module.exports = {
app: {
'app.js': '// app',

components: {
// A standalone component
'top-level-component.hbs': '{{!-- top-level-component.hbs --}}',
'top-level-component.ts': '// top-level-component.ts',

// A template-only component
'template-only-component.hbs': '{{!-- template-only-component.hbs --}}',

// A nested component
'parent-component.hbs': '{{!-- parent-component.hbs --}}',
'parent-component.ts': '// parent-component.ts',
'parent-component': {
'child-component.hbs': '{{!-- parent-component/child-component.hbs --}}',
'child-component.ts': '// parent-component/child-component.ts',
'child-component': {
'grandchild-component.hbs': '{{!-- parent-component/child-component/grandchild-component.hbs --}}',
'grandchild-component.ts': '// parent-component/child-component/grandchild-component.ts'
}
},

// Another nested component
nested1: {
'nested-component.hbs': '{{!-- nested1/nested-component.hbs --}}',
'nested-component.ts': '// nested1/nested-component.ts',
nested2: {
'super-nested-component.hbs': '{{!-- nested1/nested2/super-nested-component.hbs --}}',
'super-nested-component.ts': '// nested1/nested2/super-nested-component.ts'
}
},

// A component with layoutName
'layout-name': {
'has-layout-name.ts': [
'// top-level-component.ts',
'Component.extend({ layoutName: "components/layout-name/layout-name-template" });'
].join('\n')
},

// A component with partial
'partials': {
'with-partial.hbs': [
'{{!-- with-partial.hbs --}}',
'{{partial "components/partials/partial-one-template"}}',
'{{partial "components/partials/partial-two-template"}}',
'{{partial "components/partials/partial-three-template"}}'
].join('\n')
}
},

templates: {
'application.hbs': '{{outlet}}',

components: {
// A component with layoutName
'layout-name': {
'layout-name-template.hbs': '{{!-- layout-name-template.hbs --}}'
},

// A partial template
'partials': {
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}'
}
}
}
},
};
83 changes: 83 additions & 0 deletions test/fixtures/classic-to-nested/example-ts/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module.exports = {
app: {
'app.js': '// app',

components: {
// A standalone component
'top-level-component.ts': '// top-level-component.ts',

// A nested component
'parent-component.ts': '// parent-component.ts',
'parent-component': {
'child-component.ts': '// parent-component/child-component.ts',
'child-component': {
'grandchild-component.ts': '// parent-component/child-component/grandchild-component.ts'
}
},

// Another nested component
nested1: {
'nested-component.ts': '// nested1/nested-component.ts',
nested2: {
'super-nested-component.ts': '// nested1/nested2/super-nested-component.ts'
}
},

// A component with layoutName
'layout-name': {
'has-layout-name.ts': [
'// top-level-component.ts',
'Component.extend({ layoutName: "components/layout-name/layout-name-template" });'
].join('\n')
}
},

templates: {
'application.hbs': '{{outlet}}',

components: {
// A standalone component
'top-level-component.hbs': '{{!-- top-level-component.hbs --}}',

// A template-only component
'template-only-component.hbs': '{{!-- template-only-component.hbs --}}',

// A nested component
'parent-component.hbs': '{{!-- parent-component.hbs --}}',
'parent-component': {
'child-component.hbs': '{{!-- parent-component/child-component.hbs --}}',
'child-component': {
'grandchild-component.hbs': '{{!-- parent-component/child-component/grandchild-component.hbs --}}'
}
},

// Another nested component
nested1: {
'nested-component.hbs': '{{!-- nested1/nested-component.hbs --}}',
nested2: {
'super-nested-component.hbs': '{{!-- nested1/nested2/super-nested-component.hbs --}}'
}
},

// A component with layoutName
'layout-name': {
'layout-name-template.hbs': '{{!-- layout-name-template.hbs --}}'
},

// A partial template
'partials': {
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}',

'with-partial.hbs': [
'{{!-- with-partial.hbs --}}',
'{{partial "components/partials/partial-one-template"}}',
'{{partial "components/partials/partial-two-template"}}',
'{{partial "components/partials/partial-three-template"}}'
].join('\n')
}
}
}
}
};
84 changes: 84 additions & 0 deletions test/fixtures/classic-to-nested/example-ts/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module.exports = {
app: {
'app.js': '// app',

components: {
// A standalone component
'top-level-component': {
'index.hbs': '{{!-- top-level-component.hbs --}}',
'index.ts': '// top-level-component.ts',
},

// A template-only component
'template-only-component': {
'index.hbs': '{{!-- template-only-component.hbs --}}'
},

// A nested component
'parent-component': {
'index.hbs': '{{!-- parent-component.hbs --}}',
'index.ts': '// parent-component.ts',
'child-component': {
'index.hbs': '{{!-- parent-component/child-component.hbs --}}',
'index.ts': '// parent-component/child-component.ts',
'grandchild-component': {
'index.hbs': '{{!-- parent-component/child-component/grandchild-component.hbs --}}',
'index.ts': '// parent-component/child-component/grandchild-component.ts'
}
}
},

// Another nested component
nested1: {
'nested-component': {
'index.hbs': '{{!-- nested1/nested-component.hbs --}}',
'index.ts': '// nested1/nested-component.ts',
},
nested2: {
'super-nested-component': {
'index.hbs': '{{!-- nested1/nested2/super-nested-component.hbs --}}',
'index.ts': '// nested1/nested2/super-nested-component.ts'
}
}
},

// A component with layoutName
'layout-name': {
'has-layout-name.ts': [
'// top-level-component.ts',
'Component.extend({ layoutName: "components/layout-name/layout-name-template" });'
].join('\n')
},

// A component with partial
'partials': {
'with-partial': {
'index.hbs': [
'{{!-- with-partial.hbs --}}',
'{{partial "components/partials/partial-one-template"}}',
'{{partial "components/partials/partial-two-template"}}',
'{{partial "components/partials/partial-three-template"}}'
].join('\n')
}
}
},

templates: {
'application.hbs': '{{outlet}}',

components: {
// A component with layoutName
'layout-name': {
'layout-name-template.hbs': '{{!-- layout-name-template.hbs --}}'
},

// A partial template
'partials': {
'partial-one-template.hbs': '{{!-- partial-one-template.hbs --}}',
'partial-two-template.hbs': '{{!-- partial-two-template.hbs --}}',
'-partial-three-template.hbs': '{{!-- partial-three-template.hbs --}}'
}
}
}
},
};

0 comments on commit 23766e2

Please sign in to comment.