From aa0ba91c7d82748d9dfdbbce1c8c9d83676e3de0 Mon Sep 17 00:00:00 2001 From: Jason Mulligan <jason.mulligan@avoidwork.com> Date: Sun, 5 Mar 2017 10:38:33 -0500 Subject: [PATCH] Defaulting `options.cwd` to `process.cwd()`, passing `options.cwd` to `worker.js`, fixing `worker.js` by setting `global.__dirname` to `obj.cwd` on init, really fixes #10 --- .eslintrc | 39 +++++++++++++++++++-------------------- Gruntfile.js | 27 ++++++++++++++++----------- lib/index.js | 18 +++++++++++------- lib/worker.js | 2 +- package.json | 16 ++++++++-------- src/index.js | 8 ++++++-- src/worker.js | 2 +- 7 files changed, 62 insertions(+), 50 deletions(-) diff --git a/.eslintrc b/.eslintrc index fadf09d..b6b08c3 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,21 +1,23 @@ { - "parser": "babel-eslint", - "rules": { - "strict": 0 - }, "env": { - "browser": true, + "browser": false, "node": true, "es6": true, - "amd": true + "amd": false }, "ecmaFeatures": { - "jsx": true, + "jsx": false, "superInFunctions": false, "classes": false, "modules": [2] }, + "globals": { + "self": true, + "postMessage": true + }, "rules": { + "arrow-parens": [2, "as-needed"], + "arrow-spacing": [2, {"before": true, "after": true}], "block-scoped-var": [0], "brace-style": [2, "1tbs", { "allowSingleLine": true }], "camelcase": [0], @@ -33,9 +35,9 @@ "func-names": [0], "func-style": [0, "declaration"], "generator-star-spacing": [2, "after"], - "strict": [2, "always"], "guard-for-in": [0], "handle-callback-err": [0], + "indent": ["error", "tab", {"VariableDeclarator": {"var": 1, "let": 1, "const": 1}, "SwitchCase": 1}], "key-spacing": [2, { "beforeColon": false, "afterColon": true }], "quotes": [2, "double", "avoid-escape"], "max-depth": [0, 4], @@ -46,6 +48,7 @@ "new-parens": [2], "new-cap": [2, { "capIsNewExceptions": ["ToInteger", "ToObject", "ToPrimitive", "ToUint32"] }], "newline-after-var": [0], + "newline-before-return": [2], "no-alert": [2], "no-array-constructor": [2], "no-bitwise": [0], @@ -64,16 +67,14 @@ "no-duplicate-case": [2], "no-else-return": [0], "no-empty": [2], - "no-empty-character-class": [2], - "no-empty-label": [2], "no-eq-null": [0], "no-eval": [2], "no-ex-assign": [2], "no-extend-native": [1], "no-extra-bind": [2], "no-extra-boolean-cast": [2], - "no-extra-parens": [0], "no-extra-semi": [1], + "no-empty-character-class": [2], "no-fallthrough": [2], "no-floating-decimal": [2], "no-func-assign": [2], @@ -97,14 +98,14 @@ "no-negated-in-lhs": [2], "no-nested-ternary": [0], "no-new": [2], - "no-new-func": [2], + "no-new-func": [0], "no-new-object": [2], "no-new-require": [0], "no-new-wrappers": [2], "no-obj-calls": [2], "no-octal": [2], "no-octal-escape": [2], - "no-param-reassign": [2], + "no-param-reassign": [0], "no-path-concat": [0], "no-plusplus": [0], "no-process-env": [0], @@ -113,14 +114,13 @@ "no-redeclare": [2], "no-regex-spaces": [2], "no-reserved-keys": [0], - "no-restricted-modules": [0], + "no-reno-new-funced-modules": [0], "no-return-assign": [2], "no-script-url": [2], "no-self-compare": [0], "no-sequences": [2], "no-shadow": [2], "no-shadow-restricted-names": [2], - "semi-spacing": [2], "no-spaced-func": [2], "no-sparse-arrays": [2], "no-sync": [0], @@ -133,7 +133,7 @@ "no-underscore-dangle": [0], "no-unreachable": [2], "no-unused-expressions": [2], - "no-unused-vars": [1, { "vars": "all", "args": "after-used" }], + "no-unused-vars": [2, { "vars": "all", "args": "after-used" }], "no-use-before-define": [2], "no-void": [0], "no-warning-comments": [0, { "terms": ["todo", "fixme", "xxx"], "location": "start" }], @@ -148,9 +148,9 @@ "semi": [2], "semi-spacing": [2, { "before": false, "after": true }], "sort-vars": [0], - "space-after-keywords": [2, "always"], + "keyword-spacing": [2], "space-before-function-paren": [2, { "anonymous": "always", "named": "always" }], - "space-before-blocks": [0, "always"], + "space-before-blocks": [2, "always"], "space-in-brackets": [0, "never", { "singleValue": true, "arraysInArrays": false, @@ -161,10 +161,9 @@ }], "space-in-parens": [2, "never"], "space-infix-ops": [2], - "space-return-throw-case": [2], "space-unary-ops": [2, { "words": true, "nonwords": false }], "spaced-line-comment": [0, "always"], - "strict": [1], + "strict": [0], "use-isnan": [2], "valid-jsdoc": [0], "valid-typeof": [2], diff --git a/Gruntfile.js b/Gruntfile.js index 0ae1f95..8b67003 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,9 +1,10 @@ module.exports = function (grunt) { grunt.initConfig({ - pkg : grunt.file.readJSON("package.json"), + pkg: grunt.file.readJSON("package.json"), babel: { options: { - sourceMap: false + sourceMap: false, + presets: ["babel-preset-es2015"] }, dist: { files: [{ @@ -16,19 +17,23 @@ module.exports = function (grunt) { } }, eslint: { - target: ["src/*.js"] + target: [ + "Gruntfile.js", + "src/*.js", + "test/*_test.js" + ] }, - nodeunit : { - all : ["test/*_test.js"] + nodeunit: { + all: ["test/*_test.js"] }, - watch : { - js : { - files : ["src/*.js"], - tasks : "default" + watch: { + js: { + files: ["src/*.js"], + tasks: "default" }, pkg: { - files : "package.json", - tasks : "default" + files: "package.json", + tasks: "default" } } }); diff --git a/lib/index.js b/lib/index.js index 3336bd0..5436222 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ "use strict"; -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } @@ -9,18 +9,22 @@ var path = require("path"), worker = path.join(__dirname, "worker.js"), events = /^(error|message)$/; -var Worker = (function () { +var Worker = function () { function Worker(arg) { var _this = this; - var args = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1]; - var options = arguments.length <= 2 || arguments[2] === undefined ? undefined : arguments[2]; + var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; + var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { cwd: process.cwd() }; _classCallCheck(this, Worker); var isfn = typeof arg === "function", input = isfn ? arg.toString() : arg; + if (!options.cwd) { + options.cwd = process.cwd(); + } + this.child = fork(worker, args, options); this.onerror = undefined; this.onmessage = undefined; @@ -33,7 +37,7 @@ var Worker = (function () { this.child.on("message", function (msg) { var message = JSON.parse(msg); - var error = undefined; + var error = void 0; if (!message.error && _this.onmessage) { _this.onmessage.call(_this, message); @@ -47,7 +51,7 @@ var Worker = (function () { } }); - this.child.send({ input: input, isfn: isfn }); + this.child.send({ input: input, isfn: isfn, cwd: options.cwd }); } _createClass(Worker, [{ @@ -70,6 +74,6 @@ var Worker = (function () { }]); return Worker; -})(); +}(); module.exports = Worker; diff --git a/lib/worker.js b/lib/worker.js index 59c5061..c8e5d68 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -43,7 +43,7 @@ process.once("message", function (obj) { } }; - global.__dirname = __dirname; + global.__dirname = obj.cwd; global.__filename = __filename; global.require = require; diff --git a/package.json b/package.json index 146fe80..31a2613 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tiny-worker", - "version": "2.0.1", + "version": "2.0.2", "description": "Tiny WebWorker for Server", "main": "lib/index.js", "scripts": { @@ -23,13 +23,13 @@ }, "homepage": "https://github.com/avoidwork/tiny-worker", "devDependencies": { - "babel-eslint": "^4.1.0", - "grunt": "^0.4.5", - "grunt-babel": "^5.0.0", - "grunt-cli": "^0.1.13", - "grunt-contrib-nodeunit": "^0.4.1", - "grunt-contrib-watch": "^0.6.1", - "grunt-eslint": "^17.1.0" + "babel-preset-es2015": "~6.22.0", + "grunt": "~1.0.1", + "grunt-babel": "~6.0.0", + "grunt-cli": "~1.2.0", + "grunt-contrib-nodeunit": "~1.0.0", + "grunt-contrib-watch": "~1.0.0", + "grunt-eslint": "~19.0.0" }, "dependencies": {} } diff --git a/src/index.js b/src/index.js index 27adfe2..b024d6e 100644 --- a/src/index.js +++ b/src/index.js @@ -4,10 +4,14 @@ const path = require("path"), events = /^(error|message)$/; class Worker { - constructor (arg, args = undefined, options = undefined) { + constructor (arg, args = undefined, options = {cwd: process.cwd()}) { let isfn = typeof arg === "function", input = isfn ? arg.toString() : arg; + if (!options.cwd) { + options.cwd = process.cwd(); + } + this.child = fork(worker, args, options); this.onerror = undefined; this.onmessage = undefined; @@ -34,7 +38,7 @@ class Worker { } }); - this.child.send({input: input, isfn: isfn}); + this.child.send({input: input, isfn: isfn, cwd: options.cwd}); } addEventListener (event, fn) { diff --git a/src/worker.js b/src/worker.js index 478264d..907f620 100644 --- a/src/worker.js +++ b/src/worker.js @@ -41,7 +41,7 @@ process.once("message", obj => { } }; - global.__dirname = __dirname; + global.__dirname = obj.cwd; global.__filename = __filename; global.require = require; -- GitLab