codepen-commander 1.2.0 → 1.4.0
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/README.md +104 -10
- package/api/client.js +129 -106
- package/cli.js +34 -29
- package/commands/auth/default.js +40 -0
- package/commands/auth/index.js +83 -0
- package/commands/auth/list.js +78 -0
- package/commands/auth/login.js +136 -0
- package/commands/auth/logout.js +186 -0
- package/commands/auth/status.js +212 -0
- package/commands/build.js +256 -0
- package/commands/clone.js +13 -581
- package/commands/delete.js +553 -37
- package/commands/deploy.js +164 -154
- package/commands/domains.js +83 -39
- package/commands/fork.js +13 -317
- package/commands/info.js +152 -0
- package/commands/init.js +375 -0
- package/commands/list.js +95 -78
- package/commands/new.js +155 -83
- package/commands/open.js +29 -19
- package/commands/privacy.js +52 -36
- package/commands/pull.js +123 -110
- package/commands/push.js +222 -365
- package/commands/restore.js +251 -0
- package/commands/rollback.js +141 -0
- package/commands/serve.js +119 -0
- package/commands/settings.js +126 -0
- package/commands/shell.js +305 -0
- package/commands/status.js +133 -39
- package/commands/sync.js +330 -0
- package/commands/undeploy.js +102 -0
- package/commands/unlink.js +191 -0
- package/commands/versions.js +100 -163
- package/config/auth.js +4 -374
- package/config/pen-blocks.js +166 -0
- package/config/project.js +179 -240
- package/config/user-store.js +364 -0
- package/constants.js +42 -0
- package/errors.js +17 -0
- package/package.json +1 -1
- package/shell/index.html +79 -0
- package/shell/scripts.js +276 -0
- package/shell/styles.css +492 -0
- package/site/blocks/classic.js +270 -0
- package/site/blocks/context.js +63 -0
- package/site/blocks/index.js +4 -0
- package/site/blocks/packages.js +242 -0
- package/site/blocks/unsupported-page.js +328 -0
- package/site/builder.js +475 -0
- package/site/detect.js +65 -0
- package/site/ignore-files.js +90 -0
- package/site/manifest.js +227 -0
- package/site/server.js +483 -0
- package/site/shell.js +363 -0
- package/sync/fs.js +19 -17
- package/sync/info.js +6 -81
- package/utils/browser.js +17 -10
- package/utils/cli-options.js +79 -0
- package/utils/codepen-url.js +183 -0
- package/utils/format.js +39 -30
- package/utils/fs-safe.js +95 -0
- package/utils/html.js +40 -1
- package/utils/json.js +4 -19
- package/utils/output.js +37 -0
- package/utils/package-paths.js +51 -0
- package/utils/prompts/index.js +5 -0
- package/utils/prompts/multi-select.js +185 -0
- package/utils/prompts/secret.js +65 -0
- package/utils/prompts/select.js +141 -0
- package/utils/prompts/text.js +99 -0
- package/utils/prompts/tty-list.js +165 -0
- package/utils/prompts/types.js +35 -0
- package/utils/strings.js +98 -0
- package/utils/watcher.js +82 -0
- package/{commands → workflows}/auth-context.js +13 -7
- package/workflows/batch-pens.js +1647 -0
- package/workflows/batch-target.js +184 -0
- package/workflows/classic-convert.js +241 -0
- package/workflows/clone-pen.js +381 -0
- package/workflows/create-pen.js +254 -0
- package/workflows/discover-pens.js +91 -0
- package/workflows/fork-pen.js +314 -0
- package/workflows/missing-pen.js +212 -0
- package/workflows/pull-pen.js +204 -0
- package/workflows/push-pen.js +318 -0
- package/{commands → workflows}/scaffold.js +4 -3
- package/workflows/sync-pen.js +227 -0
- package/{commands → workflows}/unowned-pen.js +16 -12
- package/commands/auth.js +0 -593
- package/commands/create-pen.js +0 -251
- package/commands/meta.js +0 -184
- package/utils/prompts.js +0 -316
- /package/{types.js → api/types.js} +0 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { API_KEY_FILENAME, getKeyType, validateProfileName, } from '../../config/auth.js';
|
|
2
|
+
import { readAuthStore, setDefaultProfile } from '../../config/user-store.js';
|
|
3
|
+
import { CliError } from '../../errors.js';
|
|
4
|
+
import { fmt } from '../../utils/format.js';
|
|
5
|
+
import { printJsonResponse } from '../../utils/json.js';
|
|
6
|
+
import { defaultOutput } from '../../utils/output.js';
|
|
7
|
+
import { isInteractive, promptSelect } from '../../utils/prompts/index.js';
|
|
8
|
+
export async function runAuthDefaultCommand(options = {}) {
|
|
9
|
+
const output = options.output ?? defaultOutput;
|
|
10
|
+
const interactive = !options.json && (options.interactive ?? isInteractive());
|
|
11
|
+
const selectImpl = options.selectProfile ?? promptSelect;
|
|
12
|
+
const { store } = await readAuthStore({ homeDir: options.homeDir });
|
|
13
|
+
const profileNames = Object.keys(store.profiles).sort((a, b) => a.localeCompare(b));
|
|
14
|
+
if (profileNames.length === 0) {
|
|
15
|
+
throw new CliError(404, `No profiles found in ~/${API_KEY_FILENAME}. Run \`cdpn auth login\` first.`);
|
|
16
|
+
}
|
|
17
|
+
let target = options.profileArg?.trim() || options.explicitProfile;
|
|
18
|
+
if (!target) {
|
|
19
|
+
if (!interactive) {
|
|
20
|
+
throw new CliError(400, `Specify a profile name: \`cdpn auth default <profile>\`. Available: ${profileNames.join(', ')}`, { available_profiles: profileNames });
|
|
21
|
+
}
|
|
22
|
+
const defaultIdx = Math.max(0, store.defaultProfile ? profileNames.indexOf(store.defaultProfile) : 0);
|
|
23
|
+
target = await selectImpl('Select your default CodePen profile:', profileNames.map((name) => ({
|
|
24
|
+
value: name,
|
|
25
|
+
label: name,
|
|
26
|
+
description: `${getKeyType(store.profiles[name])}${name === store.defaultProfile ? ' · current default' : ''}`,
|
|
27
|
+
})), defaultIdx);
|
|
28
|
+
}
|
|
29
|
+
const validTarget = validateProfileName(target);
|
|
30
|
+
await setDefaultProfile(validTarget, { homeDir: options.homeDir });
|
|
31
|
+
if (options.json) {
|
|
32
|
+
printJsonResponse(200, `Default profile set to "${validTarget}".`, {
|
|
33
|
+
default_profile: validTarget,
|
|
34
|
+
profiles: profileNames,
|
|
35
|
+
}, output);
|
|
36
|
+
return { defaultProfile: validTarget, profiles: profileNames };
|
|
37
|
+
}
|
|
38
|
+
output.log(`${fmt.green('✓')} Default profile set to ${fmt.bold(validTarget)}.`);
|
|
39
|
+
return { defaultProfile: validTarget, profiles: profileNames };
|
|
40
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { API_KEY_FILENAME } from '../../config/auth.js';
|
|
2
|
+
import { resolveInheritedJsonOption } from '../../utils/cli-options.js';
|
|
3
|
+
import { getGlobalProfileOption } from '../../workflows/auth-context.js';
|
|
4
|
+
import { runAuthDefaultCommand } from './default.js';
|
|
5
|
+
import { runAuthListCommand } from './list.js';
|
|
6
|
+
import { runAuthLoginCommand } from './login.js';
|
|
7
|
+
import { runAuthLogoutCommand } from './logout.js';
|
|
8
|
+
import { runAuthStatusCommand } from './status.js';
|
|
9
|
+
export { runAuthDefaultCommand, runAuthListCommand, runAuthLoginCommand, runAuthLogoutCommand, runAuthStatusCommand, };
|
|
10
|
+
export function registerAuthCommand(program) {
|
|
11
|
+
const authCmd = program
|
|
12
|
+
.command('auth')
|
|
13
|
+
.description('Manage CodePen API v2 authentication and profiles')
|
|
14
|
+
.option('--json', 'Output result as JSON (for status, list, default, logout)');
|
|
15
|
+
authCmd
|
|
16
|
+
.command('status')
|
|
17
|
+
.description('Show loaded CodePen API keys/profiles and verify them')
|
|
18
|
+
.option('--offline', 'Skip live verification against the CodePen API')
|
|
19
|
+
.option('--json', 'Output authentication status as JSON')
|
|
20
|
+
.action(async (options, command) => {
|
|
21
|
+
const result = await runAuthStatusCommand({
|
|
22
|
+
offline: options.offline,
|
|
23
|
+
json: resolveInheritedJsonOption(options, command),
|
|
24
|
+
requestedProfile: getGlobalProfileOption(command),
|
|
25
|
+
});
|
|
26
|
+
if (result.exitCode !== undefined) {
|
|
27
|
+
process.exitCode = result.exitCode;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
authCmd
|
|
31
|
+
.command('list')
|
|
32
|
+
.alias('ls')
|
|
33
|
+
.description(`List stored CodePen profiles in ~/${API_KEY_FILENAME}`)
|
|
34
|
+
.option('--json', 'Output stored profiles as JSON')
|
|
35
|
+
.action(async (options, command) => {
|
|
36
|
+
await runAuthListCommand({
|
|
37
|
+
json: resolveInheritedJsonOption(options, command),
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
authCmd
|
|
41
|
+
.command('default [profile]')
|
|
42
|
+
.description(`Set the default CodePen profile in ~/${API_KEY_FILENAME}`)
|
|
43
|
+
.option('--json', 'Output result as JSON')
|
|
44
|
+
.action(async (profileArg, options, command) => {
|
|
45
|
+
await runAuthDefaultCommand({
|
|
46
|
+
profileArg,
|
|
47
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
48
|
+
json: resolveInheritedJsonOption(options, command),
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
authCmd
|
|
52
|
+
.command('login [profile]')
|
|
53
|
+
.description(`Open CodePen API settings, verify a key, and save a profile to ~/${API_KEY_FILENAME}`)
|
|
54
|
+
.option('--default', 'Set this profile as the default profile')
|
|
55
|
+
.option('--no-open', 'Do not automatically open the browser')
|
|
56
|
+
.action(async (profileArg, options, command) => {
|
|
57
|
+
const result = await runAuthLoginCommand({
|
|
58
|
+
profileArg,
|
|
59
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
60
|
+
default: options.default,
|
|
61
|
+
open: options.open,
|
|
62
|
+
});
|
|
63
|
+
if (result.exitCode !== undefined) {
|
|
64
|
+
process.exitCode = result.exitCode;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
authCmd
|
|
68
|
+
.command('logout [profile]')
|
|
69
|
+
.description(`Remove a stored profile or the entire ~/${API_KEY_FILENAME} file`)
|
|
70
|
+
.option('--all', `Remove all stored profiles (~/${API_KEY_FILENAME})`)
|
|
71
|
+
.option('--json', 'Output logout result as JSON')
|
|
72
|
+
.action(async (profileArg, options, command) => {
|
|
73
|
+
const result = await runAuthLogoutCommand({
|
|
74
|
+
profileArg,
|
|
75
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
76
|
+
all: options.all,
|
|
77
|
+
json: resolveInheritedJsonOption(options, command),
|
|
78
|
+
});
|
|
79
|
+
if (result.exitCode !== undefined) {
|
|
80
|
+
process.exitCode = result.exitCode;
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { API_KEY_ENV_VAR, API_KEY_FILENAME, getKeyType, maskApiKey, } from '../../config/auth.js';
|
|
2
|
+
import { findProject } from '../../config/project.js';
|
|
3
|
+
import { readAuthStore } from '../../config/user-store.js';
|
|
4
|
+
import { fmt, formatTable } from '../../utils/format.js';
|
|
5
|
+
import { printJsonResponse } from '../../utils/json.js';
|
|
6
|
+
import { defaultOutput } from '../../utils/output.js';
|
|
7
|
+
export async function runAuthListCommand(options = {}) {
|
|
8
|
+
const output = options.output ?? defaultOutput;
|
|
9
|
+
const env = options.env ?? process.env;
|
|
10
|
+
const cwd = options.cwd ?? process.cwd();
|
|
11
|
+
const { store, filePath, insecurePermissions } = await readAuthStore({
|
|
12
|
+
homeDir: options.homeDir,
|
|
13
|
+
});
|
|
14
|
+
const profileNames = Object.keys(store.profiles).sort((a, b) => a.localeCompare(b));
|
|
15
|
+
const project = await findProject(cwd).catch(() => null);
|
|
16
|
+
const projectProfile = project?.config.profile ?? null;
|
|
17
|
+
if (options.json) {
|
|
18
|
+
const message = profileNames.length === 0
|
|
19
|
+
? `No profiles stored in ~/${API_KEY_FILENAME}.`
|
|
20
|
+
: `Found ${profileNames.length} stored profile(s).`;
|
|
21
|
+
printJsonResponse(200, message, {
|
|
22
|
+
file_path: filePath,
|
|
23
|
+
default_profile: store.defaultProfile,
|
|
24
|
+
project_profile: projectProfile,
|
|
25
|
+
env_var_set: Boolean(env[API_KEY_ENV_VAR]?.trim()),
|
|
26
|
+
insecure_permissions: insecurePermissions,
|
|
27
|
+
profiles: profileNames.map((name) => {
|
|
28
|
+
const key = store.profiles[name];
|
|
29
|
+
return {
|
|
30
|
+
name,
|
|
31
|
+
key_type: getKeyType(key),
|
|
32
|
+
masked_key: maskApiKey(key),
|
|
33
|
+
is_default: name === store.defaultProfile,
|
|
34
|
+
is_project_profile: name === projectProfile,
|
|
35
|
+
};
|
|
36
|
+
}),
|
|
37
|
+
}, output);
|
|
38
|
+
return {
|
|
39
|
+
profiles: profileNames,
|
|
40
|
+
defaultProfile: store.defaultProfile,
|
|
41
|
+
projectProfile,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (profileNames.length === 0) {
|
|
45
|
+
output.log(fmt.dim(`No profiles stored in ${filePath}. Run \`cdpn auth login\` to add one.`));
|
|
46
|
+
if (env[API_KEY_ENV_VAR]) {
|
|
47
|
+
output.log(fmt.dim(`(${API_KEY_ENV_VAR} is currently set in your environment.)`));
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
profiles: profileNames,
|
|
51
|
+
defaultProfile: store.defaultProfile,
|
|
52
|
+
projectProfile,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const rows = profileNames.map((name) => {
|
|
56
|
+
const key = store.profiles[name];
|
|
57
|
+
const markers = [];
|
|
58
|
+
if (name === store.defaultProfile)
|
|
59
|
+
markers.push(fmt.cyan('default'));
|
|
60
|
+
if (name === projectProfile)
|
|
61
|
+
markers.push(fmt.green('current folder'));
|
|
62
|
+
return [
|
|
63
|
+
fmt.bold(name),
|
|
64
|
+
getKeyType(key),
|
|
65
|
+
fmt.cyan(maskApiKey(key)),
|
|
66
|
+
markers.length > 0 ? markers.join(', ') : fmt.dim('—'),
|
|
67
|
+
];
|
|
68
|
+
});
|
|
69
|
+
output.log(formatTable(['Profile', 'Type', 'Key', 'Markers'], rows));
|
|
70
|
+
if (insecurePermissions) {
|
|
71
|
+
output.log(fmt.yellow(`\nWarning: ${filePath} is readable by other users. Tighten permissions with: chmod 600 ~/${API_KEY_FILENAME}`));
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
profiles: profileNames,
|
|
75
|
+
defaultProfile: store.defaultProfile,
|
|
76
|
+
projectProfile,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { CodePenClient } from '../../api/client.js';
|
|
2
|
+
import { isCodePenApiError } from '../../api/errors.js';
|
|
3
|
+
import { API_KEY_ENV_VAR, API_KEY_FILENAME, DEFAULT_PROFILE_NAME, getKeyType, getShellExportSnippets, isRecognizedKeyFormat, isValidProfileName, maskApiKey, validateProfileName, } from '../../config/auth.js';
|
|
4
|
+
import { readAuthStore, saveProfileToFile } from '../../config/user-store.js';
|
|
5
|
+
import { CODEPEN_API_SETTINGS_URL } from '../../constants.js';
|
|
6
|
+
import { CliError } from '../../errors.js';
|
|
7
|
+
import { openBrowser } from '../../utils/browser.js';
|
|
8
|
+
import { extractCodePenOwnerFromUrl } from '../../utils/codepen-url.js';
|
|
9
|
+
import { fmt } from '../../utils/format.js';
|
|
10
|
+
import { defaultOutput } from '../../utils/output.js';
|
|
11
|
+
import { isInteractive, promptConfirm, promptSecret, promptSelect, promptText, } from '../../utils/prompts/index.js';
|
|
12
|
+
export async function runAuthLoginCommand(options = {}) {
|
|
13
|
+
const output = options.output ?? defaultOutput;
|
|
14
|
+
const env = options.env ?? process.env;
|
|
15
|
+
const interactive = options.interactive ?? isInteractive();
|
|
16
|
+
const secretImpl = options.prompts?.secret ?? promptSecret;
|
|
17
|
+
const confirmImpl = options.prompts?.confirm ?? promptConfirm;
|
|
18
|
+
const textImpl = options.prompts?.text ?? promptText;
|
|
19
|
+
const selectImpl = options.prompts?.select ?? promptSelect;
|
|
20
|
+
const openImpl = options.openBrowser ?? openBrowser;
|
|
21
|
+
if (!interactive) {
|
|
22
|
+
throw new CliError(400, `Interactive login requires a TTY. Set ${API_KEY_ENV_VAR} in your environment instead.`);
|
|
23
|
+
}
|
|
24
|
+
const explicitProfile = options.profileArg?.trim() || options.explicitProfile;
|
|
25
|
+
if (explicitProfile) {
|
|
26
|
+
validateProfileName(explicitProfile);
|
|
27
|
+
}
|
|
28
|
+
if (options.open !== false) {
|
|
29
|
+
output.log(`Opening ${fmt.cyan(CODEPEN_API_SETTINGS_URL)} in your browser…`);
|
|
30
|
+
const launched = await openImpl(CODEPEN_API_SETTINGS_URL);
|
|
31
|
+
if (!launched) {
|
|
32
|
+
output.log(fmt.yellow(`Could not launch browser automatically. Open this URL manually:\n ${CODEPEN_API_SETTINGS_URL}`));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
output.log(`Generate or reveal your API key at:\n ${fmt.cyan(CODEPEN_API_SETTINGS_URL)}`);
|
|
37
|
+
}
|
|
38
|
+
output.log(fmt.dim('Click "Generate API Key" (or "Reveal") and paste it below.\n'));
|
|
39
|
+
let apiKey = '';
|
|
40
|
+
let detectedOwner = null;
|
|
41
|
+
for (;;) {
|
|
42
|
+
apiKey = await secretImpl('CodePen API key');
|
|
43
|
+
if (!apiKey) {
|
|
44
|
+
output.log(fmt.yellow('API key cannot be empty.'));
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (!isRecognizedKeyFormat(apiKey)) {
|
|
48
|
+
output.log(fmt.yellow('Heads up: CodePen API v2 keys usually start with `cp_user_` or `cp_team_`.'));
|
|
49
|
+
}
|
|
50
|
+
output.write('Verifying key with CodePen API v2… ');
|
|
51
|
+
const client = new CodePenClient({ apiKey, baseUrl: options.baseUrl });
|
|
52
|
+
try {
|
|
53
|
+
const samplePen = await client.verifyAuth();
|
|
54
|
+
detectedOwner = samplePen?.urls?.editor
|
|
55
|
+
? extractCodePenOwnerFromUrl(samplePen.urls.editor)
|
|
56
|
+
: null;
|
|
57
|
+
output.log(fmt.green('✓ Valid!'));
|
|
58
|
+
output.log(`Recognized ${fmt.bold(getKeyType(apiKey))} key (${fmt.cyan(maskApiKey(apiKey))})` +
|
|
59
|
+
(detectedOwner ? ` ${fmt.dim(`· account: ${detectedOwner}`)}` : '') +
|
|
60
|
+
'.\n');
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
output.log(fmt.red('✗ Failed'));
|
|
65
|
+
const msg = isCodePenApiError(error) ? error.message : error.message;
|
|
66
|
+
output.log(fmt.red(` ${msg}`));
|
|
67
|
+
const retry = await confirmImpl('Try entering a different key?', true);
|
|
68
|
+
if (!retry) {
|
|
69
|
+
return { saved: false, exitCode: 1 };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const storageChoice = await selectImpl('How would you like to store your API key?', [
|
|
74
|
+
{
|
|
75
|
+
value: 'file',
|
|
76
|
+
label: `Save to ~/${API_KEY_FILENAME}`,
|
|
77
|
+
description: 'supports multiple profiles, written with owner-only (0600) permissions',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
value: 'env',
|
|
81
|
+
label: 'Add to my shell environment variables myself',
|
|
82
|
+
description: 'print export lines for ~/.zshrc, ~/.bashrc, etc.',
|
|
83
|
+
},
|
|
84
|
+
], 0);
|
|
85
|
+
if (storageChoice === 'file') {
|
|
86
|
+
const { store } = await readAuthStore({ homeDir: options.homeDir });
|
|
87
|
+
const existingNames = Object.keys(store.profiles);
|
|
88
|
+
let profileName = explicitProfile ?? '';
|
|
89
|
+
if (!profileName) {
|
|
90
|
+
const suggested = detectedOwner && isValidProfileName(detectedOwner)
|
|
91
|
+
? detectedOwner
|
|
92
|
+
: existingNames.length === 0
|
|
93
|
+
? DEFAULT_PROFILE_NAME
|
|
94
|
+
: undefined;
|
|
95
|
+
for (;;) {
|
|
96
|
+
const entered = await textImpl('Profile name', suggested);
|
|
97
|
+
if (isValidProfileName(entered)) {
|
|
98
|
+
profileName = entered.trim();
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
output.log(fmt.yellow('Use 1-64 characters: letters, numbers, hyphens, underscores, or dots (starting with a letter or number).'));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const alreadyExists = Object.hasOwn(store.profiles, profileName);
|
|
105
|
+
if (alreadyExists) {
|
|
106
|
+
const overwrite = await confirmImpl(`Profile ${fmt.bold(profileName)} already exists in ~/${API_KEY_FILENAME}. Overwrite its API key?`, true);
|
|
107
|
+
if (!overwrite) {
|
|
108
|
+
output.log('Aborted.');
|
|
109
|
+
return { saved: false };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const hasOtherProfiles = existingNames.some((n) => n !== profileName);
|
|
113
|
+
let setAsDefault = Boolean(options.default) || !hasOtherProfiles;
|
|
114
|
+
if (!setAsDefault && store.defaultProfile !== profileName) {
|
|
115
|
+
setAsDefault = await confirmImpl(`Set ${fmt.bold(profileName)} as your default profile?`, false);
|
|
116
|
+
}
|
|
117
|
+
const saved = await saveProfileToFile(profileName, apiKey, {
|
|
118
|
+
setAsDefault,
|
|
119
|
+
homeDir: options.homeDir,
|
|
120
|
+
});
|
|
121
|
+
const defaultNote = saved.isDefault ? ` ${fmt.cyan('(default)')}` : '';
|
|
122
|
+
output.log(`\n${fmt.green('✓')} Saved profile ${fmt.bold(saved.profile)}${defaultNote} to ${fmt.bold(saved.filePath)} ${fmt.dim('(mode 0600)')}`);
|
|
123
|
+
if (env[API_KEY_ENV_VAR]) {
|
|
124
|
+
output.log(fmt.yellow(`Note: ${API_KEY_ENV_VAR} is currently set in your shell and will take precedence over ~/${API_KEY_FILENAME} until unset (unless --profile is passed).`));
|
|
125
|
+
}
|
|
126
|
+
return { saved: true, storage: 'file', profile: saved.profile };
|
|
127
|
+
}
|
|
128
|
+
const snippets = getShellExportSnippets(apiKey);
|
|
129
|
+
output.log('\nAdd the appropriate line to your shell configuration file:\n');
|
|
130
|
+
for (const snippet of snippets) {
|
|
131
|
+
output.log(` ${fmt.bold(snippet.shell)} ${fmt.dim(`(${snippet.rcFile})`)}:`);
|
|
132
|
+
output.log(` ${fmt.cyan(snippet.line)}\n`);
|
|
133
|
+
}
|
|
134
|
+
output.log(fmt.dim('Then reload your shell (e.g. `source ~/.zshrc`) and run `cdpn auth status` to verify.'));
|
|
135
|
+
return { saved: true, storage: 'env' };
|
|
136
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { API_KEY_ENV_VAR, API_KEY_FILENAME, getKeyType, } from '../../config/auth.js';
|
|
2
|
+
import { clearAllProfilesFromFile, readAuthStore, removeProfileFromFile, } from '../../config/user-store.js';
|
|
3
|
+
import { CliError } from '../../errors.js';
|
|
4
|
+
import { fmt } from '../../utils/format.js';
|
|
5
|
+
import { printJsonResponse } from '../../utils/json.js';
|
|
6
|
+
import { defaultOutput } from '../../utils/output.js';
|
|
7
|
+
import { isInteractive, promptSelect } from '../../utils/prompts/index.js';
|
|
8
|
+
export async function runAuthLogoutCommand(options = {}) {
|
|
9
|
+
const output = options.output ?? defaultOutput;
|
|
10
|
+
const env = options.env ?? process.env;
|
|
11
|
+
const interactive = !options.json && (options.interactive ?? isInteractive());
|
|
12
|
+
const selectImpl = options.selectProfile ?? promptSelect;
|
|
13
|
+
const { store, exists } = await readAuthStore({ homeDir: options.homeDir });
|
|
14
|
+
const profileNames = Object.keys(store.profiles).sort((a, b) => a.localeCompare(b));
|
|
15
|
+
const envVarStillSet = Boolean(env[API_KEY_ENV_VAR]);
|
|
16
|
+
const explicitTarget = options.profileArg?.trim() || options.explicitProfile;
|
|
17
|
+
let removed = false;
|
|
18
|
+
let removedProfiles = [];
|
|
19
|
+
let remainingProfiles = [...profileNames];
|
|
20
|
+
if (!exists || profileNames.length === 0) {
|
|
21
|
+
if (options.json) {
|
|
22
|
+
if (explicitTarget && !options.all) {
|
|
23
|
+
printJsonResponse(404, `Profile "${explicitTarget}" was not found in ~/${API_KEY_FILENAME}.`, {
|
|
24
|
+
removed: false,
|
|
25
|
+
removed_profiles: [],
|
|
26
|
+
remaining_profiles: [],
|
|
27
|
+
default_profile: null,
|
|
28
|
+
file_deleted: false,
|
|
29
|
+
env_var_still_set: envVarStillSet,
|
|
30
|
+
}, output);
|
|
31
|
+
return { removed: false, removedProfiles: [], remainingProfiles: [], exitCode: 1 };
|
|
32
|
+
}
|
|
33
|
+
printJsonResponse(200, `No ~/${API_KEY_FILENAME} file found.`, {
|
|
34
|
+
removed: false,
|
|
35
|
+
removed_profiles: [],
|
|
36
|
+
remaining_profiles: [],
|
|
37
|
+
default_profile: null,
|
|
38
|
+
file_deleted: false,
|
|
39
|
+
env_var_still_set: envVarStillSet,
|
|
40
|
+
}, output);
|
|
41
|
+
return { removed: false, removedProfiles: [], remainingProfiles: [] };
|
|
42
|
+
}
|
|
43
|
+
output.log(fmt.dim(`No ~/${API_KEY_FILENAME} file found.`));
|
|
44
|
+
}
|
|
45
|
+
else if (options.all) {
|
|
46
|
+
const cleared = await clearAllProfilesFromFile({ homeDir: options.homeDir });
|
|
47
|
+
const msg = cleared.fileDeleted
|
|
48
|
+
? `Removed ~/${API_KEY_FILENAME} (all profiles).`
|
|
49
|
+
: `Removed all profiles from ~/${API_KEY_FILENAME} (kept global config).`;
|
|
50
|
+
removed = true;
|
|
51
|
+
removedProfiles = [...profileNames];
|
|
52
|
+
remainingProfiles = [];
|
|
53
|
+
if (options.json) {
|
|
54
|
+
printJsonResponse(200, msg, {
|
|
55
|
+
removed: true,
|
|
56
|
+
removed_profiles: profileNames,
|
|
57
|
+
remaining_profiles: [],
|
|
58
|
+
default_profile: null,
|
|
59
|
+
file_deleted: cleared.fileDeleted,
|
|
60
|
+
env_var_still_set: envVarStillSet,
|
|
61
|
+
}, output);
|
|
62
|
+
return { removed, removedProfiles, remainingProfiles };
|
|
63
|
+
}
|
|
64
|
+
output.log(`${fmt.green('✓')} ${msg}`);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
let target = explicitTarget;
|
|
68
|
+
if (!target) {
|
|
69
|
+
if (profileNames.length === 1) {
|
|
70
|
+
const cleared = await clearAllProfilesFromFile({ homeDir: options.homeDir });
|
|
71
|
+
const msg = cleared.fileDeleted
|
|
72
|
+
? `Removed profile ${profileNames[0]} (~/${API_KEY_FILENAME}).`
|
|
73
|
+
: `Removed profile ${profileNames[0]} from ~/${API_KEY_FILENAME} (kept global config).`;
|
|
74
|
+
removed = true;
|
|
75
|
+
removedProfiles = [profileNames[0]];
|
|
76
|
+
remainingProfiles = [];
|
|
77
|
+
if (options.json) {
|
|
78
|
+
printJsonResponse(200, msg, {
|
|
79
|
+
removed: true,
|
|
80
|
+
removed_profiles: [profileNames[0]],
|
|
81
|
+
remaining_profiles: [],
|
|
82
|
+
default_profile: null,
|
|
83
|
+
file_deleted: cleared.fileDeleted,
|
|
84
|
+
env_var_still_set: envVarStillSet,
|
|
85
|
+
}, output);
|
|
86
|
+
return { removed, removedProfiles, remainingProfiles };
|
|
87
|
+
}
|
|
88
|
+
output.log(cleared.fileDeleted
|
|
89
|
+
? `${fmt.green('✓')} Removed profile ${fmt.bold(profileNames[0])} (~/${API_KEY_FILENAME}).`
|
|
90
|
+
: `${fmt.green('✓')} Removed profile ${fmt.bold(profileNames[0])} from ~/${API_KEY_FILENAME} ${fmt.dim('(kept global config)')}.`);
|
|
91
|
+
}
|
|
92
|
+
else if (interactive) {
|
|
93
|
+
const ALL_SENTINEL = '__ALL__';
|
|
94
|
+
const choice = await selectImpl(`Which profile would you like to remove from ~/${API_KEY_FILENAME}?`, [
|
|
95
|
+
...profileNames.map((name) => ({
|
|
96
|
+
value: name,
|
|
97
|
+
label: name,
|
|
98
|
+
description: `${getKeyType(store.profiles[name])}${name === store.defaultProfile ? ' · default' : ''}`,
|
|
99
|
+
})),
|
|
100
|
+
{
|
|
101
|
+
value: ALL_SENTINEL,
|
|
102
|
+
label: `Remove all profiles (~/${API_KEY_FILENAME})`,
|
|
103
|
+
},
|
|
104
|
+
], 0);
|
|
105
|
+
if (choice === ALL_SENTINEL) {
|
|
106
|
+
const cleared = await clearAllProfilesFromFile({ homeDir: options.homeDir });
|
|
107
|
+
removed = true;
|
|
108
|
+
removedProfiles = [...profileNames];
|
|
109
|
+
remainingProfiles = [];
|
|
110
|
+
output.log(cleared.fileDeleted
|
|
111
|
+
? `${fmt.green('✓')} Removed ~/${API_KEY_FILENAME} (all profiles).`
|
|
112
|
+
: `${fmt.green('✓')} Removed all profiles from ~/${API_KEY_FILENAME} ${fmt.dim('(kept global config)')}.`);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
target = choice;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
throw new CliError(400, `Multiple profiles exist (${profileNames.join(', ')}). Run \`cdpn auth logout <profile>\` or \`cdpn auth logout --all\`.`, { available_profiles: profileNames });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (target) {
|
|
123
|
+
const result = await removeProfileFromFile(target, { homeDir: options.homeDir });
|
|
124
|
+
const remaining = [...result.remainingProfiles].sort((a, b) => a.localeCompare(b));
|
|
125
|
+
remainingProfiles = remaining;
|
|
126
|
+
if (!result.removed) {
|
|
127
|
+
if (options.json) {
|
|
128
|
+
printJsonResponse(404, `Profile "${target}" was not found in ~/${API_KEY_FILENAME}.`, {
|
|
129
|
+
removed: false,
|
|
130
|
+
removed_profiles: [],
|
|
131
|
+
remaining_profiles: remaining,
|
|
132
|
+
default_profile: result.newDefault,
|
|
133
|
+
file_deleted: false,
|
|
134
|
+
env_var_still_set: envVarStillSet,
|
|
135
|
+
}, output);
|
|
136
|
+
return { removed: false, removedProfiles: [], remainingProfiles: remaining, exitCode: 1 };
|
|
137
|
+
}
|
|
138
|
+
output.log(fmt.yellow(`Profile "${target}" was not found in ~/${API_KEY_FILENAME}.`));
|
|
139
|
+
}
|
|
140
|
+
else if (remaining.length === 0) {
|
|
141
|
+
removed = true;
|
|
142
|
+
removedProfiles = [target];
|
|
143
|
+
const msg = result.fileDeleted
|
|
144
|
+
? `Removed profile ${target} and deleted ~/${API_KEY_FILENAME}.`
|
|
145
|
+
: `Removed profile ${target} from ~/${API_KEY_FILENAME} (kept global config).`;
|
|
146
|
+
if (options.json) {
|
|
147
|
+
printJsonResponse(200, msg, {
|
|
148
|
+
removed: true,
|
|
149
|
+
removed_profiles: [target],
|
|
150
|
+
remaining_profiles: [],
|
|
151
|
+
default_profile: null,
|
|
152
|
+
file_deleted: result.fileDeleted,
|
|
153
|
+
env_var_still_set: envVarStillSet,
|
|
154
|
+
}, output);
|
|
155
|
+
return { removed, removedProfiles, remainingProfiles };
|
|
156
|
+
}
|
|
157
|
+
output.log(result.fileDeleted
|
|
158
|
+
? `${fmt.green('✓')} Removed profile ${fmt.bold(target)} and deleted ~/${API_KEY_FILENAME}.`
|
|
159
|
+
: `${fmt.green('✓')} Removed profile ${fmt.bold(target)} from ~/${API_KEY_FILENAME} ${fmt.dim('(kept global config)')}.`);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
removed = true;
|
|
163
|
+
removedProfiles = [target];
|
|
164
|
+
if (options.json) {
|
|
165
|
+
printJsonResponse(200, `Removed profile ${target} from ~/${API_KEY_FILENAME}.`, {
|
|
166
|
+
removed: true,
|
|
167
|
+
removed_profiles: [target],
|
|
168
|
+
remaining_profiles: remaining,
|
|
169
|
+
default_profile: result.newDefault,
|
|
170
|
+
file_deleted: false,
|
|
171
|
+
env_var_still_set: envVarStillSet,
|
|
172
|
+
}, output);
|
|
173
|
+
return { removed, removedProfiles, remainingProfiles };
|
|
174
|
+
}
|
|
175
|
+
output.log(`${fmt.green('✓')} Removed profile ${fmt.bold(target)} from ~/${API_KEY_FILENAME}.`);
|
|
176
|
+
if (result.newDefault) {
|
|
177
|
+
output.log(fmt.dim(` Default profile: ${result.newDefault}`));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (envVarStillSet) {
|
|
183
|
+
output.log(fmt.yellow(`Note: ${API_KEY_ENV_VAR} is still set in your environment. Unset it in your shell config to log out completely.`));
|
|
184
|
+
}
|
|
185
|
+
return { removed, removedProfiles, remainingProfiles };
|
|
186
|
+
}
|