From 5702c19a865da0b6cc4b8d60bcc0a15ae6c2e354 Mon Sep 17 00:00:00 2001 From: Bert De Block Date: Thu, 26 Jan 2023 20:58:27 +0100 Subject: [PATCH] Support a `router.ts` file when generating routes --- blueprints/route/index.js | 22 +++++++++++++----- node-tests/blueprints/route-test.js | 36 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/blueprints/route/index.js b/blueprints/route/index.js index a36743f9289..4bc7040f664 100644 --- a/blueprints/route/index.js +++ b/blueprints/route/index.js @@ -5,6 +5,7 @@ const path = require('path'); const chalk = require('chalk'); const stringUtil = require('ember-cli-string-utils'); const EmberRouterGenerator = require('ember-router-generator'); +const SilentError = require('silent-error'); const maybePolyfillTypeScriptBlueprints = require('../-maybe-polyfill-typescript-blueprints'); @@ -148,21 +149,30 @@ function updateRouter(action, options) { } } -function findRouter(options) { +function findRouterPath(options) { let routerPathParts = [options.project.root]; - let root = 'app'; if (options.dummy && options.project.isEmberCLIAddon()) { - routerPathParts = routerPathParts.concat(['tests', 'dummy', root, 'router.js']); + routerPathParts.push('tests', 'dummy', 'app'); } else { - routerPathParts = routerPathParts.concat([root, 'router.js']); + routerPathParts.push('app'); } - return routerPathParts; + for (const routerFile of ['router.js', 'router.ts']) { + let routerPath = path.join(...routerPathParts, routerFile); + + if (fs.existsSync(routerPath)) { + return routerPath; + } + } + + throw new SilentError( + 'Could not find router file. Please make sure your project has a `router.js` or `router.ts` file.' + ); } function writeRoute(action, name, options) { - let routerPath = path.join.apply(null, findRouter(options)); + let routerPath = findRouterPath(options); let source = fs.readFileSync(routerPath, 'utf-8'); let routes = new EmberRouterGenerator(source); diff --git a/node-tests/blueprints/route-test.js b/node-tests/blueprints/route-test.js index d03f3504701..ec47eaba74f 100644 --- a/node-tests/blueprints/route-test.js +++ b/node-tests/blueprints/route-test.js @@ -301,6 +301,24 @@ describe('Blueprint: route', function () { }); }); }); + + it('using a `router.ts` file', async function () { + fs.moveSync('app/router.js', 'app/router.ts'); + + await emberGenerate(['route', 'foo']); + expect(file('app/router.ts')).to.contain("this.route('foo')"); + + await emberDestroy(['route', 'foo']); + expect(file('app/router.ts')).to.not.contain("this.route('foo')"); + }); + + it('throws a helpful error if a router file could not be found', async function () { + fs.removeSync('app/router.js'); + + await expect(emberGenerate(['route', 'foo'])).to.be.rejectedWith( + 'Could not find router file. Please make sure your project has a `router.js` or `router.ts` file.' + ); + }); }); describe('in addon - octane', function () { @@ -514,6 +532,24 @@ describe('Blueprint: route', function () { }); }); }); + + it('using a `router.ts` file', async function () { + fs.moveSync('tests/dummy/app/router.js', 'tests/dummy/app/router.ts'); + + await emberGenerate(['route', 'foo', '--dummy']); + expect(file('tests/dummy/app/router.ts')).to.contain("this.route('foo')"); + + await emberDestroy(['route', 'foo', '--dummy']); + expect(file('tests/dummy/app/router.ts')).to.not.contain("this.route('foo')"); + }); + + it('throws a helpful error if a router file could not be found', async function () { + fs.removeSync('tests/dummy/app/router.js'); + + await expect(emberGenerate(['route', 'foo', '--dummy'])).to.be.rejectedWith( + 'Could not find router file. Please make sure your project has a `router.js` or `router.ts` file.' + ); + }); }); describe('in in-repo-addon', function () {