Last active
September 29, 2016 12:16
-
-
Save phillipj/7596dbc5ae4af0c74a3267dfdbab176f to your computer and use it in GitHub Desktop.
A node.js script to create a bundle containing an npm package, and all of its dependencies.
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
/* | |
* This script will download a package (and all of its dependencies) from the | |
* online NPM registry, then create a gzip'd tarball containing that package | |
* and all of its dependencies. This archive can then be copied to a machine | |
* without internet access and installed using npm. | |
* | |
* The idea is pretty simple: | |
* - npm install [package] | |
* - rewrite [package]/package.json to copy dependencies to bundleDependencies | |
* - npm pack [package] | |
* | |
* It is necessary to do this (intead of using pac) because when npm installs | |
* a module, it will actually strip out the node_modules folder from the | |
* tarball unless bundleDependencies is set. | |
* | |
* Author: Jack Gill (https://github.com/jackgill) | |
* Date: 11/27/2013 | |
* License: MIT License (see end of file) | |
*/ | |
// Original gist: https://gist.github.com/jackgill/7687308 | |
// Slightly modified by @phillipj too enable specifying @version of packages | |
'use strict'; | |
var fs = require('fs'); | |
var cp = require('child_process'); | |
var path = require('path'); | |
if (process.argv.length != 3) { | |
console.log("Usage: %s <package name>[@version]", process.argv[1]); | |
console.log("\nExamples:"); | |
console.log("\tnode pack.js react"); | |
console.log("\tnode pack.js [email protected]"); | |
console.log("\tnode pack.js @angular/[email protected]"); | |
process.exit(0); | |
} | |
var fullPackageStr = process.argv[2]; // might include @version suffix | |
// extracts package or @scope/package name, removing the possible @version suffix | |
var packageName = fullPackageStr.match(/^(@?[\w\/]+)(@.*)?$/)[1]; | |
// Copy dependencies to bundleDependencies | |
function rewritePackageJSON(fileName) { | |
var contents = fs.readFileSync(fileName); | |
var json = JSON.parse(contents); | |
if (json.dependencies) { | |
json.bundleDependencies = Object.keys(json.dependencies); | |
fs.writeFileSync(fileName, JSON.stringify(json, null, 2)); | |
} | |
} | |
// Install the package from the online registry | |
var installProcess = cp.exec('npm install ' + fullPackageStr, function(err, stdout) { | |
if (err) { | |
console.log("\n✘ Error executing npm install:", err); | |
process.exit(0); | |
} | |
// console.log(stdout) | |
// Set bundleDependencies for the package | |
rewritePackageJSON(path.join('node_modules', packageName, 'package.json')); | |
// Create a new .tgz file which bundles all dependencies | |
var packProcess = cp.exec('npm pack ' + path.join('node_modules', packageName), function(err) { | |
if (err) { | |
console.log("\n✘ Error executing npm pack:", err); | |
} | |
else { | |
console.log("\n✔ Bundled package", fullPackageStr); | |
} | |
}); | |
pipeToProcessOutputs(packProcess); | |
}); | |
pipeToProcessOutputs(installProcess); | |
function pipeToProcessOutputs(aChildProcess) { | |
aChildProcess.stderr.pipe(process.stderr); | |
aChildProcess.stdout.pipe(process.stdout); | |
} | |
/* | |
The MIT License (MIT) | |
Copyright (c) 2013 Jack Gill | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
regex not matching packaging containing dashes. [\w/-_] will do the trick.