From 8fede5fc9d01ca9b681507e0311148f55fcf5746 Mon Sep 17 00:00:00 2001 From: masnn Date: Wed, 14 Apr 2021 17:39:24 +0000 Subject: [PATCH] utils: fix findFile --- packages/hydrooj/src/service/server.ts | 2 +- packages/ui-default/backendlib/template.js | 10 ++++++++-- packages/ui-default/build/main.js | 1 + packages/utils/lib/utils.ts | 19 ++++++++++--------- packages/utils/package.json | 2 +- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/hydrooj/src/service/server.ts b/packages/hydrooj/src/service/server.ts index 017d5438c..6b72d8042 100644 --- a/packages/hydrooj/src/service/server.ts +++ b/packages/hydrooj/src/service/server.ts @@ -283,7 +283,7 @@ export const UiContextBase = { function serializer(k: string, v: any) { if (k.startsWith('_') && k !== '_id') return undefined; - if (typeof k === 'bigint') return `BigInt::${v.toString()}`; + if (typeof v === 'bigint') return `BigInt::${v.toString()}`; return v; } diff --git a/packages/ui-default/backendlib/template.js b/packages/ui-default/backendlib/template.js index 4ea3d0c9b..92cdb063d 100644 --- a/packages/ui-default/backendlib/template.js +++ b/packages/ui-default/backendlib/template.js @@ -112,10 +112,16 @@ class Loader extends nunjucks.Loader { } } +const replacer = (k, v) => { + if (k.startsWith('_') && k !== '_id') return undefined; + if (typeof v === 'bigint') return `BigInt::${v.toString()}`; + return v; +}; + class Nunjucks extends nunjucks.Environment { constructor() { super(new Loader(), { autoescape: true, trimBlocks: true }); - this.addFilter('json', (self) => JSON.stringify(self)); + this.addFilter('json', (self) => JSON.stringify(self, replacer)); this.addFilter('parseYaml', (self) => yaml.load(self)); this.addFilter('dumpYaml', (self) => yaml.dump(self)); this.addFilter('xss', (self) => xss.process(self)); @@ -127,7 +133,7 @@ class Nunjucks extends nunjucks.Environment { this.addFilter('base64_encode', (s) => Buffer.from(s).toString('base64')); this.addFilter('base64_decode', (s) => Buffer.from(s, 'base64').toString()); this.addFilter('bitand', (self, val) => self & val); - this.addFilter('toString', (self) => (typeof self === 'string' ? self : JSON.stringify(self))); + this.addFilter('toString', (self) => (typeof self === 'string' ? self : JSON.stringify(self, replacer))); this.addFilter('content', (content, language, html) => { let s = ''; try { diff --git a/packages/ui-default/build/main.js b/packages/ui-default/build/main.js index 12182a5f5..0961a2bf8 100644 --- a/packages/ui-default/build/main.js +++ b/packages/ui-default/build/main.js @@ -17,6 +17,7 @@ function runWebpack({ const server = new WebpackDevServer(compiler, { compress: true, hot: true, + disableHostCheck: true, stats: 'errors-warnings', index: '', proxy: { diff --git a/packages/utils/lib/utils.ts b/packages/utils/lib/utils.ts index 47fbe09d7..bb8252c03 100644 --- a/packages/utils/lib/utils.ts +++ b/packages/utils/lib/utils.ts @@ -319,13 +319,14 @@ export async function findFile(pathname: string, doThrow = true) { if (pkg.startsWith('@')) pkg = `${pkg}/${eles.shift()}`; const rest = eles.join('/'); try { - const p = require.resolve(pkg); + const p = path.dirname(require.resolve(path.join(pkg, 'package.json'))); if (await fs.pathExists(path.resolve(p, rest))) return path.resolve(p, rest); } catch (e) { } } - if (await fs.pathExists(path.resolve(os.homedir(), pathname))) return path.resolve(__dirname, pathname); - if (await fs.pathExists(path.resolve(os.homedir(), '.hydro', pathname))) return path.resolve(__dirname, pathname); - if (await fs.pathExists(path.resolve(os.homedir(), '.config', 'hydro', pathname))) return path.resolve(__dirname, pathname); + if (await fs.pathExists(path.resolve(os.homedir(), pathname))) return path.resolve(os.homedir(), pathname); + if (await fs.pathExists(path.resolve(os.homedir(), '.hydro', pathname))) return path.resolve(os.homedir(), '.hydro', pathname); + // eslint-disable-next-line max-len + if (await fs.pathExists(path.resolve(os.homedir(), '.config', 'hydro', pathname))) return path.resolve(os.homedir(), '.config', 'hydro', pathname); if (doThrow) throw new Error(`File ${pathname} not found`); return null; } @@ -343,13 +344,13 @@ export function findFileSync(pathname: string, doThrow = true) { if (pkg.startsWith('@')) pkg = `${pkg}/${eles.shift()}`; const rest = eles.join('/'); try { - const p = require.resolve(pkg); - if (fs.pathExistsSync(path.resolve(p, rest))) return path.resolve(p, rest); + const p = path.dirname(require.resolve(path.join(pkg, 'package.json'))); + if (fs.statSync(path.resolve(p, rest))) return path.resolve(p, rest); } catch (e) { } } - if (fs.pathExistsSync(path.resolve(os.homedir(), pathname))) return path.resolve(__dirname, pathname); - if (fs.pathExistsSync(path.resolve(os.homedir(), '.hydro', pathname))) return path.resolve(__dirname, pathname); - if (fs.pathExistsSync(path.resolve(os.homedir(), '.config', 'hydro', pathname))) return path.resolve(__dirname, pathname); + if (fs.pathExistsSync(path.resolve(os.homedir(), pathname))) return path.resolve(os.homedir(), pathname); + if (fs.pathExistsSync(path.resolve(os.homedir(), '.hydro', pathname))) return path.resolve(os.homedir(), '.hydro', pathname); + if (fs.pathExistsSync(path.resolve(os.homedir(), '.config', 'hydro', pathname))) return path.resolve(os.homedir(), '.config', 'hydro', pathname); if (doThrow) throw new Error(`File ${pathname} not found`); return null; } diff --git a/packages/utils/package.json b/packages/utils/package.json index 5142b2fef..af0022571 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/utils", - "version": "1.0.2", + "version": "1.0.3", "description": "hydrooj utils", "main": "package.json", "repository": "https://github.com/hydro-dev/Hydro.git",