learn-secrets-sdk 1.6.5 → 1.6.6
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/index.mjs +94 -21
- package/package.json +1 -1
package/dist/cli/index.mjs
CHANGED
|
@@ -4,6 +4,9 @@ import "./chunk-Y6FXYEAI.mjs";
|
|
|
4
4
|
// src/cli/index.ts
|
|
5
5
|
import { Command } from "commander";
|
|
6
6
|
|
|
7
|
+
// src/cli/commands/login.ts
|
|
8
|
+
import fs2 from "fs";
|
|
9
|
+
|
|
7
10
|
// src/types.ts
|
|
8
11
|
var SecretsSDKError = class extends Error {
|
|
9
12
|
constructor(message, status, response) {
|
|
@@ -441,17 +444,76 @@ Log in on ${this.baseUrl}`);
|
|
|
441
444
|
// src/cli/commands/login.ts
|
|
442
445
|
async function loginCommand(options = {}) {
|
|
443
446
|
console.log("Authenticating with Learn Secrets...\n");
|
|
447
|
+
const baseUrl = options.baseUrl || "https://cloudprototype.org";
|
|
444
448
|
const mgmt = new SecretsManagement({
|
|
445
449
|
appId: "temp",
|
|
446
450
|
// Will be set after login
|
|
447
|
-
baseUrl
|
|
451
|
+
baseUrl
|
|
448
452
|
});
|
|
449
453
|
try {
|
|
450
454
|
const result = await mgmt.login();
|
|
451
455
|
console.log(`
|
|
452
456
|
Logged in as: ${result.user}`);
|
|
453
457
|
console.log("Credentials saved to ~/.learn/credentials.json");
|
|
454
|
-
console.log("\
|
|
458
|
+
console.log("\nFetching your projects...");
|
|
459
|
+
const creds = readCredentials();
|
|
460
|
+
if (!creds) {
|
|
461
|
+
throw new Error("Failed to read credentials");
|
|
462
|
+
}
|
|
463
|
+
const projectsResponse = await fetch(`${baseUrl}/api/projects`, {
|
|
464
|
+
headers: {
|
|
465
|
+
Authorization: `Bearer ${creds.access_token}`
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
if (!projectsResponse.ok) {
|
|
469
|
+
throw new Error("Failed to fetch projects");
|
|
470
|
+
}
|
|
471
|
+
const { projects } = await projectsResponse.json();
|
|
472
|
+
if (projects.length === 0) {
|
|
473
|
+
console.log("\nNo projects found.");
|
|
474
|
+
console.log("Create a project at: https://cloudprototype.org/dashboard");
|
|
475
|
+
console.log("\nAfter creating a project, run: learn-secrets login");
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
let selectedProject;
|
|
479
|
+
if (projects.length === 1) {
|
|
480
|
+
selectedProject = projects[0];
|
|
481
|
+
console.log(`
|
|
482
|
+
Auto-selected project: ${selectedProject.name} (${selectedProject.appid})`);
|
|
483
|
+
} else {
|
|
484
|
+
console.log("\nYou have multiple projects:");
|
|
485
|
+
projects.forEach((p, idx) => {
|
|
486
|
+
console.log(` ${idx + 1}. ${p.name} (${p.appid})`);
|
|
487
|
+
});
|
|
488
|
+
const readline = await import("readline");
|
|
489
|
+
const rl = readline.createInterface({
|
|
490
|
+
input: process.stdin,
|
|
491
|
+
output: process.stdout
|
|
492
|
+
});
|
|
493
|
+
const answer = await new Promise((resolve2) => {
|
|
494
|
+
rl.question("\nSelect a project (1-" + projects.length + "): ", resolve2);
|
|
495
|
+
});
|
|
496
|
+
rl.close();
|
|
497
|
+
const selection = parseInt(answer.trim());
|
|
498
|
+
if (isNaN(selection) || selection < 1 || selection > projects.length) {
|
|
499
|
+
console.error("Invalid selection");
|
|
500
|
+
process.exit(1);
|
|
501
|
+
}
|
|
502
|
+
selectedProject = projects[selection - 1];
|
|
503
|
+
console.log(`
|
|
504
|
+
Selected: ${selectedProject.name}`);
|
|
505
|
+
}
|
|
506
|
+
const configFile = "secrets-config.json";
|
|
507
|
+
const config = {
|
|
508
|
+
project: selectedProject.appid,
|
|
509
|
+
origins: []
|
|
510
|
+
};
|
|
511
|
+
fs2.writeFileSync(configFile, JSON.stringify(config, null, 2));
|
|
512
|
+
console.log(`
|
|
513
|
+
Project configuration saved to: ${configFile}`);
|
|
514
|
+
console.log("\nNext steps:");
|
|
515
|
+
console.log(" 1. Run: learn-secrets init --origins yourdomain.com --env .netlify_env");
|
|
516
|
+
console.log(" 2. This will upload your secrets and generate an SDK token");
|
|
455
517
|
} catch (error) {
|
|
456
518
|
console.error("\nLogin failed:", error.message);
|
|
457
519
|
process.exit(1);
|
|
@@ -459,7 +521,7 @@ Logged in as: ${result.user}`);
|
|
|
459
521
|
}
|
|
460
522
|
|
|
461
523
|
// src/cli/commands/init.ts
|
|
462
|
-
import
|
|
524
|
+
import fs3 from "fs";
|
|
463
525
|
|
|
464
526
|
// src/cli/utils/env-parser.ts
|
|
465
527
|
import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
|
|
@@ -627,8 +689,20 @@ async function initCommand(options) {
|
|
|
627
689
|
console.error("Example: learn-secrets init --origins mysite.com,localhost");
|
|
628
690
|
process.exit(1);
|
|
629
691
|
}
|
|
630
|
-
|
|
631
|
-
|
|
692
|
+
let projectId = options.project;
|
|
693
|
+
const configFile = "secrets-config.json";
|
|
694
|
+
if (!projectId && fs3.existsSync(configFile)) {
|
|
695
|
+
try {
|
|
696
|
+
const config = JSON.parse(fs3.readFileSync(configFile, "utf8"));
|
|
697
|
+
projectId = config.project;
|
|
698
|
+
if (projectId) {
|
|
699
|
+
console.log(`Using project from config: ${projectId}`);
|
|
700
|
+
}
|
|
701
|
+
} catch (err) {
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
if (!projectId) {
|
|
705
|
+
console.error('Error: --project is required (or run "learn-secrets login" first)');
|
|
632
706
|
console.error("Example: learn-secrets init --project my-app-id --origins mysite.com");
|
|
633
707
|
process.exit(1);
|
|
634
708
|
}
|
|
@@ -638,7 +712,7 @@ async function initCommand(options) {
|
|
|
638
712
|
process.exit(1);
|
|
639
713
|
}
|
|
640
714
|
console.log(`
|
|
641
|
-
Onboarding site for project: ${
|
|
715
|
+
Onboarding site for project: ${projectId}`);
|
|
642
716
|
console.log(`Origins: ${origins.join(", ")}
|
|
643
717
|
`);
|
|
644
718
|
const envFile = options.env || ".env";
|
|
@@ -677,7 +751,7 @@ Onboarding site for project: ${options.project}`);
|
|
|
677
751
|
}
|
|
678
752
|
console.log("\nUploading secrets...");
|
|
679
753
|
const mgmt = new SecretsManagement({
|
|
680
|
-
appId:
|
|
754
|
+
appId: projectId,
|
|
681
755
|
baseUrl: options.baseUrl
|
|
682
756
|
});
|
|
683
757
|
try {
|
|
@@ -706,14 +780,13 @@ Onboarding site for project: ${options.project}`);
|
|
|
706
780
|
}
|
|
707
781
|
console.log("\nYour site is ready!");
|
|
708
782
|
console.log(`Visit: https://ctklearn.carsontkempf.workers.dev to manage secrets`);
|
|
709
|
-
const configFile = "secrets-config.json";
|
|
710
783
|
const config = {
|
|
711
|
-
project:
|
|
784
|
+
project: projectId,
|
|
712
785
|
origins
|
|
713
786
|
};
|
|
714
|
-
|
|
787
|
+
fs3.writeFileSync(configFile, JSON.stringify(config, null, 2));
|
|
715
788
|
console.log(`
|
|
716
|
-
|
|
789
|
+
Updated local config: ${configFile}`);
|
|
717
790
|
} catch (error) {
|
|
718
791
|
console.error("\nUpload failed:", error.message);
|
|
719
792
|
if (error.status === 401) {
|
|
@@ -921,8 +994,8 @@ LEARN_ORIGIN=${options.origin}
|
|
|
921
994
|
# Token prefix: ${sdkToken.slice(-4)}
|
|
922
995
|
`;
|
|
923
996
|
try {
|
|
924
|
-
const
|
|
925
|
-
|
|
997
|
+
const fs6 = await import("fs");
|
|
998
|
+
fs6.writeFileSync(configPath, configContent, "utf-8");
|
|
926
999
|
console.log(`
|
|
927
1000
|
\u{1F4BE} Configuration saved to: ${configPath}`);
|
|
928
1001
|
} catch (error) {
|
|
@@ -933,30 +1006,30 @@ LEARN_ORIGIN=${options.origin}
|
|
|
933
1006
|
}
|
|
934
1007
|
|
|
935
1008
|
// src/cli/commands/config.ts
|
|
936
|
-
import
|
|
1009
|
+
import fs4 from "fs";
|
|
937
1010
|
async function configCommand(options) {
|
|
938
1011
|
const configFile = "secrets-config.json";
|
|
939
|
-
if (!
|
|
1012
|
+
if (!fs4.existsSync(configFile)) {
|
|
940
1013
|
console.error("Run learn-secrets init first to generate config.");
|
|
941
1014
|
return;
|
|
942
1015
|
}
|
|
943
|
-
const config = JSON.parse(
|
|
1016
|
+
const config = JSON.parse(fs4.readFileSync(configFile, "utf8"));
|
|
944
1017
|
if (options.origins) {
|
|
945
1018
|
config.origins = options.origins.split(",").map((o) => o.trim());
|
|
946
1019
|
console.log("Updated origins:", config.origins);
|
|
947
1020
|
}
|
|
948
|
-
|
|
1021
|
+
fs4.writeFileSync(configFile, JSON.stringify(config, null, 2));
|
|
949
1022
|
}
|
|
950
1023
|
|
|
951
1024
|
// src/cli/commands/deploy.ts
|
|
952
|
-
import
|
|
1025
|
+
import fs5 from "fs";
|
|
953
1026
|
async function deployCommand() {
|
|
954
1027
|
const configFile = "secrets-config.json";
|
|
955
|
-
if (!
|
|
1028
|
+
if (!fs5.existsSync(configFile)) {
|
|
956
1029
|
console.error("No config found.");
|
|
957
1030
|
return;
|
|
958
1031
|
}
|
|
959
|
-
const config = JSON.parse(
|
|
1032
|
+
const config = JSON.parse(fs5.readFileSync(configFile, "utf8"));
|
|
960
1033
|
console.log("Deploying secrets config to backend:", config);
|
|
961
1034
|
}
|
|
962
1035
|
|
|
@@ -969,7 +1042,7 @@ program.command("setup").description("Complete setup: create project, generate t
|
|
|
969
1042
|
program.command("login").description("Authenticate with Learn Secrets via browser").option("--base-url <url>", "Custom base URL for API").action(async (options) => {
|
|
970
1043
|
await loginCommand(options);
|
|
971
1044
|
});
|
|
972
|
-
program.command("init").description("Initialize secrets for a new site").requiredOption("-o, --origins <domains>", "Comma-separated list of allowed origins (e.g., mysite.com,localhost)").
|
|
1045
|
+
program.command("init").description("Initialize secrets for a new site").requiredOption("-o, --origins <domains>", "Comma-separated list of allowed origins (e.g., mysite.com,localhost)").option("-p, --project <appid>", "Project app ID (auto-detected from login if not specified)").option("-e, --env <file>", "Path to .env file (default: .env)", ".env").option("--base-url <url>", "Custom base URL for API").option("-y, --yes", "Skip confirmation prompt").action(async (options) => {
|
|
973
1046
|
await initCommand(options);
|
|
974
1047
|
});
|
|
975
1048
|
program.command("config").description("Configure allowed origins").option("--origins <origins>", "Comma separated origins").action(async (options) => {
|