Skip to content

Commit e95909e

Browse files
committed
Fixes compiler version in postinstall command
1 parent 41b6cdb commit e95909e

File tree

2 files changed

+92
-9
lines changed

2 files changed

+92
-9
lines changed

dist/index.js

+49-7
Original file line numberDiff line numberDiff line change
@@ -95238,6 +95238,14 @@ const { isUint8Array, isArrayBuffer } = __nccwpck_require__(84978)
9523895238
const { File: UndiciFile } = __nccwpck_require__(78511)
9523995239
const { parseMIMEType, serializeAMimeType } = __nccwpck_require__(685)
9524095240

95241+
let random
95242+
try {
95243+
const crypto = __nccwpck_require__(6005)
95244+
random = (max) => crypto.randomInt(0, max)
95245+
} catch {
95246+
random = (max) => Math.floor(Math.random(max))
95247+
}
95248+
9524195249
let ReadableStream = globalThis.ReadableStream
9524295250

9524395251
/** @type {globalThis['File']} */
@@ -95323,7 +95331,7 @@ function extractBody (object, keepalive = false) {
9532395331
// Set source to a copy of the bytes held by object.
9532495332
source = new Uint8Array(object.buffer.slice(object.byteOffset, object.byteOffset + object.byteLength))
9532595333
} else if (util.isFormDataLike(object)) {
95326-
const boundary = `----formdata-undici-0${`${Math.floor(Math.random() * 1e11)}`.padStart(11, '0')}`
95334+
const boundary = `----formdata-undici-0${`${random(1e11)}`.padStart(11, '0')}`
9532795335
const prefix = `--${boundary}\r\nContent-Disposition: form-data`
9532895336

9532995337
/*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
@@ -113212,6 +113220,14 @@ module.exports = require("net");
113212113220

113213113221
/***/ }),
113214113222

