From 96111bdc677e8b2c7e1ce585847d9dc19e6557a6 Mon Sep 17 00:00:00 2001 From: Jerome Simeon Date: Wed, 16 Oct 2019 15:15:39 -0400 Subject: [PATCH] refactor(module) Remove dead code for load modules Signed-off-by: Jerome Simeon --- package-lock.json | 8 -- .../concerto-core/lib/module/loadModule.js | 84 ------------------- packages/concerto-core/package.json | 1 - packages/concerto-core/test/module/module.js | 65 -------------- .../concerto-core/test/module/testmodule.js | 20 ----- 5 files changed, 178 deletions(-) delete mode 100644 packages/concerto-core/lib/module/loadModule.js delete mode 100644 packages/concerto-core/test/module/module.js delete mode 100644 packages/concerto-core/test/module/testmodule.js diff --git a/package-lock.json b/package-lock.json index 8bbf22922a..9f9961bd41 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8712,14 +8712,6 @@ "npm-bundled": "^1.0.1" } }, - "npm-paths": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/npm-paths/-/npm-paths-2.0.0.tgz", - "integrity": "sha512-8H6F86qZ5O/xc+vNAxEni4baIhxtYM+HrrSXt5GCOf8fLOIhuIFWrTO8+41nrPbSw3QRk8pSO/W4xVPxiBQTKw==", - "requires": { - "global-modules": "^2.0.0" - } - }, "npm-pick-manifest": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/npm-pick-manifest/-/npm-pick-manifest-3.0.2.tgz", diff --git a/packages/concerto-core/lib/module/loadModule.js b/packages/concerto-core/lib/module/loadModule.js deleted file mode 100644 index 07bea1b397..0000000000 --- a/packages/concerto-core/lib/module/loadModule.js +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - - -const fs = require('fs'); -const path = require('path'); -const debug = require('debug')('concerto:LoadModule'); - -/** - * Helper class to support loading NPM modules - * @private - */ -class LoadModule{ - - /** - * Path to be searched is that returned from 'npm-paths' in addition to any specified in the options - * Immediate node_modules subdirectories will searched as well.. (i.e. if any dir in the search path doesn't - * end with node_modules, node_modules will be added to the dir and search in-addition) - * @private - * - * @param {String} nameToLoad name of the module to load - * @param {Object} options options to use - * @param {String[]} options.paths Array of additional paths to search (node_modules subdirectories will be searched as well. ) - * @param {boolean} options.errorNotFound Should an error be thrown on not found, otherwise resolves with null (default) - * @return {Object} resolved with the value of require(..). Or null if errorNotFound set. - */ - static loadModule(nameToLoad,options = {}) { - // put this in the fn to avoid webpkacing issues - const npmpaths = require('npm-paths'); - let finalLocation; - let searchPath; - if (options.paths){ - searchPath = npmpaths().concat(options.paths); - }else { - searchPath = npmpaths(); - } - debug('loadModule',`${nameToLoad} -- ${searchPath} -- ${options.paths}`); - for (let p of searchPath) { - // add on node_modules - - if (path.basename(p) !== 'node_modules'){ - searchPath.push(path.join(p,'node_modules')); - } - - let p2 = path.resolve(p, nameToLoad); - debug('loadModule',`checking path ${p2}`); - let found = fs.existsSync(p2); - if (found){ - finalLocation = p2; - break; - } - } - - if (finalLocation){ - debug('loadModule',`Loading ${nameToLoad} from ${finalLocation}`); - const req = require; - let module = req(finalLocation); - return module; - } else { - if (options.errorNotFound && options.errorNotFound===true){ - let err = new Error(`Unable to load ${nameToLoad} from ${searchPath}`); - throw err; - } else { - debug(`Unable to load ${nameToLoad} from ${searchPath}`); - return null; - } - } - } - -} -module.exports = LoadModule; \ No newline at end of file diff --git a/packages/concerto-core/package.json b/packages/concerto-core/package.json index 4ad3314c15..7c7e5092f0 100644 --- a/packages/concerto-core/package.json +++ b/packages/concerto-core/package.json @@ -68,7 +68,6 @@ "jsome": "2.5.0", "lorem-ipsum": "1.0.6", "moment-mini": "2.22.1", - "npm-paths": "2.0.0", "triple-beam": "1.3.0", "urijs": "1.19.1", "uuid": "3.3.2", diff --git a/packages/concerto-core/test/module/module.js b/packages/concerto-core/test/module/module.js deleted file mode 100644 index d8f5660639..0000000000 --- a/packages/concerto-core/test/module/module.js +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const LoadModule = require('../../lib/module/loadModule'); -const util = require('util'); -// const Chalk = require('chalk'); - -const chai = require('chai'); -const chaiAsPromised = require('chai-as-promised'); -chai.use(chaiAsPromised); -chai.should(); -chai.use(require('chai-things')); -const expect = chai.expect; - -const sinon = require('sinon'); - -describe('LoadModule', function() { - - - let sandbox; - beforeEach(() => { - sandbox = sinon.createSandbox(); - }); - - afterEach(() => { - sandbox.restore(); - }); - - describe('loadModule', function() { - it('should correctly module using just npmpaths', () => { - let m = LoadModule.loadModule('chalk'); - util.inspect(m).should.match(/Chalk/); - }); - it('should correctly module in the additional paths', () => { - let m = LoadModule.loadModule('testmodule.js',{paths:['.',__dirname]}); - m.sayHello().should.equal('hello'); - }); - it('should correctly return null if not found', () => { - let m = LoadModule.loadModule('wibble.js',{paths:['.',__dirname]}); - expect(m).to.be.null; - }); - it('should correctly return error if not found', () => { - (()=>{ - LoadModule.loadModule('wibble.js',{errorNotFound:true}); - }).should.throw(/Unable to load/); - - }); - }); - - - -}); diff --git a/packages/concerto-core/test/module/testmodule.js b/packages/concerto-core/test/module/testmodule.js deleted file mode 100644 index 7fc6be1e07..0000000000 --- a/packages/concerto-core/test/module/testmodule.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -'use strict'; - -// very simple module used soley for unit testing - see module.js for use - -module.exports.sayHello = function() { - return 'hello'; -}; \ No newline at end of file