Skip to content

Commit 3fb958b

Browse files
clydinhansl
authored andcommitted
fix(@angular/cli): properly detect local cli package
1 parent 54092ee commit 3fb958b

File tree

4 files changed

+35
-46
lines changed

4 files changed

+35
-46
lines changed

packages/angular/cli/lib/init.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import 'symbol-observable';
9-
const isWarningEnabled = require('../utilities/config').isWarningEnabled;
9+
// symbol polyfill must go first
10+
// tslint:disable-next-line:ordered-imports import-groups
11+
import { tags, terminal } from '@angular-devkit/core';
12+
import { resolve } from '@angular-devkit/core/node';
13+
import * as fs from 'fs';
14+
import * as path from 'path';
15+
import { SemVer } from 'semver';
16+
import { isWarningEnabled } from '../utilities/config';
1017

11-
const fs = require('fs');
1218
const packageJson = require('../package.json');
13-
const path = require('path');
14-
const stripIndents = require('@angular-devkit/core').tags.stripIndents;
15-
const yellow = require('@angular-devkit/core').terminal.yellow;
16-
const SemVer = require('semver').SemVer;
1719

1820
function _fromPackageJson(cwd?: string) {
1921
cwd = cwd || process.cwd();
@@ -45,8 +47,10 @@ if (process.env['NG_CLI_PROFILING']) {
4547
const exitHandler = (options: { cleanup?: boolean, exit?: boolean }) => {
4648
if (options.cleanup) {
4749
const cpuProfile = profiler.stopProfiling();
48-
fs.writeFileSync(path.resolve(process.cwd(), process.env.NG_CLI_PROFILING) + '.cpuprofile',
49-
JSON.stringify(cpuProfile));
50+
fs.writeFileSync(
51+
path.resolve(process.cwd(), process.env.NG_CLI_PROFILING || '') + '.cpuprofile',
52+
JSON.stringify(cpuProfile),
53+
);
5054
}
5155

5256
if (options.exit) {
@@ -59,45 +63,33 @@ if (process.env['NG_CLI_PROFILING']) {
5963
process.on('uncaughtException', () => exitHandler({ exit: true }));
6064
}
6165

62-
const isInside = (base: string, potential: string): boolean => {
63-
const absoluteBase = path.resolve(base);
64-
const absolutePotential = path.resolve(potential);
65-
const relativePotential = path.relative(absoluteBase, absolutePotential);
66-
if (!relativePotential.startsWith('..') && !path.isAbsolute(relativePotential)) {
67-
return true;
68-
}
69-
70-
return false;
71-
};
72-
7366
let cli;
7467
try {
75-
const projectLocalCli = require.resolve(
68+
const projectLocalCli = resolve(
7669
'@angular/cli',
77-
{ paths: [ path.join(process.cwd(), 'node_modules'), process.cwd() ]},
70+
{
71+
checkGlobal: false,
72+
basedir: process.cwd(),
73+
preserveSymlinks: true,
74+
},
7875
);
7976

80-
const isGlobal = isInside(path.join(__dirname, '..'), projectLocalCli);
81-
if (isGlobal) {
82-
throw new Error();
83-
}
84-
8577
// This was run from a global, check local version.
8678
const globalVersion = new SemVer(packageJson['version']);
8779
let localVersion;
8880
let shouldWarn = false;
8981

9082
try {
9183
localVersion = _fromPackageJson();
92-
shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
84+
shouldWarn = localVersion != null && globalVersion.compare(localVersion) > 0;
9385
} catch (e) {
9486
// eslint-disable-next-line no-console
9587
console.error(e);
9688
shouldWarn = true;
9789
}
9890

9991
if (shouldWarn && isWarningEnabled('versionMismatch')) {
100-
const warning = yellow(stripIndents`
92+
const warning = terminal.yellow(tags.stripIndents`
10193
Your global Angular CLI version (${globalVersion}) is greater than your local
10294
version (${localVersion}). The local Angular CLI version is used.
10395

tests/legacy-cli/e2e/tests/generate/component/component-module.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ export default function() {
1212
.then(() => expectFileToMatch(modulePath,
1313
/import { TestComponentComponent } from '.\/test-component\/test-component.component'/))
1414

15-
// E2E_DISABLE: temporarily disable pending investigation
16-
// .then(() => process.chdir(join(root, 'src', 'app')))
17-
// .then(() => ng('generate', 'component', 'test-component2', '--module', 'app.module.ts'))
18-
// .then(() => process.chdir('../..'))
19-
// .then(() => expectFileToMatch(modulePath,
20-
// /import { TestComponent2Component } from '.\/test-component2\/test-component2.component'/))
15+
.then(() => process.chdir(join(root, 'src', 'app')))
16+
.then(() => ng('generate', 'component', 'test-component2', '--module', 'app.module.ts'))
17+
.then(() => process.chdir('../..'))
18+
.then(() => expectFileToMatch(modulePath,
19+
/import { TestComponent2Component } from '.\/test-component2\/test-component2.component'/))
2120

2221
// Try to run the unit tests.
2322
.then(() => ng('build'));

tests/legacy-cli/e2e/tests/generate/directive/directive-module.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ export default function() {
1010
.then(() => expectFileToMatch(modulePath,
1111
/import { TestDirectiveDirective } from '.\/test-directive.directive'/))
1212

13-
// E2E_DISABLE: temporarily disable pending investigation
14-
// .then(() => process.chdir(join('src', 'app')))
15-
// .then(() => ng('generate', 'directive', 'test-directive2', '--module', 'app.module.ts'))
16-
// .then(() => process.chdir('../..'))
17-
// .then(() => expectFileToMatch(modulePath,
18-
// /import { TestDirective2Directive } from '.\/test-directive2.directive'/))
13+
.then(() => process.chdir(join('src', 'app')))
14+
.then(() => ng('generate', 'directive', 'test-directive2', '--module', 'app.module.ts'))
15+
.then(() => process.chdir('../..'))
16+
.then(() => expectFileToMatch(modulePath,
17+
/import { TestDirective2Directive } from '.\/test-directive2.directive'/))
1918

2019
// Try to run the unit tests.
2120
.then(() => ng('build'));

tests/legacy-cli/e2e/tests/generate/pipe/pipe-module.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ export default function() {
1010
.then(() => expectFileToMatch(modulePath,
1111
/import { TestPipePipe } from '.\/test-pipe.pipe'/))
1212

13-
// E2E_DISABLE: temporarily disable pending investigation
14-
// .then(() => process.chdir(join('src', 'app')))
15-
// .then(() => ng('generate', 'pipe', 'test-pipe2', '--module', 'app.module.ts'))
16-
// .then(() => process.chdir('../..'))
17-
// .then(() => expectFileToMatch(modulePath,
18-
// /import { TestPipe2Pipe } from '.\/test-pipe2.pipe'/))
13+
.then(() => process.chdir(join('src', 'app')))
14+
.then(() => ng('generate', 'pipe', 'test-pipe2', '--module', 'app.module.ts'))
15+
.then(() => process.chdir('../..'))
16+
.then(() => expectFileToMatch(modulePath,
17+
/import { TestPipe2Pipe } from '.\/test-pipe2.pipe'/))
1918

2019
// Try to run the unit tests.
2120
.then(() => ng('build'));

0 commit comments

Comments
 (0)