Skip to content
This repository has been archived by the owner on Mar 5, 2018. It is now read-only.

Don't change yarn.lock version comments #53

Merged
merged 1 commit into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions helpers/javascript/lib/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
};
}
Expand Down
2 changes: 1 addition & 1 deletion helpers/javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bump-core",
"dependencies": {
"yarn": "^0.24.4"
"yarn": "https://github.com/dependabot/yarn.git"
},
"devDependencies": {
"eslint": "^3.19.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"left-pad": "^1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -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"
45 changes: 30 additions & 15 deletions helpers/javascript/test/updater.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
11 changes: 7 additions & 4 deletions helpers/javascript/yarn.lock
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down