Skip to content

Instantly share code, notes, and snippets.

@wstrinz
Forked from Janiczek/elm-build-cache.mjs
Last active February 2, 2022 15:17
Show Gist options
  • Save wstrinz/d92c8456d10a7540816acee85a611b6c to your computer and use it in GitHub Desktop.
Save wstrinz/d92c8456d10a7540816acee85a611b6c to your computer and use it in GitHub Desktop.
Build ~/.elm cache manually without checking SHA1 hashes
#!/usr/bin/env node
// the .mjs extension is important
// run this inside the directory containing the `elm.json` file
// (if it's not executable, run `chmod +x elm-build-cache.mjs`)
// with VERBOSE=1 env var it will show you results of the exec commands
import { promises } from "fs";
import { exec } from "child_process";
const fs = promises;
const tempDir = "elm-stuff/elm-build-cache";
const shouldLog = process.env.VERBOSE === "1"; // false; // true;
const ourExec = async cmd =>
new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) reject(error);
if (shouldLog) console.log({ cmd, stdout, stderr });
resolve([stdout, stderr]);
});
});
const downloadDependency = async ([pkg, version]) => {
const pkgWithDash = pkg.replace("/", "-");
const zipballUrl = `https://github.com/${pkg}/zipball/${version}/`;
const zipballPath = `${tempDir}/${pkgWithDash}-${version}.zip`;
// based on info in https://package.elm-lang.org/packages/elm/project-metadata-utils/latest/
const elmHome = `~/.elm`;
const packagePath = `${elmHome}/0.19.1/packages/${pkg}/${version}`;
await ourExec(`mkdir -p ${tempDir}`);
await ourExec(`rm -rf ${packagePath}`);
await ourExec(`mkdir -p ${packagePath}`);
await ourExec(`curl -L ${zipballUrl} -o ${zipballPath}`);
await ourExec(`unzip ${zipballPath} -d ${tempDir}`);
await ourExec(`mv ${tempDir}/${pkgWithDash}-*/* ${packagePath}`);
await ourExec(`rm -rf '${zipballPath}'`);
};
const pickLowerBound = version => version.slice(0, version.indexOf(" "));
const getPackageDeps = elmJson =>
Object.fromEntries(
Object.entries(elmJson.dependencies).map(([k, v]) => [k, pickLowerBound(v)])
);
const getAppDeps = elmJson => ({
...elmJson.dependencies.direct,
...elmJson.dependencies.indirect,
});
const getElmJsonDeps = elmJson =>
elmJson.type === "application"
? getAppDeps(elmJson)
: getPackageDeps(elmJson);
const main = async () => {
await ourExec(`rm -rf '${tempDir}'`);
const elmJson = JSON.parse(await fs.readFile("./elm.json", "utf-8"));
const deps = getElmJsonDeps(elmJson);
console.log({ deps });
await Promise.all(Object.entries(deps).map(downloadDependency));
await ourExec(`rm -rf '${tempDir}'`);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment