-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (32 loc) · 795 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var path = require('path');
var assert = require('assert');
/**
* Prepare string that contains path for require()
*
* @example
* ```js
* var requiredPath = require('required-path');
* var file = 'file.js';
*
* try {
* require(file);
* } catch(err) {
* err; → // "Error: Cannot find module 'file.js'"
* }
*
* var prepared = requiredPath(file);
* require(prepared); → // now it's okay
* ```
* @public
* @param {String} pathStr - path that will be prepared for require
* @returns {String}
*/
module.exports = function(pathStr) {
assert(typeof(pathStr) === 'string', 'Provide path as String.');
if (path.isAbsolute(pathStr)) {
return pathStr;
} else {
// we don't need path.sep check tests in windows
return './' + pathStr;
}
};