From c991fa75269df0a21f32f900ed0a4f5aca78cacd Mon Sep 17 00:00:00 2001 From: prometheansacrifice <3097018+ManasJayanth@users.noreply.github.com> Date: Sat, 27 Jul 2024 09:41:53 +0530 Subject: [PATCH] Fixes for projects that use custom working directory (#30) * Makes working-directory absolute if it wasn't * Make manifest.esy property optional * Fixes working directory while npm-release * Guard the prepare-npm-artifacts mode against missing esy.release config * Fixes workingDirectory of esy status command * Fixes workingDirectory of esy npm-release command * Build on Ubuntu --- README.md | 2 +- action.yml | 4 ++- dist/index.js | 54 ++++++++++++++++++++++++---------------- index.ts | 68 ++++++++++++++++++++++++++++++--------------------- 4 files changed, 77 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 427de15..3062d7c 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ Path where esy can setup the cache. Default: `$HOME/.esy` #### `working-directory` Working directory of the project. Useful for projects that place esy project -under a folder. +under a folder. It's converted into an absolute path, if it already isn't. Default: Action workspace root. diff --git a/action.yml b/action.yml index e553e66..d0ef387 100644 --- a/action.yml +++ b/action.yml @@ -8,7 +8,9 @@ inputs: description: A cache key for retrieving esy sources cache. required: true working-directory: - description: Working directory for esy + description: + Working directory for esy. It's converted into an absolute path, if it + already isn't required: false esy-prefix: description: Prefix of esy folder diff --git a/dist/index.js b/dist/index.js index 1133f83..d3cbd55 100644 --- a/dist/index.js +++ b/dist/index.js @@ -139524,11 +139524,18 @@ if (partsSeparatedBtAT.length > 1 && partsSeparatedBtAT[0] !== "") { console.error("Please specify the version (or NPM dist-tag) in the setup-esy-version field"); process.exit(-1); } +let workingDirectory = core.getInput("working-directory") || process.cwd(); +workingDirectory = external_path_.isAbsolute(workingDirectory) + ? workingDirectory + : external_path_.join(process.cwd(), workingDirectory); function run(name, command, args) { return __awaiter(this, void 0, void 0, function* () { const PATH = process.env.PATH ? process.env.PATH : ""; core.startGroup(name); - yield (0,exec.exec)(command, args, { env: Object.assign(Object.assign({}, process.env), { PATH }) }); + yield (0,exec.exec)(command, args, { + env: Object.assign(Object.assign({}, process.env), { PATH }), + cwd: workingDirectory, + }); core.endGroup(); }); } @@ -139589,7 +139596,8 @@ const index_platform = external_os_.platform(); const arch = external_os_.arch(); function main() { return __awaiter(this, void 0, void 0, function* () { - const workingDirectory = core.getInput("working-directory") || process.cwd(); + // Otherwise, when we change directories and then back (workingDirectory -> /tmp -> workingDirectory) + // chdir() would try to enter a path relative to that path try { if (setupEsy) { let tarballUrl, checksum, esyPackageVersion, esyPackageName; @@ -139708,29 +139716,34 @@ function compress(dir, outputFile) { } function prepareNPMArtifacts() { return __awaiter(this, void 0, void 0, function* () { + var _a; const statusCmd = manifestKey ? `esy ${manifestKey} status` : "esy status"; try { - const manifestFilePath = JSON.parse(external_child_process_.execSync(statusCmd).toString()).rootPackageConfigPath; + const manifestFilePath = JSON.parse(external_child_process_.execSync(statusCmd, { cwd: workingDirectory }).toString()).rootPackageConfigPath; const manifest = JSON.parse(external_fs_.readFileSync(manifestFilePath).toString()); - if (manifest.esy.release) { + if ((_a = manifest.esy) === null || _a === void 0 ? void 0 : _a.release) { yield runEsyCommand("Running esy npm-release", ["npm-release"]); + let tarFile = `npm-tarball.tgz`; + yield compress(external_path_.join(workingDirectory, "_release"), tarFile); + const artifactName = `esy-npm-release-${index_platform}-${arch}`; + console.log("Artifact name: ", artifactName); + const { id, size } = yield artifact_default().uploadArtifact(artifactName, [tarFile], process.env.GITHUB_WORKSPACE, { + // The level of compression for Zlib to be applied to the artifact archive. + // - 0: No compression + // - 1: Best speed + // - 6: Default compression (same as GNU Gzip) + // - 9: Best compression + compressionLevel: 0, + // optional: how long to retain the artifact + // if unspecified, defaults to repository/org retention settings (the limit of this value) + retentionDays: 10, + }); + console.log(`Created artifact with id: ${id} (bytes: ${size}`); + } + else { + console.error(external_fs_.readFileSync(manifestFilePath).toString()); + throw new Error(`No config found in ${manifestFilePath} for npm-release. See https://esy.sh/docs/npm-release to learn more.`); } - let tarFile = `npm-tarball.tgz`; - yield compress("_release", tarFile); - const artifactName = `esy-npm-release-${index_platform}-${arch}`; - console.log("Artifact name: ", artifactName); - const { id, size } = yield artifact_default().uploadArtifact(artifactName, [tarFile], process.env.GITHUB_WORKSPACE, { - // The level of compression for Zlib to be applied to the artifact archive. - // - 0: No compression - // - 1: Best speed - // - 6: Default compression (same as GNU Gzip) - // - 9: Best compression - compressionLevel: 0, - // optional: how long to retain the artifact - // if unspecified, defaults to repository/org retention settings (the limit of this value) - retentionDays: 10, - }); - console.log(`Created artifact with id: ${id} (bytes: ${size}`); } catch (error) { if (error instanceof Error) { @@ -139744,7 +139757,6 @@ function prepareNPMArtifacts() { } function bundleNPMArtifacts() { return __awaiter(this, void 0, void 0, function* () { - const workingDirectory = core.getInput("working-directory") || process.cwd(); external_fs_.statSync(workingDirectory); process.chdir(workingDirectory); const releaseFolder = external_path_.join(workingDirectory, "_npm-release"); diff --git a/index.ts b/index.ts index 81a67cc..ff9113c 100644 --- a/index.ts +++ b/index.ts @@ -48,11 +48,18 @@ if (partsSeparatedBtAT.length > 1 && partsSeparatedBtAT[0] !== "") { ); process.exit(-1); } +let workingDirectory = core.getInput("working-directory") || process.cwd(); +workingDirectory = path.isAbsolute(workingDirectory) + ? workingDirectory + : path.join(process.cwd(), workingDirectory); async function run(name: string, command: string, args: string[]) { const PATH = process.env.PATH ? process.env.PATH : ""; core.startGroup(name); - await exec(command, args, { env: { ...process.env, PATH } }); + await exec(command, args, { + env: { ...process.env, PATH }, + cwd: workingDirectory, + }); core.endGroup(); } @@ -128,7 +135,8 @@ function computeChecksum(filePath: string, algo: string) { const platform = os.platform(); const arch = os.arch(); async function main() { - const workingDirectory = core.getInput("working-directory") || process.cwd(); + // Otherwise, when we change directories and then back (workingDirectory -> /tmp -> workingDirectory) + // chdir() would try to enter a path relative to that path try { if (setupEsy) { let tarballUrl, checksum, esyPackageVersion, esyPackageName; @@ -283,35 +291,40 @@ async function prepareNPMArtifacts() { const statusCmd = manifestKey ? `esy ${manifestKey} status` : "esy status"; try { const manifestFilePath = JSON.parse( - cp.execSync(statusCmd).toString() + cp.execSync(statusCmd, { cwd: workingDirectory }).toString() ).rootPackageConfigPath; const manifest = JSON.parse(fs.readFileSync(manifestFilePath).toString()); - if (manifest.esy.release) { + if (manifest.esy?.release) { await runEsyCommand("Running esy npm-release", ["npm-release"]); - } - let tarFile = `npm-tarball.tgz`; - await compress("_release", tarFile); - - const artifactName = `esy-npm-release-${platform}-${arch}`; - console.log("Artifact name: ", artifactName); - const { id, size } = await artifact.uploadArtifact( - artifactName, - [tarFile], - process.env.GITHUB_WORKSPACE!, - { - // The level of compression for Zlib to be applied to the artifact archive. - // - 0: No compression - // - 1: Best speed - // - 6: Default compression (same as GNU Gzip) - // - 9: Best compression - compressionLevel: 0, - // optional: how long to retain the artifact - // if unspecified, defaults to repository/org retention settings (the limit of this value) - retentionDays: 10, - } - ); + let tarFile = `npm-tarball.tgz`; + await compress(path.join(workingDirectory, "_release"), tarFile); + + const artifactName = `esy-npm-release-${platform}-${arch}`; + console.log("Artifact name: ", artifactName); + const { id, size } = await artifact.uploadArtifact( + artifactName, + [tarFile], + process.env.GITHUB_WORKSPACE!, + { + // The level of compression for Zlib to be applied to the artifact archive. + // - 0: No compression + // - 1: Best speed + // - 6: Default compression (same as GNU Gzip) + // - 9: Best compression + compressionLevel: 0, + // optional: how long to retain the artifact + // if unspecified, defaults to repository/org retention settings (the limit of this value) + retentionDays: 10, + } + ); - console.log(`Created artifact with id: ${id} (bytes: ${size}`); + console.log(`Created artifact with id: ${id} (bytes: ${size}`); + } else { + console.error(fs.readFileSync(manifestFilePath).toString()); + throw new Error( + `No config found in ${manifestFilePath} for npm-release. See https://esy.sh/docs/npm-release to learn more.` + ); + } } catch (error) { if (error instanceof Error) { core.setFailed(error.message); @@ -322,7 +335,6 @@ async function prepareNPMArtifacts() { } async function bundleNPMArtifacts() { - const workingDirectory = core.getInput("working-directory") || process.cwd(); fs.statSync(workingDirectory); process.chdir(workingDirectory); const releaseFolder = path.join(workingDirectory, "_npm-release");