Skip to content

Commit 645e0c5

Browse files
Broccohansl
authored andcommitted
fix(@angular/cli): Move update check to inProject command logic
fixes #10145
1 parent 526b519 commit 645e0c5

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

packages/@angular/cli/models/command-runner.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import {
77
ArgumentStrategy
88
} from '../models/command';
99
import { oneLine } from 'common-tags';
10-
import { logging } from '@angular-devkit/core';
10+
import { logging, normalize, tags } from '@angular-devkit/core';
1111
import { camelize } from '@angular-devkit/core/src/utils/strings';
12+
import { findUp } from '../utilities/find-up';
1213

1314
import * as yargsParser from 'yargs-parser';
1415
import * as fs from 'fs';
1516
import { join } from 'path';
1617

18+
const SilentError = require('silent-error');
19+
1720
export interface CommandMap {
1821
[key: string]: CommandConstructor;
1922
}
@@ -204,7 +207,7 @@ function verifyCommandInScope(command: Command, scope = CommandScope.everywhere)
204207
} else {
205208
errorMessage = `This command can not be run inside of a CLI project.`;
206209
}
207-
throw new Error(errorMessage);
210+
throw new SilentError(errorMessage);
208211
}
209212
}
210213
}
@@ -220,7 +223,30 @@ function verifyWorkspace(command: Command, executionScope: CommandScope, root: s
220223
if (fs.existsSync(join(root, '.angular.json'))) {
221224
return;
222225
}
223-
throw new Error('Invalid project: missing workspace file.');
226+
227+
// Check if there's an old config file meaning that the project has not been updated
228+
const oldConfigFileNames = [
229+
normalize('.angular-cli.json'),
230+
normalize('angular-cli.json'),
231+
];
232+
const oldConfigFilePath = (root && findUp(oldConfigFileNames, root, true))
233+
|| findUp(oldConfigFileNames, process.cwd(), true)
234+
|| findUp(oldConfigFileNames, __dirname, true);
235+
236+
// If an old configuration file is found, throw an exception.
237+
if (oldConfigFilePath) {
238+
throw new SilentError(tags.stripIndent`
239+
An old project has been detected, which needs to be updated to Angular CLI 6.
240+
241+
Please run the following commands to update this project.
242+
243+
ng update @angular/cli --migrate-only --from=1.7.1
244+
npm i
245+
`);
246+
}
247+
248+
// If no configuration file is found (old or new), throw an exception.
249+
throw new SilentError('Invalid project: missing workspace file.');
224250
}
225251
}
226252

@@ -233,7 +259,7 @@ async function runHelp(command: Command, options: any): Promise<void> {
233259
async function validateAndRunCommand(command: Command, options: any): Promise<any> {
234260
const isValid = await command.validate(options);
235261
if (isValid !== undefined && !isValid) {
236-
throw new Error(`Validation error. Invalid command`);
262+
throw new SilentError(`Validation error. Invalid command`);
237263
}
238264
return await command.run(options);
239265
}

packages/@angular/cli/models/workspace-loader.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { Observable, of } from 'rxjs';
1111
import { concatMap, tap } from 'rxjs/operators';
1212
import * as fs from 'fs';
1313
import { homedir } from 'os';
14-
import { stripIndent } from 'common-tags';
1514
import { findUp } from '../utilities/find-up';
1615

1716

@@ -39,7 +38,6 @@ export class WorkspaceLoader {
3938

4039
// TODO: do this with the host instead of fs.
4140
private _getProjectWorkspaceFilePath(projectPath?: string): Observable<Path | null> {
42-
this._assertUpdatedWorkspace(projectPath);
4341
// Find the workspace file, either where specified, in the Angular CLI project
4442
// (if it's in node_modules) or from the current process.
4543
const workspaceFilePath = (projectPath && findUp(this._configFileNames, projectPath))
@@ -83,26 +81,4 @@ export class WorkspaceLoader {
8381
tap(workspace => this._workspaceCacheMap.set(workspacePath, workspace))
8482
);
8583
}
86-
87-
private _assertUpdatedWorkspace(projectPath?: string) {
88-
const oldConfigFileNames = [
89-
normalize('.angular-cli.json'),
90-
normalize('angular-cli.json'),
91-
];
92-
93-
const oldConfigFilePath = (projectPath && findUp(oldConfigFileNames, projectPath))
94-
|| findUp(oldConfigFileNames, process.cwd())
95-
|| findUp(oldConfigFileNames, __dirname);
96-
97-
if (oldConfigFilePath) {
98-
throw new Error(stripIndent`
99-
An old project has been detected, which needs to be updated to Angular CLI 6.
100-
101-
Please run the following commands to update this project.
102-
103-
ng update @angular/cli --migrate-only --from=1.7.1
104-
npm i
105-
`);
106-
}
107-
}
10884
}

packages/@angular/cli/utilities/find-up.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from 'path';
22
import { existsSync } from 'fs';
33

4-
export function findUp(names: string | string[], from: string, stopOnNodeModules = false) {
4+
export function findUp(names: string | string[], from: string, stopOnPackageJson = false) {
55
if (!Array.isArray(names)) {
66
names = [names];
77
}
@@ -16,9 +16,9 @@ export function findUp(names: string | string[], from: string, stopOnNodeModules
1616
}
1717
}
1818

19-
if (stopOnNodeModules) {
20-
const nodeModuleP = path.join(currentDir, 'node_modules');
21-
if (existsSync(nodeModuleP)) {
19+
if (stopOnPackageJson) {
20+
const packageJsonPth = path.join(currentDir, 'package.json');
21+
if (existsSync(packageJsonPth)) {
2222
return null;
2323
}
2424
}

0 commit comments

Comments
 (0)