Skip to content

Commit

Permalink
successfully installed and add flow dependency to package.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
vickyc2266 committed Oct 24, 2024
1 parent 66a9b1e commit d9493e0
Show file tree
Hide file tree
Showing 37 changed files with 5,124 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-flow"],
"plugins": ["babel-plugin-syntax-hermes-parser"],
}
14 changes: 14 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[ignore]

[include]

[libs]

[lints]
untyped-type-import=error
internal-type=error
deprecated-type-bool=error

[options]

[strict]
96 changes: 96 additions & 0 deletions lib/admin/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict';

const fs = require('fs');
const path = require('path');
const sanitizeHTML = require('sanitize-html');
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

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces

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

View workflow job for this annotation

GitHub Actions / test

Missing trailing comma
} = require('../translator');

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

View workflow job for this annotation

GitHub Actions / test

Expected 1 empty line after require statement not followed by another require

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));

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces
}
async function getAdminNamespaces() {
const directories = await file.walk(path.resolve(nconf.get('views_dir'), 'admin'));

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces
return filterDirectories(directories);

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces
}
function sanitize(html) {
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, ' ');
}
function nsToTitle(namespace) {
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
};
}
async function fallback(namespace) {
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)));
}
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 cache = {};
async function getDictionary(language) {
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);
40 changes: 40 additions & 0 deletions lib/admin/versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const request = require('../request');
const meta = require('../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;
}
exports.getLatestVersion = getLatestVersion;
exports.isPrerelease = isPrerelease;
require('../promisify')(exports);
7 changes: 7 additions & 0 deletions lib/als.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const {
AsyncLocalStorage
} = require('async_hooks');
const asyncLocalStorage = new AsyncLocalStorage();
module.exports = asyncLocalStorage;
Loading

0 comments on commit d9493e0

Please sign in to comment.