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) {
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;
}
+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 {
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 {
+1
View File
@@ -17,6 +17,7 @@ function runWebpack({
const server = new WebpackDevServer(compiler, {
compress: true,
hot: true,
disableHostCheck: true,
stats: 'errors-warnings',
index: '',
proxy: {
+10 -9
View File
@@ -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;
}
+1 -1
View File
@@ -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",