113223+
/***/ 6005:
113224+
/***/ ((module) => {
113225+
113226+
"use strict";
113227+
module.exports = require("node:crypto");
113228+
113229+
/***/ }),
113230+
113215113231
/***/ 15673:
113216113232
/***/ ((module) => {
113217113233

@@ -137462,8 +137478,8 @@ const create = makeCommand(createFileSync, createFile, createSync, createAsync,
137462137478
//# sourceMappingURL=create.js.map
137463137479
;// CONCATENATED MODULE: external "node:assert"
137464137480
const external_node_assert_namespaceObject = require("node:assert");
137465-
;// CONCATENATED MODULE: external "node:crypto"
137466-
const external_node_crypto_namespaceObject = require("node:crypto");
137481+
// EXTERNAL MODULE: external "node:crypto"
137482+
var external_node_crypto_ = __nccwpck_require__(6005);
137467137483
;// CONCATENATED MODULE: ./node_modules/tar/dist/esm/get-write-flag.js
137468137484
// Get the appropriate flag to use for creating files
137469137485
// We use fmap on Windows platforms for files less than
@@ -138329,7 +138345,7 @@ const unlinkFile = (path, cb) => {
138329138345
if (!unpack_isWindows) {
138330138346
return external_node_fs_.unlink(path, cb);
138331138347
}
138332-
const name = path + '.DELETE.' + (0,external_node_crypto_namespaceObject.randomBytes)(16).toString('hex');
138348+
const name = path + '.DELETE.' + (0,external_node_crypto_.randomBytes)(16).toString('hex');
138333138349
external_node_fs_.rename(path, name, er => {
138334138350
if (er) {
138335138351
return cb(er);
@@ -138343,7 +138359,7 @@ const unlinkFileSync = (path) => {
138343138359
if (!unpack_isWindows) {
138344138360
return external_node_fs_.unlinkSync(path);
138345138361
}
138346-
const name = path + '.DELETE.' + (0,external_node_crypto_namespaceObject.randomBytes)(16).toString('hex');
138362+
const name = path + '.DELETE.' + (0,external_node_crypto_.randomBytes)(16).toString('hex');
138347138363
external_node_fs_.renameSync(path, name);
138348138364
external_node_fs_.unlinkSync(name);
138349138365
};
@@ -139820,6 +139836,32 @@ function bundleNPMArtifacts() {
139820139836
return external_child_process_.execSync(cmd).toString().trim();
139821139837
}
139822139838
const version = exec("git describe --tags --always");
139839+
function getCompilerVersion(sandbox) {
139840+
let lockFileFolder;
139841+
if (sandbox) {
139842+
lockFileFolder = "esy.lock";
139843+
}
139844+
else {
139845+
lockFileFolder = `${sandbox}.esy.lock`;
139846+
}
139847+
console.log(`Looking up ${lockFileFolder} for compiler version`);
139848+
const lockFile = JSON.parse(external_fs_.readFileSync(external_path_.join(process.cwd(), lockFileFolder, "index.json"))
139849+
.toString());
139850+
const ocamlPackages = Object.keys(lockFile.node).filter((k) => k.startsWith("ocaml@"));
139851+
if (ocamlPackages.length === 0) {
139852+
throw new Error("Couldn't figure ocaml compiler version from lock file because no ocaml-like packages were found");
139853+
}
139854+
const ocamlPackageID = ocamlPackages[0];
139855+
const ocamlPackageIDParts = ocamlPackageID.split("@");
139856+
if (ocamlPackageIDParts.length !== 3) {
139857+
throw new Error(`Couldn't figure ocaml compiler version from lock file because PackageId wasn't in expected format: ${ocamlPackageID}`);
139858+
}
139859+
return ocamlPackageIDParts[1];
139860+
}
139861+
const compilerVersion = getCompilerVersion();
139862+
console.log("Found compiler version", compilerVersion);
139863+
const staticCompilerVersion = getCompilerVersion("static.esy");
139864+
console.log("Found static compiler version", staticCompilerVersion);
139823139865
const packageJson = JSON.stringify({
139824139866
name: mainPackageJson.name,
139825139867
version,
@@ -139828,8 +139870,8 @@ function bundleNPMArtifacts() {
139828139870
repository: mainPackageJson.repository,
139829139871
scripts: {
139830139872
postinstall: rewritePrefix
139831-
? "node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '4.12.0-musl.static.flambda': '4.12.0'; process.env['OCAML_PKG_NAME'] = 'ocaml'; process.env['ESY_RELEASE_REWRITE_PREFIX']=true; require('./postinstall.js')\""
139832-
: "node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '4.12.0-musl.static.flambda': '4.12.0'; process.env['OCAML_PKG_NAME'] = 'ocaml'; require('./postinstall.js')\"",
139873+
? `node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '${staticCompilerVersion}-musl.static.flambda': '${compilerVersion}'; process.env['OCAML_PKG_NAME'] = 'ocaml'; process.env['ESY_RELEASE_REWRITE_PREFIX']=true; require('./postinstall.js')\"`
139874+
: "require('./postinstall.js')\"",
139833139875
},
139834139876
bin: bins,
139835139877
files: [

index.ts

+43-2
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,47 @@ async function bundleNPMArtifacts() {
410410
return cp.execSync(cmd).toString().trim();
411411
}
412412
const version = exec("git describe --tags --always");
413+
414+
function getCompilerVersion(sandbox?: string) {
415+
let lockFileFolder;
416+
417+
if (sandbox) {
418+
lockFileFolder = "esy.lock";
419+
} else {
420+
lockFileFolder = `${sandbox}.esy.lock`;
421+
}
422+
console.log(`Looking up ${lockFileFolder} for compiler version`);
423+
const lockFile = JSON.parse(
424+
fs
425+
.readFileSync(path.join(process.cwd(), lockFileFolder, "index.json"))
426+
.toString()
427+
);
428+
const ocamlPackages = Object.keys(lockFile.node).filter((k) =>
429+
k.startsWith("ocaml@")
430+
);
431+
432+
if (ocamlPackages.length === 0) {
433+
throw new Error(
434+
"Couldn't figure ocaml compiler version from lock file because no ocaml-like packages were found"
435+
);
436+
}
437+
438+
const ocamlPackageID = ocamlPackages[0];
439+
const ocamlPackageIDParts = ocamlPackageID.split("@");
440+
441+
if (ocamlPackageIDParts.length !== 3) {
442+
throw new Error(
443+
`Couldn't figure ocaml compiler version from lock file because PackageId wasn't in expected format: ${ocamlPackageID}`
444+
);
445+
}
446+
447+
return ocamlPackageIDParts[1];
448+
}
449+
const compilerVersion = getCompilerVersion();
450+
console.log("Found compiler version", compilerVersion);
451+
const staticCompilerVersion = getCompilerVersion("static.esy");
452+
console.log("Found static compiler version", staticCompilerVersion);
453+
413454
const packageJson = JSON.stringify(
414455
{
415456
name: mainPackageJson.name,
@@ -419,8 +460,8 @@ async function bundleNPMArtifacts() {
419460
repository: mainPackageJson.repository,
420461
scripts: {
421462
postinstall: rewritePrefix
422-
? "node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '4.12.0-musl.static.flambda': '4.12.0'; process.env['OCAML_PKG_NAME'] = 'ocaml'; process.env['ESY_RELEASE_REWRITE_PREFIX']=true; require('./postinstall.js')\""
423-
: "node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '4.12.0-musl.static.flambda': '4.12.0'; process.env['OCAML_PKG_NAME'] = 'ocaml'; require('./postinstall.js')\"",
463+
? `node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '${staticCompilerVersion}-musl.static.flambda': '${compilerVersion}'; process.env['OCAML_PKG_NAME'] = 'ocaml'; process.env['ESY_RELEASE_REWRITE_PREFIX']=true; require('./postinstall.js')\"`
464+
: "require('./postinstall.js')\"",
424465
},
425466
bin: bins,
426467
files: [

0 commit comments

Comments
 (0)