sudo-prompt-x 10.0.0-beta.1

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.
@@ -0,0 +1,152 @@
1
+ import { n as e, r as t } from "./exec-DzmncB7R.mjs";
2
+ import { t as n } from "./node-1fao-dnU.mjs";
3
+ import { mkdir as r, readFile as i, rm as a, stat as o, watch as s, writeFile as c } from "node:fs/promises";
4
+ import { join as l } from "node:path/win32";
5
+ import { setInterval as u } from "node:timers/promises";
6
+ function transformOptions(e, n) {
7
+ n = {
8
+ hooks: { resolve: (e) => Promise.resolve(e) },
9
+ ...t(e, n)
10
+ };
11
+ return {
12
+ ...n,
13
+ command: e
14
+ };
15
+ }
16
+ const d = "utf-8";
17
+ async function exec(e, t) {
18
+ let n = transformOptions(e, t);
19
+ try {
20
+ let e = await setup(n);
21
+ await elevate(e);
22
+ return await read(e);
23
+ } finally {
24
+ await a(n.tmpdir, {
25
+ force: !0,
26
+ recursive: !0
27
+ }).catch(() => {});
28
+ }
29
+ }
30
+ async function setup(e) {
31
+ let { command: t, tmpdir: n, env: i, cwd: a, signal: o, hooks: s } = e;
32
+ await r(await s.resolve(n), { recursive: !0 });
33
+ let u = l(n, "stdout");
34
+ let f = l(n, "stderr");
35
+ let p = l(n, "status");
36
+ let m = l(n, "command.bat");
37
+ let h = [
38
+ "@echo off",
39
+ `call "${m}" > "${u}" 2> "${f}"`,
40
+ `(echo %ERRORLEVEL%) > "${p}"`
41
+ ].join("\r\n");
42
+ let g = l(n, "execute.bat");
43
+ let _ = [
44
+ "@echo off",
45
+ "chcp 65001>nul",
46
+ `pushd ${a}`,
47
+ i ? Object.entries(i).map(([e, t]) => {
48
+ if (t === void 0) return null;
49
+ return `set ${e}=${t.replace(/([<>\\|&^])/g, "^$1")}`;
50
+ }).filter(Boolean).join("\r\n") : "",
51
+ t,
52
+ "popd"
53
+ ].join("\r\n");
54
+ await Promise.all([
55
+ c(await s.resolve(g), h, {
56
+ encoding: d,
57
+ signal: o
58
+ }),
59
+ c(await s.resolve(m), _, {
60
+ encoding: d,
61
+ signal: o
62
+ }),
63
+ c(await s.resolve(p), "", {
64
+ encoding: d,
65
+ signal: o
66
+ })
67
+ ]);
68
+ return {
69
+ options: e,
70
+ executeScriptPath: g,
71
+ commandScriptPath: m,
72
+ stdoutPath: u,
73
+ stderrPath: f,
74
+ statusPath: p
75
+ };
76
+ }
77
+ async function elevate({ executeScriptPath: e, options: { signal: t } }) {
78
+ let r = [
79
+ "powershell.exe",
80
+ "Start-Process",
81
+ "-FilePath",
82
+ `"'${e.replace(/'/g, "`'")}'"`,
83
+ "-WindowStyle hidden",
84
+ "-Verb runAs"
85
+ ].join(" ");
86
+ try {
87
+ await n(r, {
88
+ encoding: d,
89
+ signal: t
90
+ });
91
+ } catch (e) {
92
+ throw Error("User did not grant permission.", { cause: e });
93
+ }
94
+ }
95
+ async function readCode({ statusPath: e, options: { signal: t, hooks: n } }) {
96
+ let r = await n.resolve(e);
97
+ let { size: a } = await o(r);
98
+ if (a < 2) return null;
99
+ let s = await i(r, {
100
+ encoding: d,
101
+ signal: t
102
+ });
103
+ return Number.parseInt(s.trim(), 10);
104
+ }
105
+ async function readCodePolling(e) {
106
+ let { options: { signal: t } } = e;
107
+ for await (let n of u(500, null, { signal: t })) {
108
+ let t = await readCode(e);
109
+ if (t === null) continue;
110
+ return t;
111
+ }
112
+ throw null;
113
+ }
114
+ async function readCodeWatcher(e) {
115
+ let { statusPath: t, options: { signal: n, hooks: r } } = e;
116
+ let i = new AbortController();
117
+ let a = s(await r.resolve(t), { signal: n ? AbortSignal.any([n, i.signal]) : i.signal });
118
+ for await (let t of a) {
119
+ let t = await readCode(e);
120
+ if (t === null) continue;
121
+ i.abort();
122
+ return t;
123
+ }
124
+ throw null;
125
+ }
126
+ async function read(t) {
127
+ let { stdoutPath: n, stderrPath: r, options: { command: a, signal: o, hooks: s } } = t;
128
+ let c = new AbortController();
129
+ let l = o ? AbortSignal.any([o, c.signal]) : c.signal;
130
+ let u = {
131
+ ...t,
132
+ options: {
133
+ ...t.options,
134
+ signal: l
135
+ }
136
+ };
137
+ let f = await readCode(u) || await Promise.race([readCodeWatcher(u), readCodePolling(u)]);
138
+ c.abort();
139
+ let [p, m] = await Promise.all([i(await s.resolve(n), {
140
+ encoding: d,
141
+ signal: o
142
+ }), i(await s.resolve(r), {
143
+ encoding: d,
144
+ signal: o
145
+ })]);
146
+ return e(a, {
147
+ code: f,
148
+ stdout: p,
149
+ stderr: m
150
+ });
151
+ }
152
+ export { exec as t };
@@ -0,0 +1,42 @@
1
+ const e = require("../exec-BjS_tGOr.cjs");
2
+ const t = require("../node-D37hqdIB.cjs");
3
+ const n = require("../windows-DhHQkh7V.cjs");
4
+ let r = require("node:path/win32");
5
+ async function tmpdir() {
6
+ let { stdout: e } = await t.n("powershell.exe", ["-command", "[System.IO.Path]::GetTempPath()"]);
7
+ return e.trim();
8
+ }
9
+ const i = /* @__PURE__ */ new Map();
10
+ async function linuxToWindows(e) {
11
+ let n = e.toString();
12
+ if (i.has(n)) return i.get(n);
13
+ let { stdout: r } = await t.n("wslpath", ["-w", n]);
14
+ let a = r.trim();
15
+ i.set(n, a);
16
+ i.set(a, n);
17
+ return a;
18
+ }
19
+ async function windowsToLinux(e) {
20
+ let n = e.toString();
21
+ if (i.has(n)) return i.get(n);
22
+ let { stdout: r } = await t.n("wslpath", ["-u", n]);
23
+ let a = r.trim();
24
+ i.set(n, a);
25
+ i.set(a, n);
26
+ return a;
27
+ }
28
+ async function transformOptions(t, n) {
29
+ return {
30
+ ...e.r(t, n),
31
+ tmpdir: n?.tmpdir ?? (0, r.join)(await tmpdir(), crypto.randomUUID())
32
+ };
33
+ }
34
+ async function exec(e, t) {
35
+ let r = await transformOptions(e, t);
36
+ return await n.t(e, {
37
+ ...r,
38
+ cwd: await linuxToWindows(r.cwd),
39
+ hooks: { resolve: windowsToLinux }
40
+ });
41
+ }
42
+ exports.exec = exec;
@@ -0,0 +1,9 @@
1
+ import { i as ExecOptionsBase, n as ExecResult } from "../index-kvxLnCHI.cjs";
2
+
3
+ //#region src/platforms/wsl/options.d.ts
4
+ interface ExecOptionsWsl extends ExecOptionsBase {}
5
+ //#endregion
6
+ //#region src/platforms/wsl/index.d.ts
7
+ declare function exec(command: string, options?: ExecOptionsWsl): Promise<ExecResult>;
8
+ //#endregion
9
+ export { exec };
@@ -0,0 +1,9 @@
1
+ import { i as ExecOptionsBase, n as ExecResult } from "../index-vd1MVV7_.mjs";
2
+
3
+ //#region src/platforms/wsl/options.d.ts
4
+ interface ExecOptionsWsl extends ExecOptionsBase {}
5
+ //#endregion
6
+ //#region src/platforms/wsl/index.d.ts
7
+ declare function exec(command: string, options?: ExecOptionsWsl): Promise<ExecResult>;
8
+ //#endregion
9
+ export { exec };
@@ -0,0 +1,42 @@
1
+ import { r as e } from "../exec-DzmncB7R.mjs";
2
+ import { n as t } from "../node-1fao-dnU.mjs";
3
+ import { t as n } from "../windows-DlvX_MV7.mjs";
4
+ import { join as r } from "node:path/win32";
5
+ async function tmpdir() {
6
+ let { stdout: e } = await t("powershell.exe", ["-command", "[System.IO.Path]::GetTempPath()"]);
7
+ return e.trim();
8
+ }
9
+ const i = /* @__PURE__ */ new Map();
10
+ async function linuxToWindows(e) {
11
+ let n = e.toString();
12
+ if (i.has(n)) return i.get(n);
13
+ let { stdout: r } = await t("wslpath", ["-w", n]);
14
+ let a = r.trim();
15
+ i.set(n, a);
16
+ i.set(a, n);
17
+ return a;
18
+ }
19
+ async function windowsToLinux(e) {
20
+ let n = e.toString();
21
+ if (i.has(n)) return i.get(n);
22
+ let { stdout: r } = await t("wslpath", ["-u", n]);
23
+ let a = r.trim();
24
+ i.set(n, a);
25
+ i.set(a, n);
26
+ return a;
27
+ }
28
+ async function transformOptions(t, n) {
29
+ return {
30
+ ...e(t, n),
31
+ tmpdir: n?.tmpdir ?? r(await tmpdir(), crypto.randomUUID())
32
+ };
33
+ }
34
+ async function exec(e, t) {
35
+ let r = await transformOptions(e, t);
36
+ return await n(e, {
37
+ ...r,
38
+ cwd: await linuxToWindows(r.cwd),
39
+ hooks: { resolve: windowsToLinux }
40
+ });
41
+ }
42
+ export { exec };
package/package.json ADDED
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "sudo-prompt-x",
3
+ "version": "10.0.0-beta.1",
4
+ "type": "module",
5
+ "description": "Run a command using sudo, prompting the user with an OS dialog if necessary",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.cts",
9
+ "exports": {
10
+ ".": {
11
+ "require": "./dist/index.cjs",
12
+ "import": "./dist/index.mjs"
13
+ },
14
+ "./linux": {
15
+ "require": "./dist/linux/index.cjs",
16
+ "import": "./dist/linux/index.mjs"
17
+ },
18
+ "./mac": {
19
+ "require": "./dist/mac/index.cjs",
20
+ "import": "./dist/mac/index.mjs"
21
+ },
22
+ "./windows": {
23
+ "require": "./dist/windows/index.cjs",
24
+ "import": "./dist/windows/index.mjs"
25
+ },
26
+ "./wsl": {
27
+ "require": "./dist/wsl/index.cjs",
28
+ "import": "./dist/wsl/index.mjs"
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "files": [
33
+ "LICENSE",
34
+ "README.md",
35
+ "dist",
36
+ "package.json"
37
+ ],
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/bpasero/sudo-prompt.git"
41
+ },
42
+ "keywords": [
43
+ "sudo",
44
+ "os",
45
+ "dialog",
46
+ "prompt",
47
+ "command",
48
+ "exec",
49
+ "user access control",
50
+ "UAC",
51
+ "privileges",
52
+ "administrative",
53
+ "elevate",
54
+ "run as administrator"
55
+ ],
56
+ "author": "Joran Dirk Greef",
57
+ "license": "MIT",
58
+ "bugs": {
59
+ "url": "https://github.com/bpasero/sudo-prompt/issues"
60
+ },
61
+ "homepage": "https://github.com/bpasero/sudo-prompt#readme",
62
+ "devDependencies": {
63
+ "@types/node": "^20.0.0",
64
+ "jszip": "^3.10.1",
65
+ "tsdown": "^0.18.2",
66
+ "typescript": "^5.0.0"
67
+ },
68
+ "engines": {
69
+ "node": ">=18.17.0"
70
+ },
71
+ "dependencies": {
72
+ "wsl-path": "^4.0.0",
73
+ "wsl-utils": "^0.3.0"
74
+ },
75
+ "scripts": {
76
+ "build": "tsdown --config-loader unrun",
77
+ "test": "npm run build && node dist/test.js",
78
+ "test:concurrent": "npm run build && node dist/test-concurrent.js"
79
+ }
80
+ }