Skip to content

Commit c63de69

Browse files
authoredApr 28, 2018
Merge pull request #376 from serverless-heaven/rebase-yarn-lock-file
Rebase yarn lock file with file references correctly
2 parents e79a23c + a185262 commit c63de69

File tree

4 files changed

+107
-11
lines changed

4 files changed

+107
-11
lines changed
 

‎lib/packExternalModules.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const isBuiltinModule = require('is-builtin-module');
99
const Packagers = require('./packagers');
1010

1111
function rebaseFileReferences(pathToPackageRoot, moduleVersion) {
12-
if (/^file:[^/]{2}/.test(moduleVersion)) {
12+
if (/^(?:file:[^/]{2}|\.\/|\.\.\/)/.test(moduleVersion)) {
1313
const filePath = _.replace(moduleVersion, /^file:/, '');
14-
return _.replace(`file:${pathToPackageRoot}/${filePath}`, /\\/g, '/');
14+
return _.replace(`${_.startsWith(moduleVersion, 'file:') ? 'file:' : ''}${pathToPackageRoot}/${filePath}`, /\\/g, '/');
1515
}
1616

1717
return moduleVersion;
@@ -246,20 +246,20 @@ module.exports = {
246246
if (exists) {
247247
this.serverless.cli.log('Package lock found - Using locked versions');
248248
try {
249-
let packageLockJson = this.serverless.utils.readFileSync(packageLockPath);
249+
let packageLockFile = this.serverless.utils.readFileSync(packageLockPath);
250250
/**
251251
* We should not be modifying 'package-lock.json'
252252
* because this file should be treat as internal to npm.
253253
*
254254
* Rebase package-lock is a temporary workaround and must be
255255
* removed as soon as https://github.com/npm/npm/issues/19183 gets fixed.
256256
*/
257-
packager.rebaseLockfile(relPath, packageLockJson);
258-
if (_.isObject(packageLockJson)) {
259-
packageLockJson = JSON.stringify(packageLockJson, null, 2);
257+
packageLockFile = packager.rebaseLockfile(relPath, packageLockFile);
258+
if (_.isObject(packageLockFile)) {
259+
packageLockFile = JSON.stringify(packageLockFile, null, 2);
260260
}
261261

262-
this.serverless.utils.writeFileSync(path.join(compositeModulePath, packager.lockfileName), packageLockJson);
262+
this.serverless.utils.writeFileSync(path.join(compositeModulePath, packager.lockfileName), packageLockFile);
263263
hasPackageLock = true;
264264
} catch(err) {
265265
this.serverless.cli.log(`Warning: Could not read lock file: ${err.message}`);

‎lib/packagers/npm.js

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class NPM {
7676
NPM.rebaseLockfile(pathToPackageRoot, lockedDependency);
7777
});
7878
}
79+
80+
return lockfile;
7981
}
8082

8183
static install(cwd) {

‎lib/packagers/yarn.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,23 @@ class Yarn {
7979
});
8080
}
8181

82-
// TODO: Check if we need that for Yarn
83-
static rebaseLockfile(/* pathToPackageRoot, lockfile */) {
82+
static rebaseLockfile(pathToPackageRoot, lockfile) {
83+
const fileVersionMatcher = /[^"/]@(?:file:)?((?:\.\/|\.\.\/).*?)[":,]/gm;
84+
const replacements = [];
85+
let match;
86+
87+
// Detect all references and create replacement line strings
88+
while ((match = fileVersionMatcher.exec(lockfile)) !== null) {
89+
replacements.push({
90+
oldRef: match[1],
91+
newRef: _.replace(`${pathToPackageRoot}/${match[1]}`, /\\/g, '/')
92+
});
93+
}
94+
95+
// Replace all lines in lockfile
96+
return _.reduce(replacements, (__, replacement) => {
97+
return _.replace(__, replacement.oldRef, replacement.newRef);
98+
}, lockfile);
8499
}
85100

86101
static install(cwd, packagerOptions) {

‎lib/packagers/yarn.test.js

+81-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,87 @@ describe('yarn', () => {
115115
it('should return the original lockfile', () => {
116116
const testContent = 'eugfogfoigqwoeifgoqwhhacvaisvciuviwefvc';
117117
const testContent2 = 'eugfogfoigqwoeifgoqwhhacvaisvciuviwefvc';
118-
yarnModule.rebaseLockfile('.', testContent);
119-
expect(testContent).to.equal(testContent2);
118+
expect(yarnModule.rebaseLockfile('.', testContent)).to.equal(testContent2);
119+
});
120+
121+
it('should rebase file references', () => {
122+
const testContent = `
123+
acorn@^2.1.0, acorn@^2.4.0:
124+
version "2.7.0"
125+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-2.7.0.tgz#ab6e7d9d886aaca8b085bc3312b79a198433f0e7"
126+
127+
acorn@^3.0.4:
128+
version "3.3.0"
129+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a"
130+
131+
otherModule@file:../../otherModule/the-new-version:
132+
version "1.2.0"
133+
134+
acorn@^2.1.0, acorn@^2.4.0:
135+
version "2.7.0"
136+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-2.7.0.tgz#ab6e7d9d886aaca8b085bc3312b79a198433f0e7"
137+
138+
"@myCompany/myModule@../../myModule/the-new-version":
139+
version "6.1.0"
140+
dependencies:
141+
aws-xray-sdk "^1.1.6"
142+
aws4 "^1.6.0"
143+
base-x "^3.0.3"
144+
bluebird "^3.5.1"
145+
chalk "^1.1.3"
146+
cls-bluebird "^2.1.0"
147+
continuation-local-storage "^3.2.1"
148+
lodash "^4.17.4"
149+
moment "^2.20.0"
150+
redis "^2.8.0"
151+
request "^2.83.0"
152+
ulid "^0.1.0"
153+
uuid "^3.1.0"
154+
155+
acorn@^5.0.0, acorn@^5.5.0:
156+
version "5.5.3"
157+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.3.tgz#f473dd47e0277a08e28e9bec5aeeb04751f0b8c9"
158+
`;
159+
160+
const expectedContent = `
161+
acorn@^2.1.0, acorn@^2.4.0:
162+
version "2.7.0"
163+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-2.7.0.tgz#ab6e7d9d886aaca8b085bc3312b79a198433f0e7"
164+
165+
acorn@^3.0.4:
166+
version "3.3.0"
167+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a"
168+
169+
otherModule@file:../../project/../../otherModule/the-new-version:
170+
version "1.2.0"
171+
172+
acorn@^2.1.0, acorn@^2.4.0:
173+
version "2.7.0"
174+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-2.7.0.tgz#ab6e7d9d886aaca8b085bc3312b79a198433f0e7"
175+
176+
"@myCompany/myModule@../../project/../../myModule/the-new-version":
177+
version "6.1.0"
178+
dependencies:
179+
aws-xray-sdk "^1.1.6"
180+
aws4 "^1.6.0"
181+
base-x "^3.0.3"
182+
bluebird "^3.5.1"
183+
chalk "^1.1.3"
184+
cls-bluebird "^2.1.0"
185+
continuation-local-storage "^3.2.1"
186+
lodash "^4.17.4"
187+
moment "^2.20.0"
188+
redis "^2.8.0"
189+
request "^2.83.0"
190+
ulid "^0.1.0"
191+
uuid "^3.1.0"
192+
193+
acorn@^5.0.0, acorn@^5.5.0:
194+
version "5.5.3"
195+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.3.tgz#f473dd47e0277a08e28e9bec5aeeb04751f0b8c9"
196+
`;
197+
198+
expect(yarnModule.rebaseLockfile('../../project', testContent)).to.equal(expectedContent);
120199
});
121200
});
122201

0 commit comments

Comments
 (0)