You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
656 B
33 lines
656 B
2 years ago
|
'use strict';
|
||
|
|
||
|
const { resolve, join, dirname } = require('path');
|
||
|
const { readFile } = require('hexo-fs');
|
||
|
|
||
|
function findPkg(cwd, args = {}) {
|
||
|
if (args.cwd) {
|
||
|
cwd = resolve(cwd, args.cwd);
|
||
|
}
|
||
|
|
||
|
return checkPkg(cwd);
|
||
|
}
|
||
|
|
||
|
function checkPkg(path) {
|
||
|
const pkgPath = join(path, 'package.json');
|
||
|
|
||
|
return readFile(pkgPath).then(content => {
|
||
|
const json = JSON.parse(content);
|
||
|
if (typeof json.hexo === 'object') return path;
|
||
|
}).catch(err => {
|
||
|
if (err && err.code === 'ENOENT') {
|
||
|
const parent = dirname(path);
|
||
|
|
||
|
if (parent === path) return;
|
||
|
return checkPkg(parent);
|
||
|
}
|
||
|
|
||
|
throw err;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
module.exports = findPkg;
|