@gjsify/utils 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/cancel-handler.js +41 -0
- package/lib/cjs/cli.js +40 -0
- package/lib/cjs/error.js +40 -0
- package/lib/cjs/file.js +42 -0
- package/lib/cjs/fs.js +50 -0
- package/lib/cjs/index.js +30 -0
- package/lib/cjs/log.js +188 -0
- package/lib/cjs/message.js +32 -0
- package/lib/cjs/os.js +110 -0
- package/lib/cjs/path.js +79 -0
- package/lib/cjs/process.js +70 -0
- package/lib/cjs/system.js +77 -0
- package/lib/cjs/tty.js +32 -0
- package/lib/cjs/types/cancel-signals.js +15 -0
- package/lib/cjs/types/error-data.js +15 -0
- package/lib/cjs/types/index.js +24 -0
- package/lib/cjs/types/signal-methods.js +15 -0
- package/lib/cjs/types/stack-trace-frame.js +15 -0
- package/lib/cjs/types/structured-log-data.js +15 -0
- package/lib/cjs/types/uncaught-exception-data.js +15 -0
- package/lib/cjs/types/unhandled-rejection-data.js +15 -0
- package/lib/cjs/types/user-info.js +15 -0
- package/lib/cjs/version.js +26 -0
- package/lib/esm/cancel-handler.js +22 -0
- package/lib/esm/cli.js +11 -0
- package/lib/esm/error.js +21 -0
- package/lib/esm/file.js +13 -0
- package/lib/esm/fs.js +21 -0
- package/lib/esm/index.js +14 -0
- package/lib/esm/log.js +159 -0
- package/lib/esm/message.js +13 -0
- package/lib/esm/os.js +81 -0
- package/lib/esm/path.js +50 -0
- package/lib/esm/process.js +41 -0
- package/lib/esm/system.js +47 -0
- package/lib/esm/tty.js +13 -0
- package/lib/esm/types/cancel-signals.js +0 -0
- package/lib/esm/types/error-data.js +0 -0
- package/lib/esm/types/index.js +8 -0
- package/lib/esm/types/signal-methods.js +0 -0
- package/lib/esm/types/stack-trace-frame.js +0 -0
- package/lib/esm/types/structured-log-data.js +0 -0
- package/lib/esm/types/uncaught-exception-data.js +0 -0
- package/lib/esm/types/unhandled-rejection-data.js +0 -0
- package/lib/esm/types/user-info.js +0 -0
- package/lib/esm/version.js +7 -0
- package/lib/types/cancel-handler.d.ts +4 -0
- package/lib/types/cli.d.ts +1 -0
- package/lib/types/error.d.ts +7 -0
- package/lib/types/file.d.ts +1 -0
- package/lib/types/fs.d.ts +3 -0
- package/lib/types/index.d.ts +14 -0
- package/lib/types/log.d.ts +29 -0
- package/lib/types/message.d.ts +2 -0
- package/lib/types/os.d.ts +7 -0
- package/lib/types/path.d.ts +6 -0
- package/lib/types/process.d.ts +6 -0
- package/lib/types/system.d.ts +5 -0
- package/lib/types/tty.d.ts +5 -0
- package/lib/types/types/cancel-signals.d.ts +5 -0
- package/lib/types/types/error-data.d.ts +7 -0
- package/lib/types/types/index.d.ts +8 -0
- package/lib/types/types/signal-methods.d.ts +62 -0
- package/lib/types/types/stack-trace-frame.d.ts +6 -0
- package/lib/types/types/structured-log-data.d.ts +5 -0
- package/lib/types/types/uncaught-exception-data.d.ts +4 -0
- package/lib/types/types/unhandled-rejection-data.d.ts +5 -0
- package/lib/types/types/user-info.d.ts +8 -0
- package/lib/types/version.d.ts +1 -0
- package/package.json +46 -0
- package/src/cancel-handler.ts +24 -0
- package/src/cli.ts +10 -0
- package/src/error.ts +38 -0
- package/src/file.ts +12 -0
- package/src/fs.ts +24 -0
- package/src/index.ts +15 -0
- package/src/log.spec.ts +32 -0
- package/src/log.ts +218 -0
- package/src/message.ts +11 -0
- package/src/os.ts +94 -0
- package/src/path.ts +52 -0
- package/src/process.spec.ts +24 -0
- package/src/process.ts +46 -0
- package/src/system.ts +56 -0
- package/src/test.ts +6 -0
- package/src/tty.spec.ts +10 -0
- package/src/tty.ts +10 -0
- package/src/types/cancel-signals.ts +6 -0
- package/src/types/error-data.ts +8 -0
- package/src/types/index.ts +8 -0
- package/src/types/signal-methods.ts +63 -0
- package/src/types/stack-trace-frame.ts +7 -0
- package/src/types/structured-log-data.ts +5 -0
- package/src/types/uncaught-exception-data.ts +5 -0
- package/src/types/unhandled-rejection-data.ts +6 -0
- package/src/types/user-info.ts +9 -0
- package/src/version.ts +4 -0
- package/test.gjs.js +34793 -0
- package/test.gjs.js.map +7 -0
- package/test.gjs.js.meta.json +1 -0
- package/tsconfig.json +21 -0
- package/tsconfig.types.json +7 -0
- package/tsconfig.types.tsbuildinfo +1 -0
package/lib/esm/os.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { cli } from "./cli.js";
|
|
2
|
+
import GLib from "@girs/glib-2.0";
|
|
3
|
+
import Gio from "@girs/gio-2.0";
|
|
4
|
+
let _arch = "";
|
|
5
|
+
let _os = "";
|
|
6
|
+
let _target = "";
|
|
7
|
+
const userInfo = {};
|
|
8
|
+
const getArch = () => {
|
|
9
|
+
if (_arch) {
|
|
10
|
+
return _arch;
|
|
11
|
+
}
|
|
12
|
+
_arch = cli("uname -m").trim();
|
|
13
|
+
return _arch;
|
|
14
|
+
};
|
|
15
|
+
const getOs = () => {
|
|
16
|
+
if (_os) {
|
|
17
|
+
return _os;
|
|
18
|
+
}
|
|
19
|
+
const os = cli("uname -o").trim();
|
|
20
|
+
if (/\bDarwin\b/i.test(os)) {
|
|
21
|
+
_os = "darwin";
|
|
22
|
+
return _os;
|
|
23
|
+
}
|
|
24
|
+
if (/\bLinux\b/i.test(os)) {
|
|
25
|
+
_os = "linux";
|
|
26
|
+
return _os;
|
|
27
|
+
}
|
|
28
|
+
_os = "win32";
|
|
29
|
+
return _os;
|
|
30
|
+
};
|
|
31
|
+
const getVendor = () => "gjsify";
|
|
32
|
+
const getEnv = () => "gnu";
|
|
33
|
+
const getTarget = () => {
|
|
34
|
+
if (_target) {
|
|
35
|
+
return _target;
|
|
36
|
+
}
|
|
37
|
+
_target = `${getArch()}-${getVendor()}-${getOs()}-${getEnv()}`;
|
|
38
|
+
return _target;
|
|
39
|
+
};
|
|
40
|
+
const extractUserInfo = (passwd, username) => {
|
|
41
|
+
const lines = passwd.split("\n");
|
|
42
|
+
for (const line of lines) {
|
|
43
|
+
const parts = line.split(":");
|
|
44
|
+
if (parts[0] === username) {
|
|
45
|
+
return {
|
|
46
|
+
username: parts[0],
|
|
47
|
+
// password: parts[1],
|
|
48
|
+
userId: parseInt(parts[2], 10),
|
|
49
|
+
groupId: parseInt(parts[3], 10),
|
|
50
|
+
userInfo: parts[4],
|
|
51
|
+
homeDirectory: parts[5],
|
|
52
|
+
shell: parts[6]
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
};
|
|
58
|
+
const getUserInfo = (username) => {
|
|
59
|
+
const file = Gio.File.new_for_path("/etc/passwd");
|
|
60
|
+
if (!username) {
|
|
61
|
+
username = GLib.get_user_name();
|
|
62
|
+
}
|
|
63
|
+
if (userInfo[username]) {
|
|
64
|
+
return userInfo[username];
|
|
65
|
+
}
|
|
66
|
+
const [success, contents] = file.load_contents(null);
|
|
67
|
+
if (!success) {
|
|
68
|
+
throw new Error(`Failed to load ${file.get_path()}`);
|
|
69
|
+
}
|
|
70
|
+
const contentStr = new TextDecoder().decode(contents);
|
|
71
|
+
userInfo[username] = extractUserInfo(contentStr, username);
|
|
72
|
+
return userInfo[username];
|
|
73
|
+
};
|
|
74
|
+
export {
|
|
75
|
+
getArch,
|
|
76
|
+
getEnv,
|
|
77
|
+
getOs,
|
|
78
|
+
getTarget,
|
|
79
|
+
getUserInfo,
|
|
80
|
+
getVendor
|
|
81
|
+
};
|
package/lib/esm/path.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import Gio from "@girs/gio-2.0";
|
|
2
|
+
import GLib from "@girs/glib-2.0";
|
|
3
|
+
const { File } = Gio;
|
|
4
|
+
const _getProgramDir = (programFile) => {
|
|
5
|
+
const info = programFile.query_info("standard::", Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
6
|
+
if (info.get_is_symlink()) {
|
|
7
|
+
const symlinkFile = programFile.get_parent().resolve_relative_path(info.get_symlink_target());
|
|
8
|
+
return symlinkFile.get_parent();
|
|
9
|
+
} else {
|
|
10
|
+
return programFile.get_parent();
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const resolve = (dir, ...filenames) => {
|
|
14
|
+
let file = File.new_for_path(dir);
|
|
15
|
+
for (const filename of filenames) {
|
|
16
|
+
file = file.resolve_relative_path(filename);
|
|
17
|
+
}
|
|
18
|
+
return file;
|
|
19
|
+
};
|
|
20
|
+
const getProgramExe = () => {
|
|
21
|
+
const currentDir = GLib.get_current_dir();
|
|
22
|
+
return File.new_for_path(currentDir).resolve_relative_path(imports.system.programInvocationName);
|
|
23
|
+
};
|
|
24
|
+
const getProgramDir = () => {
|
|
25
|
+
return _getProgramDir(getProgramExe()).get_path();
|
|
26
|
+
};
|
|
27
|
+
const getPathSeparator = () => {
|
|
28
|
+
const currentDir = GLib.get_current_dir();
|
|
29
|
+
return /^\//.test(currentDir) ? "/" : "\\";
|
|
30
|
+
};
|
|
31
|
+
const getNodeModulesPath = () => {
|
|
32
|
+
let dir = File.new_for_path(getProgramDir());
|
|
33
|
+
let found = false;
|
|
34
|
+
do {
|
|
35
|
+
dir = dir.resolve_relative_path("..");
|
|
36
|
+
const nodeModulesDir = dir.resolve_relative_path("node_modules");
|
|
37
|
+
found = nodeModulesDir.query_exists(null);
|
|
38
|
+
if (found) {
|
|
39
|
+
dir = nodeModulesDir;
|
|
40
|
+
}
|
|
41
|
+
} while (dir.has_parent(null) && !found);
|
|
42
|
+
return dir;
|
|
43
|
+
};
|
|
44
|
+
export {
|
|
45
|
+
getNodeModulesPath,
|
|
46
|
+
getPathSeparator,
|
|
47
|
+
getProgramDir,
|
|
48
|
+
getProgramExe,
|
|
49
|
+
resolve
|
|
50
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import Gio from "@girs/gio-2.0";
|
|
2
|
+
import GLib from "@girs/glib-2.0";
|
|
3
|
+
let PID = 0;
|
|
4
|
+
let PPID = 0;
|
|
5
|
+
let LOCALE = "";
|
|
6
|
+
const getLocale = () => {
|
|
7
|
+
if (!LOCALE) {
|
|
8
|
+
LOCALE = GLib.getenv("LANG").split(".")[0].replace("_", "-");
|
|
9
|
+
}
|
|
10
|
+
return LOCALE;
|
|
11
|
+
};
|
|
12
|
+
const getPid = () => {
|
|
13
|
+
if (!PID) {
|
|
14
|
+
PID = new Gio.Credentials().get_unix_pid();
|
|
15
|
+
}
|
|
16
|
+
return PID;
|
|
17
|
+
};
|
|
18
|
+
const getPpid = () => {
|
|
19
|
+
if (!PPID) {
|
|
20
|
+
const path = `/proc/${getPid()}/status`;
|
|
21
|
+
const file = Gio.File.new_for_path(path);
|
|
22
|
+
const [success, contents] = file.load_contents(null);
|
|
23
|
+
if (success) {
|
|
24
|
+
const contentStr = new TextDecoder().decode(contents);
|
|
25
|
+
const numStr = contentStr.match(/^PPid:.*$/gm)[0]?.match(/(\d+)$/gm)[0];
|
|
26
|
+
PPID = Number(numStr);
|
|
27
|
+
} else {
|
|
28
|
+
console.error(new Error("Failed to read file: " + path));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return PPID;
|
|
32
|
+
};
|
|
33
|
+
const getTitle = () => {
|
|
34
|
+
return GLib.get_prgname();
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
getLocale,
|
|
38
|
+
getPid,
|
|
39
|
+
getPpid,
|
|
40
|
+
getTitle
|
|
41
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import GLib from "@girs/glib-2.0";
|
|
2
|
+
import Gio from "@girs/gio-2.0";
|
|
3
|
+
import { existsSync } from "./fs.js";
|
|
4
|
+
const File = Gio.File;
|
|
5
|
+
const args = [];
|
|
6
|
+
const getArgv = () => {
|
|
7
|
+
if (args.length) {
|
|
8
|
+
return args;
|
|
9
|
+
}
|
|
10
|
+
const [__filename] = GLib.filename_from_uri(import.meta.url);
|
|
11
|
+
args.push(__filename);
|
|
12
|
+
ARGV.forEach((arg) => {
|
|
13
|
+
if (arg[0] !== "-") {
|
|
14
|
+
args.push(
|
|
15
|
+
existsSync(arg) ? File.new_for_path(arg).get_path() : arg
|
|
16
|
+
);
|
|
17
|
+
} else {
|
|
18
|
+
args.push(arg);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return args;
|
|
22
|
+
};
|
|
23
|
+
const getArgs = () => {
|
|
24
|
+
return getArgv().slice(2);
|
|
25
|
+
};
|
|
26
|
+
const parseArgv = (argv) => {
|
|
27
|
+
let result = {};
|
|
28
|
+
let currentKey = null;
|
|
29
|
+
for (let i = 0; i < argv.length; i++) {
|
|
30
|
+
let arg = argv[i];
|
|
31
|
+
if (arg.startsWith("--")) {
|
|
32
|
+
currentKey = arg.substring(2);
|
|
33
|
+
result[currentKey] = true;
|
|
34
|
+
} else if (currentKey !== null) {
|
|
35
|
+
result[currentKey] = arg;
|
|
36
|
+
currentKey = null;
|
|
37
|
+
} else {
|
|
38
|
+
result[arg] = true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
export {
|
|
44
|
+
getArgs,
|
|
45
|
+
getArgv,
|
|
46
|
+
parseArgv
|
|
47
|
+
};
|
package/lib/esm/tty.js
ADDED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./cancel-signals.js";
|
|
2
|
+
export * from "./error-data.js";
|
|
3
|
+
export * from "./signal-methods.js";
|
|
4
|
+
export * from "./stack-trace-frame.js";
|
|
5
|
+
export * from "./structured-log-data.js";
|
|
6
|
+
export * from "./uncaught-exception-data.js";
|
|
7
|
+
export * from "./unhandled-rejection-data.js";
|
|
8
|
+
export * from "./user-info.js";
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const cli: (commandLine: string) => string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines the static Error.captureStackTrace method,
|
|
3
|
+
* this is not present in SpiderMonkey because it comes from V8
|
|
4
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#static_methods
|
|
5
|
+
* @see https://nodejs.org/dist/latest-v18.x/docs/api/errors.html#errorcapturestacktracetargetobject-constructoropt
|
|
6
|
+
*/
|
|
7
|
+
export declare const initErrorV8Methods: (ErrorConstructor: typeof Error) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const readJSON: (path: string) => any;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from './types/index.js';
|
|
2
|
+
export * from './cancel-handler.js';
|
|
3
|
+
export * from './cli.js';
|
|
4
|
+
export * from './error.js';
|
|
5
|
+
export * from './file.js';
|
|
6
|
+
export * from './fs.js';
|
|
7
|
+
export * from './log.js';
|
|
8
|
+
export * from './message.js';
|
|
9
|
+
export * from './os.js';
|
|
10
|
+
export * from './path.js';
|
|
11
|
+
export * from './process.js';
|
|
12
|
+
export * from './system.js';
|
|
13
|
+
export * from './tty.js';
|
|
14
|
+
export * from './version.js';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import GLib from '@girs/glib-2.0';
|
|
2
|
+
import type { StructuredLogData, SignalMethods, StackTraceFrame, ErrorData, UncaughtExceptionData, UnhandledRejectionData } from './types/index.js';
|
|
3
|
+
export declare const logLevelToString: (logLevel: GLib.LogLevelFlags) => "FLAG_FATAL" | "FLAG_RECURSION" | "LEVEL_CRITICAL" | "LEVEL_DEBUG" | "LEVEL_ERROR" | "LEVEL_INFO" | "LEVEL_MASK" | "LEVEL_MESSAGE" | "LEVEL_WARNING" | "UNKNOWN";
|
|
4
|
+
export declare const parseStackTrace: (stackTraceLine: string) => StackTraceFrame | null;
|
|
5
|
+
export declare const extractErrorData: (errorMessage: string) => ErrorData;
|
|
6
|
+
export interface LogSignals extends SignalMethods {
|
|
7
|
+
connect(sigName: "unhandledRejection", callback: (self: LogSignals, structuredData: StructuredLogData, promiseData: UnhandledRejectionData) => void): number;
|
|
8
|
+
connect(sigName: "uncaughtException", callback: (self: LogSignals, structuredData: StructuredLogData, errorData: UncaughtExceptionData) => void): number;
|
|
9
|
+
emit(sigName: "unhandledRejection", structuredData: StructuredLogData, promiseData: UnhandledRejectionData): void;
|
|
10
|
+
emit(sigName: "uncaughtException", structuredData: StructuredLogData, errorData: UncaughtExceptionData): void;
|
|
11
|
+
}
|
|
12
|
+
export declare class LogSignals {
|
|
13
|
+
private static instance;
|
|
14
|
+
/** This is a singleton because log_set_writer_func may only be called once */
|
|
15
|
+
private constructor();
|
|
16
|
+
static getSingleton(): LogSignals;
|
|
17
|
+
private initHandler;
|
|
18
|
+
/**
|
|
19
|
+
* A log handler to emit `unhandledRejection` and `uncaughtException` events
|
|
20
|
+
* @param level The log level
|
|
21
|
+
* @param data The structured log data
|
|
22
|
+
* @returns `true` to catch the log or `false` to output the error to the console as usual
|
|
23
|
+
*/
|
|
24
|
+
handler(level: GLib.LogLevelFlags, structuredData: StructuredLogData): boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Emits log signals like `unhandledRejection` and `unhandledRejection`
|
|
28
|
+
*/
|
|
29
|
+
export declare const logSignals: LogSignals;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { UserInfo } from './types/index.js';
|
|
2
|
+
export declare const getArch: () => string;
|
|
3
|
+
export declare const getOs: () => string;
|
|
4
|
+
export declare const getVendor: () => string;
|
|
5
|
+
export declare const getEnv: () => string;
|
|
6
|
+
export declare const getTarget: () => string;
|
|
7
|
+
export declare const getUserInfo: (username?: string) => UserInfo;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import Gio from '@girs/gio-2.0';
|
|
2
|
+
export declare const resolve: (dir: string, ...filenames: string[]) => Gio.File;
|
|
3
|
+
export declare const getProgramExe: () => Gio.File;
|
|
4
|
+
export declare const getProgramDir: () => string;
|
|
5
|
+
export declare const getPathSeparator: () => "/" | "\\";
|
|
6
|
+
export declare const getNodeModulesPath: () => Gio.File;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const STDIN_FD = 0;
|
|
2
|
+
export declare const STDOUT_FD = 1;
|
|
3
|
+
export declare const STDERR_FD = 2;
|
|
4
|
+
/** Check if the stdout file descriptor exists, if this is the case a tty (terminal) should exists (untested) */
|
|
5
|
+
export declare const existsTty: () => boolean;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './cancel-signals.js';
|
|
2
|
+
export * from './error-data.js';
|
|
3
|
+
export * from './signal-methods.js';
|
|
4
|
+
export * from './stack-trace-frame.js';
|
|
5
|
+
export * from './structured-log-data.js';
|
|
6
|
+
export * from './uncaught-exception-data.js';
|
|
7
|
+
export * from './unhandled-rejection-data.js';
|
|
8
|
+
export * from './user-info.js';
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* You can use the `Signals.addSignalMethods` method to apply the `Signals` convenience methods to an `Object`.
|
|
3
|
+
* Generally, this is called on an object prototype, but may also be called on an object instance.
|
|
4
|
+
* You can use this Interface for this object or prototype to make the methods in typescript known
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const Signals = imports.signals;
|
|
8
|
+
*
|
|
9
|
+
* // Define an interface with the same name of your class to make the methods known
|
|
10
|
+
* interface Events extends SignalMethods {}
|
|
11
|
+
*
|
|
12
|
+
* class Events {}
|
|
13
|
+
* Signals.addSignalMethods(Events.prototype);
|
|
14
|
+
*
|
|
15
|
+
* const events = new Events();
|
|
16
|
+
*
|
|
17
|
+
* // Typescript will not complain here
|
|
18
|
+
* events.emit("test-signal", "test argument");
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export interface SignalMethods {
|
|
22
|
+
/**
|
|
23
|
+
* Connects a callback to a signal for an object. Pass the returned ID to
|
|
24
|
+
* `disconect()` to remove the handler.
|
|
25
|
+
*
|
|
26
|
+
* If `callback` returns `true`, emission will stop and no other handlers will be
|
|
27
|
+
* invoked.
|
|
28
|
+
*
|
|
29
|
+
* > Warning: Unlike GObject signals, `this` within a signal callback will always
|
|
30
|
+
* > refer to the global object (ie. `globalThis`).
|
|
31
|
+
*
|
|
32
|
+
* @param sigName A signal name
|
|
33
|
+
* @param callback A callback function
|
|
34
|
+
* @returns A handler ID
|
|
35
|
+
*/
|
|
36
|
+
connect(sigName: string, callback: (self: any, ...args: any[]) => void): number;
|
|
37
|
+
/**
|
|
38
|
+
* Emits a signal for an object. Emission stops if a signal handler returns `true`.
|
|
39
|
+
*
|
|
40
|
+
* Unlike GObject signals, it is not necessary to declare signals or define their
|
|
41
|
+
* signature. Simply call `emit()` with whatever signal name you wish, with
|
|
42
|
+
* whatever arguments you wish.
|
|
43
|
+
* @param sigName A signal name
|
|
44
|
+
* @param args Any number of arguments, of any type
|
|
45
|
+
*/
|
|
46
|
+
emit(sigName: string, ...args: any[]): void;
|
|
47
|
+
/**
|
|
48
|
+
* Disconnects a handler for a signal.
|
|
49
|
+
* @param id The ID of the handler to be disconnected
|
|
50
|
+
*/
|
|
51
|
+
disconnect(id: number): void;
|
|
52
|
+
/**
|
|
53
|
+
* Disconnects all signal handlers for an object.
|
|
54
|
+
*/
|
|
55
|
+
disconnectAll(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Checks if a handler ID is connected.
|
|
58
|
+
* @param id The ID of the handler to be disconnected
|
|
59
|
+
* @returns `true` if connected, or `false` if not
|
|
60
|
+
*/
|
|
61
|
+
signalHandlerIsConnected(id: number): boolean;
|
|
62
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getGjsVersion: () => string;
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gjsify/utils",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Utils module for gjsify",
|
|
5
|
+
"main": "lib/cjs/index.js",
|
|
6
|
+
"module": "lib/esm/index.js",
|
|
7
|
+
"types": "lib/types/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./lib/types/index.d.ts",
|
|
13
|
+
"default": "./lib/esm/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./lib/types/index.d.ts",
|
|
17
|
+
"default": "./lib/cjs/index.js"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
|
|
23
|
+
"print:name": "echo '@gjsify/utils'",
|
|
24
|
+
"build": "yarn print:name && yarn build:gjsify && yarn build:types",
|
|
25
|
+
"build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
|
|
26
|
+
"build:types": "tsc --project tsconfig.types.json",
|
|
27
|
+
"build:test": "yarn build:test:gjs",
|
|
28
|
+
"build:test:gjs": "gjsify build src/test.ts --app gjs --outfile test.gjs.js",
|
|
29
|
+
"test": "yarn print:name && yarn build:gjsify && yarn build:test && yarn test:gjs",
|
|
30
|
+
"test:gjs": "gjs -m test.gjs.js"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"gjs",
|
|
34
|
+
"node",
|
|
35
|
+
"fs"
|
|
36
|
+
],
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@gjsify/cli": "^0.0.2",
|
|
39
|
+
"typescript": "^5.1.3"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@girs/gio-2.0": "2.76.1-3.1.0",
|
|
43
|
+
"@girs/gjs": "^3.1.0",
|
|
44
|
+
"@girs/glib-2.0": "2.76.1-3.1.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CancelSignals } from './types/index.js';
|
|
2
|
+
|
|
3
|
+
let nextRid = 1;
|
|
4
|
+
const resources: {[rid: number]: CancelSignals} = {};
|
|
5
|
+
|
|
6
|
+
export const createCancelHandler = () => {
|
|
7
|
+
const rid = nextRid++;
|
|
8
|
+
const cancelHandler = {} as CancelSignals;
|
|
9
|
+
imports.signals.addSignalMethods(cancelHandler);
|
|
10
|
+
|
|
11
|
+
resources[rid] = cancelHandler;
|
|
12
|
+
|
|
13
|
+
return rid;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const closeCancelHandler = (rid: number) => {
|
|
17
|
+
const cancelHandler = getCancelHandler(rid);
|
|
18
|
+
cancelHandler?.emit('close');
|
|
19
|
+
delete resources[rid];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const getCancelHandler = (rid: number): CancelSignals | undefined => {
|
|
23
|
+
return resources[rid];
|
|
24
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import GLib from '@girs/glib-2.0';
|
|
2
|
+
const byteArray = imports.byteArray;
|
|
3
|
+
|
|
4
|
+
export const cli = (commandLine: string): string => {
|
|
5
|
+
const [res, out, err, status] = GLib.spawn_command_line_sync(commandLine);
|
|
6
|
+
|
|
7
|
+
if(err.byteLength) throw new Error(byteArray.toString(err));
|
|
8
|
+
|
|
9
|
+
return byteArray.toString(out);
|
|
10
|
+
};
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines the static Error.captureStackTrace method,
|
|
3
|
+
* this is not present in SpiderMonkey because it comes from V8
|
|
4
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#static_methods
|
|
5
|
+
* @see https://nodejs.org/dist/latest-v18.x/docs/api/errors.html#errorcapturestacktracetargetobject-constructoropt
|
|
6
|
+
*/
|
|
7
|
+
export const initErrorV8Methods = (ErrorConstructor: typeof Error) => {
|
|
8
|
+
// See https://nodejs.org/dist/latest-v18.x/docs/api/errors.html#errorcapturestacktracetargetobject-constructoropt
|
|
9
|
+
if (!(Error as any).captureStackTrace) {
|
|
10
|
+
/**
|
|
11
|
+
* A non-standard V8 function.
|
|
12
|
+
* Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.
|
|
13
|
+
* @param targetObject
|
|
14
|
+
* @param constructorOpt
|
|
15
|
+
*/
|
|
16
|
+
(Error as any).captureStackTrace = function(targetObject: object, constructorOpt?: Function) {
|
|
17
|
+
const container = new Error();
|
|
18
|
+
|
|
19
|
+
const target = constructorOpt || targetObject;
|
|
20
|
+
|
|
21
|
+
Object.defineProperty(target, 'stack', {
|
|
22
|
+
configurable: true,
|
|
23
|
+
get: function getStack() {
|
|
24
|
+
var stack = container.stack;
|
|
25
|
+
|
|
26
|
+
Object.defineProperty(this, 'stack', {
|
|
27
|
+
value: stack
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return stack;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// TODO Error.stackTraceLimit()
|
|
37
|
+
// TODO Error.prepareStackTrace()
|
|
38
|
+
}
|
package/src/file.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import GLib from '@girs/glib-2.0';
|
|
2
|
+
|
|
3
|
+
const byteArray = imports.byteArray;
|
|
4
|
+
|
|
5
|
+
export const readJSON = (path: string) => {
|
|
6
|
+
const [ok, contents] = GLib.file_get_contents(path);
|
|
7
|
+
if (ok) {
|
|
8
|
+
const map = JSON.parse(byteArray.toString(contents));
|
|
9
|
+
return map;
|
|
10
|
+
}
|
|
11
|
+
throw new Error(`Error on require "${path}"`);
|
|
12
|
+
}
|