Created
March 15, 2019 01:33
-
-
Save rwilcox/7649e12d71345a1cd4acb6eb82f63a35 to your computer and use it in GitHub Desktop.
Ever wanted to just give someone a single .js file, but also wanted to pull in a Node module? Fret no more!
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
#!/usr/bin/env node | |
/* | |
Node script are kind of cool, but they are hard to low-tech write a Node script for someone and give it to them. | |
Sure you can publish a package to NPM, set the binary flag, go through some house keeping... but sometimes that's just | |
not worth the effort. | |
So: can you distribute a Node script that will install it's own modules from a single file? | |
Yes: with PERL. | |
Steps: | |
1. Declare modules to install with single line comments + two dollar signs. This is shell script code executed | |
on the user's machine | |
2. Test your require statements in some kind of try / catch block. On catch output a shell command the user | |
should run. | |
This is not as slick as Groovy's Grab (http://docs.groovy-lang.org/latest/html/documentation/grape.html), but | |
it makes NPM scripts a single file install (assume you have PERL installed.) | |
*/ | |
// ThIRD PARTY MODULES YOU SHOULD INSTALL OR USE MY HANDY SCRIPT TO | |
// ============================================================================ | |
// $$ npm install lodash | |
// $$ npm install async | |
// ============================================================================ | |
const process = require('process') | |
try { | |
require('lodash') | |
require('async') | |
} catch (err) { | |
console.log("# modules may not be installed. To auto install these modules execute the following shell script") | |
console.log("cat standalone.js | perl -e 'while(<>) { if (/\\/\\/\\W?\\$\\$(.+)/) { print($1); print(system($1)) }}'") | |
process.exit() | |
} | |
const _ = require('lodash') | |
_.forEach( ["hello", "world"], (e) => console.log(e) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment