Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Evelyn Zheng committed Oct 24, 2024
1 parent c6b1103 commit e229a7d
Show file tree
Hide file tree
Showing 35 changed files with 4,267 additions and 4,232 deletions.
122 changes: 62 additions & 60 deletions lib/admin/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,90 +7,92 @@ const nconf = require('nconf');
const winston = require('winston');
const file = require('../file');

Check failure on line 8 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module '../file'

Check failure on line 8 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Missing file extension for "../file"
const {
Translator
Translator,
} = require('../translator');

Check failure on line 11 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module '../translator'

Check failure on line 11 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Missing file extension for "../translator"

function filterDirectories(directories) {
return directories.map(dir => dir.replace(/^.*(admin.*?).tpl$/, '$1').split(path.sep).join('/')).filter(dir => !dir.endsWith('.js') && !dir.includes('/partials/') && /\/.*\//.test(dir) && !/manage\/(category|group|category-analytics)$/.test(dir));
return directories.map(dir => dir.replace(/^.*(admin.*?).tpl$/, '$1').split(path.sep).join('/')).filter(dir => !dir.endsWith('.js') && !dir.includes('/partials/') && /\/.*\//.test(dir) && !/manage\/(category|group|category-analytics)$/.test(dir));
}
async function getAdminNamespaces() {
const directories = await file.walk(path.resolve(nconf.get('views_dir'), 'admin'));
return filterDirectories(directories);
const directories = await file.walk(path.resolve(nconf.get('views_dir'), 'admin'));
return filterDirectories(directories);
}
function sanitize(html) {
return sanitizeHTML(html, {
allowedTags: [],
allowedAttributes: []
});
return sanitizeHTML(html, {
allowedTags: [],
allowedAttributes: [],
});
}
function simplify(translations) {
return translations.replace(/(?:\{{1,2}[^}]*?\}{1,2})/g, '').replace(/(?:[ \t]*[\n\r]+[ \t]*)+/g, '\n').replace(/[\t ]+/g, ' ');
return translations.replace(/(?:\{{1,2}[^}]*?\}{1,2})/g, '').replace(/(?:[ \t]*[\n\r]+[ \t]*)+/g, '\n').replace(/[\t ]+/g, ' ');
}
function nsToTitle(namespace) {
return namespace.replace('admin/', '').split('/').map(str => str[0].toUpperCase() + str.slice(1)).join(' > ').replace(/[^a-zA-Z> ]/g, ' ');
return namespace.replace('admin/', '').split('/').map(str => str[0].toUpperCase() + str.slice(1)).join(' > ')
.replace(/[^a-zA-Z> ]/g, ' ');
}
const fallbackCache = {};
async function initFallback(namespace) {
const template = await fs.promises.readFile(path.resolve(nconf.get('views_dir'), `${namespace}.tpl`), 'utf8');
const title = nsToTitle(namespace);
let translations = sanitize(template);
translations = Translator.removePatterns(translations);
translations = simplify(translations);
translations += `\n${title}`;
return {
namespace: namespace,
translations: translations,
title: title
};
const template = await fs.promises.readFile(path.resolve(nconf.get('views_dir'), `${namespace}.tpl`), 'utf8');
const title = nsToTitle(namespace);
let translations = sanitize(template);
translations = Translator.removePatterns(translations);
translations = simplify(translations);
translations += `\n${title}`;
return {
namespace: namespace,
translations: translations,
title: title,
};
}
async function fallback(namespace) {
if (fallbackCache[namespace]) {
return fallbackCache[namespace];
}
const params = await initFallback(namespace);
fallbackCache[namespace] = params;
return params;
if (fallbackCache[namespace]) {
return fallbackCache[namespace];
}
const params = await initFallback(namespace);
fallbackCache[namespace] = params;
return params;
}
async function initDict(language) {
const namespaces = await getAdminNamespaces();
return await Promise.all(namespaces.map(ns => buildNamespace(language, ns)));
const namespaces = await getAdminNamespaces();
return await Promise.all(namespaces.map(ns => buildNamespace(language, ns)));
}
async function buildNamespace(language, namespace) {
const translator = Translator.create(language);
try {
const translations = await translator.getTranslation(namespace);
if (!translations || !Object.keys(translations).length) {
return await fallback(namespace);
}
let str = Object.keys(translations).map(key => translations[key]).join('\n');
str = sanitize(str);
let title = namespace;
title = title.match(/admin\/(.+?)\/(.+?)$/);
title = `[[admin/menu:section-${title[1] === 'development' ? 'advanced' : title[1]}]]${title[2] ? ` > [[admin/menu:${title[1]}/${title[2]}]]` : ''}`;
title = await translator.translate(title);
return {
namespace: namespace,
translations: `${str}\n${title}`,
title: title
};
} catch (err) {
winston.error(err.stack);
return {
namespace: namespace,
translations: ''
};
}
const translator = Translator.create(language);
try {
const translations = await translator.getTranslation(namespace);
if (!translations || !Object.keys(translations).length) {
return await fallback(namespace);
}
let str = Object.keys(translations).map(key => translations[key]).join('\n');
str = sanitize(str);
let title = namespace;
title = title.match(/admin\/(.+?)\/(.+?)$/);
title = `[[admin/menu:section-${title[1] === 'development' ? 'advanced' : title[1]}]]${title[2] ? ` > [[admin/menu:${title[1]}/${title[2]}]]` : ''}`;
title = await translator.translate(title);
return {
namespace: namespace,
translations: `${str}\n${title}`,
title: title,
};
} catch (err) {
winston.error(err.stack);
return {
namespace: namespace,
translations: '',
};
}
}
const cache = {};
async function getDictionary(language) {
if (cache[language]) {
return cache[language];
}
const params = await initDict(language);
cache[language] = params;
return params;
if (cache[language]) {
return cache[language];
}
const params = await initDict(language);
cache[language] = params;
return params;
}
module.exports.getDictionary = getDictionary;
module.exports.filterDirectories = filterDirectories;
module.exports.simplify = simplify;
module.exports.sanitize = sanitize;
require('../promisify')(module.exports);
require('../promisify')(module.exports);

