Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX BETA] Support a router.ts file when generating routes #20361

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 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,42 @@ 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;
let jsRouterPath = path.join(...routerPathParts, 'router.js');
let tsRouterPath = path.join(...routerPathParts, 'router.ts');

let jsRouterPathExists = fs.existsSync(jsRouterPath);
let tsRouterPathExists = fs.existsSync(tsRouterPath);

if (jsRouterPathExists && tsRouterPathExists) {
throw new SilentError(
'Found both a `router.js` and `router.ts` file. Please make sure your project only has one or the other.'
);
}

if (jsRouterPathExists) {
return jsRouterPath;
}

if (tsRouterPathExists) {
return tsRouterPath;
}

throw new SilentError(
'Could not find a 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
52 changes: 52 additions & 0 deletions node-tests/blueprints/route-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,32 @@ 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 a router file. Please make sure your project has a `router.js` or `router.ts` file.'
);
});

it('throws a helpful error if both a `router.ts` and `router.js` file are found', async function () {
fs.copySync('app/router.js', 'app/router.ts');

await expect(emberGenerate(['route', 'foo'])).to.be.rejectedWith(
'Found both a `router.js` and `router.ts` file. Please make sure your project only has one or the other.'
);
});
});

describe('in addon - octane', function () {
Expand Down Expand Up @@ -514,6 +540,32 @@ 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 a router file. Please make sure your project has a `router.js` or `router.ts` file.'
);
});

it('throws a helpful error if both a `router.ts` and `router.js` file are found', async function () {
fs.copySync('tests/dummy/app/router.js', 'tests/dummy/app/router.ts');

await expect(emberGenerate(['route', 'foo', '--dummy'])).to.be.rejectedWith(
'Found both a `router.js` and `router.ts` file. Please make sure your project only has one or the other.'
);
});
});

describe('in in-repo-addon', function () {
Expand Down