Search the project's root directory.
npm install project-dirmyProject/
├── .git
├── node_modules
├── aaaa
│ └── bbbb
└── abcd
└── efgh
const ProjectDir = require('project-dir');const myProject = new ProjectDir('./', 'node_modules')
console.log(myProject.basedir); // => '/home/user/myProject'
console.log(myProject.wd); // => '/home/user/myProject'const myProject = new ProjectDir('./', '.git')
console.log(myProject.basedir); // => '/home/user/myProject'
console.log(myProject.wd); // => '/home/user/myProject'const myProject = new ProjectDir('./', ['node_modules', '.git'])
console.log(myProject.basedir); // => '/home/user/myProject'
console.log(myProject.wd); // => '/home/user/myProject'console.log(myProject.basename); // => ['node_modules', '.git']
myProject.basename = '.git'
console.log(myProject.basename); // => '.git'myProject.basedir = './abcd'; // => '/home/user/myProject'
myProject.wd; // => '/home/user/myProject/abcd'
myProject.basedir = './abcd/efgh'; // => '/home/user/myProject'
myProject.wd; // => '/home/user/myProject/abcd/efgh'// root
myProject.wd = '/'; // => '/home/user/myProject'
// relative path
myProject.wd = 'abcd'; // => '/home/user/myProject/abcd'
myProject.wd = 'efgh'; // => '/home/user/myProject/abcd/efgh'
myProject.wd = '../'; // => '/home/user/myProject/abcd'
myProject.wd = '/aaaa'; // => '/home/user/myProject/aaaa'myProject.wd = '/';
myProject.resolve('abcd'); // => '/home/user/myProject/abcd'
myProject.wd = '/aaaa';
myProject.resolve('bbbb'); // => '/home/user/myProject/aaaa/bbbb'
myProject.wd = '/aaaa';
myProject.resolve('/abcd'); // => '/home/user/myProject/abcd'myProject.retrieve('/home/user/myProject/abcd'); // => '/abcd'
myProject.retrieve('/home/user/myProject/abcd/efgh'); // => '/abcd/efgh'
myProject.retrieve('/home/user/myProject'); // => '/'
myProject.retrieve('/home/user'); // => 'null'myProject.parse('/abcd');{
root: '/home/user/myProject',
names: '.git',
wd: '/home/user/myProject',
path: 'abcd',
abs: '/abcd',
realPath: '/home/user/myProject/abcd'
}myProject.wd = '/abcd';
myProject.parse('efgh');{
root: '/home/user/myProject',
names: '.git',
wd: '/home/user/myProject/abcd',
path: 'efgh',
abs: '/abcd/efgh',
realPath: '/home/user/myProject/abcd/efgh'
}myProject.wd = '/abcd'
console.log(myProject.wd); // => '/home/user/myProject/abcd'
myProject.equal('/abcd/efgh', 'efgh'); // => true
myProject.equal('/aaaa', '../aaaa'); // => true
myProject.equal('/abcd', 'abcd'); // => falselet paths = [];
myProject.toRoot('/abcd/efgh', (curPath) => paths.push(curPath));
console.log(curPath); // => ['/abcd/efgh', '/abcd']Don't confuse ProjectPath argument. It has a difference meaning with normal path.
When the argument takes ProjectPath, do not use absolute path in real file system.
This moudle handles root as sub directory of base directory when argument is ProjectPath.
eg. If base directory is "/home/user/abcd", the Path's "/efgh" means "/efgh" and the ProjectPath's "/efgh" means "/home/user/abcd/efgh".
constructor(<Path>, <Query>): Search aQuerymatched directory fromPathto root path. and set basedir.
basename <Query>: get/set basename for dominating file.basedir <Path>: get/set project's base directory.wd <Path>: get/set project's working directory.
resolve(<ProjectPath>) -> <Path>: Resolving path based on basedirretrieve(<Path>) -> <ProjectAbsPath>: Retrieving project path from absolute real path.parse(<ProjectPath>) -> <Object>: parsing path based on basedir. below output is result of path(+ sign meanspath.resolve, * sign meanspath.relative, path means argument, others are property name).
{
root: <basedir>,
names: <basename>,
wd: <wd>,
path: <wd*path>,
abs: <basedir*(wd+path)>,
realPath: <wd+path>
}equal(<ProjectPath>, <ProjectPath>) -> <boolean>: Compare with two project path is same.toRoot(<ProjectPath>, [<Object>, ]<Function>): Recursively access fromProjectPathtobasedirand send an argument of current path toFunction.
- mocha : unit test framework
- one-mocha : mocha tester generating module
- js-doc : documentation generator
- minami : jsdoc template
npm test
