├── .gitignore ├── README.md ├── cli.js ├── example.js ├── index.js ├── package.json └── usage.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io 2 | 3 | ### OSX ### 4 | .DS_Store 5 | .AppleDouble 6 | .LSOverride 7 | 8 | # Icon must end with two \r 9 | Icon 10 | 11 | 12 | # Thumbnails 13 | ._* 14 | 15 | # Files that might appear in the root of a volume 16 | .DocumentRevisions-V100 17 | .fseventsd 18 | .Spotlight-V100 19 | .TemporaryItems 20 | .Trashes 21 | .VolumeIcon.icns 22 | 23 | # Directories potentially created on remote AFP share 24 | .AppleDB 25 | .AppleDesktop 26 | Network Trash Folder 27 | Temporary Items 28 | .apdisk 29 | 30 | 31 | ### Node ### 32 | # Logs 33 | logs 34 | *.log 35 | 36 | # Runtime data 37 | pids 38 | *.pid 39 | *.seed 40 | 41 | # Directory for instrumented libs generated by jscoverage/JSCover 42 | lib-cov 43 | 44 | # Coverage directory used by tools like istanbul 45 | coverage 46 | 47 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 48 | .grunt 49 | 50 | # node-waf configuration 51 | .lock-wscript 52 | 53 | # Compiled binary addons (http://nodejs.org/api/addons.html) 54 | build/Release 55 | 56 | # Dependency directory 57 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 58 | node_modules 59 | 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # html-inject-script 2 | 3 | Transform stream for injecting script tags into html. 4 | 5 | ### example: 6 | 7 | ```js 8 | var injectScripts = require('html-inject-script') 9 | 10 | process.stdin 11 | .pipe( injectScripts(['./app.js', './extra.js']) ) 12 | .pipe( process.stdout ) 13 | ``` 14 | 15 | input: 16 | ```html 17 | hello 18 | ``` 19 | output: 20 | ```html 21 | hello 22 | ``` 23 | 24 | ### api 25 | 26 | #### `require('html-inject-script')(scripts[, opts])` 27 | 28 | Injects an array of scripts. Accepts an object of options: 29 | - `selector` (`string`, default: `'head'`): A [hyperstream](https://github.com/substack/hyperstream) selector into which the tags are injected 30 | - `prepend` (`boolean`, default: `true`): If true, prepends. If false, appends. 31 | 32 | ### cli 33 | 34 | ```bash 35 | cat index.html | htmlinjectscript "app.js" > output.html 36 | ``` 37 | 38 | ``` 39 | usage: cat index.html | htmlinjectscript "app.js" > output.html 40 | 41 | Options: 42 | 43 | --body -b, inject into the body element (default: head) 44 | 45 | --selector -s, override head or body with a specific selector 46 | 47 | --append -a, append to selector instead (default: prepend) 48 | 49 | --help, -h display this message 50 | ``` 51 | 52 | ### Gotcha: 53 | requires the tag (head, body, or otherwise) to be present in the src. 54 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var htmlInject = require('./') 4 | var fs = require('fs') 5 | 6 | // omit the first 2 arguments ('node', 'path') 7 | argv = require('minimist')(process.argv.slice(2), { 8 | alias: { 9 | body: 'b', 10 | append: 'a', 11 | selector: 's', 12 | help: 'h' 13 | }, 14 | default: { 15 | body: false, 16 | append: false, 17 | help: false 18 | }, 19 | boolean: ['h', 'a', 'b'] 20 | }) 21 | 22 | // no args - fail 23 | if (!argv._.length) { 24 | console.log('No input arguments specified...') 25 | printUsage() 26 | process.exit(1) 27 | // help 28 | } else if (argv.help) { 29 | printUsage() 30 | // stdin -> transform -> stdout 31 | } else { 32 | process.stdin 33 | .pipe(htmlInject(argv._, { 34 | selector: argv.selector || (argv.body ? 'body' : 'head'), 35 | append: argv.append 36 | })) 37 | .pipe(process.stdout) 38 | } 39 | 40 | function printUsage() { 41 | console.log(fs.readFileSync(__dirname + '/usage.txt', 'utf8')); 42 | } 43 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var htmlInject = require('./') 2 | 3 | 4 | process.stdin // hello 5 | .pipe( htmlInject(['./app.js', './extra.js']) ) 6 | .pipe( process.stdout ) -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var hyperstream = require('hyperstream') 2 | 3 | // this is to avoid the pitfalls of having 4 | // a script closetag in js inlined in html 5 | var SCRIPT = 'script' 6 | var SCRIPT_START = '<'+SCRIPT 7 | var SCRIPT_END = '' 8 | 9 | module.exports = transformHtml 10 | 11 | function transformHtml(externalTags, opts){ 12 | opts = opts || {} 13 | opts.selector = typeof opts.selector === 'undefined' ? 'head' : opts.selector 14 | var args = {} 15 | var op = {} 16 | 17 | op[opts.append ? '_appendHtml' : '_prependHtml'] = externalTags.map(function(tag){ 18 | return SCRIPT_START+' src="'+tag+'">'+SCRIPT_END; 19 | }).join('') 20 | 21 | args[opts.selector] = op 22 | 23 | return hyperstream(args); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "html-inject-script", 3 | "version": "1.1.0", 4 | "description": "", 5 | "main": "index.js", 6 | "bin": { 7 | "htmlinjectscript": "cli.js" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "author": "", 13 | "license": "ISC", 14 | "dependencies": { 15 | "hyperstream": "^1.2.2", 16 | "minimist": "^1.2.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /usage.txt: -------------------------------------------------------------------------------- 1 | usage: cat index.html | htmlinjectscript "app.js" > output.html 2 | 3 | Options: 4 | 5 | --body -b, inject into the body element (default: head) 6 | 7 | --selector -s, override head or body with a specific selector 8 | 9 | --append -a, append to selector instead (default: prepend) 10 | 11 | --help, -h display this message 12 | --------------------------------------------------------------------------------