Skip to content

Commit c2c08d6

Browse files
committed
feat: use integrity field from yarn.lock as sha512 for nix derivation
Based off canva-public#11
1 parent 0ebc118 commit c2c08d6

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lib/print.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class Package {
157157
* src: {
158158
* type: 'url',
159159
* url: string,
160-
* sha1?: string,
160+
* hash?: string,
161161
* } | {
162162
* type: 'local',
163163
* },
@@ -200,8 +200,8 @@ class Package {
200200
*/
201201
static create(
202202
pkgInfo,
203-
// @ts-ignore because there is no optionalDependencies in the type
204-
{ version, resolved, dependencies = {}, optionalDependencies = {} }
203+
// @ts-ignore because type lacks: integrity, optionalDependencies
204+
{ version, integrity, resolved, dependencies = {}, optionalDependencies = {} }
205205
) {
206206
const { scope = '', name } = Id.parse(pkgInfo.name);
207207

@@ -211,14 +211,20 @@ class Package {
211211
const parsedUrl = new URL(resolved);
212212
// prettier-ignore
213213
if (['registry.yarnpkg.com', 'registry.npmjs.org'].includes(parsedUrl.host)) {
214+
let urlSha1 = parsedUrl.hash.slice(1);
215+
if (urlSha1) { urlSha1 = `sha1-${urlSha1}` }
216+
}
217+
218+
const hash = integrity || urlSha1;
219+
220+
if (hash) {
214221
src = {
215-
sha1: parsedUrl.hash.slice(1), // cut off the first ('#') character
216222
url: parsedUrl.origin + parsedUrl.pathname,
217223
type: 'url',
218224
};
219225
} else {
220226
src = {
221-
sha1: undefined, // We don't provide sha in order to force to override it manually
227+
hash: undefined, // We don't provide hash in order to force to override it manually
222228
url: parsedUrl.origin + parsedUrl.pathname,
223229
type: 'url',
224230
};
@@ -306,7 +312,7 @@ class Package {
306312
renderSrc() {
307313
switch (this.src.type) {
308314
case 'url':
309-
const { url, sha1 } = this.src;
315+
const { url, hash } = this.src;
310316
// Some of the urls can look like "https://codeload.github.com/xolvio/cucumber-js/tar.gz/cf953cb5b5de30dbcc279f59e4ebff3aa040071c",
311317
// i.e. no extention given. That's why Nix unable to recognize the type of archive so we need to have
312318
// name specified explicitly to all Nix to infer the archive type.
@@ -339,13 +345,8 @@ class Package {
339345
: ''
340346
}
341347
url = "${url}";
342-
${
343-
sha1
344-
? `sha1 = "${sha1}"`
345-
: `sha256 = abort ''
346-
347-
348-
Failed to infer \`sha256\` hash of the \`${this.renderKey()}\` package source from
348+
hash = ${hash ? `"${hash}"` : `abort ''
349+
Failed to infer hash of the \`${this.renderKey()}\` package source from
349350
\`${url}\`.
350351
351352
Override \`"${this.renderKey()}".src.sha256\` attribute in order to provide this missing piece to Nix.

0 commit comments

Comments
 (0)