aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.js
diff options
context:
space:
mode:
authorAustin Morton <apmorton@users.noreply.github.com>2020-09-26 16:59:26 -0400
committerGitHub <noreply@github.com>2020-09-26 16:59:26 -0400
commit044dcfbf8885d0115e64cf75f74a0f40f54e2370 (patch)
tree1dcc3e27139600d8e85004dc2a840bb420ccc835 /lib/utils.js
parentbac07fea6d2d4ed5fb7070c51e4cf3e56e3c155a (diff)
downloadcompiler-explorer-044dcfbf8885d0115e64cf75f74a0f40f54e2370.tar.gz
compiler-explorer-044dcfbf8885d0115e64cf75f74a0f40f54e2370.zip
Use ES6 Modules (#2132)
Diffstat (limited to 'lib/utils.js')
-rw-r--r--lib/utils.js78
1 files changed, 29 insertions, 49 deletions
diff --git a/lib/utils.js b/lib/utils.js
index 259b23612..c9a9328e2 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -22,9 +22,12 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
-const _ = require('underscore'),
- crypto = require('crypto'),
- quote = require('shell-quote');
+import crypto from 'crypto';
+import path from 'path';
+import { fileURLToPath } from 'url';
+
+import quote from 'shell-quote';
+import _ from 'underscore';
const tabsRe = /\t/g;
const lineRe = /\r?\n/;
@@ -34,7 +37,7 @@ const lineRe = /\r?\n/;
* @param {string} text
* @returns {string[]}
*/
-function splitLines(text) {
+export function splitLines(text) {
if (!text) return [];
const result = text.split(lineRe);
if (result.length > 0 && result[result.length - 1] === '')
@@ -42,8 +45,6 @@ function splitLines(text) {
return result;
}
-exports.splitLines = splitLines;
-
/***
* @callback eachLineFunc
* @param {string} line
@@ -56,18 +57,16 @@ exports.splitLines = splitLines;
* @param {eachLineFunc} func
* @param {*} [context]
*/
-function eachLine(text, func, context) {
+export function eachLine(text, func, context) {
return _.each(splitLines(text), func, context);
}
-exports.eachLine = eachLine;
-
/***
*
* @param {string} line
* @returns {string}
*/
-function expandTabs(line) {
+export function expandTabs(line) {
let extraChars = 0;
return line.replace(tabsRe, function (match, offset) {
const total = offset + extraChars;
@@ -77,8 +76,6 @@ function expandTabs(line) {
});
}
-exports.expandTabs = expandTabs;
-
/***
* @typedef {Object} lineTag
* @property {string} text
@@ -100,7 +97,7 @@ exports.expandTabs = expandTabs;
* @param pathPrefix
* @returns {lineObj[]}
*/
-function parseOutput(lines, inputFilename, pathPrefix) {
+export function parseOutput(lines, inputFilename, pathPrefix) {
const re = /^\s*<source>[(:](\d+)(:?,?(\d+):?)?[):]*\s*(.*)/;
const result = [];
eachLine(lines, function (line) {
@@ -130,34 +127,28 @@ function parseOutput(lines, inputFilename, pathPrefix) {
return result;
}
-exports.parseOutput = parseOutput;
-
/***
*
* @param {string} name
* @param {number} len
* @returns {string}
*/
-function padRight(name, len) {
+export function padRight(name, len) {
while (name.length < len) name = name + ' ';
return name;
}
-exports.padRight = padRight;
-
/***
*
* @param {string} name
* @returns {string}
*/
-function trimRight(name) {
+export function trimRight(name) {
let l = name.length;
while (l > 0 && name[l - 1] === ' ') l -= 1;
return name.substr(0, l);
}
-exports.trimRight = trimRight;
-
/***
* Anonymizes given IP.
* For IPv4, it removes the last octet
@@ -166,7 +157,7 @@ exports.trimRight = trimRight;
* @param {string} ip - IP string, of either type (localhost|IPv4|IPv6)
* @returns {string} Anonymized IP
*/
-function anonymizeIp(ip) {
+export function anonymizeIp(ip) {
if (ip.includes('localhost')) {
return ip;
} else if (ip.includes(':')) {
@@ -178,9 +169,6 @@ function anonymizeIp(ip) {
}
}
-exports.anonymizeIp = anonymizeIp;
-
-
/***
*
* @param {*} object
@@ -201,14 +189,12 @@ const DefaultHash = 'Compiler Explorer Default Version 1';
* @param {string} [HashVersion=DefaultHash] - Hash "version" key
* @returns {Buffer} - Hash of object
*/
-function getBinaryHash(object, HashVersion = DefaultHash) {
+export function getBinaryHash(object, HashVersion = DefaultHash) {
return crypto.createHmac('sha256', HashVersion)
.update(objectToHashableString(object))
.digest();
}
-exports.getBinaryHash = getBinaryHash;
-
/***
* Gets the hash (as a hex string) of the given object
*
@@ -217,14 +203,12 @@ exports.getBinaryHash = getBinaryHash;
* @param {string} [HashVersion=DefaultHash] - Hash "version" key
* @returns {string} - Hash of object
*/
-function getHash(object, HashVersion = DefaultHash) {
+export function getHash(object, HashVersion = DefaultHash) {
return crypto.createHmac('sha256', HashVersion)
.update(objectToHashableString(object))
.digest('hex');
}
-exports.getHash = getHash;
-
/***
* @typedef {Object} glEditorMainContent
* @property {string} source - Editor content
@@ -250,7 +234,7 @@ exports.getHash = getHash;
* @param {Array} content - GoldenLayout config topmost content field
* @returns {glContents}
*/
-function glGetMainContents(content) {
+export function glGetMainContents(content) {
let contents = {editors: [], compilers: []};
_.each(content, element => {
if (element.type === 'component') {
@@ -268,15 +252,13 @@ function glGetMainContents(content) {
return contents;
}
-exports.glGetMainContents = glGetMainContents;
-
/***
*
* @param {string} line
* @param {boolean} [atStart=true]
* @returns {string}
*/
-function squashHorizontalWhitespace(line, atStart) {
+export function squashHorizontalWhitespace(line, atStart) {
if (atStart === undefined) atStart = true;
if (line.trim().length === 0) {
return '';
@@ -290,14 +272,12 @@ function squashHorizontalWhitespace(line, atStart) {
return splat.join(' ');
}
-exports.squashHorizontalWhitespace = squashHorizontalWhitespace;
-
/***
*
* @param {string} prop
* @returns {boolean|number|string}
*/
-function toProperty(prop) {
+export function toProperty(prop) {
if (prop === 'true' || prop === 'yes') return true;
if (prop === 'false' || prop === 'no') return false;
if (prop.match(/^-?(0|[1-9]\d*)$/)) return parseInt(prop);
@@ -305,9 +285,6 @@ function toProperty(prop) {
return prop;
}
-exports.toProperty = toProperty;
-
-
/***
* This function replaces all the "oldValues" in line with "newValue". It handles overlapping string replacement cases,
* and is careful to return the exact same line object if there's no matches. This turns out to be super important for
@@ -317,7 +294,7 @@ exports.toProperty = toProperty;
* @param {string} newValue
* @returns {string}
*/
-function replaceAll(line, oldValue, newValue) {
+export function replaceAll(line, oldValue, newValue) {
if (oldValue.length === 0) return line;
let startPoint = 0;
for (; ;) {
@@ -329,8 +306,6 @@ function replaceAll(line, oldValue, newValue) {
return line;
}
-exports.replaceAll = replaceAll;
-
// Initially based on http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
const BASE32_ALPHABET = '13456789EGKMPTWYabcdefhjnoqrsvxz';
@@ -340,7 +315,7 @@ const BASE32_ALPHABET = '13456789EGKMPTWYabcdefhjnoqrsvxz';
* @param {Buffer} buffer
* @returns {string}
*/
-function base32Encode(buffer) {
+export function base32Encode(buffer) {
let output = '';
// This can grow up to 12 bits
let digest = 0;
@@ -375,13 +350,18 @@ function base32Encode(buffer) {
return output;
}
-exports.base32Encode = base32Encode;
-
-function splitArguments(options) {
+export function splitArguments(options) {
return _.chain(quote.parse(options || '')
.map(x => typeof (x) === 'string' ? x : x.pattern))
.compact()
.value();
}
-exports.splitArguments = splitArguments;
+/***
+ * Absolute path to the root of the application
+ */
+export const APP_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+
+export function resolvePathFromAppRoot(...args) {
+ return path.resolve(APP_ROOT, ...args);
+}