Created
December 18, 2021 12:57
-
-
Save jarek-foksa/25cfb8614bf830a14b28e111cec43fa8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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