Check failure on line 98 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module '../promisify'

Check failure on line 98 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Missing file extension for "../promisify"
57 changes: 29 additions & 28 deletions lib/admin/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@

const request = require('../request');

Check failure on line 3 in lib/admin/versions.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module '../request'

Check failure on line 3 in lib/admin/versions.js

View workflow job for this annotation

GitHub Actions / test

Missing file extension for "../request"
const meta = require('../meta');

Check failure on line 4 in lib/admin/versions.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module '../meta'

Check failure on line 4 in lib/admin/versions.js

View workflow job for this annotation

GitHub Actions / test

Missing file extension for "../meta"

let versionCache = '';
let versionCacheLastModified = '';
const isPrerelease = /^v?\d+\.\d+\.\d+-.+$/;
const latestReleaseUrl = 'https://api.github.com/repos/NodeBB/NodeBB/releases/latest';
async function getLatestVersion() {
const headers = {
Accept: 'application/vnd.github.v3+json',
'User-Agent': encodeURIComponent(`NodeBB Admin Control Panel/${meta.config.title}`)
};
if (versionCacheLastModified) {
headers['If-Modified-Since'] = versionCacheLastModified;
}
const {
body: latestRelease,
response
} = await request.get(latestReleaseUrl, {
headers: headers,
timeout: 2000
});
if (response.statusCode === 304) {
return versionCache;
}
if (response.statusCode !== 200) {
throw new Error(response.statusText);
}
if (!latestRelease || !latestRelease.tag_name) {
throw new Error('[[error:cant-get-latest-release]]');
}
const tagName = latestRelease.tag_name.replace(/^v/, '');
versionCache = tagName;
versionCacheLastModified = response.headers['last-modified'];
return versionCache;
const headers = {
Accept: 'application/vnd.github.v3+json',
'User-Agent': encodeURIComponent(`NodeBB Admin Control Panel/${meta.config.title}`),
};
if (versionCacheLastModified) {
headers['If-Modified-Since'] = versionCacheLastModified;
}
const {
body: latestRelease,
response,
} = await request.get(latestReleaseUrl, {
headers: headers,
timeout: 2000,
});
if (response.statusCode === 304) {
return versionCache;
}
if (response.statusCode !== 200) {
throw new Error(response.statusText);
}
if (!latestRelease || !latestRelease.tag_name) {
throw new Error('[[error:cant-get-latest-release]]');
}
const tagName = latestRelease.tag_name.replace(/^v/, '');
versionCache = tagName;
versionCacheLastModified = response.headers['last-modified'];
return versionCache;
}
exports.getLatestVersion = getLatestVersion;
exports.isPrerelease = isPrerelease;
require('../promisify')(exports);
require('../promisify')(exports);
5 changes: 3 additions & 2 deletions lib/als.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const {
AsyncLocalStorage
AsyncLocalStorage,
} = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();
module.exports = asyncLocalStorage;
module.exports = asyncLocalStorage;
Loading

0 comments on commit e229a7d

Please sign in to comment.