New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(package): .serialize() also writes package-lock version #2160
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ const npa = require("npm-package-arg"); | |
const path = require("path"); | ||
const loadJsonFile = require("load-json-file"); | ||
const writePkg = require("write-pkg"); | ||
const fs = require("fs"); | ||
const writeJsonFile = require("write-json-file"); | ||
const pWaterfall = require("p-waterfall"); | ||
|
||
// symbol used to "hide" internal state | ||
const PKG = Symbol("pkg"); | ||
|
@@ -30,6 +33,12 @@ function shallowCopy(json) { | |
}, {}); | ||
} | ||
|
||
// determine if a package-lock.json file exists | ||
function hasPackageLock(location) { | ||
const packageLock = path.resolve(location); | ||
return fs.existsSync(packageLock); | ||
} | ||
|
||
class Package { | ||
constructor(pkg, location, rootPath = location) { | ||
// npa will throw an error if the name is invalid | ||
|
@@ -77,6 +86,9 @@ class Package { | |
manifestLocation: { | ||
value: path.join(location, "package.json"), | ||
}, | ||
lockFileLocation: { | ||
value: path.join(location, "package-lock.json"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be |
||
}, | ||
nodeModulesLocation: { | ||
value: path.join(location, "node_modules"), | ||
}, | ||
|
@@ -181,7 +193,27 @@ class Package { | |
* @returns {Promise} resolves when write finished | ||
*/ | ||
serialize() { | ||
return writePkg(this.manifestLocation, this[PKG]).then(() => this); | ||
return writePkg(this.manifestLocation, this[PKG]) | ||
.then(() => this.writePkgLock()) | ||
.then(() => this); | ||
} | ||
|
||
/** | ||
* Write package-lock changes to disk | ||
* @returns {Promise} resolves when write finished | ||
*/ | ||
writePkgLock() { | ||
// We only care about version in the lock file; a package's name never changes | ||
const lockChanges = { version: this[PKG].version }; | ||
|
||
const tasks = [ | ||
() => loadJsonFile(this.lockFileLocation), | ||
lockFileData => Promise.resolve(Object.assign({}, lockFileData, lockChanges)), | ||
updatedLockFileData => | ||
writeJsonFile(this.lockFileLocation, updatedLockFileData, { detectIndent: true }), | ||
]; | ||
|
||
return hasPackageLock(this.lockFileLocation) ? pWaterfall(tasks) : Promise.resolve(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need |
||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the appropriate place for lockfile maintenance. It's a function of the version command, not integral to the nature of a Package.