Skip to content

Instantly share code, notes, and snippets.

@jarek-foksa
Created December 18, 2021 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarek-foksa/25cfb8614bf830a14b28e111cec43fa8 to your computer and use it in GitHub Desktop.
Save jarek-foksa/25cfb8614bf830a14b28e111cec43fa8 to your computer and use it in GitHub Desktop.
import ChildProcess from "child_process";
// @info
// Fetch a package from NPM registry without downloading any dependencies or executing any scripts.
// @type
// (string, string) => void
let fetchPackage = (name, version, tempPath, directory = `deps/${name}`) => {
return new Promise((resolve, reject) => {
let npmProcess = ChildProcess.spawnSync(
"npm",
["pack", `${name}@${version}`],
{cwd: tempPath, encoding : "utf8"}
);
if (npmProcess.error) {
console.log("Error", error.toString());
reject(error);
}
else {
let archiveName = npmProcess.stdout.trim();
Fse.ensureDirSync(`${tempPath}/${directory}/`);
let tarProcess = ChildProcess.spawn(
"tar",
["xzf", `${archiveName}`, "--strip-components", "1", "-C", directory],
{cwd: tempPath, stdio: "inherit"}
);
tarProcess.on("exit", (error) => {
Fse.removeSync(`${tempPath}/${archiveName}`);
if (error) {
console.log("Error", error.toString());
reject(error);
}
else {
resolve();
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment