github
51 of 60 branches covered (85.0%)
272 of 558 relevant lines covered (48.75%)
1.74 hits per line
1 |
'use strict';
|
|
2 |
|
1✔ |
3 |
const { resolve, join, dirname } = require('path');
|
1✔ |
4 |
const { readFile } = require('hexo-fs');
|
1✔ |
5 |
|
1✔ |
6 |
function findPkg(cwd, args = {}) { |
|
7 |
if (args.cwd) {
|
|
8 |
cwd = resolve(cwd, args.cwd); |
3✔ |
9 |
} |
3✔ |
10 |
|
10✔ |
11 |
return checkPkg(cwd);
|
10✔ |
12 |
} |
10✔ |
13 |
|
1✔ |
14 |
function checkPkg(path) { |
|
15 |
const pkgPath = join(path, 'package.json');
|
18✔ |
16 |
|
18✔ |
17 |
return readFile(pkgPath).then(content => {
|
|
18 |
const json = JSON.parse(content); |
10✔ |
19 |
if (typeof json.hexo === 'object') return path; |
|
20 |
}).catch(err => {
|
|
21 |
if (err && err.code === 'ENOENT') { |
8✔ |
22 |
const parent = dirname(path); |
8✔ |
23 |
|
8✔ |
24 |
if (parent === path) return; |
|
25 |
return checkPkg(parent);
|
8✔ |
26 |
} |
8✔ |
27 |
|
|
28 |
throw err;
|
× |
29 |
}); |
18✔ |
30 |
} |
18✔ |
31 |
|
1✔ |
32 |
module.exports = findPkg; |
1✔ |