utils: fix findFile

This commit is contained in:
masnn committed 2021-04-14 17:39:24 +00:00
1 parent 90cdc163ca
commit 8fede5fc9d
5 files changed
+21 -13

No files matched your search

+1 -1
View File
@@ -283,7 +283,7 @@ export const UiContextBase = {
function serializer(k: string, v: any) { function serializer(k: string, v: any) {
if (k.startsWith('_') && k !== '_id') return undefined; 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; return v;
} }
+8 -2
View File
@@ -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 { class Nunjucks extends nunjucks.Environment {
constructor() { constructor() {
super(new Loader(), { autoescape: true, trimBlocks: true }); 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('parseYaml', (self) => yaml.load(self));
this.addFilter('dumpYaml', (self) => yaml.dump(self)); this.addFilter('dumpYaml', (self) => yaml.dump(self));
this.addFilter('xss', (self) => xss.process(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_encode', (s) => Buffer.from(s).toString('base64'));
this.addFilter('base64_decode', (s) => Buffer.from(s, 'base64').toString()); this.addFilter('base64_decode', (s) => Buffer.from(s, 'base64').toString());
this.addFilter('bitand', (self, val) => self & val); 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) => { this.addFilter('content', (content, language, html) => {
let s = ''; let s = '';
try { try {
+1
View File
@@ -17,6 +17,7 @@ function runWebpack({
const server = new WebpackDevServer(compiler, { const server = new WebpackDevServer(compiler, {
compress: true, compress: true,
hot: true, hot: true,
disableHostCheck: true,
stats: 'errors-warnings', stats: 'errors-warnings',
index: '', index: '',
proxy: { proxy: {
+10 -9
View File
@@ -319,13 +319,14 @@ export async function findFile(pathname: string, doThrow = true) {
if (pkg.startsWith('@')) pkg = `${pkg}/${eles.shift()}`; if (pkg.startsWith('@')) pkg = `${pkg}/${eles.shift()}`;
const rest = eles.join('/'); const rest = eles.join('/');
try { 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); if (await fs.pathExists(path.resolve(p, rest))) return path.resolve(p, rest);
} catch (e) { } } catch (e) { }
} }
if (await fs.pathExists(path.resolve(os.homedir(), 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(__dirname, pathname); if (await fs.pathExists(path.resolve(os.homedir(), '.hydro', pathname))) return path.resolve(os.homedir(), '.hydro', pathname);
if (await fs.pathExists(path.resolve(os.homedir(), '.config', 'hydro', pathname))) return path.resolve(__dirname, 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`); if (doThrow) throw new Error(`File ${pathname} not found`);
return null; return null;
} }
@@ -343,13 +344,13 @@ export function findFileSync(pathname: string, doThrow = true) {
if (pkg.startsWith('@')) pkg = `${pkg}/${eles.shift()}`; if (pkg.startsWith('@')) pkg = `${pkg}/${eles.shift()}`;
const rest = eles.join('/'); const rest = eles.join('/');
try { try {
const p = require.resolve(pkg); const p = path.dirname(require.resolve(path.join(pkg, 'package.json')));
if (fs.pathExistsSync(path.resolve(p, rest))) return path.resolve(p, rest); if (fs.statSync(path.resolve(p, rest))) return path.resolve(p, rest);
} catch (e) { } } catch (e) { }
} }
if (fs.pathExistsSync(path.resolve(os.homedir(), 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(__dirname, 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(__dirname, 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`); if (doThrow) throw new Error(`File ${pathname} not found`);
return null; return null;
} }
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/utils", "name": "@hydrooj/utils",
"version": "1.0.2", "version": "1.0.3",
"description": "hydrooj utils", "description": "hydrooj utils",
"main": "package.json", "main": "package.json",
"repository": "https://github.com/hydro-dev/Hydro.git", "repository": "https://github.com/hydro-dev/Hydro.git",