From 211270d85fc53bfb16bf5db80e56ce329655f54f Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 27 Feb 2017 16:36:19 -0800 Subject: [PATCH] fix(@angular/cli): fix access to sections of package.json that dont exist (#5074) Fixes #5070. --- packages/@angular/cli/bin/ng | 8 +++++--- packages/@angular/cli/commands/new.ts | 1 - packages/@angular/cli/commands/version.ts | 4 ++-- packages/@angular/cli/tasks/eject.ts | 3 +++ tests/e2e/setup/010-build.ts | 6 ++++++ tests/e2e/utils/assets.ts | 7 +++++++ 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/@angular/cli/bin/ng b/packages/@angular/cli/bin/ng index e396458cbdb9..ee45c6a93e08 100755 --- a/packages/@angular/cli/bin/ng +++ b/packages/@angular/cli/bin/ng @@ -91,9 +91,11 @@ if (require('../package.json')['name'] == 'angular-cli' const packageJsonProjectPath = findUp('package.json', process.cwd(), true); if (packageJsonProjectPath && fs.existsSync(packageJsonProjectPath)) { const packageJsonProject = require(packageJsonProjectPath); - const hasOldDep = !!packageJsonProject.dependencies['angular-cli']; - const hasOldDevDep = !!packageJsonProject.devDependencies['angular-cli']; - const hasDevDep = !!packageJsonProject.devDependencies['@angular/cli']; + const deps = packageJsonProject['dependencies'] || {}; + const devDeps = packageJsonProject['devDependencies'] || {}; + const hasOldDep = !!deps['angular-cli']; + const hasOldDevDep = !!devDeps['angular-cli']; + const hasDevDep = !!devDeps['@angular/cli']; if (hasOldDep || hasOldDevDep || !hasDevDep) { const warnings = [ diff --git a/packages/@angular/cli/commands/new.ts b/packages/@angular/cli/commands/new.ts index 2b8df9dddced..0fd2945821e8 100644 --- a/packages/@angular/cli/commands/new.ts +++ b/packages/@angular/cli/commands/new.ts @@ -136,7 +136,6 @@ const NewCommand = Command.extend({ } validateProjectName(packageName); - commandOptions.name = packageName; if (commandOptions.dryRun) { commandOptions.skipGit = true; diff --git a/packages/@angular/cli/commands/version.ts b/packages/@angular/cli/commands/version.ts index da6d2f47449b..4463187c50a9 100644 --- a/packages/@angular/cli/commands/version.ts +++ b/packages/@angular/cli/commands/version.ts @@ -76,8 +76,8 @@ const VersionCommand = Command.extend({ getDependencyVersions: function(pkg: any, prefix: string): any { const modules: any = {}; - Object.keys(pkg.dependencies || {}) - .concat(Object.keys(pkg.devDependencies || {})) + Object.keys(pkg['dependencies'] || {}) + .concat(Object.keys(pkg['devDependencies'] || {})) .filter(depName => depName && depName.startsWith(prefix)) .forEach(key => modules[key] = this.getVersion(key)); diff --git a/packages/@angular/cli/tasks/eject.ts b/packages/@angular/cli/tasks/eject.ts index 06c6b9909418..728b08d6884d 100644 --- a/packages/@angular/cli/tasks/eject.ts +++ b/packages/@angular/cli/tasks/eject.ts @@ -464,6 +464,9 @@ export default Task.extend({ // Add new dependencies based on our dependencies. const ourPackageJson = require('../package.json'); + if (!packageJson['devDependencies']) { + packageJson['devDependencies'] = {}; + } packageJson['devDependencies']['webpack-dev-server'] = ourPackageJson['dependencies']['webpack-dev-server']; diff --git a/tests/e2e/setup/010-build.ts b/tests/e2e/setup/010-build.ts index 691bdffcf62d..2f3333962c7e 100644 --- a/tests/e2e/setup/010-build.ts +++ b/tests/e2e/setup/010-build.ts @@ -17,6 +17,12 @@ export default function() { .then(() => Promise.all(Object.keys(packages).map(pkgName => { return updateJsonFile(join(packages[pkgName].dist, 'package.json'), json => { Object.keys(packages).forEach(pkgName => { + if (!json['dependencies']) { + json['dependencies'] = {}; + } + if (!json['devDependencies']) { + json['devDependencies'] = {}; + } if (json['dependencies'] && pkgName in json['dependencies']) { json['dependencies'][pkgName] = packages[pkgName].dist; } else if (json['devDependencies'] && pkgName in json['devDependencies']) { diff --git a/tests/e2e/utils/assets.ts b/tests/e2e/utils/assets.ts index ce5ace7741f9..602d7e2e2d7e 100644 --- a/tests/e2e/utils/assets.ts +++ b/tests/e2e/utils/assets.ts @@ -38,6 +38,13 @@ export function createProjectFromAsset(assetName: string) { .then(() => copyAssets(assetName)) .then(dir => process.chdir(dir)) .then(() => updateJsonFile('package.json', json => { + if (!json['dependencies']) { + json['dependencies'] = {}; + } + if (!json['devDependencies']) { + json['devDependencies'] = {}; + } + for (const packageName of Object.keys(packages)) { if (json['dependencies'].hasOwnProperty(packageName)) { json['dependencies'][packageName] = packages[packageName].dist;