@emit-vision/cli 0.2.1 → 0.2.4
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/LICENSE +21 -0
- package/dist/index.js +981 -14
- package/package.json +8 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Emit Vision
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
CHANGED
|
@@ -63,7 +63,14 @@ async function apiFetch(path, options = {}) {
|
|
|
63
63
|
);
|
|
64
64
|
}
|
|
65
65
|
if (res.status === 401) {
|
|
66
|
-
|
|
66
|
+
let code = "";
|
|
67
|
+
try {
|
|
68
|
+
const body = await res.json();
|
|
69
|
+
if (body.error) code = body.error;
|
|
70
|
+
} catch {
|
|
71
|
+
}
|
|
72
|
+
const suffix = code ? ` (${code})` : "";
|
|
73
|
+
throw new ApiError(401, `Not authenticated${suffix}. Run \`emit-vision login\` first.`);
|
|
67
74
|
}
|
|
68
75
|
if (!res.ok) {
|
|
69
76
|
let msg = `HTTP ${res.status}`;
|
|
@@ -367,7 +374,7 @@ async function runKeyRotate(args) {
|
|
|
367
374
|
}
|
|
368
375
|
const result = await apiFetch(
|
|
369
376
|
`/v1/management/projects/${encodeURIComponent(projectId)}/keys/rotate`,
|
|
370
|
-
{ method: "POST" }
|
|
377
|
+
{ method: "POST", body: {} }
|
|
371
378
|
);
|
|
372
379
|
const json = args.includes("--json");
|
|
373
380
|
if (json) {
|
|
@@ -551,27 +558,910 @@ async function runLogs(args) {
|
|
|
551
558
|
}
|
|
552
559
|
|
|
553
560
|
// src/commands/status.ts
|
|
554
|
-
async function runStatus() {
|
|
561
|
+
async function runStatus(args = []) {
|
|
555
562
|
const config = loadConfig();
|
|
556
563
|
const apiUrl = config.apiUrl ?? DEFAULT_API_URL;
|
|
564
|
+
const json = args.includes("--json");
|
|
557
565
|
if (!config.pat) {
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
566
|
+
if (json) {
|
|
567
|
+
console.log(JSON.stringify({ apiUrl, tokenPrefix: null, valid: false }));
|
|
568
|
+
} else {
|
|
569
|
+
console.log("Not logged in.");
|
|
570
|
+
console.log(` API: ${apiUrl}`);
|
|
571
|
+
console.log("Run `emit-vision login` to authenticate.");
|
|
572
|
+
}
|
|
561
573
|
return;
|
|
562
574
|
}
|
|
563
|
-
|
|
564
|
-
console.log(` Token: ${config.pat.slice(0, 12)}\u2026`);
|
|
575
|
+
let valid = false;
|
|
565
576
|
try {
|
|
566
577
|
await apiFetch("/v1/management/projects");
|
|
567
|
-
|
|
578
|
+
valid = true;
|
|
568
579
|
} catch {
|
|
569
|
-
console.log(" Auth: invalid or expired \u2014 run `emit-vision login`");
|
|
570
580
|
}
|
|
581
|
+
if (json) {
|
|
582
|
+
console.log(
|
|
583
|
+
JSON.stringify({ apiUrl, tokenPrefix: config.pat.slice(0, 12), valid })
|
|
584
|
+
);
|
|
585
|
+
} else {
|
|
586
|
+
console.log(` API: ${apiUrl}`);
|
|
587
|
+
console.log(` Token: ${config.pat.slice(0, 12)}\u2026`);
|
|
588
|
+
console.log(
|
|
589
|
+
valid ? " Auth: valid \u2713" : " Auth: invalid or expired \u2014 run `emit-vision login`"
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// src/commands/audience.ts
|
|
595
|
+
import { writeFileSync as writeFileSync2 } from "fs";
|
|
596
|
+
var VALUE_FLAGS = /* @__PURE__ */ new Set([
|
|
597
|
+
"--segment",
|
|
598
|
+
"--format",
|
|
599
|
+
"--limit",
|
|
600
|
+
"--output",
|
|
601
|
+
"--tag",
|
|
602
|
+
"--list-id",
|
|
603
|
+
"--develemail-url",
|
|
604
|
+
"--develemail-key",
|
|
605
|
+
"--develemail-project"
|
|
606
|
+
]);
|
|
607
|
+
function parseFlags2(args) {
|
|
608
|
+
const flags = {};
|
|
609
|
+
for (let i = 0; i < args.length; i++) {
|
|
610
|
+
const arg = args[i];
|
|
611
|
+
if (VALUE_FLAGS.has(arg) && args[i + 1]) {
|
|
612
|
+
flags[arg.slice(2)] = args[++i];
|
|
613
|
+
} else if (arg === "--json") {
|
|
614
|
+
flags.json = "true";
|
|
615
|
+
} else if (arg === "--dry-run") {
|
|
616
|
+
flags["dry-run"] = "true";
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return flags;
|
|
620
|
+
}
|
|
621
|
+
function buildQs2(projectId, flags, extras) {
|
|
622
|
+
const params = new URLSearchParams({ projectId });
|
|
623
|
+
if (flags.segment) params.set("segment", flags.segment);
|
|
624
|
+
if (flags.limit) params.set("limit", flags.limit);
|
|
625
|
+
if (extras) {
|
|
626
|
+
for (const [k, v] of Object.entries(extras)) {
|
|
627
|
+
params.set(k, v);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return params.toString();
|
|
631
|
+
}
|
|
632
|
+
async function apiFetchText(path) {
|
|
633
|
+
const config = loadConfig();
|
|
634
|
+
const apiUrl = config.apiUrl ?? DEFAULT_API_URL;
|
|
635
|
+
const headers = {};
|
|
636
|
+
if (config.pat) headers["Authorization"] = `Bearer ${config.pat}`;
|
|
637
|
+
const res = await fetch(`${apiUrl}${path}`, { headers });
|
|
638
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
639
|
+
return res.text();
|
|
640
|
+
}
|
|
641
|
+
async function runUsersList(args) {
|
|
642
|
+
const [projectId, ...rest2] = args;
|
|
643
|
+
if (!projectId) {
|
|
644
|
+
console.error(
|
|
645
|
+
"Usage: emit-vision users list <projectId> [--limit <n>] [--json]"
|
|
646
|
+
);
|
|
647
|
+
process.exit(1);
|
|
648
|
+
}
|
|
649
|
+
const flags = parseFlags2(rest2);
|
|
650
|
+
const data = await apiFetch(
|
|
651
|
+
`/v1/users/audience?${buildQs2(projectId, flags)}`
|
|
652
|
+
);
|
|
653
|
+
if (flags.json) {
|
|
654
|
+
console.log(JSON.stringify(data.users, null, 2));
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
const rows = data.users.map((u) => [
|
|
658
|
+
u.user_id,
|
|
659
|
+
u.email || "\u2014",
|
|
660
|
+
u.name || "\u2014",
|
|
661
|
+
String(u.event_count),
|
|
662
|
+
String(u.error_count),
|
|
663
|
+
u.last_seen ? new Date(u.last_seen).toLocaleDateString() : "\u2014"
|
|
664
|
+
]);
|
|
665
|
+
printTable(["User ID", "Email", "Name", "Events", "Errors", "Last Seen"], rows);
|
|
666
|
+
}
|
|
667
|
+
async function runAudienceExport(args) {
|
|
668
|
+
const [projectId, ...rest2] = args;
|
|
669
|
+
if (!projectId) {
|
|
670
|
+
console.error(
|
|
671
|
+
"Usage: emit-vision audience export <projectId> [--segment dormant|power-user|high-errors|new] [--format csv|json] [--limit <n>] [--output <file>]"
|
|
672
|
+
);
|
|
673
|
+
process.exit(1);
|
|
674
|
+
}
|
|
675
|
+
const flags = parseFlags2(rest2);
|
|
676
|
+
const format = flags.format === "csv" ? "csv" : "json";
|
|
677
|
+
if (format === "csv") {
|
|
678
|
+
const csv = await apiFetchText(
|
|
679
|
+
`/v1/users/audience?${buildQs2(projectId, flags, { format: "csv" })}`
|
|
680
|
+
);
|
|
681
|
+
if (flags.output) {
|
|
682
|
+
writeFileSync2(flags.output, csv, "utf8");
|
|
683
|
+
const contactCount = csv.split("\n").filter(Boolean).length - 1;
|
|
684
|
+
console.log(`Wrote ${contactCount} contacts to ${flags.output}`);
|
|
685
|
+
} else {
|
|
686
|
+
process.stdout.write(csv);
|
|
687
|
+
}
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
const data = await apiFetch(
|
|
691
|
+
`/v1/users/audience?${buildQs2(projectId, flags)}`
|
|
692
|
+
);
|
|
693
|
+
console.log(JSON.stringify(data.users, null, 2));
|
|
694
|
+
}
|
|
695
|
+
async function deleMailFetch(baseUrl, apiKey, path, body) {
|
|
696
|
+
const res = await fetch(`${baseUrl}${path}`, {
|
|
697
|
+
method: "POST",
|
|
698
|
+
headers: {
|
|
699
|
+
"Content-Type": "application/json",
|
|
700
|
+
Authorization: `Bearer ${apiKey}`
|
|
701
|
+
},
|
|
702
|
+
body: JSON.stringify(body)
|
|
703
|
+
});
|
|
704
|
+
if (!res.ok) {
|
|
705
|
+
let msg = `HTTP ${res.status}`;
|
|
706
|
+
try {
|
|
707
|
+
const b = await res.json();
|
|
708
|
+
if (b.error) msg = b.error;
|
|
709
|
+
} catch {
|
|
710
|
+
}
|
|
711
|
+
throw new Error(`develemail error: ${msg}`);
|
|
712
|
+
}
|
|
713
|
+
return res.json();
|
|
714
|
+
}
|
|
715
|
+
async function runAudienceSync(args) {
|
|
716
|
+
const [projectId, ...rest2] = args;
|
|
717
|
+
if (!projectId) {
|
|
718
|
+
console.error(
|
|
719
|
+
"Usage: emit-vision audience sync <projectId> [--segment <name>] [--tag <tag>] [--develemail-url <url>] [--develemail-key <key>] [--develemail-project <id>] [--limit <n>] [--dry-run]"
|
|
720
|
+
);
|
|
721
|
+
process.exit(1);
|
|
722
|
+
}
|
|
723
|
+
const flags = parseFlags2(rest2);
|
|
724
|
+
const deleUrl = flags["develemail-url"] ?? process.env.DEVELEMAIL_BASE_URL;
|
|
725
|
+
const deleKey = flags["develemail-key"] ?? process.env.DEVELEMAIL_API_KEY;
|
|
726
|
+
const deleProject = flags["develemail-project"] ?? process.env.DEVELEMAIL_PROJECT_ID;
|
|
727
|
+
if (!deleUrl || !deleKey || !deleProject) {
|
|
728
|
+
console.error(
|
|
729
|
+
"develemail config missing. Set DEVELEMAIL_BASE_URL, DEVELEMAIL_API_KEY, and DEVELEMAIL_PROJECT_ID env vars, or pass --develemail-url, --develemail-key, --develemail-project."
|
|
730
|
+
);
|
|
731
|
+
process.exit(1);
|
|
732
|
+
}
|
|
733
|
+
const data = await apiFetch(
|
|
734
|
+
`/v1/users/audience?${buildQs2(projectId, flags)}`
|
|
735
|
+
);
|
|
736
|
+
const tags = ["emit-vision"];
|
|
737
|
+
if (flags.segment) tags.push(flags.segment);
|
|
738
|
+
if (flags.tag) tags.push(flags.tag);
|
|
739
|
+
const contacts = data.users.filter((u) => u.email).map((u) => {
|
|
740
|
+
const spaceIdx = u.name.indexOf(" ");
|
|
741
|
+
const firstName = spaceIdx === -1 ? u.name || void 0 : u.name.slice(0, spaceIdx);
|
|
742
|
+
const lastName = spaceIdx === -1 ? void 0 : u.name.slice(spaceIdx + 1);
|
|
743
|
+
return { email: u.email, firstName, lastName, tags };
|
|
744
|
+
});
|
|
745
|
+
const skipped = data.users.length - contacts.length;
|
|
746
|
+
if (flags["dry-run"]) {
|
|
747
|
+
console.log(
|
|
748
|
+
`[dry-run] Would sync ${contacts.length} contacts to develemail (${skipped} skipped, no email).`
|
|
749
|
+
);
|
|
750
|
+
const preview = contacts.slice(0, 5);
|
|
751
|
+
console.log(JSON.stringify(preview, null, 2));
|
|
752
|
+
if (contacts.length > 5)
|
|
753
|
+
console.log(`... and ${contacts.length - 5} more`);
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
const CHUNK = 5e3;
|
|
757
|
+
let totalInserted = 0;
|
|
758
|
+
let totalUpdated = 0;
|
|
759
|
+
const chunks = [];
|
|
760
|
+
for (let i = 0; i < contacts.length; i += CHUNK) {
|
|
761
|
+
chunks.push(contacts.slice(i, i + CHUNK));
|
|
762
|
+
}
|
|
763
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
764
|
+
const chunk = chunks[i];
|
|
765
|
+
if (chunks.length > 1) {
|
|
766
|
+
process.stdout.write(
|
|
767
|
+
` Chunk ${i + 1}/${chunks.length} (${chunk.length} contacts)...`
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
const result = await deleMailFetch(
|
|
771
|
+
deleUrl,
|
|
772
|
+
deleKey,
|
|
773
|
+
"/contacts/bulk",
|
|
774
|
+
{ projectId: deleProject, contacts: chunk }
|
|
775
|
+
);
|
|
776
|
+
totalInserted += result.inserted;
|
|
777
|
+
totalUpdated += result.updated;
|
|
778
|
+
if (chunks.length > 1) console.log(" done.");
|
|
779
|
+
}
|
|
780
|
+
console.log(
|
|
781
|
+
`Synced ${contacts.length} contacts to develemail (${totalInserted} inserted, ${totalUpdated} updated). ${skipped} skipped (no email).`
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
// src/commands/pulse-snippets.ts
|
|
786
|
+
var SNIPPET_TYPES = ["curl", "docker", "systemd", "crontab"];
|
|
787
|
+
function printSnippets(pingUrl, intervalSeconds, filter) {
|
|
788
|
+
if (filter && !SNIPPET_TYPES.includes(filter)) {
|
|
789
|
+
console.error(
|
|
790
|
+
`Unknown snippet type: ${filter}. Choose from: ${SNIPPET_TYPES.join(", ")}`
|
|
791
|
+
);
|
|
792
|
+
process.exit(1);
|
|
793
|
+
}
|
|
794
|
+
const types = filter ? [filter] : SNIPPET_TYPES;
|
|
795
|
+
const snippets = {
|
|
796
|
+
curl: [
|
|
797
|
+
"# cURL",
|
|
798
|
+
`curl -fsS -o /dev/null ${pingUrl} \\`,
|
|
799
|
+
` -H "Authorization: Bearer $EMIT_VISION_INGEST_KEY"`
|
|
800
|
+
].join("\n"),
|
|
801
|
+
docker: [
|
|
802
|
+
"# Docker Compose sidecar",
|
|
803
|
+
"services:",
|
|
804
|
+
" pulse-ping:",
|
|
805
|
+
" image: alpine:3.20",
|
|
806
|
+
" restart: unless-stopped",
|
|
807
|
+
" environment:",
|
|
808
|
+
" - EMIT_VISION_INGEST_KEY=${EMIT_VISION_INGEST_KEY}",
|
|
809
|
+
` - PULSE_URL=${pingUrl}`,
|
|
810
|
+
` - PING_INTERVAL=${intervalSeconds}`,
|
|
811
|
+
" entrypoint: /bin/sh",
|
|
812
|
+
" command:",
|
|
813
|
+
" - -c",
|
|
814
|
+
" - |",
|
|
815
|
+
" while true; do",
|
|
816
|
+
' wget -q --header="Authorization: Bearer $$EMIT_VISION_INGEST_KEY" \\',
|
|
817
|
+
' -O /dev/null "$$PULSE_URL" || true',
|
|
818
|
+
" sleep $$PING_INTERVAL",
|
|
819
|
+
" done"
|
|
820
|
+
].join("\n"),
|
|
821
|
+
systemd: [
|
|
822
|
+
"# systemd timer",
|
|
823
|
+
"# /etc/systemd/system/pulse-ping.service",
|
|
824
|
+
"[Unit]",
|
|
825
|
+
"Description=Emit Vision pulse ping",
|
|
826
|
+
"",
|
|
827
|
+
"[Service]",
|
|
828
|
+
"Type=oneshot",
|
|
829
|
+
`ExecStart=/usr/bin/curl -fsS -o /dev/null ${pingUrl} -H "Authorization: Bearer %d/ingest-key"`,
|
|
830
|
+
"",
|
|
831
|
+
"# /etc/systemd/system/pulse-ping.timer",
|
|
832
|
+
"[Unit]",
|
|
833
|
+
"Description=Pulse ping timer",
|
|
834
|
+
"",
|
|
835
|
+
"[Timer]",
|
|
836
|
+
`OnUnitActiveSec=${intervalSeconds}s`,
|
|
837
|
+
"OnBootSec=60s",
|
|
838
|
+
"",
|
|
839
|
+
"[Install]",
|
|
840
|
+
"WantedBy=timers.target"
|
|
841
|
+
].join("\n"),
|
|
842
|
+
crontab: (() => {
|
|
843
|
+
const minutes = Math.max(1, Math.floor(intervalSeconds / 60));
|
|
844
|
+
return [
|
|
845
|
+
"# crontab entry",
|
|
846
|
+
`*/${minutes} * * * * curl -fsS -o /dev/null ${pingUrl} -H "Authorization: Bearer $EMIT_VISION_INGEST_KEY"`
|
|
847
|
+
].join("\n");
|
|
848
|
+
})()
|
|
849
|
+
};
|
|
850
|
+
for (const type of types) {
|
|
851
|
+
console.log(snippets[type]);
|
|
852
|
+
console.log();
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// src/commands/pulse.ts
|
|
857
|
+
function parseFlag(args, flag) {
|
|
858
|
+
const idx = args.indexOf(flag);
|
|
859
|
+
return idx !== -1 ? args[idx + 1] : void 0;
|
|
860
|
+
}
|
|
861
|
+
function resolveProjectId(args) {
|
|
862
|
+
const fromFlag = parseFlag(args, "--project");
|
|
863
|
+
if (fromFlag) return fromFlag;
|
|
864
|
+
const config = loadConfig();
|
|
865
|
+
const fromConfig = config.projectId;
|
|
866
|
+
if (typeof fromConfig === "string" && fromConfig) return fromConfig;
|
|
867
|
+
console.error(
|
|
868
|
+
"Missing --project flag. Usage: emit-vision pulse init --project <projectId> --name <name>"
|
|
869
|
+
);
|
|
870
|
+
process.exit(1);
|
|
871
|
+
}
|
|
872
|
+
async function runPulseInit(args) {
|
|
873
|
+
const projectId = resolveProjectId(args);
|
|
874
|
+
const name = parseFlag(args, "--name");
|
|
875
|
+
if (!name) {
|
|
876
|
+
console.error(
|
|
877
|
+
"Missing --name flag. Usage: emit-vision pulse init --project <projectId> --name <name> [--interval <seconds>] [--grace <seconds>]"
|
|
878
|
+
);
|
|
879
|
+
process.exit(1);
|
|
880
|
+
}
|
|
881
|
+
const interval = Number(parseFlag(args, "--interval") ?? "300");
|
|
882
|
+
const grace = Number(parseFlag(args, "--grace") ?? "300");
|
|
883
|
+
if (!Number.isFinite(interval) || interval < 60) {
|
|
884
|
+
console.error("--interval must be a number >= 60");
|
|
885
|
+
process.exit(1);
|
|
886
|
+
}
|
|
887
|
+
if (!Number.isFinite(grace) || grace < 0) {
|
|
888
|
+
console.error("--grace must be a non-negative number");
|
|
889
|
+
process.exit(1);
|
|
890
|
+
}
|
|
891
|
+
const check = await apiFetch(
|
|
892
|
+
`/v1/projects/${encodeURIComponent(projectId)}/pulse-checks`,
|
|
893
|
+
{
|
|
894
|
+
method: "POST",
|
|
895
|
+
body: {
|
|
896
|
+
name,
|
|
897
|
+
expectedIntervalSeconds: interval,
|
|
898
|
+
graceSeconds: grace
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
);
|
|
902
|
+
const config = loadConfig();
|
|
903
|
+
const apiUrl = config.apiUrl ?? "https://api.emitvision.com";
|
|
904
|
+
const pingUrl = `${apiUrl}/v1/pulse/${check.slug}`;
|
|
905
|
+
console.log(`Check created: ${check.name}`);
|
|
906
|
+
console.log(` Slug: ${check.slug}`);
|
|
907
|
+
console.log(` Interval: ${check.expectedIntervalSeconds}s`);
|
|
908
|
+
console.log(` Grace: ${check.graceSeconds}s`);
|
|
909
|
+
console.log(` Ping URL: ${pingUrl}`);
|
|
910
|
+
console.log();
|
|
911
|
+
const snippetFilter = parseFlag(args, "--snippet");
|
|
912
|
+
printSnippets(pingUrl, check.expectedIntervalSeconds, snippetFilter);
|
|
913
|
+
if (args.includes("--verify")) {
|
|
914
|
+
await verifyPing(pingUrl);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
async function runPulseList(args) {
|
|
918
|
+
const projectId = resolveProjectId(args);
|
|
919
|
+
const json = args.includes("--json");
|
|
920
|
+
const checks = await apiFetch(
|
|
921
|
+
`/v1/projects/${encodeURIComponent(projectId)}/pulse-checks`
|
|
922
|
+
);
|
|
923
|
+
if (json) {
|
|
924
|
+
console.log(JSON.stringify(checks, null, 2));
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
if (checks.length === 0) {
|
|
928
|
+
console.log(
|
|
929
|
+
"No pulse checks found. Create one with: emit-vision pulse init --project <projectId> --name <name>"
|
|
930
|
+
);
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
const rows = checks.map((c) => [
|
|
934
|
+
c.name,
|
|
935
|
+
c.slug,
|
|
936
|
+
c.status,
|
|
937
|
+
c.lastPingAt ? new Date(c.lastPingAt).toLocaleString() : "never",
|
|
938
|
+
`${c.expectedIntervalSeconds}s`
|
|
939
|
+
]);
|
|
940
|
+
printTable(["Name", "Slug", "Status", "Last Ping", "Interval"], rows);
|
|
941
|
+
}
|
|
942
|
+
var STATUS_COLORS = {
|
|
943
|
+
up: "\x1B[32m",
|
|
944
|
+
late: "\x1B[33m",
|
|
945
|
+
down: "\x1B[31m",
|
|
946
|
+
new: "\x1B[90m",
|
|
947
|
+
paused: "\x1B[90m"
|
|
948
|
+
};
|
|
949
|
+
var RESET = "\x1B[0m";
|
|
950
|
+
function relativeTime(iso) {
|
|
951
|
+
const diff = Date.now() - new Date(iso).getTime();
|
|
952
|
+
const seconds = Math.floor(diff / 1e3);
|
|
953
|
+
if (seconds < 60) return `${seconds}s ago`;
|
|
954
|
+
const minutes = Math.floor(seconds / 60);
|
|
955
|
+
if (minutes < 60) return `${minutes}m ago`;
|
|
956
|
+
const hours = Math.floor(minutes / 60);
|
|
957
|
+
if (hours < 24) return `${hours}h ago`;
|
|
958
|
+
return `${Math.floor(hours / 24)}d ago`;
|
|
959
|
+
}
|
|
960
|
+
async function runPulseStatus(args) {
|
|
961
|
+
const projectId = resolveProjectId(args);
|
|
962
|
+
const json = args.includes("--json");
|
|
963
|
+
const checks = await apiFetch(
|
|
964
|
+
`/v1/projects/${encodeURIComponent(projectId)}/pulse-checks`
|
|
965
|
+
);
|
|
966
|
+
if (json) {
|
|
967
|
+
console.log(JSON.stringify(checks, null, 2));
|
|
968
|
+
return;
|
|
969
|
+
}
|
|
970
|
+
if (checks.length === 0) {
|
|
971
|
+
console.log(
|
|
972
|
+
"No pulse checks found. Create one with: emit-vision pulse init --project <projectId> --name <name>"
|
|
973
|
+
);
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
976
|
+
const rows = checks.map((c) => {
|
|
977
|
+
const color = STATUS_COLORS[c.status] ?? "";
|
|
978
|
+
const statusLabel = `${color}${c.status}${RESET}`;
|
|
979
|
+
const lastPing = c.lastPingAt ? relativeTime(c.lastPingAt) : "never";
|
|
980
|
+
const nextExpected = c.lastPingAt ? relativeTime(
|
|
981
|
+
new Date(
|
|
982
|
+
new Date(c.lastPingAt).getTime() + c.expectedIntervalSeconds * 1e3
|
|
983
|
+
).toISOString()
|
|
984
|
+
) : "-";
|
|
985
|
+
return [
|
|
986
|
+
c.name,
|
|
987
|
+
statusLabel,
|
|
988
|
+
lastPing,
|
|
989
|
+
nextExpected,
|
|
990
|
+
`${c.expectedIntervalSeconds}s`
|
|
991
|
+
];
|
|
992
|
+
});
|
|
993
|
+
printTable(
|
|
994
|
+
["Name", "Status", "Last Ping", "Next Expected", "Interval"],
|
|
995
|
+
rows
|
|
996
|
+
);
|
|
997
|
+
}
|
|
998
|
+
async function verifyPing(pingUrl) {
|
|
999
|
+
const ingestKey = process.env.EMIT_VISION_INGEST_KEY;
|
|
1000
|
+
if (!ingestKey) {
|
|
1001
|
+
console.log(
|
|
1002
|
+
"Skipping --verify: set EMIT_VISION_INGEST_KEY env var to send a test ping."
|
|
1003
|
+
);
|
|
1004
|
+
return;
|
|
1005
|
+
}
|
|
1006
|
+
try {
|
|
1007
|
+
const res = await fetch(pingUrl, {
|
|
1008
|
+
headers: { Authorization: `Bearer ${ingestKey}` }
|
|
1009
|
+
});
|
|
1010
|
+
if (res.ok) {
|
|
1011
|
+
console.log("\u2713 First ping successful \u2014 check is now UP");
|
|
1012
|
+
} else {
|
|
1013
|
+
console.error(
|
|
1014
|
+
`\u2717 Ping failed: HTTP ${res.status}`
|
|
1015
|
+
);
|
|
1016
|
+
}
|
|
1017
|
+
} catch (err) {
|
|
1018
|
+
console.error(
|
|
1019
|
+
`\u2717 Ping failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1020
|
+
);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// src/commands/flags.ts
|
|
1025
|
+
function valueOf(args, flag) {
|
|
1026
|
+
const idx = args.indexOf(flag);
|
|
1027
|
+
return idx !== -1 ? args[idx + 1] : void 0;
|
|
1028
|
+
}
|
|
1029
|
+
function repeatedValues(args, flag) {
|
|
1030
|
+
const values = [];
|
|
1031
|
+
for (let i = 0; i < args.length; i++) {
|
|
1032
|
+
if (args[i] === flag && args[i + 1]) {
|
|
1033
|
+
values.push(args[++i]);
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
return values;
|
|
1037
|
+
}
|
|
1038
|
+
async function runFlagsList(args) {
|
|
1039
|
+
const [projectId, ...rest2] = args;
|
|
1040
|
+
if (!projectId) {
|
|
1041
|
+
console.error(
|
|
1042
|
+
"Usage: emit-vision flags list <projectId> [--limit <n>] [--offset <n>] [--json]"
|
|
1043
|
+
);
|
|
1044
|
+
process.exit(1);
|
|
1045
|
+
}
|
|
1046
|
+
const limit = valueOf(rest2, "--limit");
|
|
1047
|
+
const offset = valueOf(rest2, "--offset");
|
|
1048
|
+
const json = rest2.includes("--json");
|
|
1049
|
+
const params = new URLSearchParams({ projectId });
|
|
1050
|
+
if (limit) params.set("limit", limit);
|
|
1051
|
+
if (offset) params.set("offset", offset);
|
|
1052
|
+
const { flags } = await apiFetch(
|
|
1053
|
+
`/v1/flags?${params.toString()}`
|
|
1054
|
+
);
|
|
1055
|
+
if (json) {
|
|
1056
|
+
console.log(JSON.stringify(flags, null, 2));
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
if (flags.length === 0) {
|
|
1060
|
+
console.log("No flags found for this project.");
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
printTable(
|
|
1064
|
+
["Key", "Name", "Enabled", "Rollout", "Updated"],
|
|
1065
|
+
flags.map((f) => [
|
|
1066
|
+
f.key,
|
|
1067
|
+
f.name,
|
|
1068
|
+
f.isEnabled ? "yes" : "no",
|
|
1069
|
+
`${f.rolloutPercentage ?? 100}%`,
|
|
1070
|
+
f.updatedAt ? new Date(f.updatedAt).toLocaleDateString() : "\u2014"
|
|
1071
|
+
])
|
|
1072
|
+
);
|
|
1073
|
+
}
|
|
1074
|
+
async function runFlagsEvaluate(args) {
|
|
1075
|
+
const usage = "Usage: emit-vision flags evaluate <projectId> --environment <env> [--user-key <key>] [--flag <key>]... [--json]";
|
|
1076
|
+
const [projectId, ...rest2] = args;
|
|
1077
|
+
if (!projectId) {
|
|
1078
|
+
console.error(usage);
|
|
1079
|
+
process.exit(1);
|
|
1080
|
+
}
|
|
1081
|
+
const environment = valueOf(rest2, "--environment");
|
|
1082
|
+
if (!environment) {
|
|
1083
|
+
console.error(usage);
|
|
1084
|
+
process.exit(1);
|
|
1085
|
+
}
|
|
1086
|
+
const userKey = valueOf(rest2, "--user-key");
|
|
1087
|
+
const flagKeys = repeatedValues(rest2, "--flag");
|
|
1088
|
+
const json = rest2.includes("--json");
|
|
1089
|
+
const body = { environment };
|
|
1090
|
+
if (userKey) body.userKey = userKey;
|
|
1091
|
+
if (flagKeys.length > 0) body.flagKeys = flagKeys;
|
|
1092
|
+
const { evaluations } = await apiFetch(`/v1/flags/evaluate?projectId=${encodeURIComponent(projectId)}`, {
|
|
1093
|
+
method: "POST",
|
|
1094
|
+
body
|
|
1095
|
+
});
|
|
1096
|
+
if (json) {
|
|
1097
|
+
console.log(JSON.stringify(evaluations, null, 2));
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
const keys = Object.keys(evaluations);
|
|
1101
|
+
if (keys.length === 0) {
|
|
1102
|
+
console.log("No flags evaluated.");
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
printTable(
|
|
1106
|
+
["Flag", "Variant", "Value", "Reason"],
|
|
1107
|
+
keys.map((key) => {
|
|
1108
|
+
const evaluation = evaluations[key];
|
|
1109
|
+
return [
|
|
1110
|
+
key,
|
|
1111
|
+
evaluation.variantKey,
|
|
1112
|
+
String(evaluation.variantValue),
|
|
1113
|
+
evaluation.reason
|
|
1114
|
+
];
|
|
1115
|
+
})
|
|
1116
|
+
);
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
// src/commands/flags-mutations.ts
|
|
1120
|
+
import { createInterface as createInterface3 } from "readline";
|
|
1121
|
+
async function resolveFlagIdByKey(projectId, flagKey) {
|
|
1122
|
+
const limit = 100;
|
|
1123
|
+
let offset = 0;
|
|
1124
|
+
for (; ; ) {
|
|
1125
|
+
const params = new URLSearchParams({
|
|
1126
|
+
projectId,
|
|
1127
|
+
limit: String(limit),
|
|
1128
|
+
offset: String(offset)
|
|
1129
|
+
});
|
|
1130
|
+
const { flags } = await apiFetch(
|
|
1131
|
+
`/v1/flags?${params.toString()}`
|
|
1132
|
+
);
|
|
1133
|
+
const match = flags.find((f) => f.key === flagKey);
|
|
1134
|
+
if (match) return match.id;
|
|
1135
|
+
if (flags.length < limit) break;
|
|
1136
|
+
offset += limit;
|
|
1137
|
+
}
|
|
1138
|
+
throw new Error(`No flag with key "${flagKey}" in project ${projectId}.`);
|
|
1139
|
+
}
|
|
1140
|
+
async function patchFlag(projectId, flagId, body) {
|
|
1141
|
+
const { flag } = await apiFetch(
|
|
1142
|
+
`/v1/flags/${encodeURIComponent(flagId)}?projectId=${encodeURIComponent(projectId)}`,
|
|
1143
|
+
{ method: "PATCH", body }
|
|
1144
|
+
);
|
|
1145
|
+
return flag;
|
|
1146
|
+
}
|
|
1147
|
+
async function runFlagsToggle(args, isEnabled) {
|
|
1148
|
+
const commandName = isEnabled ? "enable" : "disable";
|
|
1149
|
+
const [projectId, flagKey, ...rest2] = args;
|
|
1150
|
+
if (!projectId || !flagKey) {
|
|
1151
|
+
console.error(
|
|
1152
|
+
`Usage: emit-vision flags ${commandName} <projectId> <flagKey> [--json]`
|
|
1153
|
+
);
|
|
1154
|
+
process.exit(1);
|
|
1155
|
+
}
|
|
1156
|
+
const json = rest2.includes("--json");
|
|
1157
|
+
const flagId = await resolveFlagIdByKey(projectId, flagKey);
|
|
1158
|
+
const flag = await patchFlag(projectId, flagId, { isEnabled });
|
|
1159
|
+
if (json) {
|
|
1160
|
+
console.log(JSON.stringify(flag, null, 2));
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
1163
|
+
console.log(`${flag.key} is now ${flag.isEnabled ? "enabled" : "disabled"}.`);
|
|
1164
|
+
}
|
|
1165
|
+
async function runFlagsEnable(args) {
|
|
1166
|
+
await runFlagsToggle(args, true);
|
|
1167
|
+
}
|
|
1168
|
+
async function runFlagsDisable(args) {
|
|
1169
|
+
await runFlagsToggle(args, false);
|
|
1170
|
+
}
|
|
1171
|
+
async function runFlagsRollout(args) {
|
|
1172
|
+
const usage = "Usage: emit-vision flags rollout <projectId> <flagKey> <percent> [--json]";
|
|
1173
|
+
const [projectId, flagKey, percentArg, ...rest2] = args;
|
|
1174
|
+
if (!projectId || !flagKey || percentArg === void 0) {
|
|
1175
|
+
console.error(usage);
|
|
1176
|
+
process.exit(1);
|
|
1177
|
+
}
|
|
1178
|
+
const rolloutPercentage = Number(percentArg);
|
|
1179
|
+
if (!Number.isInteger(rolloutPercentage) || rolloutPercentage < 0 || rolloutPercentage > 100) {
|
|
1180
|
+
console.error(
|
|
1181
|
+
`Rollout percentage must be an integer between 0 and 100, got "${percentArg}".`
|
|
1182
|
+
);
|
|
1183
|
+
process.exit(1);
|
|
1184
|
+
}
|
|
1185
|
+
const json = rest2.includes("--json");
|
|
1186
|
+
const flagId = await resolveFlagIdByKey(projectId, flagKey);
|
|
1187
|
+
const flag = await patchFlag(projectId, flagId, { rolloutPercentage });
|
|
1188
|
+
if (json) {
|
|
1189
|
+
console.log(JSON.stringify(flag, null, 2));
|
|
1190
|
+
return;
|
|
1191
|
+
}
|
|
1192
|
+
console.log(`${flag.key} rollout is now ${flag.rolloutPercentage}%.`);
|
|
1193
|
+
}
|
|
1194
|
+
async function runFlagsArchive(args) {
|
|
1195
|
+
const usage = "Usage: emit-vision flags archive <projectId> <flagKey> [--yes] [--json]";
|
|
1196
|
+
const [projectId, flagKey, ...rest2] = args;
|
|
1197
|
+
if (!projectId || !flagKey) {
|
|
1198
|
+
console.error(usage);
|
|
1199
|
+
process.exit(1);
|
|
1200
|
+
}
|
|
1201
|
+
const confirmed = rest2.includes("--yes") || rest2.includes("-y");
|
|
1202
|
+
const json = rest2.includes("--json");
|
|
1203
|
+
if (!confirmed) {
|
|
1204
|
+
if (!process.stdin.isTTY) {
|
|
1205
|
+
console.error(
|
|
1206
|
+
"stdin is not a terminal.\nPass --yes to confirm flag archival non-interactively."
|
|
1207
|
+
);
|
|
1208
|
+
process.exit(1);
|
|
1209
|
+
}
|
|
1210
|
+
const rl = createInterface3({ input: process.stdin, output: process.stdout });
|
|
1211
|
+
const answer = await new Promise((resolve) => {
|
|
1212
|
+
rl.question(
|
|
1213
|
+
`Archive flag "${flagKey}"? This cannot be undone. [y/N] `,
|
|
1214
|
+
(a) => {
|
|
1215
|
+
rl.close();
|
|
1216
|
+
resolve(a.trim().toLowerCase());
|
|
1217
|
+
}
|
|
1218
|
+
);
|
|
1219
|
+
});
|
|
1220
|
+
if (answer !== "y" && answer !== "yes") {
|
|
1221
|
+
console.log("Aborted.");
|
|
1222
|
+
return;
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
const flagId = await resolveFlagIdByKey(projectId, flagKey);
|
|
1226
|
+
const result = await apiFetch(
|
|
1227
|
+
`/v1/flags/${encodeURIComponent(flagId)}?projectId=${encodeURIComponent(projectId)}`,
|
|
1228
|
+
{ method: "DELETE" }
|
|
1229
|
+
);
|
|
1230
|
+
if (json) {
|
|
1231
|
+
console.log(JSON.stringify(result, null, 2));
|
|
1232
|
+
return;
|
|
1233
|
+
}
|
|
1234
|
+
console.log(`${flagKey} archived.`);
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
// src/commands/flags-create.ts
|
|
1238
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
1239
|
+
|
|
1240
|
+
// src/commands/flags-validate.ts
|
|
1241
|
+
var FLAG_KEY_RE = /^[a-z0-9_-]+$/;
|
|
1242
|
+
function isFlagKey(value) {
|
|
1243
|
+
return typeof value === "string" && value.length >= 1 && value.length <= 100 && FLAG_KEY_RE.test(value);
|
|
1244
|
+
}
|
|
1245
|
+
function isVariantValue(value) {
|
|
1246
|
+
return value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
1247
|
+
}
|
|
1248
|
+
function validateVariant(variant, index) {
|
|
1249
|
+
if (typeof variant !== "object" || variant === null) {
|
|
1250
|
+
return `"variants[${index}]" must be an object`;
|
|
1251
|
+
}
|
|
1252
|
+
const v = variant;
|
|
1253
|
+
if (!isFlagKey(v.key)) {
|
|
1254
|
+
return `"variants[${index}].key" must be 1-100 lowercase alphanumeric/-/_ characters`;
|
|
1255
|
+
}
|
|
1256
|
+
if (typeof v.displayName !== "string" || v.displayName.length < 1 || v.displayName.length > 100) {
|
|
1257
|
+
return `"variants[${index}].displayName" must be a string between 1 and 100 characters`;
|
|
1258
|
+
}
|
|
1259
|
+
if (!isVariantValue(v.value)) {
|
|
1260
|
+
return `"variants[${index}].value" must be a string, number, boolean, or null`;
|
|
1261
|
+
}
|
|
1262
|
+
if (!Number.isInteger(v.weight) || v.weight < 0 || v.weight > 100) {
|
|
1263
|
+
return `"variants[${index}].weight" must be an integer between 0 and 100`;
|
|
1264
|
+
}
|
|
1265
|
+
if (v.sortOrder !== void 0 && (!Number.isInteger(v.sortOrder) || v.sortOrder < 0)) {
|
|
1266
|
+
return `"variants[${index}].sortOrder" must be a non-negative integer`;
|
|
1267
|
+
}
|
|
1268
|
+
return null;
|
|
1269
|
+
}
|
|
1270
|
+
function validateFlagCreatePayload(payload) {
|
|
1271
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
|
|
1272
|
+
return "payload must be a JSON object";
|
|
1273
|
+
}
|
|
1274
|
+
const p = payload;
|
|
1275
|
+
if (!isFlagKey(p.key)) {
|
|
1276
|
+
return `"key" must be 1-100 lowercase alphanumeric/-/_ characters`;
|
|
1277
|
+
}
|
|
1278
|
+
if (typeof p.name !== "string" || p.name.length < 1 || p.name.length > 200) {
|
|
1279
|
+
return `"name" must be a string between 1 and 200 characters`;
|
|
1280
|
+
}
|
|
1281
|
+
if (p.description !== void 0 && (typeof p.description !== "string" || p.description.length > 1e3)) {
|
|
1282
|
+
return `"description" must be a string up to 1000 characters`;
|
|
1283
|
+
}
|
|
1284
|
+
if (p.isEnabled !== void 0 && typeof p.isEnabled !== "boolean") {
|
|
1285
|
+
return `"isEnabled" must be a boolean`;
|
|
1286
|
+
}
|
|
1287
|
+
if (!isFlagKey(p.defaultVariant)) {
|
|
1288
|
+
return `"defaultVariant" must be a valid variant key`;
|
|
1289
|
+
}
|
|
1290
|
+
if (p.environments !== void 0) {
|
|
1291
|
+
const envs = p.environments;
|
|
1292
|
+
if (!Array.isArray(envs) || envs.some((e) => typeof e !== "string" || e.length < 1)) {
|
|
1293
|
+
return `"environments" must be an array of non-empty strings`;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
if (p.rolloutPercentage !== void 0) {
|
|
1297
|
+
const pct = p.rolloutPercentage;
|
|
1298
|
+
if (!Number.isInteger(pct) || pct < 0 || pct > 100) {
|
|
1299
|
+
return `"rolloutPercentage" must be an integer between 0 and 100`;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
if (!Array.isArray(p.variants) || p.variants.length < 1) {
|
|
1303
|
+
return `"variants" must be a non-empty array`;
|
|
1304
|
+
}
|
|
1305
|
+
for (let i = 0; i < p.variants.length; i++) {
|
|
1306
|
+
const err = validateVariant(p.variants[i], i);
|
|
1307
|
+
if (err) return err;
|
|
1308
|
+
}
|
|
1309
|
+
return null;
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// src/commands/flags-create.ts
|
|
1313
|
+
function booleanVariants() {
|
|
1314
|
+
return [
|
|
1315
|
+
{ key: "off", displayName: "Off", value: false, weight: 0, sortOrder: 0 },
|
|
1316
|
+
{ key: "on", displayName: "On", value: true, weight: 100, sortOrder: 1 }
|
|
1317
|
+
];
|
|
1318
|
+
}
|
|
1319
|
+
function readJsonSource(source) {
|
|
1320
|
+
const label = source === "-" ? "stdin" : source;
|
|
1321
|
+
let raw;
|
|
1322
|
+
try {
|
|
1323
|
+
raw = source === "-" ? readFileSync2(0, "utf-8") : readFileSync2(source, "utf-8");
|
|
1324
|
+
} catch (err) {
|
|
1325
|
+
console.error(
|
|
1326
|
+
`Could not read ${label}: ${err instanceof Error ? err.message : String(err)}`
|
|
1327
|
+
);
|
|
1328
|
+
process.exit(1);
|
|
1329
|
+
}
|
|
1330
|
+
try {
|
|
1331
|
+
return JSON.parse(raw);
|
|
1332
|
+
} catch (err) {
|
|
1333
|
+
console.error(
|
|
1334
|
+
`Invalid JSON in ${label}: ${err instanceof Error ? err.message : String(err)}`
|
|
1335
|
+
);
|
|
1336
|
+
process.exit(1);
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
async function createFromJson(projectId, source, json) {
|
|
1340
|
+
const payload = readJsonSource(source);
|
|
1341
|
+
const error = validateFlagCreatePayload(payload);
|
|
1342
|
+
if (error) {
|
|
1343
|
+
console.error(`Invalid flag definition: ${error}`);
|
|
1344
|
+
process.exit(1);
|
|
1345
|
+
}
|
|
1346
|
+
const { flag } = await apiFetch(
|
|
1347
|
+
`/v1/flags?projectId=${encodeURIComponent(projectId)}`,
|
|
1348
|
+
{ method: "POST", body: payload }
|
|
1349
|
+
);
|
|
1350
|
+
if (json) {
|
|
1351
|
+
console.log(JSON.stringify(flag, null, 2));
|
|
1352
|
+
return;
|
|
1353
|
+
}
|
|
1354
|
+
console.log(`${flag.key} created.`);
|
|
1355
|
+
}
|
|
1356
|
+
async function createBoolean(projectId, key, rest2) {
|
|
1357
|
+
const usage = 'Usage: emit-vision flags create <projectId> <key> --name "<name>" [--description <text>] [--enabled] [--rollout <n>] [--json]';
|
|
1358
|
+
if (!FLAG_KEY_RE.test(key)) {
|
|
1359
|
+
console.error(
|
|
1360
|
+
`Invalid flag key "${key}": must be lowercase alphanumeric, _ or -.`
|
|
1361
|
+
);
|
|
1362
|
+
process.exit(1);
|
|
1363
|
+
}
|
|
1364
|
+
const name = valueOf(rest2, "--name");
|
|
1365
|
+
if (!name) {
|
|
1366
|
+
console.error(usage);
|
|
1367
|
+
process.exit(1);
|
|
1368
|
+
}
|
|
1369
|
+
const description = valueOf(rest2, "--description");
|
|
1370
|
+
const rolloutArg = valueOf(rest2, "--rollout");
|
|
1371
|
+
const enabled = rest2.includes("--enabled");
|
|
1372
|
+
const json = rest2.includes("--json");
|
|
1373
|
+
let rolloutPercentage;
|
|
1374
|
+
if (rolloutArg !== void 0) {
|
|
1375
|
+
rolloutPercentage = Number(rolloutArg);
|
|
1376
|
+
if (!Number.isInteger(rolloutPercentage) || rolloutPercentage < 0 || rolloutPercentage > 100) {
|
|
1377
|
+
console.error(
|
|
1378
|
+
`Rollout percentage must be an integer between 0 and 100, got "${rolloutArg}".`
|
|
1379
|
+
);
|
|
1380
|
+
process.exit(1);
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
const body = {
|
|
1384
|
+
key,
|
|
1385
|
+
name,
|
|
1386
|
+
isEnabled: enabled,
|
|
1387
|
+
defaultVariant: enabled ? "on" : "off",
|
|
1388
|
+
variants: booleanVariants()
|
|
1389
|
+
};
|
|
1390
|
+
if (description !== void 0) body.description = description;
|
|
1391
|
+
if (rolloutPercentage !== void 0) body.rolloutPercentage = rolloutPercentage;
|
|
1392
|
+
const { flag } = await apiFetch(
|
|
1393
|
+
`/v1/flags?projectId=${encodeURIComponent(projectId)}`,
|
|
1394
|
+
{ method: "POST", body }
|
|
1395
|
+
);
|
|
1396
|
+
if (json) {
|
|
1397
|
+
console.log(JSON.stringify(flag, null, 2));
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
console.log(`${flag.key} created.`);
|
|
1401
|
+
}
|
|
1402
|
+
async function runFlagsCreate(args) {
|
|
1403
|
+
const usage = 'Usage: emit-vision flags create <projectId> <key> --name "<name>" [--description <text>] [--enabled] [--rollout <n>] [--json]\n or: emit-vision flags create <projectId> --from-json <file|-> [--json]';
|
|
1404
|
+
const [projectId, ...rest2] = args;
|
|
1405
|
+
if (!projectId) {
|
|
1406
|
+
console.error(usage);
|
|
1407
|
+
process.exit(1);
|
|
1408
|
+
}
|
|
1409
|
+
const fromJson = valueOf(rest2, "--from-json");
|
|
1410
|
+
const json = rest2.includes("--json");
|
|
1411
|
+
if (fromJson !== void 0) {
|
|
1412
|
+
await createFromJson(projectId, fromJson, json);
|
|
1413
|
+
return;
|
|
1414
|
+
}
|
|
1415
|
+
const [key, ...flagRest] = rest2;
|
|
1416
|
+
if (!key) {
|
|
1417
|
+
console.error(usage);
|
|
1418
|
+
process.exit(1);
|
|
1419
|
+
}
|
|
1420
|
+
await createBoolean(projectId, key, flagRest);
|
|
1421
|
+
}
|
|
1422
|
+
async function runFlagsUpdate(args) {
|
|
1423
|
+
const usage = 'Usage: emit-vision flags update <projectId> <flagKey> [--name "<name>"] [--description <text>] [--environments <csv>] [--default-variant <key>] [--json]';
|
|
1424
|
+
const [projectId, flagKey, ...rest2] = args;
|
|
1425
|
+
if (!projectId || !flagKey) {
|
|
1426
|
+
console.error(usage);
|
|
1427
|
+
process.exit(1);
|
|
1428
|
+
}
|
|
1429
|
+
const name = valueOf(rest2, "--name");
|
|
1430
|
+
const description = valueOf(rest2, "--description");
|
|
1431
|
+
const environments = valueOf(rest2, "--environments");
|
|
1432
|
+
const defaultVariant = valueOf(rest2, "--default-variant");
|
|
1433
|
+
const json = rest2.includes("--json");
|
|
1434
|
+
if (defaultVariant !== void 0 && !FLAG_KEY_RE.test(defaultVariant)) {
|
|
1435
|
+
console.error(
|
|
1436
|
+
`Invalid --default-variant "${defaultVariant}": must be lowercase alphanumeric, _ or -.`
|
|
1437
|
+
);
|
|
1438
|
+
process.exit(1);
|
|
1439
|
+
}
|
|
1440
|
+
const body = {};
|
|
1441
|
+
if (name !== void 0) body.name = name;
|
|
1442
|
+
if (description !== void 0) body.description = description;
|
|
1443
|
+
if (environments !== void 0) {
|
|
1444
|
+
body.environments = environments.split(",").map((e) => e.trim()).filter(Boolean);
|
|
1445
|
+
}
|
|
1446
|
+
if (defaultVariant !== void 0) body.defaultVariant = defaultVariant;
|
|
1447
|
+
if (Object.keys(body).length === 0) {
|
|
1448
|
+
console.error(
|
|
1449
|
+
`${usage}
|
|
1450
|
+
At least one of --name, --description, --environments, --default-variant is required.`
|
|
1451
|
+
);
|
|
1452
|
+
process.exit(1);
|
|
1453
|
+
}
|
|
1454
|
+
const flagId = await resolveFlagIdByKey(projectId, flagKey);
|
|
1455
|
+
const flag = await patchFlag(projectId, flagId, body);
|
|
1456
|
+
if (json) {
|
|
1457
|
+
console.log(JSON.stringify(flag, null, 2));
|
|
1458
|
+
return;
|
|
1459
|
+
}
|
|
1460
|
+
console.log(`${flag.key} updated.`);
|
|
571
1461
|
}
|
|
572
1462
|
|
|
573
1463
|
// src/index.ts
|
|
574
|
-
var VERSION = "0.2.
|
|
1464
|
+
var VERSION = "0.2.4";
|
|
575
1465
|
var [cmd, subcmd, ...rest] = process.argv.slice(2);
|
|
576
1466
|
async function main() {
|
|
577
1467
|
if (cmd === "--version" || cmd === "-v") {
|
|
@@ -624,7 +1514,55 @@ async function main() {
|
|
|
624
1514
|
} else if (cmd === "logs") {
|
|
625
1515
|
await runLogs(subcmd ? [subcmd, ...rest] : rest);
|
|
626
1516
|
} else if (cmd === "status") {
|
|
627
|
-
await runStatus();
|
|
1517
|
+
await runStatus(subcmd ? [subcmd, ...rest] : rest);
|
|
1518
|
+
} else if (cmd === "users") {
|
|
1519
|
+
if (subcmd === "list") {
|
|
1520
|
+
await runUsersList(rest);
|
|
1521
|
+
} else {
|
|
1522
|
+
printHelp();
|
|
1523
|
+
process.exit(1);
|
|
1524
|
+
}
|
|
1525
|
+
} else if (cmd === "audience") {
|
|
1526
|
+
if (subcmd === "export") {
|
|
1527
|
+
await runAudienceExport(rest);
|
|
1528
|
+
} else if (subcmd === "sync") {
|
|
1529
|
+
await runAudienceSync(rest);
|
|
1530
|
+
} else {
|
|
1531
|
+
printHelp();
|
|
1532
|
+
process.exit(1);
|
|
1533
|
+
}
|
|
1534
|
+
} else if (cmd === "pulse") {
|
|
1535
|
+
if (subcmd === "init") {
|
|
1536
|
+
await runPulseInit(rest);
|
|
1537
|
+
} else if (subcmd === "list") {
|
|
1538
|
+
await runPulseList(rest);
|
|
1539
|
+
} else if (subcmd === "status") {
|
|
1540
|
+
await runPulseStatus(rest);
|
|
1541
|
+
} else {
|
|
1542
|
+
printHelp();
|
|
1543
|
+
process.exit(1);
|
|
1544
|
+
}
|
|
1545
|
+
} else if (cmd === "flags") {
|
|
1546
|
+
if (subcmd === "list") {
|
|
1547
|
+
await runFlagsList(rest);
|
|
1548
|
+
} else if (subcmd === "evaluate") {
|
|
1549
|
+
await runFlagsEvaluate(rest);
|
|
1550
|
+
} else if (subcmd === "enable") {
|
|
1551
|
+
await runFlagsEnable(rest);
|
|
1552
|
+
} else if (subcmd === "disable") {
|
|
1553
|
+
await runFlagsDisable(rest);
|
|
1554
|
+
} else if (subcmd === "rollout") {
|
|
1555
|
+
await runFlagsRollout(rest);
|
|
1556
|
+
} else if (subcmd === "archive") {
|
|
1557
|
+
await runFlagsArchive(rest);
|
|
1558
|
+
} else if (subcmd === "create") {
|
|
1559
|
+
await runFlagsCreate(rest);
|
|
1560
|
+
} else if (subcmd === "update") {
|
|
1561
|
+
await runFlagsUpdate(rest);
|
|
1562
|
+
} else {
|
|
1563
|
+
printHelp();
|
|
1564
|
+
process.exit(1);
|
|
1565
|
+
}
|
|
628
1566
|
} else {
|
|
629
1567
|
printHelp();
|
|
630
1568
|
if (cmd !== void 0) process.exit(1);
|
|
@@ -632,9 +1570,13 @@ async function main() {
|
|
|
632
1570
|
} catch (err) {
|
|
633
1571
|
if (err instanceof ApiError) {
|
|
634
1572
|
if (err.status === 401) {
|
|
635
|
-
console.error(
|
|
1573
|
+
console.error(err.message);
|
|
636
1574
|
} else if (err.status === 402) {
|
|
637
1575
|
console.error(err.message);
|
|
1576
|
+
} else if (err.status === 403 && err.message === "insufficient_role") {
|
|
1577
|
+
console.error(
|
|
1578
|
+
"Permission denied: this action requires an admin role on the project. Ask a project admin to grant you admin access, or use a PAT that has it."
|
|
1579
|
+
);
|
|
638
1580
|
} else {
|
|
639
1581
|
console.error(`Error: ${err.message}`);
|
|
640
1582
|
}
|
|
@@ -653,7 +1595,7 @@ Commands:
|
|
|
653
1595
|
login [--token <pat>] [--api-url <url>] Authenticate (non-interactive: --token or EMIT_VISION_PAT env)
|
|
654
1596
|
login --browser [--yes] [--api-url <url>] Authenticate via browser OAuth
|
|
655
1597
|
logout Clear stored credentials
|
|
656
|
-
status
|
|
1598
|
+
status [--json] Show auth status and verify token
|
|
657
1599
|
|
|
658
1600
|
project list List your projects
|
|
659
1601
|
project create <name> [--json] Create a new project
|
|
@@ -672,6 +1614,31 @@ Commands:
|
|
|
672
1614
|
logs <projectId> [--from <iso>] [--to <iso>] [--severity <level>] [--limit <n>] [--json]
|
|
673
1615
|
Recent log entries
|
|
674
1616
|
|
|
1617
|
+
users list <projectId> [--limit <n>] [--json] Identified users with email, name, and activity stats
|
|
1618
|
+
audience export <projectId> [--segment dormant|power-user|high-errors|new] [--format csv|json] [--limit <n>] [--output <file>]
|
|
1619
|
+
Export audience contacts (default: JSON to stdout)
|
|
1620
|
+
audience sync <projectId> [--segment <name>] [--tag <tag>] [--limit <n>] [--dry-run]
|
|
1621
|
+
[--develemail-url <url>] [--develemail-key <key>] [--develemail-project <id>]
|
|
1622
|
+
Push audience to develemail contacts
|
|
1623
|
+
|
|
1624
|
+
pulse init --project <projectId> --name <name> [--interval <s>] [--grace <s>] [--verify] [--snippet <type>]
|
|
1625
|
+
Create a pulse check, print ping snippets (cURL, Docker, systemd, crontab)
|
|
1626
|
+
pulse list --project <projectId> [--json] List pulse checks for a project
|
|
1627
|
+
pulse status --project <projectId> [--json] Show check health with colored status indicators
|
|
1628
|
+
|
|
1629
|
+
flags list <projectId> [--limit <n>] [--offset <n>] [--json] List feature flags for a project
|
|
1630
|
+
flags evaluate <projectId> --environment <env> [--user-key <key>] [--flag <key>]... [--json]
|
|
1631
|
+
Evaluate flags for a user/environment
|
|
1632
|
+
flags enable <projectId> <flagKey> [--json] Enable a flag (requires admin role)
|
|
1633
|
+
flags disable <projectId> <flagKey> [--json] Disable a flag (requires admin role)
|
|
1634
|
+
flags rollout <projectId> <flagKey> <percent> [--json] Set rollout percentage 0-100 (requires admin role)
|
|
1635
|
+
flags archive <projectId> <flagKey> [--yes] [--json] Archive a flag (requires admin role, --yes skips confirmation)
|
|
1636
|
+
flags create <projectId> <key> --name <name> [--description <text>] [--enabled] [--rollout <n>] [--json]
|
|
1637
|
+
Create a boolean flag (requires admin role)
|
|
1638
|
+
flags create <projectId> --from-json <file|-> [--json] Create a flag from a full flagCreateSchema JSON payload
|
|
1639
|
+
flags update <projectId> <flagKey> [--name <name>] [--description <text>] [--environments <csv>] [--default-variant <key>] [--json]
|
|
1640
|
+
Update non-variant fields on a flag (requires admin role)
|
|
1641
|
+
|
|
675
1642
|
--version, -v Print CLI version
|
|
676
1643
|
`);
|
|
677
1644
|
}
|
package/package.json
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@emit-vision/cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "CLI for emit-vision project management",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"bin": {
|
|
9
|
-
"emit-vision": "
|
|
9
|
+
"emit-vision": "dist/index.js"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
|
-
"dist"
|
|
12
|
+
"dist",
|
|
13
|
+
"LICENSE"
|
|
13
14
|
],
|
|
14
15
|
"scripts": {
|
|
15
16
|
"build": "tsup --config tsup.config.ts",
|
|
16
17
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
18
|
+
"test": "vitest run",
|
|
17
19
|
"prepack": "pnpm run build"
|
|
18
20
|
},
|
|
19
21
|
"publishConfig": {
|
|
20
22
|
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"vitest": "^4.0.15"
|
|
21
26
|
}
|
|
22
27
|
}
|