bit-office 0.1.1 → 0.1.3

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/index.js CHANGED
@@ -4227,16 +4227,37 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
4227
4227
  import { homedir } from "os";
4228
4228
  import { randomBytes } from "crypto";
4229
4229
  var __dirname = dirname(fileURLToPath(import.meta.url));
4230
+ var CONFIG_DIR = resolve(homedir(), ".bit-office");
4231
+ var CONFIG_FILE = resolve(CONFIG_DIR, "config.json");
4232
+ function ensureConfigDir() {
4233
+ if (!existsSync(CONFIG_DIR)) {
4234
+ mkdirSync(CONFIG_DIR, { recursive: true });
4235
+ }
4236
+ }
4237
+ function loadSavedConfig() {
4238
+ if (!existsSync(CONFIG_FILE)) return {};
4239
+ try {
4240
+ return JSON.parse(readFileSync(CONFIG_FILE, "utf-8"));
4241
+ } catch {
4242
+ return {};
4243
+ }
4244
+ }
4245
+ function saveConfig(cfg) {
4246
+ ensureConfigDir();
4247
+ writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2), "utf-8");
4248
+ }
4249
+ function hasSetupRun() {
4250
+ return existsSync(CONFIG_FILE);
4251
+ }
4230
4252
  function getOrCreateMachineId() {
4231
- const configDir = resolve(homedir(), ".bit-office");
4232
- const configFile = resolve(configDir, "machine-id");
4233
- if (existsSync(configFile)) {
4234
- return readFileSync(configFile, "utf-8").trim();
4253
+ ensureConfigDir();
4254
+ const idFile = resolve(CONFIG_DIR, "machine-id");
4255
+ if (existsSync(idFile)) {
4256
+ return readFileSync(idFile, "utf-8").trim();
4235
4257
  }
4236
4258
  const id = `mac-${randomBytes(4).toString("hex")}`;
4237
- mkdirSync(configDir, { recursive: true });
4238
- writeFileSync(configFile, id, "utf-8");
4239
- console.log(`[Config] Generated machine ID: ${id} (saved to ${configFile})`);
4259
+ writeFileSync(idFile, id, "utf-8");
4260
+ console.log(`[Config] Generated machine ID: ${id}`);
4240
4261
  return id;
4241
4262
  }
4242
4263
  function resolveWebDir() {
@@ -4245,19 +4266,22 @@ function resolveWebDir() {
4245
4266
  if (existsSync(bundled)) return bundled;
4246
4267
  return resolve(__dirname, "../../web/out");
4247
4268
  }
4248
- function parseTelegramTokens() {
4249
- const raw = process.env.TELEGRAM_BOT_TOKENS;
4250
- if (!raw) return [];
4251
- return raw.split(",").map((t) => t.trim() || void 0);
4269
+ function buildConfig() {
4270
+ const saved = loadSavedConfig();
4271
+ return {
4272
+ machineId: getOrCreateMachineId(),
4273
+ defaultWorkspace: process.cwd(),
4274
+ wsPort: 9090,
4275
+ ablyApiKey: process.env.ABLY_API_KEY || saved.ablyApiKey || void 0,
4276
+ webDir: resolveWebDir(),
4277
+ telegramBotTokens: process.env.TELEGRAM_BOT_TOKENS ? process.env.TELEGRAM_BOT_TOKENS.split(",").map((t) => t.trim() || void 0) : (saved.telegramBotTokens ?? []).map((t) => t || void 0)
4278
+ };
4279
+ }
4280
+ var config = buildConfig();
4281
+ function reloadConfig() {
4282
+ const fresh = buildConfig();
4283
+ Object.assign(config, fresh);
4252
4284
  }
4253
- var config = {
4254
- machineId: getOrCreateMachineId(),
4255
- defaultWorkspace: process.cwd(),
4256
- wsPort: 9090,
4257
- ablyApiKey: process.env.ABLY_API_KEY || void 0,
4258
- webDir: resolveWebDir(),
4259
- telegramBotTokens: parseTelegramTokens()
4260
- };
4261
4285
 
4262
4286
  // src/ws-server.ts
4263
4287
  import { networkInterfaces } from "os";
@@ -4928,6 +4952,34 @@ var telegramChannel = {
4928
4952
  }
4929
4953
  };
4930
4954
 
4955
+ // src/setup.ts
4956
+ import { createInterface } from "readline";
4957
+ function ask(rl, question) {
4958
+ return new Promise((resolve2) => {
4959
+ rl.question(question, (answer) => resolve2(answer.trim()));
4960
+ });
4961
+ }
4962
+ async function runSetup() {
4963
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
4964
+ console.log("");
4965
+ console.log("\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557");
4966
+ console.log("\u2551 Bit Office \u2014 First Setup \u2551");
4967
+ console.log("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D");
4968
+ console.log("");
4969
+ console.log("Press Enter to skip any step.\n");
4970
+ console.log("\u2500\u2500 Remote Access (Ably) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500");
4971
+ console.log("Enables access from outside your LAN.");
4972
+ const ablyApiKey = await ask(rl, "Ably API Key (optional): ");
4973
+ rl.close();
4974
+ saveConfig({
4975
+ ablyApiKey: ablyApiKey || void 0
4976
+ // telegramBotTokens: tokens,
4977
+ });
4978
+ console.log("\n\u2713 Config saved to ~/.bit-office/config.json");
4979
+ if (ablyApiKey) console.log(" \u2022 Ably: enabled");
4980
+ console.log(" \u2022 Run with --setup to reconfigure\n");
4981
+ }
4982
+
4931
4983
  // src/index.ts
4932
4984
  import { nanoid as nanoid3 } from "nanoid";
4933
4985
  import { exec } from "child_process";
@@ -5043,6 +5095,10 @@ function handleCommand(parsed) {
5043
5095
  }
5044
5096
  }
5045
5097
  async function main() {
5098
+ if (!hasSetupRun() || process.argv.includes("--setup")) {
5099
+ await runSetup();
5100
+ reloadConfig();
5101
+ }
5046
5102
  console.log(`[Gateway] Starting for machine: ${config.machineId}`);
5047
5103
  showPairCode();
5048
5104
  await initTransports(handleCommand);