Initial commit

master
Sven Slootweg 8 years ago
parent 7236ba69cd
commit b139231241

@ -0,0 +1 @@
/node_modules/

@ -0,0 +1,29 @@
# get-project-root
TODO
## License
[WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), whichever you prefer. A donation and/or attribution are appreciated, but not required.
## Donate
My income consists largely of donations for my projects. If this module is useful to you, consider [making a donation](http://cryto.net/~joepie91/donate.html)!
You can donate using Bitcoin, PayPal, Flattr, cash-in-mail, SEPA transfers, and pretty much anything else.
## Contributing
Pull requests welcome. Please make sure your modifications are in line with the overall code style, and ensure that you're editing the files in `src/`, not those in `lib/`.
Build tool of choice is `gulp`; simply run `gulp` while developing, and it will watch for changes.
Be aware that by making a pull request, you agree to release your modifications under the licenses stated above.
## Usage
TODO
## API
TODO

@ -0,0 +1,18 @@
var gulp = require("gulp");
var presetES2015 = require("@joepie91/gulp-preset-es2015");
var source = ["src/**/*.js"]
gulp.task('babel', function() {
return gulp.src(source)
.pipe(presetES2015({
basePath: __dirname
}))
.pipe(gulp.dest("lib/"));
});
gulp.task("watch", function () {
gulp.watch(source, ["babel"]);
});
gulp.task("default", ["babel", "watch"]);

@ -0,0 +1,3 @@
'use strict';
module.exports = require("./lib");

@ -0,0 +1,28 @@
{
"name": "get-project-root",
"version": "0.0.1",
"description": "Locates the 'root directory' of the current project",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "http://git.cryto.net/joepie91/node-get-project-root.git"
},
"keywords": [
"root",
"package.json",
"path"
],
"author": "Sven Slootweg",
"license": "WTFPL",
"dependencies": {
"default-value": "0.0.2"
},
"devDependencies": {
"@joepie91/gulp-preset-es2015": "^1.0.1",
"babel-preset-es2015": "^6.6.0",
"gulp": "^3.9.1"
}
}

@ -0,0 +1,82 @@
'use strict';
const fs = require("fs");
const path = require("path");
const defaultValue = require("default-value");
let defaultCheckers = {
"package.json": function(directory) {
let parentPackageFile = path.join(directory, `..${path.sep}..${path.sep}package.json`);
try {
fs.accessSync(parentPackageFile);
return false;
} catch (err) {
return true;
}
},
"vcs": function(directory) {
let files = fs.readdirSync(directory);
return [".git", ".hg"].some((target) => {
return (files.indexOf(target) !== -1);
});
}
}
function reachedRoot(candidatePath) {
/* Returns true if we hit the root of the filesystem; like / or C:\ */
return /^(?:\/|a-z:\\)$/i.test(candidatePath);
}
module.exports = function(options) {
let currentPath = defaultValue(options.basePath, __dirname);
let checker = defaultValue(options.checker, "package.json");
if (typeof checker === "string") {
if (defaultCheckers[checker] != null) {
checker = defaultCheckers[checker];
} else {
throw new Error("No such checker exists.");
}
}
/* Locate the first thing that has a package.json, in case we're in a
* subdirectory within a module. Only after we've found the nearest
* package.json, we will start using the checker.
*/
let baseFound = false;
while (baseFound === false) {
baseFound = (fs.readdirSync(currentPath).indexOf("package.json") !== -1);
if (reachedRoot(currentPath)) {
throw new Error("Reached filesystem root without encountering a module");
}
if (baseFound === false) {
currentPath = path.join(currentPath, "..");
}
}
let rootFound = false;
while (rootFound === false) {
rootFound = checker(currentPath);
if (rootFound !== true && rootFound !== false) {
throw new Error("Checker must always return either true or false");
}
if (reachedRoot(currentPath)) {
throw new Error("Reached filesystem root without encountering the project root");
}
if (rootFound === false) {
currentPath = path.join(currentPath, `..${path.sep}..`);
}
}
return currentPath;
}

@ -0,0 +1,8 @@
'use strict';
const getProjectRoot = require("./");
console.log(getProjectRoot({
basePath: process.argv[2],
checker: "package.json"
}));
Loading…
Cancel
Save