@gjsify/utils 0.0.4 → 0.1.0

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.
Files changed (119) hide show
  1. package/README.md +26 -0
  2. package/lib/esm/base64.js +72 -0
  3. package/lib/esm/byte-array.js +6 -0
  4. package/lib/esm/cli.js +1 -2
  5. package/lib/esm/defer.js +6 -0
  6. package/lib/esm/encoding.js +35 -0
  7. package/lib/esm/fs.js +2 -1
  8. package/lib/esm/gio-errors.js +98 -0
  9. package/lib/esm/gio.js +39 -0
  10. package/lib/esm/globals.js +8 -0
  11. package/lib/esm/index.js +10 -8
  12. package/lib/esm/main-loop.js +28 -0
  13. package/lib/esm/next-tick.js +6 -0
  14. package/lib/esm/structured-clone.js +169 -0
  15. package/lib/types/base64.d.ts +8 -0
  16. package/lib/types/byte-array.d.ts +7 -0
  17. package/lib/types/defer.d.ts +7 -0
  18. package/lib/types/encoding.d.ts +11 -0
  19. package/lib/types/gio-errors.d.ts +28 -0
  20. package/lib/types/gio.d.ts +17 -0
  21. package/lib/types/globals.d.ts +5 -0
  22. package/lib/types/index.d.ts +10 -8
  23. package/lib/types/main-loop.d.ts +20 -0
  24. package/lib/types/next-tick.d.ts +5 -0
  25. package/lib/types/structured-clone.d.ts +15 -0
  26. package/package.json +18 -20
  27. package/src/base64.ts +78 -0
  28. package/src/byte-array.ts +12 -0
  29. package/src/defer.ts +11 -0
  30. package/src/encoding.ts +36 -0
  31. package/src/fs.ts +2 -2
  32. package/src/gio-errors.ts +101 -0
  33. package/src/gio.ts +67 -0
  34. package/src/globals.ts +13 -0
  35. package/src/index.ts +10 -9
  36. package/src/main-loop.ts +62 -0
  37. package/src/next-tick.ts +16 -0
  38. package/src/structured-clone.ts +242 -0
  39. package/src/test.ts +1 -3
  40. package/tsconfig.json +27 -10
  41. package/tsconfig.tsbuildinfo +1 -0
  42. package/lib/cjs/cancel-handler.js +0 -22
  43. package/lib/cjs/cli.js +0 -11
  44. package/lib/cjs/error.js +0 -21
  45. package/lib/cjs/file.js +0 -13
  46. package/lib/cjs/fs.js +0 -21
  47. package/lib/cjs/index.js +0 -14
  48. package/lib/cjs/log.js +0 -159
  49. package/lib/cjs/message.js +0 -13
  50. package/lib/cjs/os.js +0 -81
  51. package/lib/cjs/path.js +0 -50
  52. package/lib/cjs/process.js +0 -41
  53. package/lib/cjs/system.js +0 -47
  54. package/lib/cjs/tty.js +0 -13
  55. package/lib/cjs/types/cancel-signals.js +0 -0
  56. package/lib/cjs/types/error-data.js +0 -0
  57. package/lib/cjs/types/index.js +0 -8
  58. package/lib/cjs/types/signal-methods.js +0 -0
  59. package/lib/cjs/types/stack-trace-frame.js +0 -0
  60. package/lib/cjs/types/structured-log-data.js +0 -0
  61. package/lib/cjs/types/uncaught-exception-data.js +0 -0
  62. package/lib/cjs/types/unhandled-rejection-data.js +0 -0
  63. package/lib/cjs/types/user-info.js +0 -0
  64. package/lib/cjs/version.js +0 -7
  65. package/lib/esm/cancel-handler.js +0 -22
  66. package/lib/esm/log.js +0 -159
  67. package/lib/esm/os.js +0 -81
  68. package/lib/esm/process.js +0 -41
  69. package/lib/esm/system.js +0 -47
  70. package/lib/esm/tty.js +0 -13
  71. package/lib/esm/types/cancel-signals.js +0 -0
  72. package/lib/esm/types/error-data.js +0 -0
  73. package/lib/esm/types/index.js +0 -8
  74. package/lib/esm/types/signal-methods.js +0 -0
  75. package/lib/esm/types/stack-trace-frame.js +0 -0
  76. package/lib/esm/types/structured-log-data.js +0 -0
  77. package/lib/esm/types/uncaught-exception-data.js +0 -0
  78. package/lib/esm/types/unhandled-rejection-data.js +0 -0
  79. package/lib/esm/types/user-info.js +0 -0
  80. package/lib/esm/version.js +0 -7
  81. package/lib/types/cancel-handler.d.ts +0 -4
  82. package/lib/types/log.d.ts +0 -29
  83. package/lib/types/os.d.ts +0 -7
  84. package/lib/types/process.d.ts +0 -6
  85. package/lib/types/system.d.ts +0 -5
  86. package/lib/types/tty.d.ts +0 -5
  87. package/lib/types/types/cancel-signals.d.ts +0 -5
  88. package/lib/types/types/error-data.d.ts +0 -7
  89. package/lib/types/types/index.d.ts +0 -8
  90. package/lib/types/types/signal-methods.d.ts +0 -62
  91. package/lib/types/types/stack-trace-frame.d.ts +0 -6
  92. package/lib/types/types/structured-log-data.d.ts +0 -5
  93. package/lib/types/types/uncaught-exception-data.d.ts +0 -4
  94. package/lib/types/types/unhandled-rejection-data.d.ts +0 -5
  95. package/lib/types/types/user-info.d.ts +0 -8
  96. package/lib/types/version.d.ts +0 -1
  97. package/src/cancel-handler.ts +0 -24
  98. package/src/log.ts +0 -218
  99. package/src/os.ts +0 -94
  100. package/src/process.spec.ts +0 -24
  101. package/src/process.ts +0 -46
  102. package/src/system.ts +0 -56
  103. package/src/tty.spec.ts +0 -10
  104. package/src/tty.ts +0 -10
  105. package/src/types/cancel-signals.ts +0 -6
  106. package/src/types/error-data.ts +0 -8
  107. package/src/types/index.ts +0 -8
  108. package/src/types/signal-methods.ts +0 -63
  109. package/src/types/stack-trace-frame.ts +0 -7
  110. package/src/types/structured-log-data.ts +0 -5
  111. package/src/types/uncaught-exception-data.ts +0 -5
  112. package/src/types/unhandled-rejection-data.ts +0 -6
  113. package/src/types/user-info.ts +0 -9
  114. package/src/version.ts +0 -4
  115. package/test.gjs.js +0 -34758
  116. package/test.gjs.js.map +0 -7
  117. package/test.gjs.js.meta.json +0 -1
  118. package/tsconfig.types.json +0 -7
  119. package/tsconfig.types.tsbuildinfo +0 -1
package/lib/cjs/os.js DELETED
@@ -1,81 +0,0 @@
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/cjs/path.js DELETED
@@ -1,50 +0,0 @@
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
- };
@@ -1,41 +0,0 @@
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
- };
package/lib/cjs/system.js DELETED
@@ -1,47 +0,0 @@
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/cjs/tty.js DELETED
@@ -1,13 +0,0 @@
1
- import { existsFD } from "./fs.js";
2
- const STDIN_FD = 0;
3
- const STDOUT_FD = 1;
4
- const STDERR_FD = 2;
5
- const existsTty = () => {
6
- return existsFD(STDOUT_FD);
7
- };
8
- export {
9
- STDERR_FD,
10
- STDIN_FD,
11
- STDOUT_FD,
12
- existsTty
13
- };
File without changes
File without changes
@@ -1,8 +0,0 @@
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
@@ -1,7 +0,0 @@
1
- const getGjsVersion = () => {
2
- const v = imports.system.version.toString();
3
- return `${v[0]}.${+(v[1] + v[2])}.${+(v[3] + v[4])}`;
4
- };
5
- export {
6
- getGjsVersion
7
- };
@@ -1,22 +0,0 @@
1
- let nextRid = 1;
2
- const resources = {};
3
- const createCancelHandler = () => {
4
- const rid = nextRid++;
5
- const cancelHandler = {};
6
- imports.signals.addSignalMethods(cancelHandler);
7
- resources[rid] = cancelHandler;
8
- return rid;
9
- };
10
- const closeCancelHandler = (rid) => {
11
- const cancelHandler = getCancelHandler(rid);
12
- cancelHandler?.emit("close");
13
- delete resources[rid];
14
- };
15
- const getCancelHandler = (rid) => {
16
- return resources[rid];
17
- };
18
- export {
19
- closeCancelHandler,
20
- createCancelHandler,
21
- getCancelHandler
22
- };
package/lib/esm/log.js DELETED
@@ -1,159 +0,0 @@
1
- import GLib from "@girs/glib-2.0";
2
- const logLevelToString = (logLevel) => {
3
- switch (logLevel) {
4
- case GLib.LogLevelFlags.FLAG_FATAL:
5
- return "FLAG_FATAL";
6
- case GLib.LogLevelFlags.FLAG_RECURSION:
7
- return "FLAG_RECURSION";
8
- case GLib.LogLevelFlags.LEVEL_CRITICAL:
9
- return "LEVEL_CRITICAL";
10
- case GLib.LogLevelFlags.LEVEL_DEBUG:
11
- return "LEVEL_DEBUG";
12
- case GLib.LogLevelFlags.LEVEL_ERROR:
13
- return "LEVEL_ERROR";
14
- case GLib.LogLevelFlags.LEVEL_INFO:
15
- return "LEVEL_INFO";
16
- case GLib.LogLevelFlags.LEVEL_MASK:
17
- return "LEVEL_MASK";
18
- case GLib.LogLevelFlags.LEVEL_MESSAGE:
19
- return "LEVEL_MESSAGE";
20
- case GLib.LogLevelFlags.LEVEL_WARNING:
21
- return "LEVEL_WARNING";
22
- default:
23
- return "UNKNOWN";
24
- }
25
- };
26
- const STACK_TRACE_REGEX = /^.*@(.*):(\d+):(\d+)/;
27
- const parseStackTrace = (stackTraceLine) => {
28
- const match = stackTraceLine.match(STACK_TRACE_REGEX);
29
- if (match) {
30
- const [, fileName, lineNumber, columnNumber] = match;
31
- return { fileName, lineNumber: Number(lineNumber), columnNumber: Number(columnNumber), line: stackTraceLine };
32
- }
33
- return null;
34
- };
35
- const getStackTraceStartLineIndex = (lines) => {
36
- for (let i = 1; i < lines.length; i++) {
37
- const line = lines[i];
38
- if (STACK_TRACE_REGEX.test(line)) {
39
- return i;
40
- }
41
- }
42
- return -1;
43
- };
44
- const extractErrorData = (errorMessage) => {
45
- const lines = errorMessage.split("\n");
46
- for (let line of lines) {
47
- line = line.trim();
48
- }
49
- const endOfErrorType = lines[0].indexOf(": ");
50
- let errorType = "Error";
51
- if (endOfErrorType > 0) {
52
- errorType = lines[0].slice(0, endOfErrorType);
53
- if (globalThis[errorType]) {
54
- lines[0] = lines[0].slice(endOfErrorType + 2);
55
- } else {
56
- errorType = "Error";
57
- }
58
- }
59
- let stackTraceLineIndex = getStackTraceStartLineIndex(lines);
60
- const message = lines.slice(0, stackTraceLineIndex).join("\n");
61
- const stackTraceLines = lines.slice(stackTraceLineIndex);
62
- const frames = [];
63
- for (const stackTraceLine of stackTraceLines) {
64
- const frame = parseStackTrace(stackTraceLine);
65
- if (frame) {
66
- frames.push(frame);
67
- }
68
- }
69
- return {
70
- errorType,
71
- message,
72
- frames,
73
- stackTraceLines
74
- };
75
- };
76
- const reconstructErrorFromMessage = (errorMessage) => {
77
- const { errorType, frames, message, stackTraceLines } = extractErrorData(errorMessage);
78
- const ErrorType = globalThis[errorType];
79
- const error = new ErrorType();
80
- error.message = message;
81
- error.stack = stackTraceLines.join("\n");
82
- return {
83
- error,
84
- errorType,
85
- frames,
86
- message,
87
- stackTraceLines
88
- };
89
- };
90
- class LogSignals {
91
- static instance;
92
- /** This is a singleton because log_set_writer_func may only be called once */
93
- constructor() {
94
- this.initHandler();
95
- }
96
- static getSingleton() {
97
- if (LogSignals.instance) {
98
- return LogSignals.instance;
99
- }
100
- LogSignals.instance = new LogSignals();
101
- return LogSignals.instance;
102
- }
103
- initHandler() {
104
- GLib.log_set_writer_func((level, fields) => {
105
- const decoder = new TextDecoder("utf-8");
106
- const message = decoder.decode(fields?.MESSAGE);
107
- const priority = Number(decoder.decode(fields?.PRIORITY));
108
- const domain = decoder.decode(fields?.GLIB_DOMAIN);
109
- const data = {
110
- message,
111
- priority,
112
- domain
113
- };
114
- if (!this.handler(level, data)) {
115
- level |= GLib.LogLevelFlags.FLAG_RECURSION;
116
- GLib.log_default_handler(domain, level, message, null);
117
- }
118
- return GLib.LogWriterOutput.HANDLED;
119
- });
120
- }
121
- /**
122
- * A log handler to emit `unhandledRejection` and `uncaughtException` events
123
- * @param level The log level
124
- * @param data The structured log data
125
- * @returns `true` to catch the log or `false` to output the error to the console as usual
126
- */
127
- handler(level, structuredData) {
128
- if (level === GLib.LogLevelFlags.LEVEL_WARNING && structuredData.domain === "Gjs" && structuredData.message.startsWith("Unhandled promise rejection")) {
129
- try {
130
- const errorData = reconstructErrorFromMessage(structuredData.message);
131
- const fakePromise = new Promise(() => {
132
- });
133
- logSignals.emit("unhandledRejection", structuredData, {
134
- reason: errorData.error,
135
- promise: fakePromise,
136
- errorType: errorData.errorType,
137
- frames: errorData.frames,
138
- message: errorData.message,
139
- stackTraceLines: errorData.stackTraceLines
140
- });
141
- } catch (error) {
142
- printerr(error);
143
- }
144
- } else if (level === GLib.LogLevelFlags.LEVEL_CRITICAL) {
145
- const errorData = reconstructErrorFromMessage(structuredData.message);
146
- logSignals.emit("uncaughtException", structuredData, errorData);
147
- }
148
- return false;
149
- }
150
- }
151
- imports.signals.addSignalMethods(LogSignals.prototype);
152
- const logSignals = LogSignals.getSingleton();
153
- export {
154
- LogSignals,
155
- extractErrorData,
156
- logLevelToString,
157
- logSignals,
158
- parseStackTrace
159
- };
package/lib/esm/os.js DELETED
@@ -1,81 +0,0 @@
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
- };
@@ -1,41 +0,0 @@
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
- };
package/lib/esm/system.js DELETED
@@ -1,47 +0,0 @@
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 DELETED
@@ -1,13 +0,0 @@
1
- import { existsFD } from "./fs.js";
2
- const STDIN_FD = 0;
3
- const STDOUT_FD = 1;
4
- const STDERR_FD = 2;
5
- const existsTty = () => {
6
- return existsFD(STDOUT_FD);
7
- };
8
- export {
9
- STDERR_FD,
10
- STDIN_FD,
11
- STDOUT_FD,
12
- existsTty
13
- };
File without changes
File without changes
@@ -1,8 +0,0 @@
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
@@ -1,7 +0,0 @@
1
- const getGjsVersion = () => {
2
- const v = imports.system.version.toString();
3
- return `${v[0]}.${+(v[1] + v[2])}.${+(v[3] + v[4])}`;
4
- };
5
- export {
6
- getGjsVersion
7
- };
@@ -1,4 +0,0 @@
1
- import type { CancelSignals } from './types/index.js';
2
- export declare const createCancelHandler: () => number;
3
- export declare const closeCancelHandler: (rid: number) => void;
4
- export declare const getCancelHandler: (rid: number) => CancelSignals | undefined;