@superpanel-sdk/cli 0.0.0-stage → 0.1.1

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.
Files changed (78) hide show
  1. package/README.md +16 -2
  2. package/bin/superpanel.mjs +23 -0
  3. package/dist/api/platform.d.ts +31 -0
  4. package/dist/api/platform.js +39 -0
  5. package/dist/auth/auth.d.ts +162 -0
  6. package/dist/auth/auth.js +923 -0
  7. package/dist/auth/backend-url.d.ts +7 -0
  8. package/dist/auth/backend-url.js +41 -0
  9. package/dist/auth/cli-login.d.ts +8 -0
  10. package/dist/auth/cli-login.js +92 -0
  11. package/dist/auth/clipboard.d.ts +8 -0
  12. package/dist/auth/clipboard.js +42 -0
  13. package/dist/auth/file-lock.d.ts +25 -0
  14. package/dist/auth/file-lock.js +173 -0
  15. package/dist/auth/http.d.ts +34 -0
  16. package/dist/auth/http.js +48 -0
  17. package/dist/auth/index.d.ts +13 -0
  18. package/dist/auth/index.js +117 -0
  19. package/dist/auth/origin-guard.d.ts +4 -0
  20. package/dist/auth/origin-guard.js +9 -0
  21. package/dist/auth/origin.d.ts +17 -0
  22. package/dist/auth/origin.js +142 -0
  23. package/dist/auth/pending-login.d.ts +5 -0
  24. package/dist/auth/pending-login.js +67 -0
  25. package/dist/commands/auth-token.d.ts +26 -0
  26. package/dist/commands/auth-token.js +71 -0
  27. package/dist/commands/git-method.d.ts +54 -0
  28. package/dist/commands/git-method.js +180 -0
  29. package/dist/commands/setup.d.ts +88 -0
  30. package/dist/commands/setup.js +366 -0
  31. package/dist/commands/skill.d.ts +29 -0
  32. package/dist/commands/skill.js +169 -0
  33. package/dist/commands/ssh-key.d.ts +13 -0
  34. package/dist/commands/ssh-key.js +83 -0
  35. package/dist/git/credential.d.ts +7 -0
  36. package/dist/git/credential.js +66 -0
  37. package/dist/git/index.d.ts +36 -0
  38. package/dist/git/index.js +119 -0
  39. package/dist/git/run.d.ts +13 -0
  40. package/dist/git/run.js +32 -0
  41. package/dist/git/setup.d.ts +97 -0
  42. package/dist/git/setup.js +471 -0
  43. package/dist/handover/clone.d.ts +8 -0
  44. package/dist/handover/clone.js +39 -0
  45. package/dist/handover/repo-help.d.ts +7 -0
  46. package/dist/handover/repo-help.js +55 -0
  47. package/dist/handover/run.d.ts +5 -0
  48. package/dist/handover/run.js +67 -0
  49. package/dist/inbound/index.d.ts +44 -0
  50. package/dist/inbound/index.js +129 -0
  51. package/dist/index.d.ts +3 -0
  52. package/dist/index.js +251 -0
  53. package/dist/logs/index.d.ts +19 -0
  54. package/dist/logs/index.js +132 -0
  55. package/dist/npm-launch.d.ts +19 -0
  56. package/dist/npm-launch.js +80 -0
  57. package/dist/ops/agent.d.ts +3 -0
  58. package/dist/ops/agent.js +14 -0
  59. package/dist/ops/cache.d.ts +18 -0
  60. package/dist/ops/cache.js +92 -0
  61. package/dist/ops/parse.d.ts +6 -0
  62. package/dist/ops/parse.js +106 -0
  63. package/dist/ops/register.d.ts +18 -0
  64. package/dist/ops/register.js +174 -0
  65. package/dist/ops/run.d.ts +20 -0
  66. package/dist/ops/run.js +127 -0
  67. package/dist/ops/types.d.ts +41 -0
  68. package/dist/ops/types.js +16 -0
  69. package/dist/protocol.d.ts +4 -0
  70. package/dist/protocol.js +17 -0
  71. package/dist/reserved.d.ts +4 -0
  72. package/dist/reserved.js +25 -0
  73. package/dist/turns/index.d.ts +20 -0
  74. package/dist/turns/index.js +108 -0
  75. package/dist/version.d.ts +2 -0
  76. package/dist/version.js +11 -0
  77. package/package.json +48 -4
  78. package/skill/SKILL.md +17 -0
