Skip to content

Commit

Permalink
Support a router.ts file when generating routes
Browse files Browse the repository at this point in the history
  • Loading branch information
bertdeblock committed Jan 26, 2023
1 parent 15a87bd commit adc4b7e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
22 changes: 16 additions & 6 deletions blueprints/route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions node-tests/blueprints/route-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 the 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 () {
Expand Down Expand Up @@ -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 the 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 () {
Expand Down

0 comments on commit adc4b7e

Please sign in to comment.