diff --git a/helpers/javascript/lib/updater.js b/helpers/javascript/lib/updater.js index 5b7b9c91..59427a3f 100644 --- a/helpers/javascript/lib/updater.js +++ b/helpers/javascript/lib/updater.js @@ -61,7 +61,22 @@ async function allDependencyPatterns(config) { ); } +// Replace the version comments in the new lockfile with the ones from the old +// lockfile. If they weren't present in the old lockfile, delete them. +function recoverVersionComments(oldLockfile, newLockfile) { + const yarnRegex = /^# yarn v(\S+)\n/gm; + const nodeRegex = /^# node v(\S+)\n/gm; + const oldMatch = regex => [].concat(oldLockfile.match(regex))[0]; + return newLockfile + .replace(yarnRegex, match => oldMatch(yarnRegex) || "") + .replace(nodeRegex, match => oldMatch(nodeRegex) || ""); +} + async function updateDependencyFiles(directory, depName, desiredVersion) { + const readFile = fileName => + fs.readFileSync(path.join(directory, fileName)).toString(); + const originalYarnLock = readFile("yarn.lock"); + const flags = { ignoreScripts: true }; const reporter = new NoopReporter(); const config = new Config(reporter); @@ -85,15 +100,11 @@ async function updateDependencyFiles(directory, depName, desiredVersion) { // Despite the innocent-sounding name, this actually does all the hard work await add.init(); - const updatedYarnLock = fs - .readFileSync(path.join(directory, "yarn.lock")) - .toString(); - const updatedPackageJson = fs - .readFileSync(path.join(directory, "package.json")) - .toString(); + const updatedYarnLock = readFile("yarn.lock"); + const updatedPackageJson = readFile("package.json"); return { - "yarn.lock": updatedYarnLock, + "yarn.lock": recoverVersionComments(originalYarnLock, updatedYarnLock), "package.json": updatedPackageJson }; } diff --git a/helpers/javascript/package.json b/helpers/javascript/package.json index 7f29bd05..15cac212 100644 --- a/helpers/javascript/package.json +++ b/helpers/javascript/package.json @@ -1,7 +1,7 @@ { "name": "bump-core", "dependencies": { - "yarn": "^0.24.4" + "yarn": "https://github.com/dependabot/yarn.git" }, "devDependencies": { "eslint": "^3.19.0", diff --git a/helpers/javascript/test/fixtures/updater/with-version-comments/package.json b/helpers/javascript/test/fixtures/updater/with-version-comments/package.json new file mode 100644 index 00000000..00039fe5 --- /dev/null +++ b/helpers/javascript/test/fixtures/updater/with-version-comments/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "left-pad": "^1.0.0" + } +} diff --git a/helpers/javascript/test/fixtures/updater/with-version-comments/yarn.lock b/helpers/javascript/test/fixtures/updater/with-version-comments/yarn.lock new file mode 100644 index 00000000..a051ee2c --- /dev/null +++ b/helpers/javascript/test/fixtures/updater/with-version-comments/yarn.lock @@ -0,0 +1,13 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 +# yarn v0.0.0-0 +# node v0.0.0 + + +is-positive@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/is-positive/-/is-positive-3.1.0.tgz#857db584a1ba5d1cb2980527fc3b6c435d37b0fd" + +left-pad@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.0.0.tgz#c84e2417581bbb8eaf2b9e3d7a122e572ab1af37" diff --git a/helpers/javascript/test/updater.test.js b/helpers/javascript/test/updater.test.js index c3d9b0ea..88cdf9e1 100644 --- a/helpers/javascript/test/updater.test.js +++ b/helpers/javascript/test/updater.test.js @@ -2,36 +2,51 @@ const path = require("path"); const os = require("os"); const fs = require("fs-extra"); const nock = require("nock"); -const updater = require("../lib/updater"); +const updateDependencyFiles = require("../lib/updater").updateDependencyFiles; const helpers = require("./helpers"); describe("updater", () => { let tempDir; beforeEach(() => { - tempDir = fs.mkdtempSync(os.tmpdir() + path.sep); - }); - afterEach(() => fs.removeSync(tempDir)); - - it("something", async () => { nock("https://registry.yarnpkg.com") .get("/left-pad") .reply(200, helpers.loadFixture("yarnpkg-left-pad.json")); - const srcPackageJson = "test/fixtures/updater/original/package.json"; - const destPackageJson = path.join(tempDir, "package.json"); - await fs.copy(srcPackageJson, destPackageJson); + tempDir = fs.mkdtempSync(os.tmpdir() + path.sep); + }); + afterEach(() => fs.removeSync(tempDir)); + + async function copyDependencies(sourceDir, destDir) { + const srcPackageJson = `test/fixtures/updater/${sourceDir}/package.json`; + await fs.copy(srcPackageJson, `${destDir}/package.json`); - const srcYarnLock = "test/fixtures/updater/original/yarn.lock"; - const destYarnLock = path.join(tempDir, "yarn.lock"); - await fs.copy(srcYarnLock, destYarnLock); + const srcYarnLock = `test/fixtures/updater/${sourceDir}/yarn.lock`; + await fs.copy(srcYarnLock, `${destDir}/yarn.lock`); + } - const name = "left-pad"; - const version = "1.1.3"; - const result = await updater.updateDependencyFiles(tempDir, name, version); + it("generates an updated package.json and yarn.lock", async () => { + await copyDependencies("original", tempDir); + const result = await updateDependencyFiles(tempDir, "left-pad", "1.1.3"); expect(result).toEqual({ "package.json": helpers.loadFixture("updater/updated/package.json"), "yarn.lock": helpers.loadFixture("updater/updated/yarn.lock") }); }); + + it("doesn't modify existing version comments", async () => { + await copyDependencies("with-version-comments", tempDir); + + const result = await updateDependencyFiles(tempDir, "left-pad", "1.1.3"); + expect(result["yarn.lock"]).toContain("\n# yarn v0.0.0-0\n"); + expect(result["yarn.lock"]).toContain("\n# node v0.0.0\n"); + }); + + it("doesn't add version comments if they're not already there", async () => { + await copyDependencies("original", tempDir); + + const result = await updateDependencyFiles(tempDir, "left-pad", "1.1.3"); + expect(result["yarn.lock"]).not.toContain("\n# yarn v"); + expect(result["yarn.lock"]).not.toContain("\n# node"); + }); }); diff --git a/helpers/javascript/yarn.lock b/helpers/javascript/yarn.lock index c998e3f0..1bd4a48c 100644 --- a/helpers/javascript/yarn.lock +++ b/helpers/javascript/yarn.lock @@ -1,5 +1,7 @@ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 +# yarn v0.24.4 +# node v7.10.0 abab@^1.0.3: @@ -2912,9 +2914,9 @@ yargs@~3.10.0: decamelize "^1.0.0" window-size "0.1.0" -yarn@^0.24.4: - version "0.24.4" - resolved "https://registry.yarnpkg.com/yarn/-/yarn-0.24.4.tgz#a4bfa3bb768192706771337f959721cfc0555093" +"yarn@https://github.com/dependabot/yarn.git": + version "0.26.0-0" + resolved "https://github.com/dependabot/yarn.git#c1c19d6b735b35cbc07425f6469f48c2284bb807" dependencies: babel-runtime "^6.0.0" bytes "^2.4.0" @@ -2925,6 +2927,7 @@ yarn@^0.24.4: death "^1.0.0" debug "^2.2.0" detect-indent "^5.0.0" + glob "^7.1.1" gunzip-maybe "^1.4.0" ini "^1.3.4" inquirer "^3.0.1" @@ -2933,7 +2936,7 @@ yarn@^0.24.4: is-ci "^1.0.10" leven "^2.0.0" loud-rejection "^1.2.0" - minimatch "^3.0.3" + micromatch "^2.3.11" mkdirp "^0.5.1" node-emoji "^1.0.4" object-path "^0.11.2"