Skip to content

Instantly share code, notes, and snippets.

@Meir017
Last active August 25, 2022 19:53
Show Gist options
  • Save Meir017/ba29c6b7434482256f8d12917cd63a40 to your computer and use it in GitHub Desktop.
Save Meir017/ba29c6b7434482256f8d12917cd63a40 to your computer and use it in GitHub Desktop.
convert node modules to tgz
const { existSync, writeFileSync, mkdirSync } = require('fs');
const { execSync } = require('child_process');
if (!existsSync('package.json')) {
writeFileSync('package.json', JSON.stringify({ dependencies: {} }, null, 2));
}
function saferequire(name) {
try {
return require(name);
} catch (error) {
console.log(`installing package ${name}`);
console.log(execSync(`npm install ${name} --save-dev`).toString());
return require(name);
}
}
const download = require('download-file');
const packageLockJson = require('./package-lock.json');
const tarballsDirectory = './tarballs';
const tarballs = [];
enumerateDependencies(packageLockJson.dependencies);
function enumerateDependencies(dependencies) {
for (const dependencyName in dependencies) {
const dependency = dependencies[dependencyName];
if (dependency.resolved) {
tarballs.push({
filename: `${dependencyName}-${dependency.version}.tgz`,
url: dependency.resolved
});
}
if (dependency.dependencies) {
enumerateDependencies(dependency.dependencies);
}
}
}
if (!existSync(tarballsDirectory)) {
mkdirSync(tarballsDirectory);
}
tarballs.forEach(tarball => {
download(tarball.url, {
directory: tarballsDirectory,
filename: tarball.filename
});
});
function download(url, dest, cb) {
const file = fs.createWriteStream(dest);
const request = https.get(url, response => {
response.pipe(file);
file.on('finish', () => {
file.close(cb);
});
});
}
@sharmaashish54
Copy link

How to use it ?

@Meir017
Copy link
Author

Meir017 commented Jun 24, 2020

run with node inside a directory that has a package-lock.json file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment