forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (81 loc) · 2.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*eslint-disable no-console */
// This file hooks up on require calls to transpile TypeScript.
const fs = require('fs');
const ts = require('typescript');
const old = require.extensions['.ts'];
require.extensions['.ts'] = function(m, filename) {
// If we're in node module, either call the old hook or simply compile the
// file without transpilation. We do not touch node_modules/**.
// We do touch `angular-cli` files anywhere though.
if (!filename.match(/angular-cli/) && filename.match(/node_modules/)) {
if (old) {
return old(m, filename);
}
return m._compile(fs.readFileSync(filename), filename);
}
// Node requires all require hooks to be sync.
const source = fs.readFileSync(filename).toString();
try {
const result = ts.transpile(source, {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJs
});
// Send it to node to execute.
return m._compile(result, filename);
} catch (err) {
console.error('Error while running script "' + filename + '":');
console.error(err.stack);
throw err;
}
};
/**
* Monkey patch `ember-cli/lib/utilities/find-build-file` to find our build
* file when looking for `ember-cli-build.js`. This needs to be the first
* thing that happens before we `require()` any ember files.
*
* TODO: Remove this hack and replace it with some configuration when/if we
* move away from ember. Or when ember allows us to configure this in
* an addon.
*/
const findBuildFileRequirePath = 'ember-cli/lib/utilities/find-build-file';
const originalFindBuildFile = require(findBuildFileRequirePath);
const mod = require.cache[require.resolve(findBuildFileRequirePath)];
mod.exports = function patchedFindBuildFile(name) {
if (name == 'ember-cli-build.js') {
const result = originalFindBuildFile.call(this, 'angular-cli-build.js');
// Fallback to ember-cli-build if angular-cli-build isn't found.
if (result) {
return result;
}
}
return originalFindBuildFile.apply(this, arguments);
};
const cli = require('ember-cli/lib/cli');
const path = require('path');
module.exports = function(options) {
const oldStdoutWrite = process.stdout.write;
process.stdout.write = function (line) {
line = line.toString();
if (line.match(/version:|WARNING:/)) {
return;
}
line = line.replace(/ember-cli(?!.com)/g, 'angular-cli')
.replace(/ember(?!-cli.com)/g, 'ng');
return oldStdoutWrite.apply(process.stdout, arguments);
};
const oldStderrWrite = process.stderr.write;
process.stderr.write = function (line) {
line = line.toString()
.replace(/ember-cli(?!.com)/g, 'angular-cli')
.replace(/ember(?!-cli.com)/g, 'ng');
return oldStderrWrite.apply(process.stdout, arguments);
};
options.cli = {
name: 'ng',
root: path.join(__dirname, '..', '..'),
npmPackage: 'angular-cli'
};
// ensure the environemnt variable for dynamic paths
process.env.PWD = process.env.PWD || process.cwd();
return cli(options);
}