Last active
August 25, 2022 19:53
-
-
Save Meir017/ba29c6b7434482256f8d12917cd63a40 to your computer and use it in GitHub Desktop.
convert node modules to tgz
This file contains 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
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); | |
}); | |
}); | |
} |
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
How to use it ?