Skip to content

Commit

Permalink
fix(@angular/cli): fix access to sections of package.json that dont e…
Browse files Browse the repository at this point in the history
…xist (#5074)

Fixes #5070.
  • Loading branch information
hansl authored Feb 28, 2017
1 parent 97bfb12 commit 211270d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
8 changes: 5 additions & 3 deletions packages/@angular/cli/bin/ng
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
1 change: 0 additions & 1 deletion packages/@angular/cli/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ const NewCommand = Command.extend({
}

validateProjectName(packageName);

commandOptions.name = packageName;
if (commandOptions.dryRun) {
commandOptions.skipGit = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/@angular/cli/commands/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
3 changes: 3 additions & 0 deletions packages/@angular/cli/tasks/eject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down
6 changes: 6 additions & 0 deletions tests/e2e/setup/010-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down
7 changes: 7 additions & 0 deletions tests/e2e/utils/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 211270d

Please sign in to comment.