├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.12 4 | - 4 5 | - 6 6 | - 7 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Daniel Wirtz All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of its author, nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | harmonize 2 | ========= 3 | [![Travis](https://img.shields.io/travis/dcodeIO/node-harmonize.svg)](https://travis-ci.org/dcodeIO/node-harmonize) [![npm](https://img.shields.io/npm/v/harmonize.svg)](https://www.npmjs.com/package/harmonize) 4 | 5 | Enables `--harmony` flags programmatically. 6 | 7 | Usage 8 | ----- 9 | 10 | Enabling just the `--harmony` flag: 11 | 12 | ```js 13 | require("harmonize")(); 14 | ``` 15 | 16 | Enabling specific features: 17 | 18 | ```js 19 | require("harmonize")([ 20 | "harmony", 21 | "harmony_sharedarraybuffer" 22 | ]); 23 | ``` 24 | 25 | Note that unsupported flags are simply ignored. 26 | 27 | How it works 28 | ------------ 29 | 30 | ```js 31 | var harmonize = require("harmonize"); 32 | // ^ Transparently spawns another node process with --v8-options and 33 | // parses enabled and supported harmony flags. You can also inspect 34 | // these: console.log(harmonize.enabled, harmonize.supported); 35 | 36 | harmonize([ "harmony", ... ]); 37 | // ^ Interrupts process flow within the parent and starts a new process 38 | // with the harmony flags you provided. 39 | 40 | // Everything below is executed within the harmonized child only. 41 | ``` 42 | 43 | Quirks 44 | ------ 45 | While no code below the call to `harmonize()` is executed within the 46 | parent, it must still be parseable without any additional flags. 47 | 48 | For example, if you are enabling generators which aren't supported by 49 | your node version without the respective flag, using generators syntax 50 | within the main file will result in a parse error. In such cases, just 51 | move code that requires a flag into a separate file and `require` it 52 | instead below `harmonize()`, which will prevent the parse error. 53 | 54 | **License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause) 55 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | module.exports = harmonize; 3 | 4 | var child_process = require("child_process"); 5 | 6 | /** 7 | * Enables --harmony flags programmatically. 8 | * @param {Array.} [flags] Flags to enable, defaults to just `harmony` 9 | * @returns {undefined} 10 | * @property {Array.} enabled Enabled flags 11 | * @property {Array.} supported Supported flags 12 | */ 13 | function harmonize(flags) { 14 | 15 | // assume to be the spawned child if there are any enabled flags 16 | if (harmonize.enabled.length) 17 | return; 18 | 19 | // default to just "harmony" 20 | if (!Array.isArray(flags)) 21 | flags = [ "harmony" ]; 22 | 23 | // filter out unsupported flags while adding hyphens 24 | for (var i = 0; i < flags.length;) { 25 | if (harmonize.supported.indexOf(flags[i]) < 0) 26 | flags.splice(i, 1); 27 | else 28 | flags[i] = "--" + flags[i++]; 29 | } 30 | 31 | // now just spawn a child with inherited stdio using the specified flags 32 | child_process.spawn(process.argv[0], flags.concat(process.argv.slice(1)), { stdio: 'inherit' }) 33 | .on("close", function(code) { 34 | process.exit(code); 35 | }); 36 | 37 | // and interrupt process flow in the parent (do not try this at home!) 38 | process.once("uncaughtException", function(e) {}); 39 | throw "harmony"; 40 | }; 41 | 42 | // Get enabled flags 43 | harmonize.enabled = (function() { 44 | var flags = []; 45 | for (var i = 0, match; i < process.execArgv.length; ++i) 46 | if (match = /\-\-(harmony[\w\-]*)/.exec(process.execArgv[i])) 47 | flags.push(match[1]); 48 | return flags; 49 | })(); 50 | 51 | // Get supported flags 52 | harmonize.supported = (function() { 53 | var flags = []; 54 | var output = child_process.spawnSync(process.argv[0], ["--v8-options"]).output.toString("utf8"); 55 | for (var re = /\-\-(harmony[\w\-]*)/g, match; match = re.exec(output);) 56 | flags.push(match[1]); 57 | return flags; 58 | })(); 59 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "harmonize", 3 | "description": "Enables --harmony flags programmatically.", 4 | "author": "Daniel Wirtz", 5 | "version": "2.0.1", 6 | "main": "index", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/dcodeIO/node-harmonize.git" 10 | }, 11 | "bugs": { 12 | "url": "https://github.com/dcodeIO/node-harmonize/issues" 13 | }, 14 | "keywords": ["node", "harmony", "es6"], 15 | "engines": { 16 | "node": ">=0.12" 17 | }, 18 | "scripts": { 19 | "test": "node test" 20 | }, 21 | "license": "BSD-3-Clause" 22 | } -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var harmonize = require("./index"); 3 | if (!harmonize.enabled.length) 4 | console.log("IN PARENT"); 5 | else 6 | console.log("IN CHILD"); 7 | harmonize(); 8 | 9 | // code above is executed for both the parent and the child 10 | // code below is executed within the child only 11 | 12 | assert.deepEqual(harmonize.enabled, ["harmony"], "should enable --harmony"); 13 | assert.ok(harmonize.supported, "should grep supported flags"); 14 | 15 | console.log("-- enabled flags --"); 16 | console.log(harmonize.enabled); 17 | console.log("-- supported flags --"); 18 | console.log(harmonize.supported); 19 | --------------------------------------------------------------------------------