Skip to content

Commit

Permalink
fix(@angular/cli): properly detect local cli package
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin authored and hansl committed Jun 6, 2018
1 parent 54092ee commit 3fb958b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
48 changes: 20 additions & 28 deletions packages/angular/cli/lib/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/
import 'symbol-observable';
const isWarningEnabled = require('../utilities/config').isWarningEnabled;
// symbol polyfill must go first
// tslint:disable-next-line:ordered-imports import-groups
import { tags, terminal } from '@angular-devkit/core';
import { resolve } from '@angular-devkit/core/node';
import * as fs from 'fs';
import * as path from 'path';
import { SemVer } from 'semver';
import { isWarningEnabled } from '../utilities/config';

const fs = require('fs');
const packageJson = require('../package.json');
const path = require('path');
const stripIndents = require('@angular-devkit/core').tags.stripIndents;
const yellow = require('@angular-devkit/core').terminal.yellow;
const SemVer = require('semver').SemVer;

function _fromPackageJson(cwd?: string) {
cwd = cwd || process.cwd();
Expand Down Expand Up @@ -45,8 +47,10 @@ if (process.env['NG_CLI_PROFILING']) {
const exitHandler = (options: { cleanup?: boolean, exit?: boolean }) => {
if (options.cleanup) {
const cpuProfile = profiler.stopProfiling();
fs.writeFileSync(path.resolve(process.cwd(), process.env.NG_CLI_PROFILING) + '.cpuprofile',
JSON.stringify(cpuProfile));
fs.writeFileSync(
path.resolve(process.cwd(), process.env.NG_CLI_PROFILING || '') + '.cpuprofile',
JSON.stringify(cpuProfile),
);
}

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

const isInside = (base: string, potential: string): boolean => {
const absoluteBase = path.resolve(base);
const absolutePotential = path.resolve(potential);
const relativePotential = path.relative(absoluteBase, absolutePotential);
if (!relativePotential.startsWith('..') && !path.isAbsolute(relativePotential)) {
return true;
}

return false;
};

let cli;
try {
const projectLocalCli = require.resolve(
const projectLocalCli = resolve(
'@angular/cli',
{ paths: [ path.join(process.cwd(), 'node_modules'), process.cwd() ]},
{
checkGlobal: false,
basedir: process.cwd(),
preserveSymlinks: true,
},
);

const isGlobal = isInside(path.join(__dirname, '..'), projectLocalCli);
if (isGlobal) {
throw new Error();
}

// This was run from a global, check local version.
const globalVersion = new SemVer(packageJson['version']);
let localVersion;
let shouldWarn = false;

try {
localVersion = _fromPackageJson();
shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
shouldWarn = localVersion != null && globalVersion.compare(localVersion) > 0;
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
shouldWarn = true;
}

if (shouldWarn && isWarningEnabled('versionMismatch')) {
const warning = yellow(stripIndents`
const warning = terminal.yellow(tags.stripIndents`
Your global Angular CLI version (${globalVersion}) is greater than your local
version (${localVersion}). The local Angular CLI version is used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ export default function() {
.then(() => expectFileToMatch(modulePath,
/import { TestComponentComponent } from '.\/test-component\/test-component.component'/))

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

// Try to run the unit tests.
.then(() => ng('build'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ export default function() {
.then(() => expectFileToMatch(modulePath,
/import { TestDirectiveDirective } from '.\/test-directive.directive'/))

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

// Try to run the unit tests.
.then(() => ng('build'));
Expand Down
11 changes: 5 additions & 6 deletions tests/legacy-cli/e2e/tests/generate/pipe/pipe-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ export default function() {
.then(() => expectFileToMatch(modulePath,
/import { TestPipePipe } from '.\/test-pipe.pipe'/))

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

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

0 comments on commit 3fb958b

Please sign in to comment.