Skip to content

Commit 7768f8a

Browse files
Open editor on clicking compilation error
1 parent 3371e52 commit 7768f8a

5 files changed

Lines changed: 588 additions & 444 deletions

File tree

lib/templates/error.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,14 +1085,16 @@
10851085
</header>
10861086
<main class="container">
10871087
<article class="content">
1088+
<div onclick="fetch('/ember_open_editor_for_template_compilation_error')">
10881089
<h1 class="error-header">Dang! Looks like a {{errorType}}.</h1>
1089-
{{#if location.file}}
1090-
<h2 class="error-message">{{location.file}}{{#if location.line}}:{{location.line}}{{/if}}</h2>
1091-
{{/if}}
1090+
{{#if location.file}}
1091+
<h2 class="error-message">{{location.file}}{{#if location.line}}:{{location.line}}{{/if}}</h2>
1092+
{{/if}}
10921093

1093-
{{#if codeFrame}}
1094-
<pre><code class="javascript">{{codeFrame}}</code></pre>
1095-
{{/if}}
1094+
{{#if codeFrame}}
1095+
<pre><code class="javascript">{{codeFrame}}</code></pre>
1096+
{{/if}}
1097+
</div>
10961098

10971099
<div class="collapsible">
10981100
<input id="toggle" type="checkbox">

lib/utils/filename-normaliser.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
const pkg = require(`${process.cwd()}/package.json`);
6+
7+
module.exports = function normaliseFileName(fileName) {
8+
let projectName = pkg.name;
9+
let normalisedFileName = fileName.split(path.sep);
10+
11+
// Classic Structure
12+
// actual location : projectName/app/templates/templateName.hbs
13+
// location as per stack : project-name/templates/templateName.hbs
14+
// should be normalised to: app/templates/templateName.hbs
15+
16+
// Pods
17+
// actual location : projectName/app/routeName/templateName.hbs
18+
// location as per stack : project-name/routeName/templateName.hbs
19+
// should be normalised to: app/routeName/templateName.hbs
20+
21+
// in-repo engine
22+
// actual location : projectName/lib/engine-name/addon/templates/templateName.hbs
23+
// location as per stack : engine-name/templates/cars.hbs
24+
// should be normalised to: lib/engine-name/addon/templates/templateName.hbs
25+
26+
// module unification
27+
// actual location : projectName/src/ui/components/my-component/template.hbs
28+
// location as per stack : src/ui/components/my-component/template.hbs
29+
// should be normalised to: src/ui/components/my-component/template.hbs
30+
31+
// handle templates inside application with either pod or classic structure
32+
let topMostParent = normalisedFileName[0];
33+
if (topMostParent === projectName) {
34+
normalisedFileName[0] = 'app';
35+
} else if (topMostParent === 'src') {
36+
// do nothing
37+
} else {
38+
// handle templates inside in-repo engines
39+
normalisedFileName = ['lib', normalisedFileName.shift(), 'addon', ...normalisedFileName];
40+
}
41+
42+
return normalisedFileName.join(path.sep);
43+
}

lib/watcher-middleware.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const url = require('url');
44
const path = require('path');
5+
const launchEditor = require('launch-editor');
56

67
const errorHandler = require('./utils/error-handler');
8+
const normaliseFileName = require('./utils/filename-normaliser');
79

810
module.exports = function watcherMiddleware(watcher, options) {
911
options = options || {};
@@ -34,6 +36,23 @@ module.exports = function watcherMiddleware(watcher, options) {
3436

3537
next();
3638
}, (buildError) => {
39+
if (request.url === '/ember_open_editor_for_template_compilation_error') {
40+
let errorLocation = buildError.broccoliPayload.error.location || {};
41+
let fileName = errorLocation.file;
42+
if (fileName) {
43+
let normalisedFileName = normaliseFileName(fileName);
44+
45+
launchEditor(normalisedFileName, () => {
46+
if (!process.env.EDITOR && !process.env.VISUAL) {
47+
console.log(
48+
'To set up the editor integration, set the env variables EDITOR or VISUAL ' +
49+
'to an editor of your choice and restart the server.'
50+
);
51+
}
52+
});
53+
}
54+
}
55+
3756
errorHandler(response, {
3857
'buildError': buildError,
3958
'liveReloadPath': options.liveReloadPath

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"homepage": "https://github.com/ember-cli/broccoli-middleware",
2121
"dependencies": {
2222
"handlebars": "^4.0.4",
23+
"launch-editor": "^2.2.1",
2324
"mime-types": "^2.1.18"
2425
},
2526
"devDependencies": {

0 commit comments

Comments
 (0)