Skip to content

Commit a7ec7c9

Browse files
committed
location: prevent creating a database in a non-database file
by refusing to use the current directory if no LevelDB database exists already unless the path is passed explicitly
1 parent 9dee398 commit a7ec7c9

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/location.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,28 @@
44
* tries to find the location of the database.
55
*
66
*/
7+
78
module.exports = function (argv) {
89

910
var location = typeof argv.location === 'string';
10-
argv.path = location && argv.location || argv._[0] || process.cwd();
11+
argv.path = location && argv.location || argv._[0];
12+
if (!argv.path) {
13+
if (cwdIsADatabase()) {
14+
argv.path = process.cwd();
15+
}
16+
else {
17+
console.error('no database found');
18+
return process.exit(1);
19+
};
20+
};
21+
};
22+
23+
function cwdIsADatabase () {
24+
try {
25+
var CURRENT = require('fs').readFileSync('./CURRENT').toString();
26+
return CURRENT.split('-')[0] === 'MANIFEST';
27+
} catch (err) {
28+
return false;
29+
};
1130
};
1231

0 commit comments

Comments
 (0)