@elephant-wiki/cli 0.1.0 → 0.1.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/dist/elephant.mjs +120 -33
- package/package.json +1 -1
package/dist/elephant.mjs
CHANGED
|
@@ -3555,7 +3555,7 @@ import { join as join13, resolve as resolve5, sep as sep8 } from "node:path";
|
|
|
3555
3555
|
import { createHash as createHash4 } from "node:crypto";
|
|
3556
3556
|
import { homedir } from "node:os";
|
|
3557
3557
|
import { join as join10, resolve as resolve2 } from "node:path";
|
|
3558
|
-
var defaultOrigin = "https://
|
|
3558
|
+
var defaultOrigin = "https://web-production-e6454e4a.up.railway.app";
|
|
3559
3559
|
function dataDir(env, platform = process.platform, home = homedir()) {
|
|
3560
3560
|
if (env.ELEPHANT_DATA_DIR) return resolve2(env.ELEPHANT_DATA_DIR);
|
|
3561
3561
|
if (platform === "win32") return join10(env.LOCALAPPDATA || join10(home, "AppData", "Local"), "ElephantWiki");
|
|
@@ -3635,8 +3635,8 @@ async function excludeLocally(repo, target) {
|
|
|
3635
3635
|
// src/platform/service.ts
|
|
3636
3636
|
import { execFile as execFile3 } from "node:child_process";
|
|
3637
3637
|
import { existsSync as existsSync7 } from "node:fs";
|
|
3638
|
-
import { mkdir as mkdir4, rm as rm6 } from "node:fs/promises";
|
|
3639
|
-
import { homedir as homedir2 } from "node:os";
|
|
3638
|
+
import { mkdir as mkdir4, rm as rm6, writeFile as writeFile2 } from "node:fs/promises";
|
|
3639
|
+
import { homedir as homedir2, userInfo } from "node:os";
|
|
3640
3640
|
import { dirname as dirname5, join as join12 } from "node:path";
|
|
3641
3641
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
3642
3642
|
import { promisify as promisify3 } from "node:util";
|
|
@@ -3667,12 +3667,21 @@ function escapeXml(value) {
|
|
|
3667
3667
|
function escapeSystemdArg(value) {
|
|
3668
3668
|
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
3669
3669
|
}
|
|
3670
|
+
function escapeJScriptString(value) {
|
|
3671
|
+
return JSON.stringify(value).replace(
|
|
3672
|
+
/[^\x20-\x7e]/g,
|
|
3673
|
+
(ch) => `\\u${ch.charCodeAt(0).toString(16).padStart(4, "0")}`
|
|
3674
|
+
);
|
|
3675
|
+
}
|
|
3670
3676
|
function launchAgentPath(homeDir) {
|
|
3671
3677
|
return join12(homeDir, "Library", "LaunchAgents", `${launchAgentLabel}.plist`);
|
|
3672
3678
|
}
|
|
3673
3679
|
function systemdUnitPath(homeDir) {
|
|
3674
3680
|
return join12(homeDir, ".config", "systemd", "user", systemdUnitName);
|
|
3675
3681
|
}
|
|
3682
|
+
function windowsLauncherPath(dataDir2, taskName = windowsTaskName) {
|
|
3683
|
+
return join12(dataDir2, `${taskName}.launcher.js`);
|
|
3684
|
+
}
|
|
3676
3685
|
function renderLaunchAgentPlist(opts) {
|
|
3677
3686
|
const logFile = opts.logFile ?? join12(opts.dataDir, "logs", "daemon.log");
|
|
3678
3687
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
@@ -3724,21 +3733,75 @@ RestartSec=5
|
|
|
3724
3733
|
WantedBy=default.target
|
|
3725
3734
|
`;
|
|
3726
3735
|
}
|
|
3727
|
-
function
|
|
3728
|
-
const
|
|
3729
|
-
return
|
|
3736
|
+
function currentWindowsUser(env = process.env) {
|
|
3737
|
+
const user = env.USERNAME || userInfo().username;
|
|
3738
|
+
return env.USERDOMAIN ? `${env.USERDOMAIN}\\${user}` : user;
|
|
3739
|
+
}
|
|
3740
|
+
function renderWindowsLauncherScript(opts) {
|
|
3741
|
+
const commandLine = `"${opts.nodePath}" "${opts.cliEntry}" daemon run`;
|
|
3742
|
+
return [
|
|
3743
|
+
'var shell = new ActiveXObject("WScript.Shell");',
|
|
3744
|
+
`shell.Environment("PROCESS")("ELEPHANT_DATA_DIR") = ${escapeJScriptString(opts.dataDir)};`,
|
|
3745
|
+
`var code = shell.Run(${escapeJScriptString(commandLine)}, 0, true);`,
|
|
3746
|
+
"WScript.Quit(code);",
|
|
3747
|
+
""
|
|
3748
|
+
].join("\r\n");
|
|
3749
|
+
}
|
|
3750
|
+
function renderWindowsTaskXml(opts) {
|
|
3751
|
+
const user = escapeXml(opts.userId);
|
|
3752
|
+
return `<?xml version="1.0" encoding="UTF-16"?>
|
|
3753
|
+
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
|
3754
|
+
<RegistrationInfo>
|
|
3755
|
+
<Description>Elephant Wiki Local Folder Sync daemon</Description>
|
|
3756
|
+
</RegistrationInfo>
|
|
3757
|
+
<Triggers>
|
|
3758
|
+
<LogonTrigger>
|
|
3759
|
+
<Enabled>true</Enabled>
|
|
3760
|
+
<UserId>${user}</UserId>
|
|
3761
|
+
</LogonTrigger>
|
|
3762
|
+
</Triggers>
|
|
3763
|
+
<Principals>
|
|
3764
|
+
<Principal id="Author">
|
|
3765
|
+
<UserId>${user}</UserId>
|
|
3766
|
+
<LogonType>InteractiveToken</LogonType>
|
|
3767
|
+
<RunLevel>LeastPrivilege</RunLevel>
|
|
3768
|
+
</Principal>
|
|
3769
|
+
</Principals>
|
|
3770
|
+
<Settings>
|
|
3771
|
+
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
|
|
3772
|
+
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
|
|
3773
|
+
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
|
|
3774
|
+
<StartWhenAvailable>true</StartWhenAvailable>
|
|
3775
|
+
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
|
|
3776
|
+
<RestartOnFailure>
|
|
3777
|
+
<Interval>PT1M</Interval>
|
|
3778
|
+
<Count>999</Count>
|
|
3779
|
+
</RestartOnFailure>
|
|
3780
|
+
<Enabled>true</Enabled>
|
|
3781
|
+
</Settings>
|
|
3782
|
+
<Actions Context="Author">
|
|
3783
|
+
<Exec>
|
|
3784
|
+
<Command>wscript.exe</Command>
|
|
3785
|
+
<Arguments>${escapeXml(`//E:JScript //B //NoLogo "${opts.launcherPath}"`)}</Arguments>
|
|
3786
|
+
</Exec>
|
|
3787
|
+
</Actions>
|
|
3788
|
+
</Task>
|
|
3789
|
+
`;
|
|
3730
3790
|
}
|
|
3731
|
-
function
|
|
3732
|
-
return ["/
|
|
3791
|
+
function buildSchtasksCreateArgs(xmlPath, taskName = windowsTaskName) {
|
|
3792
|
+
return ["/create", "/tn", taskName, "/xml", xmlPath, "/f"];
|
|
3733
3793
|
}
|
|
3734
|
-
function
|
|
3735
|
-
return ["/
|
|
3794
|
+
function buildSchtasksRunArgs(taskName = windowsTaskName) {
|
|
3795
|
+
return ["/run", "/tn", taskName];
|
|
3736
3796
|
}
|
|
3737
|
-
function
|
|
3738
|
-
return ["/
|
|
3797
|
+
function buildSchtasksEndArgs(taskName = windowsTaskName) {
|
|
3798
|
+
return ["/end", "/tn", taskName];
|
|
3739
3799
|
}
|
|
3740
|
-
function
|
|
3741
|
-
return ["/
|
|
3800
|
+
function buildSchtasksDeleteArgs(taskName = windowsTaskName) {
|
|
3801
|
+
return ["/delete", "/tn", taskName, "/f"];
|
|
3802
|
+
}
|
|
3803
|
+
function buildSchtasksQueryArgs(taskName = windowsTaskName) {
|
|
3804
|
+
return ["/query", "/tn", taskName];
|
|
3742
3805
|
}
|
|
3743
3806
|
async function installService(ctx) {
|
|
3744
3807
|
const exec = ctx.exec ?? defaultServiceExec;
|
|
@@ -3757,25 +3820,45 @@ async function installService(ctx) {
|
|
|
3757
3820
|
await exec("launchctl", ["unload", "-w", unit]).catch(() => {
|
|
3758
3821
|
});
|
|
3759
3822
|
const loaded = await exec("launchctl", ["load", "-w", unit]);
|
|
3760
|
-
if (loaded.exitCode !== 0)
|
|
3823
|
+
if (loaded.exitCode !== 0) {
|
|
3824
|
+
await rm6(unit, { force: true });
|
|
3825
|
+
throw new Error(`launchctl load failed: ${loaded.stderr || loaded.stdout}`);
|
|
3826
|
+
}
|
|
3761
3827
|
return { platform: ctx.platform, unitPath: unit, taskName: launchAgentLabel };
|
|
3762
3828
|
}
|
|
3763
3829
|
if (ctx.platform === "linux") {
|
|
3764
3830
|
const unit = systemdUnitPath(home);
|
|
3765
3831
|
await mkdir4(dirname5(unit), { recursive: true });
|
|
3766
3832
|
await writeAtomic(unit, renderSystemdUnit(opts), 420);
|
|
3833
|
+
const failed = async (message) => {
|
|
3834
|
+
await rm6(unit, { force: true });
|
|
3835
|
+
return new Error(message);
|
|
3836
|
+
};
|
|
3767
3837
|
const reload = await exec("systemctl", ["--user", "daemon-reload"]);
|
|
3768
|
-
if (reload.exitCode !== 0) throw
|
|
3838
|
+
if (reload.exitCode !== 0) throw await failed(`systemctl --user daemon-reload failed: ${reload.stderr || reload.stdout}`);
|
|
3769
3839
|
const enable = await exec("systemctl", ["--user", "enable", "--now", systemdUnitName]);
|
|
3770
|
-
if (enable.exitCode !== 0) throw
|
|
3840
|
+
if (enable.exitCode !== 0) throw await failed(`systemctl --user enable failed: ${enable.stderr || enable.stdout}`);
|
|
3771
3841
|
return { platform: ctx.platform, unitPath: unit, taskName: systemdUnitName };
|
|
3772
3842
|
}
|
|
3773
3843
|
if (ctx.platform === "win32") {
|
|
3774
|
-
const
|
|
3775
|
-
|
|
3776
|
-
|
|
3844
|
+
const taskName = ctx.taskName ?? windowsTaskName;
|
|
3845
|
+
const launcherPath = windowsLauncherPath(ctx.dataDir, taskName);
|
|
3846
|
+
await writeAtomic(launcherPath, renderWindowsLauncherScript(opts), 420);
|
|
3847
|
+
const xmlPath = join12(ctx.dataDir, `${taskName}.task.xml`);
|
|
3848
|
+
const xml = renderWindowsTaskXml({ launcherPath, userId: ctx.windowsUser ?? currentWindowsUser() });
|
|
3849
|
+
await writeFile2(xmlPath, Buffer.concat([Buffer.from([255, 254]), Buffer.from(xml, "utf16le")]));
|
|
3850
|
+
try {
|
|
3851
|
+
const create = await exec("schtasks", buildSchtasksCreateArgs(xmlPath, taskName));
|
|
3852
|
+
if (create.exitCode !== 0) {
|
|
3853
|
+
await rm6(launcherPath, { force: true });
|
|
3854
|
+
throw new Error(`schtasks /create failed: ${create.stderr || create.stdout}`);
|
|
3855
|
+
}
|
|
3856
|
+
} finally {
|
|
3857
|
+
await rm6(xmlPath, { force: true });
|
|
3858
|
+
}
|
|
3859
|
+
const run4 = await exec("schtasks", buildSchtasksRunArgs(taskName));
|
|
3777
3860
|
if (run4.exitCode !== 0) throw new Error(`schtasks /run failed: ${run4.stderr || run4.stdout}`);
|
|
3778
|
-
return { platform: ctx.platform, unitPath:
|
|
3861
|
+
return { platform: ctx.platform, unitPath: launcherPath, taskName };
|
|
3779
3862
|
}
|
|
3780
3863
|
throw new Error(`Unsupported platform for user-level daemon service: ${ctx.platform}`);
|
|
3781
3864
|
}
|
|
@@ -3799,10 +3882,11 @@ async function uninstallService(ctx) {
|
|
|
3799
3882
|
return;
|
|
3800
3883
|
}
|
|
3801
3884
|
if (ctx.platform === "win32") {
|
|
3802
|
-
await exec("schtasks", buildSchtasksEndArgs()).catch(() => {
|
|
3885
|
+
await exec("schtasks", buildSchtasksEndArgs(ctx.taskName)).catch(() => {
|
|
3803
3886
|
});
|
|
3804
|
-
await exec("schtasks", buildSchtasksDeleteArgs()).catch(() => {
|
|
3887
|
+
await exec("schtasks", buildSchtasksDeleteArgs(ctx.taskName)).catch(() => {
|
|
3805
3888
|
});
|
|
3889
|
+
await rm6(windowsLauncherPath(ctx.dataDir, ctx.taskName), { force: true });
|
|
3806
3890
|
return;
|
|
3807
3891
|
}
|
|
3808
3892
|
throw new Error(`Unsupported platform for user-level daemon service: ${ctx.platform}`);
|
|
@@ -3813,7 +3897,7 @@ async function isServiceInstalled(ctx) {
|
|
|
3813
3897
|
if (ctx.platform === "darwin") return existsSync7(launchAgentPath(home));
|
|
3814
3898
|
if (ctx.platform === "linux") return existsSync7(systemdUnitPath(home));
|
|
3815
3899
|
if (ctx.platform === "win32") {
|
|
3816
|
-
const res = await exec("schtasks", buildSchtasksQueryArgs());
|
|
3900
|
+
const res = await exec("schtasks", buildSchtasksQueryArgs(ctx.taskName));
|
|
3817
3901
|
return res.exitCode === 0;
|
|
3818
3902
|
}
|
|
3819
3903
|
return false;
|
|
@@ -3847,7 +3931,9 @@ async function link(ctx, rawSlug, options, handoff) {
|
|
|
3847
3931
|
}
|
|
3848
3932
|
if (!root.paused_reason) {
|
|
3849
3933
|
if (handoff) throw new CliError("This folder is already linked to this Project.");
|
|
3850
|
-
|
|
3934
|
+
const exitCode2 = await resume(ctx, root, root.stateFile);
|
|
3935
|
+
await ensureBackgroundSync(ctx, options);
|
|
3936
|
+
return exitCode2;
|
|
3851
3937
|
}
|
|
3852
3938
|
}
|
|
3853
3939
|
throw new CliError(`${target} is already linked to ${root.project_slug ?? "the whole Bundle"}. Unlink it first or choose another folder.`);
|
|
@@ -3918,19 +4004,16 @@ Pass --git-exclude to exclude it locally, or use: elephant link ${slug} --dir .e
|
|
|
3918
4004
|
ctx.stdout(`Linked ${slug} to ${target}.
|
|
3919
4005
|
`);
|
|
3920
4006
|
const exitCode = await pull2(ctx, openRoot(file, binding.root_id), file);
|
|
3921
|
-
if (!handoff)
|
|
3922
|
-
const hadActiveRoot = roots.some((root) => !root.paused_reason && root.root_id !== replacedRoot?.root_id);
|
|
3923
|
-
await ensureBackgroundSync(ctx, hadActiveRoot, options);
|
|
3924
|
-
}
|
|
4007
|
+
if (!handoff) await ensureBackgroundSync(ctx, options);
|
|
3925
4008
|
return exitCode;
|
|
3926
4009
|
}
|
|
3927
|
-
async function ensureBackgroundSync(ctx,
|
|
4010
|
+
async function ensureBackgroundSync(ctx, options) {
|
|
3928
4011
|
try {
|
|
3929
4012
|
await ipcCall(ctx.dataDir, "reload", {}, ctx.fetch);
|
|
3930
4013
|
return;
|
|
3931
4014
|
} catch {
|
|
3932
4015
|
}
|
|
3933
|
-
if (options.noDaemon || ctx.env.ELEPHANT_NO_DAEMON === "1"
|
|
4016
|
+
if (options.noDaemon || ctx.env.ELEPHANT_NO_DAEMON === "1") return;
|
|
3934
4017
|
if (!ctx.serviceExec && process.env.VITEST) return;
|
|
3935
4018
|
const serviceCtx = { platform: ctx.platform, dataDir: ctx.dataDir, homeDir: ctx.homeDir, exec: ctx.serviceExec };
|
|
3936
4019
|
if (await isServiceInstalled(serviceCtx).catch(() => false)) return;
|
|
@@ -4763,7 +4846,11 @@ async function init(ctx, rawDir, options) {
|
|
|
4763
4846
|
replacedRoot = root;
|
|
4764
4847
|
continue;
|
|
4765
4848
|
}
|
|
4766
|
-
if (!root.paused_reason)
|
|
4849
|
+
if (!root.paused_reason) {
|
|
4850
|
+
const exitCode2 = await resume2(ctx, root, root.stateFile);
|
|
4851
|
+
await ensureBackgroundSync(ctx, options);
|
|
4852
|
+
return exitCode2;
|
|
4853
|
+
}
|
|
4767
4854
|
}
|
|
4768
4855
|
throw new CliError(`${target} is already linked to ${root.project_slug ?? "the whole Bundle"}. Unlink it first or choose another folder.`);
|
|
4769
4856
|
}
|
|
@@ -4835,7 +4922,7 @@ Pass --git-exclude to exclude it locally, or choose a folder outside Git.`);
|
|
|
4835
4922
|
ctx.stdout(`Initialized whole-Bundle Sync root at ${target}.
|
|
4836
4923
|
`);
|
|
4837
4924
|
const exitCode = await pull3(ctx, openRoot2(file, binding.root_id), file);
|
|
4838
|
-
await ensureBackgroundSync(ctx,
|
|
4925
|
+
await ensureBackgroundSync(ctx, options);
|
|
4839
4926
|
return exitCode;
|
|
4840
4927
|
}
|
|
4841
4928
|
function openRoot2(file, rootId) {
|