Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/tools/utils.ts
1396 views
1
/*
2
* Copyright (C) 2021 William Stein <[email protected]>
3
* Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
4
*
5
* Distributed under terms of the BSD license
6
*/
7
8
import { statSync } from "fs";
9
import { delimiter } from "path";
10
import { createHash } from "crypto";
11
import { EventEmitter } from "events";
12
import { promisify } from "util";
13
import { normalize, join, dirname } from "path";
14
15
export const basePath = normalize(join(dirname(module.filename), "..", ".."));
16
export const importPath = join(basePath, "src", "lib");
17
export const libraryPath = join(basePath, "dist", "compiler");
18
19
export const comment_contents =
20
/\/\*!?(?:\@preserve)?[ \t]*(?:\r\n|\n)([\s\S]*?)(?:\r\n|\n)[ \t]*\*\//;
21
const colors = ["red", "green", "yellow", "blue", "magenta", "cyan", "white"];
22
23
function ansi(code: number): string {
24
code = code || 0;
25
return String.fromCharCode(27) + "[" + code + "m";
26
}
27
28
export function pathExists(path: string): boolean {
29
try {
30
statSync(path);
31
return true;
32
} catch (e) {
33
if (e.code != "ENOENT") {
34
throw e;
35
}
36
}
37
return false;
38
}
39
40
export function colored(
41
string: string,
42
color?: string,
43
bold?: boolean
44
): string {
45
if (!SUPPORTS_COLOR) {
46
return string;
47
}
48
const prefix: string[] = [];
49
if (bold) {
50
prefix.push(ansi(1));
51
}
52
if (color) {
53
prefix.push(ansi(colors.indexOf(color) + 31));
54
}
55
return prefix.join("") + string + ansi(0);
56
}
57
58
function supports_color(): boolean {
59
const stdout = process.stdout;
60
if (!stdout.isTTY) {
61
return false;
62
}
63
64
if (process.platform === "win32") {
65
return false;
66
}
67
68
if ("COLORTERM" in process.env) {
69
return true;
70
}
71
72
if (process.env.TERM === "dumb") {
73
return false;
74
}
75
76
if (
77
process.env.TERM &&
78
/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)
79
) {
80
return true;
81
}
82
83
return false;
84
}
85
86
const SUPPORTS_COLOR: boolean = supports_color();
87
88
export function repeat(str: string, num: number): string {
89
return new Array(num + 1).join(str);
90
}
91
92
export function generators_available(): boolean {
93
var gen;
94
try {
95
eval("gen = function *(){}"); // jshint ignore:line
96
return (
97
typeof gen === "function" && gen.constructor.name == "GeneratorFunction"
98
);
99
} catch (e) {
100
return false;
101
}
102
}
103
104
export function wrap(lines: string[], width: number): string[] {
105
const ans: string[] = [];
106
let prev = "";
107
for (let line of lines) {
108
line = prev + line;
109
prev = "";
110
if (line.length > width) {
111
prev = line.slice(width);
112
if (prev) {
113
prev += " ";
114
}
115
line = line.slice(0, width - 1);
116
if (line.slice(-1) !== " ") {
117
line += "-";
118
}
119
}
120
ans.push(line);
121
}
122
if (prev) {
123
return ans.concat(wrap([prev], width));
124
}
125
return ans;
126
}
127
128
export function merge(): any {
129
// Simple merge of properties from all objects
130
const ans = {};
131
Array.prototype.slice.call(arguments).forEach((arg) => {
132
Object.keys(arg).forEach(function (key) {
133
ans[key] = arg[key];
134
});
135
});
136
return ans;
137
}
138
139
export function getImportDirs(paths_string?: string, ignore_env?: boolean) {
140
const paths: string[] = [];
141
function merge(new_path: string) {
142
if (!paths.includes(new_path)) {
143
paths.push(new_path);
144
}
145
}
146
if (!ignore_env && process?.env?.PYLANGPATH) {
147
process.env.PYLANGPATH.split(delimiter).forEach(merge);
148
}
149
if (paths_string) {
150
paths_string.split(delimiter).forEach(merge);
151
}
152
return paths;
153
}
154
155
export function sha1sum(data) {
156
var h = createHash("sha1");
157
h.update(data);
158
return h.digest("hex");
159
}
160
161
export async function once(obj: EventEmitter, event: string): Promise<any> {
162
if (!(obj instanceof EventEmitter)) {
163
// just in case typescript doesn't catch something:
164
throw Error("obj must be an EventEmitter");
165
}
166
function wait(cb: Function): void {
167
obj.once(event, (...args) => {
168
cb(undefined, args);
169
});
170
}
171
return await promisify(wait)();
172
}
173
174