@swarm.ing/pieui 2.0.13 → 2.0.14

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/cli.js CHANGED
@@ -193140,6 +193140,7 @@ var printUsage = () => {
193140
193140
  console.log("Commands:");
193141
193141
  console.log(" create <AppName> Create a Next.js app and run pieui init inside it");
193142
193142
  console.log(" create-pie-app <AppName> Create a blank Next.js web template for PieUI (bun create next-app under the hood)");
193143
+ console.log(" login Sign in to PieUI and save credentials to .pie/config.json");
193143
193144
  console.log(" init Initialize piecomponents directory with registry.ts");
193144
193145
  console.log(" card add [type] <ComponentName> [--io] [--ajax] Create a new component in piecomponents directory");
193145
193146
  console.log(" page add <path> Create app/<path>/page.tsx from the standard Pie page template");
@@ -193188,6 +193189,7 @@ var printUsage = () => {
193188
193189
  console.log(" complex-container Container with array content");
193189
193190
  console.log("");
193190
193191
  console.log("Examples:");
193192
+ console.log(" pieui login");
193191
193193
  console.log(" pieui init");
193192
193194
  console.log(" pieui create my-pie-app");
193193
193195
  console.log(" pieui create-pie-app my-pie-app");
@@ -198518,6 +198520,139 @@ var createCommand = (appName) => {
198518
198520
  runBunCommand(bunBin, ["run", "dev"], appDir);
198519
198521
  };
198520
198522
 
198523
+ // src/code/commands/login.ts
198524
+ var import_node_child_process = require("node:child_process");
198525
+ var import_node_crypto = require("node:crypto");
198526
+ var import_node_fs = __toESM(require("node:fs"));
198527
+ var import_node_path2 = __toESM(require("node:path"));
198528
+ var CONNECT_BASE = "https://pieui.swarm.ing/connect";
198529
+ var CREDENTIALS_API = "https://api-pieui.swarm.ing/api/external/credentials";
198530
+ var CODE_LENGTH = 32;
198531
+ var generateCode = (length = CODE_LENGTH) => {
198532
+ const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
198533
+ let out = "";
198534
+ for (let i = 0;i < length; i++) {
198535
+ out += alphabet[import_node_crypto.randomInt(alphabet.length)];
198536
+ }
198537
+ return out;
198538
+ };
198539
+ var tryOpenBrowser = (url) => {
198540
+ try {
198541
+ if (process.platform === "win32") {
198542
+ import_node_child_process.execFile("cmd", ["/c", "start", "", url], () => {
198543
+ });
198544
+ } else if (process.platform === "darwin") {
198545
+ import_node_child_process.execFile("open", [url], () => {
198546
+ });
198547
+ } else {
198548
+ import_node_child_process.execFile("xdg-open", [url], () => {
198549
+ });
198550
+ }
198551
+ } catch {
198552
+ }
198553
+ };
198554
+ var defaultSleep = (ms2) => new Promise((resolve) => setTimeout(resolve, ms2));
198555
+ var PIE_ENV_MAP = [
198556
+ ["PIE_USER_ID", "user_id"],
198557
+ ["PIE_PROJECT", "project"],
198558
+ ["PIE_API_KEY", "api_key"]
198559
+ ];
198560
+ var formatEnvValue = (val) => {
198561
+ if (/[\s#'"\\]/.test(val) || val === "") {
198562
+ return `"${val.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
198563
+ }
198564
+ return val;
198565
+ };
198566
+ var appendPieCredentialsToEnv = (cwd, config) => {
198567
+ if (typeof config !== "object" || config === null) {
198568
+ return;
198569
+ }
198570
+ const c = config;
198571
+ const entries = {};
198572
+ for (const [envKey, jsonKey] of PIE_ENV_MAP) {
198573
+ const v2 = c[jsonKey];
198574
+ if (v2 !== undefined && v2 !== null) {
198575
+ entries[envKey] = String(v2);
198576
+ }
198577
+ }
198578
+ if (Object.keys(entries).length === 0) {
198579
+ return;
198580
+ }
198581
+ const envPath = import_node_path2.default.join(cwd, ".env");
198582
+ let content = import_node_fs.default.existsSync(envPath) ? import_node_fs.default.readFileSync(envPath, "utf8") : "";
198583
+ if (content.length > 0 && !content.endsWith(`
198584
+ `)) {
198585
+ content += `
198586
+ `;
198587
+ }
198588
+ const additions = PIE_ENV_MAP.filter(([envKey]) => (envKey in entries)).map(([envKey]) => `${envKey}=${formatEnvValue(entries[envKey])}`);
198589
+ content += `${additions.join(`
198590
+ `)}
198591
+ `;
198592
+ import_node_fs.default.writeFileSync(envPath, content, "utf8");
198593
+ console.log(`[pie] Appended credentials to ${envPath}`);
198594
+ };
198595
+ async function loginCommand(options = {}) {
198596
+ const {
198597
+ cwd = process.cwd(),
198598
+ pollIntervalMs = 3000,
198599
+ openBrowser = tryOpenBrowser,
198600
+ fetchImpl = fetch,
198601
+ sleepImpl = defaultSleep
198602
+ } = options;
198603
+ const code = generateCode(CODE_LENGTH);
198604
+ const connectUrl = `${CONNECT_BASE}?${new URLSearchParams({ code }).toString()}`;
198605
+ console.log(`Open link in browser:
198606
+ `);
198607
+ console.log(connectUrl);
198608
+ try {
198609
+ openBrowser(connectUrl);
198610
+ } catch {
198611
+ }
198612
+ const pieDir = import_node_path2.default.join(cwd, ".pie");
198613
+ const configPath = import_node_path2.default.join(pieDir, "config.json");
198614
+ const url = `${CREDENTIALS_API}?${new URLSearchParams({ code }).toString()}`;
198615
+ let first = true;
198616
+ while (true) {
198617
+ if (!first) {
198618
+ await sleepImpl(pollIntervalMs);
198619
+ }
198620
+ first = false;
198621
+ const ac = new AbortController;
198622
+ const timer = setTimeout(() => ac.abort(), 30000);
198623
+ let response;
198624
+ try {
198625
+ response = await fetchImpl(url, { signal: ac.signal });
198626
+ } finally {
198627
+ clearTimeout(timer);
198628
+ }
198629
+ if (!response.ok) {
198630
+ throw new Error(`[pieui] Login poll failed: HTTP ${response.status} ${response.statusText}`);
198631
+ }
198632
+ const data = await response.json();
198633
+ if (typeof data !== "object" || data === null || Array.isArray(data)) {
198634
+ continue;
198635
+ }
198636
+ const record = data;
198637
+ const status = record.status;
198638
+ if (status === "error") {
198639
+ const detail = typeof record.message === "string" && record.message || typeof record.error === "string" && record.error || "unknown error";
198640
+ throw new Error(`Login failed: ${detail}`);
198641
+ }
198642
+ if (status === "ok") {
198643
+ if (!("config" in record)) {
198644
+ throw new Error("Login succeeded but response had no 'config' field");
198645
+ }
198646
+ import_node_fs.default.mkdirSync(pieDir, { recursive: true });
198647
+ import_node_fs.default.writeFileSync(configPath, `${JSON.stringify(record.config, null, 2)}
198648
+ `, "utf8");
198649
+ console.log(`[pie] Saved credentials to ${configPath}`);
198650
+ appendPieCredentialsToEnv(cwd, record.config);
198651
+ return;
198652
+ }
198653
+ }
198654
+ }
198655
+
198521
198656
  // src/cli.ts
198522
198657
  var main = async () => {
198523
198658
  const {
@@ -198647,6 +198782,9 @@ var main = async () => {
198647
198782
  console.log(`[pieui] Append mode: ${append}`);
198648
198783
  await postbuildCommand(srcDir, outDir, append);
198649
198784
  return;
198785
+ case "login":
198786
+ await loginCommand();
198787
+ return;
198650
198788
  default:
198651
198789
  printUsage();
198652
198790
  process.exit(1);
@@ -1 +1 @@
1
- {"version":3,"file":"args.d.ts","sourceRoot":"","sources":["../../src/code/args.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKR,UAAU,EACb,MAAM,SAAS,CAAA;AAEhB,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,EAAE,KAAG,UA0I1C,CAAA;AAED,eAAO,MAAM,UAAU,YAsJtB,CAAA"}
1
+ {"version":3,"file":"args.d.ts","sourceRoot":"","sources":["../../src/code/args.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKR,UAAU,EACb,MAAM,SAAS,CAAA;AAEhB,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,EAAE,KAAG,UA0I1C,CAAA;AAED,eAAO,MAAM,UAAU,YA0JtB,CAAA"}
@@ -0,0 +1,11 @@
1
+ export declare const CONNECT_BASE = "https://pieui.swarm.ing/connect";
2
+ export declare const CREDENTIALS_API = "https://api-pieui.swarm.ing/api/external/credentials";
3
+ export type LoginCommandOptions = {
4
+ cwd?: string;
5
+ pollIntervalMs?: number;
6
+ openBrowser?: (url: string) => void;
7
+ fetchImpl?: typeof fetch;
8
+ sleepImpl?: (ms: number) => Promise<void>;
9
+ };
10
+ export declare function loginCommand(options?: LoginCommandOptions): Promise<void>;
11
+ //# sourceMappingURL=login.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"login.d.ts","sourceRoot":"","sources":["../../../src/code/commands/login.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,YAAY,oCAAoC,CAAA;AAC7D,eAAO,MAAM,eAAe,yDAC8B,CAAA;AA2E1D,MAAM,MAAM,mBAAmB,GAAG;IAC9B,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;IACnC,SAAS,CAAC,EAAE,OAAO,KAAK,CAAA;IACxB,SAAS,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC5C,CAAA;AAED,wBAAsB,YAAY,CAC9B,OAAO,GAAE,mBAAwB,GAClC,OAAO,CAAC,IAAI,CAAC,CAiFf"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swarm.ing/pieui",
3
- "version": "2.0.13",
3
+ "version": "2.0.14",
4
4
  "description": "A React component library featuring PieCard component",
5
5
  "repository": {
6
6
  "type": "git",