Skip to content

Commit

Permalink
refactor: remove some duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 21, 2020
1 parent 4d5c451 commit e54b508
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions lib/getManifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,16 @@ function getManifest(path) {
throw new SyntaxError(`Package name must be non-empty string: "${path}"`);

// Check dependencies.
if (!manifest.hasOwnProperty("dependencies")) manifest.dependencies = {};
else if (typeof manifest.dependencies !== "object")
throw new SyntaxError(`Package dependencies must be object: "${path}"`);
const checkDeps = (scope) => {
if (!manifest.hasOwnProperty(scope)) manifest[scope] = {};
else if (typeof manifest[scope] !== "object")
throw new SyntaxError(`Package ${scope} must be object: "${path}"`);
};

// Check devDependencies.
if (!manifest.hasOwnProperty("devDependencies")) manifest.devDependencies = {};
else if (typeof manifest.devDependencies !== "object")
throw new SyntaxError(`Package devDependencies must be object: "${path}"`);

// Check peerDependencies.
if (!manifest.hasOwnProperty("peerDependencies")) manifest.peerDependencies = {};
else if (typeof manifest.peerDependencies !== "object")
throw new SyntaxError(`Package peerDependencies must be object: "${path}"`);
checkDeps("dependencies");
checkDeps("devDependencies");
checkDeps("peerDependencies");
checkDeps("optionalDependencies");

// Return contents.
return manifest;
Expand Down

0 comments on commit e54b508

Please sign in to comment.