Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for package.yaml #512

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions lib/find_pkg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve, join, dirname } from 'path';
import { readFile } from 'hexo-fs';
import { readFileSync, existsSync } from 'fs';
import { load } from 'js-yaml';

interface findPkgArgs {
cwd?: string;
Expand All @@ -13,22 +14,18 @@ function findPkg(cwd: string, args: findPkgArgs = {}) {
return checkPkg(cwd);
}

function checkPkg(path: string) {
const pkgPath = join(path, 'package.json');

return readFile(pkgPath).then(content => {
const json = JSON.parse(content as string);
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);
}
async function checkPkg(path: string): Promise<string | null> {
// if a pkg file exists, and the hexo key in it is non-empty
if (readPkg(join(path, 'package.json'), JSON.parse)
|| readPkg(join(path, 'package.yaml'), load)) { return path; }
// otherwise, search in parent dir, terminate at root
const parent = dirname(path);
return parent === path ? null : checkPkg(parent);
}

throw err;
});
/** If pkg file exists, read it, parse it, and access the hexo object in it */
function readPkg(pkgPath: string, parser: CallableFunction): object | null {
return existsSync(pkgPath) ? parser(readFileSync(pkgPath))?.hexo : null;
}

export = findPkg;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"hexo-fs": "^4.1.1",
"hexo-log": "^4.0.1",
"hexo-util": "^3.3.0",
"js-yaml": "^4.1.0",
"minimist": "^1.2.5",
"picocolors": "^1.0.0",
"resolve": "^1.20.0",
Expand All @@ -58,6 +59,7 @@
"@types/bluebird": "^3.5.37",
"@types/chai": "^4.3.14",
"@types/command-exists": "^1.2.3",
"@types/js-yaml": "^4.0.9",
"@types/minimist": "^1.2.5",
"@types/mocha": "^10.0.6",
"@types/node": "^18.11.8",
Expand Down
Loading