Skip to content

Commit

Permalink
sqiasg
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasJayanth committed Jul 13, 2024
1 parent 4e0066d commit 85c3d0c
Showing 1 changed file with 45 additions and 35 deletions.
80 changes: 45 additions & 35 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,23 @@ async function run(name: string, command: string, args: string[]) {
type NpmInfo = { dist: { tarball: string; shasum: string }; version: string };
let cachedEsyNPMInfo: NpmInfo | undefined;
function getLatestEsyNPMInfo(
alternativeEsyNPMPackage: string | undefined
alternativeEsyNPMPackage: string | undefined,
): NpmInfo {
let esyPackage;
if (!alternativeEsyNPMPackage || alternativeEsyNPMPackage === "") {
// No alternative was provided. So, fallback to default
esyPackage = "esy@latest";
} else {
const [packageName, _packageVersion] = alternativeEsyNPMPackage.split("@");
const partsSeparatedBtAT = alternativeEsyNPMPackage.split("@");
if (partsSeparatedBtAT.length > 1 && partsSeparatedBtAT[0] !== "") {
// ie @ appears in such a way that it separates name and version. Not to signify namespace
// esy@latest enters this block. @prometheansacrifice/esy doesn't
console.error(
"Please specify the version (or NPM dist-tag) in the setup-esy-version field",
);
process.exit(-1);
}

const {
validForOldPackages,
validForNewPackages,
Expand All @@ -59,12 +68,12 @@ function getLatestEsyNPMInfo(
Errors:
${errors.join("\n")}`);
}
esyPackage = alternativeEsyNPMPackage;
esyPackage = `${alternativeEsyNPMPackage}@${setupEsyVersion}`;
}
try {
if (!cachedEsyNPMInfo) {
cachedEsyNPMInfo = JSON.parse(
cp.execSync(`npm info ${esyPackage} --json`).toString().trim()
cp.execSync(`npm info "${esyPackage}" --json`).toString().trim(),
);
return cachedEsyNPMInfo!;
} else {
Expand All @@ -76,7 +85,7 @@ ${errors.join("\n")}`);
}

function getEsyDownloadArtifactsMeta(
alternativeEsyNPMPackage: string | undefined
alternativeEsyNPMPackage: string | undefined,
) {
const esyNPMInfo = getLatestEsyNPMInfo(alternativeEsyNPMPackage);
const tarballUrl = esyNPMInfo.dist.tarball;
Expand Down Expand Up @@ -110,56 +119,57 @@ async function main() {
const workingDirectory = core.getInput("working-directory") || process.cwd();
try {
if (setupEsy) {
let tarballUrl, checksum, version;
let tarballUrl, checksum, esyPackageVersion, esyPackageName;
if (!setupEsyVersion || !setupEsyShaSum || !setupEsyTarball) {
const meta = getEsyDownloadArtifactsMeta(setupEsyNPMPackageName);
tarballUrl = meta.tarballUrl;
checksum = meta.shasum;
version = meta.version;
esyPackageName = meta.name;
} else {
tarballUrl = setupEsyTarball;
version = setupEsyTarball;
version = setupEsyVersion;
checksum = setupEsyShaSum;
esyPackageName = setupEsy;
}
console.log("Fetching tarball from", tarballUrl);
const downloadedEsyNPMTarball = await toolCache.downloadTool(tarballUrl);
const checksumAlgo = "sha1";
const computedChecksum = await computeChecksum(
downloadedEsyNPMTarball,
checksumAlgo
checksumAlgo,
);
if (computedChecksum !== checksum) {
throw new Error(
`Downloaded by checksum failed. url: ${setupEsyTarball} downloadPath: ${downloadedEsyNPMTarball} checksum expected: ${checksum} checksum computed: ${computedChecksum} checksum algorithm: ${checksumAlgo}`
`Downloaded by checksum failed. url: ${setupEsyTarball} downloadPath: ${downloadedEsyNPMTarball} checksum expected: ${checksum} checksum computed: ${computedChecksum} checksum algorithm: ${checksumAlgo}`,
);
} else {
console.log(
"Checksum validation succeeded. Downloaded tarball's checksum is:",
checksum
checksum,
);
}

const extractedEsyNPM = await toolCache.extractTar(
downloadedEsyNPMTarball
downloadedEsyNPMTarball,
);
core.startGroup("Running postinstall");
const esyPackagePath = path.join(extractedEsyNPM, "package");
const postInstall = JSON.parse(
fs
.readFileSync(path.join(esyPackagePath, "package.json"))
.toString()
.trim()
.trim(),
).scripts.postinstall;
process.chdir(esyPackagePath);
await exec(postInstall);
core.endGroup();
process.chdir(workingDirectory);
const cachedPath = await toolCache.cacheDir(
esyPackagePath,
"esy",
version
esyPackageName,
esyPackageVersion,
);

core.addPath(path.join(cachedPath, "bin"));
}
fs.statSync(workingDirectory);
Expand All @@ -173,9 +183,9 @@ async function main() {
process.env.GITHUB_WORKSPACE ||
process.env.HOME ||
process.env.HOMEPATH ||
"~"
"~",
),
".esy"
".esy",
);
console.log("esy-prefix", esyPrefix);
const ghOutputEsyPrefixK = "ESY_PREFIX";
Expand All @@ -187,7 +197,7 @@ async function main() {
const installCacheKey = await cache.restoreCache(
installPath,
installKey,
[]
[],
);
if (installCacheKey) {
console.log("Restored the install cache");
Expand All @@ -214,7 +224,7 @@ async function main() {
const buildCacheKey = await cache.restoreCache(
depsPath,
buildKey,
restoreKeys
restoreKeys,
);
if (buildCacheKey) {
console.log("Restored the build cache");
Expand Down Expand Up @@ -250,15 +260,15 @@ async function main() {
async function uncompress(
dest: string,
tarFile: string,
strip?: number
strip?: number,
): Promise<void> {
return new Promise((resolve, reject) => {
fs.createReadStream(tarFile)
.pipe(
tar.x({
strip: strip,
C: dest, // alias for cwd:'some-dir', also ok
})
}),
)
.on("close", () => resolve())
.on("error", reject);
Expand All @@ -277,7 +287,7 @@ async function prepareNPMArtifacts() {
const statusCmd = manifestKey ? `esy ${manifestKey} status` : "esy status";
try {
const manifestFilePath = JSON.parse(
cp.execSync(statusCmd).toString()
cp.execSync(statusCmd).toString(),
).rootPackageConfigPath;
const manifest = JSON.parse(fs.readFileSync(manifestFilePath).toString());
if (manifest.esy.release) {
Expand All @@ -302,7 +312,7 @@ async function prepareNPMArtifacts() {
// 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}`);
Expand Down Expand Up @@ -333,19 +343,19 @@ async function bundleNPMArtifacts() {
});
await uncompress(folderPath, path.join(folderPath, "npm-tarball.tgz"), 1);
return folderName;
})
}),
);
const artifactFolders = artifactFoldersList.reduce(
(acc: string[], folderName: string) => {
acc.push(folderName);
return acc;
},
[]
[],
);
const esyInstallReleaseJS = "esyInstallRelease.js";
fs.cpSync(
path.join(releaseFolder, artifactFoldersList[0], esyInstallReleaseJS),
path.join(releaseFolder, esyInstallReleaseJS)
path.join(releaseFolder, esyInstallReleaseJS),
);
console.log("Creating package.json");
const possibleEsyJsonPath = path.join(workingDirectory, "esy.json");
Expand All @@ -359,21 +369,21 @@ async function bundleNPMArtifacts() {
process.exit(1);
}
const mainPackageJson = JSON.parse(
fs.readFileSync(`${mainPackageJsonPath}`).toString()
fs.readFileSync(`${mainPackageJsonPath}`).toString(),
);
const bins = Array.isArray(mainPackageJson.esy.release.bin)
? mainPackageJson.esy.release.bin.reduce(
(acc: any, curr: string) =>
Object.assign({ [curr]: "bin/" + curr }, acc),
{}
{},
)
: Object.keys(mainPackageJson.esy.release.bin).reduce(
(acc, currKey) =>
Object.assign(
{ [currKey]: "bin/" + mainPackageJson.esy.release.bin[currKey] },
acc
acc,
),
{}
{},
);
const rewritePrefix =
mainPackageJson.esy &&
Expand Down Expand Up @@ -406,7 +416,7 @@ async function bundleNPMArtifacts() {
].concat(artifactFolders),
},
null,
2
2,
);

fs.writeFileSync(path.join(releaseFolder, "package.json"), packageJson, {
Expand All @@ -417,7 +427,7 @@ async function bundleNPMArtifacts() {
console.log("Copying LICENSE");
fs.copyFileSync(
path.join(workingDirectory, "LICENSE"),
path.join(releaseFolder, "LICENSE")
path.join(releaseFolder, "LICENSE"),
);
} catch (e) {
console.warn("No LICENSE found");
Expand All @@ -427,7 +437,7 @@ async function bundleNPMArtifacts() {
console.log("Copying README.md");
fs.copyFileSync(
path.join(workingDirectory, "README.md"),
path.join(releaseFolder, "README.md")
path.join(releaseFolder, "README.md"),
);
} catch {
console.warn("No LICENSE found");
Expand All @@ -438,7 +448,7 @@ async function bundleNPMArtifacts() {
console.log("Copying postinstall.js from", releasePostInstallJS);
fs.copyFileSync(
releasePostInstallJS,
path.join(releaseFolder, "postinstall.js")
path.join(releaseFolder, "postinstall.js"),
);

console.log("Creating placeholder files");
Expand Down Expand Up @@ -475,7 +485,7 @@ ECHO You need to have postinstall enabled`;
// optional: how long to retain the artifact
// if unspecified, defaults to repository/org retention settings (the limit of this value)
retentionDays: 10,
}
},
);

core.endGroup();
Expand Down

0 comments on commit 85c3d0c

Please sign in to comment.