2020-11-24 15:15:51 +05:30
|
|
|
import { flatten, isString } from 'lodash';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { languages } from 'monaco-editor';
|
2021-01-29 00:20:46 +05:30
|
|
|
import { performanceMarkAndMeasure } from '~/performance/utils';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { SIDE_LEFT, SIDE_RIGHT } from './constants';
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const toLowerCase = (x) => x.toLowerCase();
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
const monacoLanguages = languages.getLanguages();
|
|
|
|
const monacoExtensions = new Set(
|
2021-03-08 18:12:59 +05:30
|
|
|
flatten(monacoLanguages.map((lang) => lang.extensions?.map(toLowerCase) || [])),
|
2020-04-08 14:13:33 +05:30
|
|
|
);
|
|
|
|
const monacoMimetypes = new Set(
|
2021-03-08 18:12:59 +05:30
|
|
|
flatten(monacoLanguages.map((lang) => lang.mimetypes?.map(toLowerCase) || [])),
|
2020-04-08 14:13:33 +05:30
|
|
|
);
|
|
|
|
const monacoFilenames = new Set(
|
2021-03-08 18:12:59 +05:30
|
|
|
flatten(monacoLanguages.map((lang) => lang.filenames?.map(toLowerCase) || [])),
|
2020-04-08 14:13:33 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
const KNOWN_TYPES = [
|
|
|
|
{
|
|
|
|
isText: false,
|
|
|
|
isMatch(mimeType) {
|
|
|
|
return mimeType.toLowerCase().includes('image/');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isText: true,
|
|
|
|
isMatch(mimeType) {
|
|
|
|
return mimeType.toLowerCase().includes('text/');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isText: true,
|
|
|
|
isMatch(mimeType, fileName) {
|
|
|
|
const fileExtension = fileName.includes('.') ? `.${fileName.split('.').pop()}` : '';
|
|
|
|
|
|
|
|
return (
|
|
|
|
monacoExtensions.has(fileExtension.toLowerCase()) ||
|
|
|
|
monacoMimetypes.has(mimeType.toLowerCase()) ||
|
|
|
|
monacoFilenames.has(fileName.toLowerCase())
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
export function isTextFile({ name, raw, content, mimeType = '' }) {
|
2021-03-08 18:12:59 +05:30
|
|
|
const knownType = KNOWN_TYPES.find((type) => type.isMatch(mimeType, name));
|
2020-04-08 14:13:33 +05:30
|
|
|
if (knownType) return knownType.isText;
|
|
|
|
|
|
|
|
// does the string contain ascii characters only (ranges from space to tilde, tabs and new lines)
|
|
|
|
const asciiRegex = /^[ -~\t\n\r]+$/;
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
const fileContents = raw || content;
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
// for unknown types, determine the type by evaluating the file contents
|
2021-01-03 14:25:43 +05:30
|
|
|
return isString(fileContents) && (fileContents === '' || asciiRegex.test(fileContents));
|
2020-04-08 14:13:33 +05:30
|
|
|
}
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const createPathWithExt = (p) => {
|
2018-11-18 11:00:15 +05:30
|
|
|
const ext = p.lastIndexOf('.') >= 0 ? p.substring(p.lastIndexOf('.') + 1) : '';
|
|
|
|
|
|
|
|
return `${p.substring(1, p.lastIndexOf('.') + 1 || p.length)}${ext || '.js'}`;
|
|
|
|
};
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const trimPathComponents = (path) =>
|
2020-05-24 23:13:21 +05:30
|
|
|
path
|
|
|
|
.split('/')
|
2021-03-08 18:12:59 +05:30
|
|
|
.map((s) => s.trim())
|
2020-05-24 23:13:21 +05:30
|
|
|
.join('/');
|
|
|
|
|
|
|
|
export function registerLanguages(def, ...defs) {
|
2021-03-08 18:12:59 +05:30
|
|
|
defs.forEach((lang) => registerLanguages(lang));
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
const languageId = def.id;
|
|
|
|
|
|
|
|
languages.register(def);
|
|
|
|
languages.setMonarchTokensProvider(languageId, def.language);
|
|
|
|
languages.setLanguageConfiguration(languageId, def.conf);
|
|
|
|
}
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
export function registerSchema(schema) {
|
|
|
|
const defaults = [languages.json.jsonDefaults, languages.yaml.yamlDefaults];
|
2021-03-08 18:12:59 +05:30
|
|
|
defaults.forEach((d) =>
|
2020-11-24 15:15:51 +05:30
|
|
|
d.setDiagnosticsOptions({
|
|
|
|
validate: true,
|
|
|
|
enableSchemaRequest: true,
|
|
|
|
hover: true,
|
|
|
|
completion: true,
|
|
|
|
schemas: [schema],
|
|
|
|
}),
|
|
|
|
);
|
2020-07-28 23:09:34 +05:30
|
|
|
}
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const otherSide = (side) => (side === SIDE_RIGHT ? SIDE_LEFT : SIDE_RIGHT);
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
export function trimTrailingWhitespace(content) {
|
|
|
|
return content.replace(/[^\S\r\n]+$/gm, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPathParents(path, maxDepth = Infinity) {
|
|
|
|
const pathComponents = path.split('/');
|
|
|
|
const paths = [];
|
|
|
|
|
|
|
|
let depth = 0;
|
|
|
|
while (pathComponents.length && depth < maxDepth) {
|
|
|
|
pathComponents.pop();
|
|
|
|
|
|
|
|
let parentPath = pathComponents.join('/');
|
|
|
|
if (parentPath.startsWith('/')) parentPath = parentPath.slice(1);
|
|
|
|
if (parentPath) paths.push(parentPath);
|
|
|
|
|
|
|
|
depth += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPathParent(path) {
|
|
|
|
return getPathParents(path, 1)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a file object and returns a data uri of its contents.
|
|
|
|
*
|
|
|
|
* @param {File} file
|
|
|
|
*/
|
|
|
|
export function readFileAsDataURL(file) {
|
2021-03-08 18:12:59 +05:30
|
|
|
return new Promise((resolve) => {
|
2020-06-23 00:09:42 +05:30
|
|
|
const reader = new FileReader();
|
2021-03-08 18:12:59 +05:30
|
|
|
reader.addEventListener('load', (e) => resolve(e.target.result), { once: true });
|
2020-06-23 00:09:42 +05:30
|
|
|
reader.readAsDataURL(file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFileEOL(content = '') {
|
|
|
|
return content.includes('\r\n') ? 'CRLF' : 'LF';
|
|
|
|
}
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds or increments the numeric suffix to a filename/branch name.
|
|
|
|
* Retains underscore or dash before the numeric suffix if it already exists.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* hello -> hello-1
|
|
|
|
* hello-2425 -> hello-2425
|
|
|
|
* hello.md -> hello-1.md
|
|
|
|
* hello_2.md -> hello_3.md
|
|
|
|
* hello_ -> hello_1
|
2021-09-04 01:27:46 +05:30
|
|
|
* main-patch-22432 -> main-patch-22433
|
2021-01-03 14:25:43 +05:30
|
|
|
* patch_332 -> patch_333
|
|
|
|
*
|
|
|
|
* @param {string} filename File name or branch name
|
|
|
|
* @param {number} [randomize] Should randomize the numeric suffix instead of auto-incrementing?
|
|
|
|
*/
|
|
|
|
export function addNumericSuffix(filename, randomize = false) {
|
|
|
|
return filename.replace(/([ _-]?)(\d*)(\..+?$|$)/, (_, before, number, after) => {
|
2021-03-08 18:12:59 +05:30
|
|
|
const n = randomize ? Math.random().toString().substring(2, 7).slice(-5) : Number(number) + 1;
|
2021-01-03 14:25:43 +05:30
|
|
|
return `${before || '-'}${n}${after}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export const measurePerformance = (
|
|
|
|
mark,
|
|
|
|
measureName,
|
|
|
|
measureStart = undefined,
|
|
|
|
measureEnd = mark,
|
|
|
|
) => {
|
|
|
|
performanceMarkAndMeasure({
|
|
|
|
mark,
|
|
|
|
measures: [
|
|
|
|
{
|
|
|
|
name: measureName,
|
|
|
|
start: measureStart,
|
|
|
|
end: measureEnd,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
};
|