Skip to content

Commit

Permalink
Pass start dir to function (#2)
Browse files Browse the repository at this point in the history
* Fork from ebdrup/moduleconfig
Accept startDir in function arguments to fix caching issues

* main function takes object as parameter
  • Loading branch information
m-kusnierz authored Aug 17, 2023
1 parent ba2d70b commit ac8ff93
Show file tree
Hide file tree
Showing 15 changed files with 2,771 additions and 66 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"esversion": 6,
"asi" : false,
"bitwise" : true,
"boss" : false,
Expand Down
4 changes: 4 additions & 0 deletions .mocharc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require:
- ./test/common.js
ui: bdd
recursive: true
6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ loading configuration files for node modules.
If you are building a node module that needs a configuration file, that the user of the module creates,
you have come to the right place.

[![Build Status](https://travis-ci.org/ebdrup/moduleconfig.png)](https://travis-ci.org/ebdrup/moduleconfig)

[![NPM version](https://badge.fury.io/js/moduleconfig.png)](http://badge.fury.io/js/moduleconfig)

[![Dependency Status](https://gemnasium.com/ebdrup/moduleconfig.png)](https://gemnasium.com/ebdrup/moduleconfig)

You are building a module `MyModule` with a user config file
------------------------------------------------

Expand Down Expand Up @@ -40,7 +34,7 @@ Use `moduleconfig` in the module `MyModule`

MyModule.js is the main file pointed to by the `package.json` in the `MyModule` module
```js
var moduleConfig = require("moduleconfig");
const moduleConfig = require("moduleconfig");
module.exports = moduleConfig(["config.js"], function(configFilePath){
return instantiateMyModulefromConfig(require(configFilePath));
});
Expand All @@ -51,7 +45,7 @@ You will have to implement the `instantiateMyModulefromConfig` function yourself

This will mean that this:
```
var myModule = require("MyModule");
const myModule = require("MyModule");
```

will return an `myModule` based on `Main/node_modules/ExternalModule/config.js` when `MyModule` is required
Expand Down
12 changes: 6 additions & 6 deletions lib/getConfigPath.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";
var path = require("path");
var fs = require("fs");
var existsSync = fs.existsSync || path.existsSync;
const path = require("path");
const fs = require("fs");
const existsSync = fs.existsSync || path.existsSync;

module.exports = function getErrorConfigFilePath(startDir, paths) {
var dir = startDir;
var lastDir;
let dir = startDir;
let lastDir;
while (lastDir !== dir) {
for (var i = 0; i < paths.length; i++) {
for (let i = 0; i < paths.length; i++) {
if (existsSync(path.join(dir, paths[i]))) {
return path.join(dir, paths[i]);
}
Expand Down
21 changes: 11 additions & 10 deletions lib/moduleConfig.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
"use strict";
delete require.cache[__filename]; //do not cache in require cache
delete require.cache[module.parent.filename]; //do not cache in require cache
var cache = require("./cache"); //use requires caching to have a singleton
var getConfigPath = require("./getConfigPath"); //use requires caching to have a singleton
var path = require("path");
const cache = require("./cache"); //use requires caching to have a singleton
const getConfigPath = require("./getConfigPath"); //use requires caching to have a singleton
const path = require("path");

module.exports = function moduleConfig(paths, loadPathFunction) {
var startDir = path.dirname(module.parent.parent.filename);
var configPath;
var pathsId = paths.join(",");
module.exports = function moduleConfig({ paths, loadPathFunction, startDir }) {
startDir = startDir || path.dirname(module.parent.parent.filename);

let configPath;
const pathsId = paths.join(",");

// add the paths to the paths cache
var found = false;
var pathsIndex = cache.paths.length; // set the index to the next available location
for (var p = 0; p < cache.paths.length; p++) {
let found = false;
let pathsIndex = cache.paths.length; // set the index to the next available location
for (let p = 0; p < cache.paths.length; p++) {
// check to see if this paths was already cached
if (pathsId === cache.paths[p]) {
// this was already cached, so use its already cached data
Expand Down
Loading

0 comments on commit ac8ff93

Please sign in to comment.