Skip to content

Commit

Permalink
removed grunt and added eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Idan Attias committed Nov 30, 2017
1 parent ce30f51 commit 96e00cc
Show file tree
Hide file tree
Showing 26 changed files with 236 additions and 262 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "standard",
"rules": {
"node/no-deprecated-api": "off",
"no-var": "error"
}
}
3 changes: 0 additions & 3 deletions .yo-rc.json

This file was deleted.

25 changes: 0 additions & 25 deletions Gruntfile.js

This file was deleted.

112 changes: 53 additions & 59 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,84 @@
"use strict";
var Module = require('module');
var _ = require('lodash');
var path = require('path');
'use strict'
const Module = require('module')
const _ = require('lodash')
const path = require('path')

module.exports = function generateRequireForUserCode(scopedDirs, options) {
options = _.defaults((options || {}), {autoDeleteCache: false});
module.exports = function generateRequireForUserCode (scopedDirs, options) {
options = _.defaults((options || {}), {autoDeleteCache: false})

var forExtensions = Object.keys(require.extensions);
var uniqueIdForThisScopedRequire = _.uniqueId("__dontExtendThisScopedRequire");
scopedDirs = _.map(scopedDirs, function(dir) {return path.resolve(dir)});
const forExtensions = Object.keys(require.extensions)
const uniqueIdForThisScopedRequire = _.uniqueId('__dontExtendThisScopedRequire')
scopedDirs = _.map(scopedDirs, function (dir) { return path.resolve(dir) })

var baseModule = require('./lib/stubmodule-that-does-the-require');
const baseModule = require('./lib/stubmodule-that-does-the-require')
// so that it can be re-used again with another scoped-dir, I delete it from the cache
delete Module._cache[baseModule.id];
delete Module._cache[baseModule.id]
// make relative paths work when requiring
baseModule.filename = path.resolve(scopedDirs[0], 'stubmodule-that-does-the-require.js');
baseModule.__scopedRequireModule = true;
baseModule.filename = path.resolve(scopedDirs[0], 'stubmodule-that-does-the-require.js')
baseModule.__scopedRequireModule = true

function inUserCodeDirs(modulePath) {
return _.some(scopedDirs, function(userCodeDir) {return modulePath.indexOf(userCodeDir) >= 0});
function inUserCodeDirs (modulePath) {
return _.some(scopedDirs, function (userCodeDir) { return modulePath.indexOf(userCodeDir) >= 0 })
}

function adjustPaths(m) {
m.paths = _.filter(m.paths.concat(scopedDirs), function(modulePath) { return inUserCodeDirs(modulePath); });
function adjustPaths (m) {
m.paths = _.filter(m.paths.concat(scopedDirs), function (modulePath) { return inUserCodeDirs(modulePath) })
}

adjustPaths(baseModule);
adjustPaths(baseModule)

_.forEach(forExtensions, function(ext) {
var original = require.extensions[ext];
if (original && original[uniqueIdForThisScopedRequire])
return;
_.forEach(forExtensions, function (ext) {
const original = require.extensions[ext]
if (original && original[uniqueIdForThisScopedRequire]) { return }

require.extensions[ext] = function requireThatAddsUserCodeDirs(m, filename) {
if ((!m.parent && inUserCodeDirs(m.filename)) ||
m.parent && m.parent.__scopedRequireModule && inUserCodeDirs(m.filename)) {
m.__scopedRequireModule = true;
adjustPaths(m);
require.extensions[ext] = function requireThatAddsUserCodeDirs (m, filename) {
if (((!m.parent && inUserCodeDirs(m.filename)) ||
(m.parent && m.parent.__scopedRequireModule)) && inUserCodeDirs(m.filename)) {
m.__scopedRequireModule = true
adjustPaths(m)
}

return original(m, filename);
};
Object.defineProperty(require.extensions[ext], uniqueIdForThisScopedRequire, {value: true});
});
return original(m, filename)
}
Object.defineProperty(require.extensions[ext], uniqueIdForThisScopedRequire, {value: true})
})

function deleteModuleFromCache(m) {
delete Module._cache[m.id];
function deleteModuleFromCache (m) {
delete Module._cache[m.id]
_.forEach(m.children, function (subModule) {
deleteModuleFromCache(subModule);
});
m.children = [];
deleteModuleFromCache(subModule)
})
m.children = []
}

return {
require: !options.autoDeleteCache ?
baseModule.require.bind(baseModule) :
function(path) {
var moduleExports = baseModule.require.apply(baseModule, arguments);
require: !options.autoDeleteCache
? baseModule.require.bind(baseModule)
: function (path) {
const moduleExports = baseModule.require.apply(baseModule, arguments)

deleteModuleFromCache(baseModule);
deleteModuleFromCache(baseModule)

return moduleExports;
return moduleExports
},
scopedDirs: scopedDirs,
clearCache: function () {
deleteModuleFromCache(baseModule);
deleteModuleFromCache(baseModule)
},
loadCodeAsModule: function(code, filename) {
if (filename && Module._cache[filename])
return Module._cache[filename];

var module = new Module(filename, baseModule);
module.filename = filename;
module.paths = baseModule.paths;
module.__scopedRequireModule = true;
loadCodeAsModule: function (code, filename) {
if (filename && Module._cache[filename]) { return Module._cache[filename] }

module._compile(code, module.filename || "filename-to-make-node6-happy");
const module = new Module(filename, baseModule)
module.filename = filename
module.paths = baseModule.paths
module.__scopedRequireModule = true

baseModule.children.push(module);
if (options.autoDeleteCache)
deleteModuleFromCache(baseModule);
else if (filename && !options.autoDeleteCache)
Module._cache[filename] = module;
module._compile(code, module.filename || 'filename-to-make-node6-happy')

baseModule.children.push(module)
if (options.autoDeleteCache) { deleteModuleFromCache(baseModule) } else if (filename && !options.autoDeleteCache) { Module._cache[filename] = module }

return module.exports;
return module.exports
}
}
};
}
2 changes: 1 addition & 1 deletion lib/stubmodule-that-does-the-require.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = module;
module.exports = module
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@
"user-code"
],
"dependencies": {
"lodash": "^3.1.0"
"lodash": "^4.17.4"
},
"devDependencies": {
"grunt-cli": "^0.1.13",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-contrib-watch": "^0.6.1",
"load-grunt-tasks": "^3.1.0",
"time-grunt": "^1.0.0",
"grunt-mocha-cli": "^1.11.0",
"jshint-stylish": "^1.0.0"
"eslint": "^4.12.1",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"mocha": "^4.0.1",
"mocha-env-reporter": "^3.0.0"
},
"scripts": {
"test": "grunt"
"test": "mocha --reporter mocha-env-reporter 'test/test.js'",
"posttest": "npm run lint",
"lint": "eslint index.js lib test"
}
}
6 changes: 3 additions & 3 deletions test/scoped-dir-2/scoped-module-2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.scopedFunction2 = function() {
return "scopedString2";
};
exports.scopedFunction2 = function () {
return 'scopedString2'
}
8 changes: 4 additions & 4 deletions test/scoped-dir/afolder/asubmodule.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var subModule2 = require('afolder/asubmodule2');
const subModule2 = require('afolder/asubmodule2')

module.exports = function subfunction() {
return "substring and " + subModule2();
};
module.exports = function subfunction () {
return 'substring and ' + subModule2()
}
6 changes: 3 additions & 3 deletions test/scoped-dir/afolder/asubmodule2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function subfunction() {
return "substring2";
};
module.exports = function subfunction () {
return 'substring2'
}
2 changes: 1 addition & 1 deletion test/scoped-dir/afolder/circular-ref-1.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('./circular-ref-2');
require('./circular-ref-2')
4 changes: 2 additions & 2 deletions test/scoped-dir/afolder/circular-ref-2.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require('./circular-ref-1');
global.moduleLoadSideEffect++;
require('./circular-ref-1')
global.moduleLoadSideEffect++
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function() {
return "1 and " + require('submodule-importing-from-scope2')();
};
module.exports = function () {
return '1 and ' + require('submodule-importing-from-scope2')()
}
2 changes: 1 addition & 1 deletion test/scoped-dir/module-whose-load-side-effects-2.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global.moduleLoadSideEffect++;
global.moduleLoadSideEffect++
2 changes: 1 addition & 1 deletion test/scoped-dir/module-whose-load-side-effects-3.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global.moduleLoadSideEffect3++;
global.moduleLoadSideEffect3++
2 changes: 1 addition & 1 deletion test/scoped-dir/module-whose-load-side-effects-4.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global.moduleLoadSideEffect4++;
global.moduleLoadSideEffect4++
2 changes: 1 addition & 1 deletion test/scoped-dir/module-whose-load-side-effects.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global.moduleLoadSideEffect++;
global.moduleLoadSideEffect++
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports.submodule = require('submodule-whose-load-side-effects');
module.exports.submodule = require('submodule-whose-load-side-effects')
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports.submodule = require('afolder/circular-ref-1');
module.exports.submodule = require('afolder/circular-ref-1')
2 changes: 1 addition & 1 deletion test/scoped-dir/node_modules/anNpmModule/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/scoped-dir/scoped-module-that-imports-relatively.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var subModule = require('./afolder/asubmodule');
const subModule = require('./afolder/asubmodule')

module.exports.aFunction = function() {
return "astring and " + subModule();
};
module.exports.aFunction = function () {
return 'astring and ' + subModule()
}
8 changes: 4 additions & 4 deletions test/scoped-dir/scoped-module-that-imports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var subModule = require('afolder/asubmodule');
const subModule = require('afolder/asubmodule')

module.exports.aFunction = function() {
return "astring and " + subModule();
};
module.exports.aFunction = function () {
return 'astring and ' + subModule()
}
8 changes: 4 additions & 4 deletions test/scoped-dir/scoped-module-that-uses-lodash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _ = require('lodash');
const _ = require('lodash')

exports.scopedFunction = function() {
return _.camelCase("Scoped Camel");
};
exports.scopedFunction = function () {
return _.camelCase('Scoped Camel')
}
4 changes: 2 additions & 2 deletions test/scoped-dir/scoped-module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const scopedString = require('anNpmModule')

exports.scopedFunction = function() {
return scopedString;
exports.scopedFunction = function () {
return scopedString
}
6 changes: 3 additions & 3 deletions test/scoped-dir/submodule-importing-from-scope2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function() {
return "2 and " + require('scoped-module-2').scopedFunction2();
};
module.exports = function () {
return '2 and ' + require('scoped-module-2').scopedFunction2()
}
2 changes: 1 addition & 1 deletion test/scoped-dir/submodule-whose-load-side-effects.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global.moduleLoadSideEffect++;
global.moduleLoadSideEffect++
Loading

0 comments on commit 96e00cc

Please sign in to comment.