diff --git a/src/file-reference.ts b/src/file-reference.ts index 8507d48..008d59d 100644 --- a/src/file-reference.ts +++ b/src/file-reference.ts @@ -1,3 +1,4 @@ +import { pathExists } from "fs-extra/esm"; import { join, parse, type ParsedPath } from "node:path"; export class FileReference { @@ -23,6 +24,10 @@ export class FileReference { this.subDir = subDir; } + exists(): Promise { + return pathExists(this.path()); + } + parse(): ParsedPath { return parse(this.path()); } diff --git a/src/generator.ts b/src/generator.ts index bb7764a..c6dc836 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -39,8 +39,15 @@ type GeneratorArg = { type: "boolean" | "positional" | "string"; }; -type ModifyTargetFile = (targetFile: FileReference, args: Args) => void; -type ModifyTemplateFile = (templateFile: FileReference, args: Args) => void; +type ModifyTargetFile = ( + targetFile: FileReference, + args: Args, +) => Promise | void; + +type ModifyTemplateFile = ( + templateFile: FileReference, + args: Args, +) => Promise | void; // eslint-disable-next-line @typescript-eslint/no-explicit-any type Args = Record; @@ -88,16 +95,16 @@ export function defineGenerator({ subDir: generatorName, }); - modifyTargetFile?.(targetFile, resolvedArgs); - modifyTemplateFile?.(templateFile, resolvedArgs); + await modifyTargetFile?.(targetFile, resolvedArgs); + await modifyTemplateFile?.(templateFile, resolvedArgs); if (targetFile.subDir === "") { targetFile.subDir = join(getSrcDir(packageJson), generatorName + "s"); } for (const arg of generatorArgs) { - arg.modifyTargetFile?.(targetFile, resolvedArgs); - arg.modifyTemplateFile?.(templateFile, resolvedArgs); + await arg.modifyTargetFile?.(targetFile, resolvedArgs); + await arg.modifyTemplateFile?.(templateFile, resolvedArgs); } const templateContent = await readFile(templateFile.path(), "utf-8"); @@ -289,8 +296,16 @@ export function typescript({ modifyTargetFile: (targetFile, args): void => { targetFile.ext = args.typescript ? tsExt : jsExt; }, - modifyTemplateFile: (templateFile, args): void => { - templateFile.ext = args.typescript ? tsExt : jsExt; + modifyTemplateFile: async (templateFile, args): Promise => { + if (args.typescript) { + templateFile.ext = tsExt; + + if ((await templateFile.exists()) === false) { + templateFile.ext = jsExt; + } + } else { + templateFile.ext = jsExt; + } }, name: "typescript", type: "boolean", diff --git a/templates/acceptance-test/acceptance-test.ts b/templates/acceptance-test/acceptance-test.ts deleted file mode 100644 index ea12390..0000000 --- a/templates/acceptance-test/acceptance-test.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { currentURL, visit } from '@ember/test-helpers'; -import { module, test } from 'qunit'; -import { setupApplicationTest } from '{{testHelpersImportPath}}'; - -module('Acceptance | {{name.path}}', function (hooks) { - setupApplicationTest(hooks); - - test('it visits /{{name.path}}', async function (assert) { - await visit('/{{name.path}}'); - - assert.strictEqual(currentURL(), '/{{name.path}}'); - }); -}); diff --git a/templates/component-test/component-test.gts b/templates/component-test/component-test.gts deleted file mode 100644 index 6b6f8af..0000000 --- a/templates/component-test/component-test.gts +++ /dev/null @@ -1,24 +0,0 @@ -import { render } from '@ember/test-helpers'; -import { module, test } from 'qunit'; -import {{name.pascal}} from '{{package.name}}/components/{{name.path}}'; -import { setupRenderingTest } from '{{testHelpersImportPath}}'; - -module('Integration | Component | {{name.pascal}}', function (hooks) { - setupRenderingTest(hooks); - - test('it renders', async function (assert) { - await render(); - - assert.dom().hasText(''); - - await render( - - ); - - assert.dom().hasText('template block text'); - }); -}); diff --git a/templates/helper-test/helper-test.gts b/templates/helper-test/helper-test.gts deleted file mode 100644 index f1c362c..0000000 --- a/templates/helper-test/helper-test.gts +++ /dev/null @@ -1,15 +0,0 @@ -import { render } from '@ember/test-helpers'; -import { module, test } from 'qunit'; -import { setupRenderingTest } from '{{testHelpersImportPath}}'; - -module('Integration | Helper | {{name.camel}}', function (hooks) { - setupRenderingTest(hooks); - - test('it renders', async function (assert) { - const inputValue = '1234'; - - await render(); - - assert.dom().hasText('1234'); - }); -}); diff --git a/templates/helper/helper.function-based.ts b/templates/helper/helper.function-based.ts deleted file mode 100644 index 477d464..0000000 --- a/templates/helper/helper.function-based.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default function {{name.camel}}(positional, named) { - return positional; -} diff --git a/templates/modifier-test/modifier-test.gts b/templates/modifier-test/modifier-test.gts deleted file mode 100644 index 676357f..0000000 --- a/templates/modifier-test/modifier-test.gts +++ /dev/null @@ -1,13 +0,0 @@ -import { render } from '@ember/test-helpers'; -import { module, test } from 'qunit'; -import { setupRenderingTest } from '{{testHelpersImportPath}}'; - -module('Integration | Modifier | {{name.camel}}', function (hooks) { - setupRenderingTest(hooks); - - test('it renders', async function (assert) { - await render(); - - assert.ok(true); - }); -}); diff --git a/templates/service-test/service-test.ts b/templates/service-test/service-test.ts deleted file mode 100644 index a50dc95..0000000 --- a/templates/service-test/service-test.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { module, test } from 'qunit'; -import { setupTest } from '{{testHelpersImportPath}}'; - -module('Unit | Service | {{name.pascal}}', function (hooks) { - setupTest(hooks); - - test('it exists', function (assert) { - const service = this.owner.lookup('service:{{name.path}}'); - - assert.ok(service); - }); -});