Skip to content

Commit 91b5719

Browse files
bertdeblockkategengler
authored andcommitted
Support a router.ts file when generating routes
(cherry picked from commit 7742bfa)
1 parent 6daf65c commit 91b5719

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

blueprints/route/index.js

+28-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path');
55
const chalk = require('chalk');
66
const stringUtil = require('ember-cli-string-utils');
77
const EmberRouterGenerator = require('ember-router-generator');
8+
const SilentError = require('silent-error');
89

910
const maybePolyfillTypeScriptBlueprints = require('../-maybe-polyfill-typescript-blueprints');
1011

@@ -148,21 +149,42 @@ function updateRouter(action, options) {
148149
}
149150
}
150151

151-
function findRouter(options) {
152+
function findRouterPath(options) {
152153
let routerPathParts = [options.project.root];
153-
let root = 'app';
154154

155155
if (options.dummy && options.project.isEmberCLIAddon()) {
156-
routerPathParts = routerPathParts.concat(['tests', 'dummy', root, 'router.js']);
156+
routerPathParts.push('tests', 'dummy', 'app');
157157
} else {
158-
routerPathParts = routerPathParts.concat([root, 'router.js']);
158+
routerPathParts.push('app');
159159
}
160160

161-
return routerPathParts;
161+
let jsRouterPath = path.join(...routerPathParts, 'router.js');
162+
let tsRouterPath = path.join(...routerPathParts, 'router.ts');
163+
164+
let jsRouterPathExists = fs.existsSync(jsRouterPath);
165+
let tsRouterPathExists = fs.existsSync(tsRouterPath);
166+
167+
if (jsRouterPathExists && tsRouterPathExists) {
168+
throw new SilentError(
169+
'Found both a `router.js` and `router.ts` file. Please make sure your project only has one or the other.'
170+
);
171+
}
172+
173+
if (jsRouterPathExists) {
174+
return jsRouterPath;
175+
}
176+
177+
if (tsRouterPathExists) {
178+
return tsRouterPath;
179+
}
180+
181+
throw new SilentError(
182+
'Could not find a router file. Please make sure your project has a `router.js` or `router.ts` file.'
183+
);
162184
}
163185

164186
function writeRoute(action, name, options) {
165-
let routerPath = path.join.apply(null, findRouter(options));
187+
let routerPath = findRouterPath(options);
166188
let source = fs.readFileSync(routerPath, 'utf-8');
167189

168190
let routes = new EmberRouterGenerator(source);

node-tests/blueprints/route-test.js

+52
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,32 @@ describe('Blueprint: route', function () {
301301
});
302302
});
303303
});
304+
305+
it('using a `router.ts` file', async function () {
306+
fs.moveSync('app/router.js', 'app/router.ts');
307+
308+
await emberGenerate(['route', 'foo']);
309+
expect(file('app/router.ts')).to.contain("this.route('foo')");
310+
311+
await emberDestroy(['route', 'foo']);
312+
expect(file('app/router.ts')).to.not.contain("this.route('foo')");
313+
});
314+
315+
it('throws a helpful error if a router file could not be found', async function () {
316+
fs.removeSync('app/router.js');
317+
318+
await expect(emberGenerate(['route', 'foo'])).to.be.rejectedWith(
319+
'Could not find a router file. Please make sure your project has a `router.js` or `router.ts` file.'
320+
);
321+
});
322+
323+
it('throws a helpful error if both a `router.ts` and `router.js` file are found', async function () {
324+
fs.copySync('app/router.js', 'app/router.ts');
325+
326+
await expect(emberGenerate(['route', 'foo'])).to.be.rejectedWith(
327+
'Found both a `router.js` and `router.ts` file. Please make sure your project only has one or the other.'
328+
);
329+
});
304330
});
305331

306332
describe('in addon - octane', function () {
@@ -514,6 +540,32 @@ describe('Blueprint: route', function () {
514540
});
515541
});
516542
});
543+
544+
it('using a `router.ts` file', async function () {
545+
fs.moveSync('tests/dummy/app/router.js', 'tests/dummy/app/router.ts');
546+
547+
await emberGenerate(['route', 'foo', '--dummy']);
548+
expect(file('tests/dummy/app/router.ts')).to.contain("this.route('foo')");
549+
550+
await emberDestroy(['route', 'foo', '--dummy']);
551+
expect(file('tests/dummy/app/router.ts')).to.not.contain("this.route('foo')");
552+
});
553+
554+
it('throws a helpful error if a router file could not be found', async function () {
555+
fs.removeSync('tests/dummy/app/router.js');
556+
557+
await expect(emberGenerate(['route', 'foo', '--dummy'])).to.be.rejectedWith(
558+
'Could not find a router file. Please make sure your project has a `router.js` or `router.ts` file.'
559+
);
560+
});
561+
562+
it('throws a helpful error if both a `router.ts` and `router.js` file are found', async function () {
563+
fs.copySync('tests/dummy/app/router.js', 'tests/dummy/app/router.ts');
564+
565+
await expect(emberGenerate(['route', 'foo', '--dummy'])).to.be.rejectedWith(
566+
'Found both a `router.js` and `router.ts` file. Please make sure your project only has one or the other.'
567+
);
568+
});
517569
});
518570

519571
describe('in in-repo-addon', function () {

0 commit comments

Comments
 (0)