Skip to content

Instantly share code, notes, and snippets.

@ulrikstrid
Last active April 28, 2020 09:17
Show Gist options
  • Save ulrikstrid/33a8e329967ee791c602e90ce498aeef to your computer and use it in GitHub Desktop.
Save ulrikstrid/33a8e329967ee791c602e90ce498aeef to your computer and use it in GitHub Desktop.
const Path = require("path");
const Fs = require("fs");
const Cp = require("child_process");
const deleteFolderRecursive = (path) => {
if (Fs.existsSync(path)) {
Fs.readdirSync(path).forEach((file, index) => {
const curPath = Path.join(path, file);
if (Fs.lstatSync(curPath).isDirectory()) {
// recurse
deleteFolderRecursive(curPath);
} else {
// delete file
Fs.unlinkSync(curPath);
}
});
Fs.rmdirSync(path);
}
};
function copyFileSync(source, target) {
var targetFile = target;
//if target is a directory a new file with the same name will be created
if (Fs.existsSync(target)) {
if (Fs.lstatSync(target).isDirectory()) {
targetFile = Path.join(target, Path.basename(source));
}
}
Fs.writeFileSync(targetFile, Fs.readFileSync(source));
}
function copyFolderRecursiveSync(source, target) {
var files = [];
//check if folder needs to be created or integrated
var targetFolder = Path.join(target, Path.basename(source));
if (!Fs.existsSync(targetFolder)) {
Fs.mkdirSync(targetFolder);
}
//copy
if (Fs.lstatSync(source).isDirectory()) {
files = Fs.readdirSync(source);
files.forEach(function (file) {
var curSource = Path.join(source, file);
if (Fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}
// Get the source installation.json to get source paths
const installationJson = Fs.readFileSync(
Path.resolve("_esy/default/installation.json")
);
// Remove version numbers to make it easier to query the file
const cleanedInstallationJson = installationJson
.toString()
.replace(/@(opam:)?[a-z0-9\.]+@[0-9a-z]+/g, "");
// Parse the cleaned JSON
const packageIndex = JSON.parse(cleanedInstallationJson);
const vendorPackages = (packages) => {
// Create vendor folder
try {
Fs.mkdirSync("vendor");
} catch (e) {
console.warn(e);
}
// Copy the wanted packages into place
packages.forEach(({ package, target, extra }) => {
const vendorPath = Path.join("vendor", target);
try {
Fs.mkdirSync(vendorPath);
} catch (e) {
console.warn(e);
}
try {
copyFolderRecursiveSync(packageIndex[package], vendorPath);
} catch (e) {
console.warn(e);
}
// Extra steps needed
if (extra != null) {
try {
extra(Path.join(vendorPath, Path.basename(packageIndex[package])));
} catch (e) {
console.warn(e);
}
}
});
};
// The packages we want with potential extra steps needed
const wantedPackages = [
{ package: "@reason-native-web/piaf", target: "piaf" },
{
package: "@opam/hmap",
target: "hmap",
extra: (path) => {
const duneFile = "(library (name hmap) (public_name hmap))";
const duneProject = "(lang dune 1.11)";
Fs.writeFileSync(Path.join(path, "src", "dune"), duneFile);
Fs.writeFileSync(Path.join(path, "dune-project"), duneProject);
Fs.copyFileSync(Path.join(path, "opam"), Path.join(path, "hmap.opam"));
},
},
];
// Vendor packages
vendorPackages(wantedPackages);
// Generate docs
Cp.execSync("esy docs");
// Print path to allow `open $(node vendor_and_build_docs.js)`
console.log(Cp.execSync("esy docs-path").toString().trim() + "/index.html");
// Cleanup
deleteFolderRecursive("vendor");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment