Skip to content

Commit

Permalink
feat: allow empty package.json for 2ndary entries
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidParks8 authored and dherges committed Oct 18, 2017
1 parent 18515af commit c0af605
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/lib/steps/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NgPackageConfig } from '../../ng-package.schema';
import { NgPackageData, DEFAULT_BUILD_FOLDER } from '../model/ng-package-data';
import { NgArtifacts } from '../model/ng-artifacts';
import { copyFiles } from '../util/copy';
import { tryReadJson } from '../util/json';
import { readJson, writeJson, readdir, lstat, Stats } from 'fs-extra';
import { merge, isArray } from 'lodash';
import * as log from '../util/log';
Expand Down Expand Up @@ -172,17 +173,21 @@ async function readSecondaryPackage(rootPackage: NgPackageData, filePath: string
const packageJsonFile = path.resolve(baseDirectory, 'package.json');

let ngPackage: NgPackageConfig = await readNgPackageFile(ngPackageFile);
const packageJson = await readJson(packageJsonFile);
const packageJson: any = await tryReadJson(packageJsonFile);

ngPackage = merge(ngPackage, packageJson.ngPackage, arrayMergeLogic);
if (!ngPackage.lib) {
ngPackage.lib = {};
}

ngPackage.lib.externals = rootPackage.libExternals;

return new NgPackageData(
rootPackage.sourcePath,
rootPackage.fullPackageName,
rootPackage.destinationPath,
baseDirectory,
ngPackage
rootPackage.libExternals
);
}

Expand Down
16 changes: 15 additions & 1 deletion src/lib/util/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,23 @@ export async function modifyJsonFiles(globPattern: string, modifyFn: (jsonObj: a

await Promise.all(
fileNames.map(async (fileName: string): Promise<void> => {
const fileContent: any = await readJson(fileName);
const fileContent: any = await tryReadJson(fileName);
const modified = modifyFn(fileContent);
await writeJson(fileName, modified);
}
));
}

/**
* Read json and don't throw if json parsing fails.
*
* @param filePath Path to the file which is parsed.
*/
export async function tryReadJson(filePath: string): Promise<any> {
try {
return await readJson(filePath);
} catch {
// this means the file was empty or not json, which is fine
return {};
}
}

0 comments on commit c0af605

Please sign in to comment.