@@ -0,0 +1,66 @@
1
+ import { findCompanyRepo } from '../auth/origin.js';
2
+ import { getGitAccount } from '../api/platform.js';
3
+ import { CliLoginRejectedError, getValidCliAuthorization, loginCommand, readCliAuthorizationStatus, } from '../auth/auth.js';
4
+ import { guardNetworkedClone } from '../auth/origin-guard.js';
5
+ async function readAttributes(stdin) {
6
+ let text = '';
7
+ for await (const chunk of stdin)
8
+ text += chunk.toString();
9
+ const attributes = {};
10
+ for (const line of text.split('\n')) {
11
+ const eq = line.indexOf('=');
12
+ if (eq > 0)
13
+ attributes[line.slice(0, eq)] = line.slice(eq + 1);
14
+ }
15
+ return attributes;
16
+ }
17
+ function giveUp(io) {
18
+ io.stdout.write('quit=1\n');
19
+ }
20
+ function loginHint(backendUrl) {
21
+ return `superpanel: run "${loginCommand(backendUrl)}" and try again\n`;
22
+ }
23
+ export async function runGitCredential(action, backendUrl, io) {
24
+ await readAttributes(io.stdin);
25
+ if (action === 'erase') {
26
+ const status = await readCliAuthorizationStatus(backendUrl);
27
+ const who = status.loggedIn
28
+ ? ` (${status.authorization.user.email} for ${status.authorization.companyName ?? status.authorization.companyId})`
29
+ : '';
30
+ io.stderr.write(`superpanel: Git refused the CLI login${who}; it was logged out, disconnected or suspended.\n${loginHint(backendUrl)}`);
31
+ return;
32
+ }
33
+ if (action !== 'get')
34
+ return;
35
+ let authorization;
36
+ try {
37
+ authorization = await getValidCliAuthorization(backendUrl);
38
+ }
39
+ catch (error) {
40
+ if (error instanceof CliLoginRejectedError) {
41
+ io.stderr.write(`superpanel: ${error.message}\n`);
42
+ }
43
+ else {
44
+ io.stderr.write(`superpanel: could not renew the CLI login (${error instanceof Error ? error.message : String(error)})\n`);
45
+ }
46
+ giveUp(io);
47
+ return;
48
+ }
49
+ if (!authorization) {
50
+ io.stderr.write(`superpanel: not logged in to ${backendUrl}.\n${loginHint(backendUrl)}`);
51
+ giveUp(io);
52
+ return;
53
+ }
54
+ try {
55
+ if (findCompanyRepo(process.cwd()))
56
+ await guardNetworkedClone(process.cwd(), backendUrl, authorization.accessToken);
57
+ else
58
+ await getGitAccount(backendUrl, authorization.accessToken);
59
+ }
60
+ catch (error) {
61
+ io.stderr.write(`superpanel: ${error instanceof Error ? error.message : String(error)}\n`);
62
+ giveUp(io);
63
+ return;
64
+ }
65
+ io.stdout.write(`username=${authorization.user.email}\npassword=${authorization.accessToken}\npassword_expiry_utc=${Math.floor(authorization.accessTokenExpiresAt / 1000)}\n`);
66
+ }
@@ -0,0 +1,36 @@
1
+ import { Command } from 'commander';
2
+ import { type GitAccount } from '../api/platform.js';
3
+ import { type GitProtocol } from '../commands/git-method.js';
4
+ import { type SetupDeps, type SshSetupResult } from './setup.js';
5
+ export type GitSetupOutcome = {
6
+ ok: boolean;
7
+ message: string;
8
+ account: GitAccount;
9
+ };
10
+ export declare function setUpGit(input: {
11
+ backendUrl: string;
12
+ token: string;
13
+ method: 'https' | 'ssh';
14
+ allowSshConfig?: boolean;
15
+ }, deps?: SetupDeps): Promise<GitSetupOutcome & {
16
+ ssh?: SshSetupResult;
17
+ }>;
18
+ export declare function setupGitCommand(): Command;
19
+ export declare function answersFromSetupGitOptions(options: {
20
+ url?: string;
21
+ gitProtocol?: string;
22
+ installCli?: boolean;
23
+ allowSshConfig?: string;
24
+ }): {
25
+ url?: string;
26
+ gitProtocol?: GitProtocol;
27
+ installCli?: boolean;
28
+ allowSshConfig?: boolean;
29
+ };
30
+ export declare function runSetupGit(options: {
31
+ url?: string;
32
+ gitProtocol?: GitProtocol;
33
+ installCli?: boolean;
34
+ allowSshConfig?: boolean;
35
+ }): Promise<void>;
36
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,119 @@
1
+ import { confirmationOptionHelp } from '../ops/types.js';
2
+ import { spawnSync } from 'node:child_process';
3
+ import { Command } from 'commander';
4
+ import { getValidCliAuthorization, loginCommand, resolveBackendUrl, withCliLogin } from '../auth/auth.js';
5
+ import { apiUrl, getJson } from '../auth/http.js';
6
+ import { guardNetworkedClone } from '../auth/origin-guard.js';
7
+ import { addSshKey, getGitAccount, PlatformApiError } from '../api/platform.js';
8
+ import { defaultCloneUsesSsh, gitSetupRefusal, prepareGitMethod } from '../commands/git-method.js';
9
+ import { hasTerminal, runningAsAgent } from '../ops/agent.js';
10
+ import { configureHttpsGit, KeyOnAnotherAccountError, setupSshGit, } from './setup.js';
11
+ function keyAdder(backendUrl, token) {
12
+ return async (publicKey, title) => {
13
+ try {
14
+ await addSshKey(backendUrl, token, { title, publicKey });
15
+ }
16
+ catch (error) {
17
+ if (error instanceof PlatformApiError && error.status === 409)
18
+ throw new KeyOnAnotherAccountError();
19
+ throw error;
20
+ }
21
+ };
22
+ }
23
+ export async function setUpGit(input, deps = {}) {
24
+ const account = await getGitAccount(input.backendUrl, input.token);
25
+ if (!account)
26
+ throw new Error('no Git account is linked to this login; ask a company admin');
27
+ if (input.method === 'https') {
28
+ if (!account.httpsCloneUrl)
29
+ throw new Error('the company repo is not provisioned yet');
30
+ const https = await configureHttpsGit({ backendUrl: input.backendUrl, httpsCloneUrl: account.httpsCloneUrl }, deps);
31
+ return {
32
+ ok: https.verified,
33
+ account,
34
+ message: !https.installed
35
+ ? https.detail
36
+ : https.verified
37
+ ? `git over HTTPS is set up for ${https.origin} (credential helper: superpanel). Clone with: git clone ${account.httpsCloneUrl}`
38
+ : `configured the credential helper for ${https.origin}, but git could not read the repo: ${https.detail}`,
39
+ };
40
+ }
41
+ if (!account.sshHost || !account.sshPort || !account.sshCloneUrl)
42
+ throw new Error('the company repo has no SSH address');
43
+ const ssh = await setupSshGit({
44
+ account: {
45
+ username: account.username,
46
+ email: account.email,
47
+ sshHost: account.sshHost,
48
+ sshPort: account.sshPort,
49
+ sshCloneUrl: account.sshCloneUrl,
50
+ },
51
+ addKey: keyAdder(input.backendUrl, input.token),
52
+ allowSshConfig: input.allowSshConfig,
53
+ }, deps);
54
+ return {
55
+ ok: ssh.status === 'ready',
56
+ account,
57
+ ssh,
58
+ message: ssh.status === 'ready' ? `${ssh.detail}. Clone with: git clone ${account.sshCloneUrl}` : ssh.detail,
59
+ };
60
+ }
61
+ export function setupGitCommand() {
62
+ return new Command('setup-git')
63
+ .description('Let git reach the company repo')
64
+ .option('--url <url>', 'Backend URL')
65
+ .option('--git-protocol <https|ssh>', 'https or ssh')
66
+ .option('--install-cli', confirmationOptionHelp('Install the CLI globally for this user (recommended)'))
67
+ .option('--no-install-cli', confirmationOptionHelp('Do not install the CLI for this user'))
68
+ .option('--allow-ssh-config <words>', confirmationOptionHelp("The person's words agreeing to edit ~/.ssh/config"));
69
+ }
70
+ export function answersFromSetupGitOptions(options) {
71
+ if (options.gitProtocol && options.gitProtocol !== 'https' && options.gitProtocol !== 'ssh') {
72
+ throw new Error('--git-protocol is https or ssh');
73
+ }
74
+ return {
75
+ url: options.url,
76
+ gitProtocol: options.gitProtocol,
77
+ installCli: options.installCli,
78
+ allowSshConfig: Boolean(options.allowSshConfig),
79
+ };
80
+ }
81
+ export async function runSetupGit(options) {
82
+ const backendUrl = resolveBackendUrl(options.url);
83
+ const authorization = await getValidCliAuthorization(backendUrl);
84
+ if (!authorization)
85
+ throw new Error(`not logged in to ${backendUrl}; run "${loginCommand(backendUrl)}" first`);
86
+ await guardNetworkedClone(process.cwd(), backendUrl, authorization.accessToken);
87
+ const me = await getJson(apiUrl(backendUrl, 'me'), authorization.accessToken);
88
+ const method = await prepareGitMethod({
89
+ command: 'setup-git',
90
+ requested: options.gitProtocol,
91
+ preferSsh: defaultCloneUsesSsh(me.payload.user?.role),
92
+ installCli: options.installCli,
93
+ backendUrl,
94
+ token: authorization.accessToken,
95
+ agent: runningAsAgent() || !hasTerminal(),
96
+ });
97
+ if (method === 'ssh' && spawnSync('ssh', ['-V'], { encoding: 'utf8' }).status !== 0) {
98
+ throw new Error('ssh is required for --git-protocol ssh.');
99
+ }
100
+ const outcome = await withCliLogin(backendUrl, async (current) => {
101
+ if (current.user.id !== authorization.user.id || current.companyId !== authorization.companyId)
102
+ throw new Error('Login changed during Git setup. Run setup-git again.');
103
+ await guardNetworkedClone(process.cwd(), backendUrl, current.accessToken);
104
+ return setUpGit({
105
+ backendUrl,
106
+ token: current.accessToken,
107
+ method,
108
+ allowSshConfig: options.allowSshConfig,
109
+ });
110
+ });
111
+ const refusal = gitSetupRefusal(outcome, {
112
+ allowSshConfig: Boolean(options.allowSshConfig),
113
+ command: 'setup-git',
114
+ backendUrl,
115
+ });
116
+ if (refusal)
117
+ throw refusal;
118
+ console.log(outcome.message);
119
+ }
@@ -0,0 +1,13 @@
1
+ export type RunResult = {
2
+ code: number;
3
+ stdout: string;
4
+ stderr: string;
5
+ };
6
+ export type Runner = (file: string, args: string[], options?: {
7
+ env?: NodeJS.ProcessEnv;
8
+ cwd?: string;
9
+ timeoutMs?: number;
10
+ input?: string;
11
+ }) => Promise<RunResult>;
12
+ export declare const run: Runner;
13
+ //# sourceMappingURL=run.d.ts.map
@@ -0,0 +1,32 @@
1
+ import { execFile } from 'node:child_process';
2
+ export const run = (file, args, options = {}) => new Promise((resolve) => {
3
+ let inputError;
4
+ const child = execFile(file, args, {
5
+ env: options.env ?? process.env,
6
+ cwd: options.cwd,
7
+ timeout: options.timeoutMs ?? 30_000,
8
+ maxBuffer: 4 * 1024 * 1024,
9
+ encoding: 'utf8',
10
+ }, (error, stdout, stderr) => {
11
+ const code = error && typeof error.code === 'number'
12
+ ? error.code
13
+ : error
14
+ ? 1
15
+ : 0;
16
+ const killed = error && error.killed;
17
+ resolve({
18
+ code: inputError && code === 0 ? 1 : code,
19
+ stdout,
20
+ stderr: killed
21
+ ? `${stderr}\n(timed out)`
22
+ : error && typeof error.code !== 'number' && !stderr
23
+ ? error.message
24
+ : stderr || inputError?.message || '',
25
+ });
26
+ });
27
+ child.stdin?.on('error', (error) => {
28
+ if (error.code !== 'EPIPE')
29
+ inputError = error;
30
+ });
31
+ child.stdin?.end(options.input);
32
+ });
@@ -0,0 +1,97 @@
1
+ import { type Runner } from './run.js';
2
+ export type SetupDeps = {
3
+ run?: Runner;
4
+ home?: string;
5
+ hostname?: string;
6
+ pathValue?: string;
7
+ globalCli?: () => string | null;
8
+ };
9
+ export declare const NOT_INSTALLED = "Git needs a working global Superpanel CLI credential helper. Run \"npm install -g @superpanel-sdk/cli@latest\", then set up git again.";
10
+ export declare function credentialHelperValue(backendUrl: string, nodePath: string, cliEntry: string): string;
11
+ export declare function credentialCliEntry(found: string): string;
12
+ export declare function shellPath(pathValue: string): string;
13
+ export declare function findOnPath(command: string, pathValue: string): string | null;
14
+ export declare function configureHttpsGit(input: {
15
+ backendUrl: string;
16
+ httpsCloneUrl: string;
17
+ }, deps?: SetupDeps): Promise<{
18
+ origin: string;
19
+ verified: boolean;
20
+ installed: boolean;
21
+ detail: string;
22
+ }>;
23
+ export declare function isolatedGit(args: string[], deps?: SetupDeps, timeoutMs?: number): Promise<import("./run.js").RunResult>;
24
+ export type SshProbe = {
25
+ kind: 'user';
26
+ username: string;
27
+ } | {
28
+ kind: 'denied';
29
+ detail: string;
30
+ } | {
31
+ kind: 'error';
32
+ detail: string;
33
+ };
34
+ export declare function probeSsh(input: {
35
+ host: string;
36
+ port?: number;
37
+ readOnly?: boolean;
38
+ }, deps?: SetupDeps): Promise<SshProbe>;
39
+ export declare function keyBody(line: string): string;
40
+ export type KeyCandidate = {
41
+ source: 'agent' | 'file';
42
+ publicKey?: string;
43
+ privatePath?: string;
44
+ locked?: boolean;
45
+ };
46
+ export declare function firstOfferedKey(input: {
47
+ host: string;
48
+ port: number;
49
+ }, deps?: SetupDeps): Promise<KeyCandidate | null>;
50
+ export declare function keyNeedsPassphrase(privatePath: string, deps?: SetupDeps): Promise<boolean>;
51
+ export declare function sshAlias(username: string): string;
52
+ export declare function orgPrefix(sshCloneUrl: string): {
53
+ prefix: string;
54
+ org: string;
55
+ } | null;
56
+ export declare function hasSshConfigBlock(username: string, deps?: SetupDeps): Promise<boolean>;
57
+ export declare function writeSshConfigBlock(input: {
58
+ host: string;
59
+ port: number;
60
+ username: string;
61
+ }, deps?: SetupDeps): Promise<string>;
62
+ export declare function resolvedSshTarget(sshCloneUrl: string, deps?: SetupDeps): Promise<{
63
+ host: string;
64
+ port?: number;
65
+ rewritten: boolean;
66
+ }>;
67
+ export type SshSetupResult = {
68
+ status: 'ready';
69
+ detail: string;
70
+ } | {
71
+ status: 'needs-confirmation';
72
+ detail: string;
73
+ } | {
74
+ status: 'needs-ssh-add';
75
+ keyPath: string;
76
+ detail: string;
77
+ } | {
78
+ status: 'failed';
79
+ detail: string;
80
+ };
81
+ export type SshSetupInput = {
82
+ account: {
83
+ username: string;
84
+ email: string;
85
+ sshHost: string;
86
+ sshPort: number;
87
+ sshCloneUrl: string;
88
+ };
89
+ addKey: (publicKey: string, title: string) => Promise<void>;
90
+ allowSshConfig?: boolean;
91
+ };
92
+ export declare class KeyOnAnotherAccountError extends Error {
93
+ constructor();
94
+ }
95
+ export declare function sshSetupQuestions(account: SshSetupInput['account'], allowConfig: boolean, deps?: SetupDeps): Promise<string[]>;
96
+ export declare function setupSshGit(input: SshSetupInput, deps?: SetupDeps): Promise<SshSetupResult>;
97
+ //# sourceMappingURL=setup.d.ts.map