codepen-commander 1.3.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 +103 -11
- 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 +548 -80
- package/commands/deploy.js +164 -169
- 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 +221 -376
- 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 +103 -231
- 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/{commands → workflows}/create-pen.js +59 -84
- 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/meta.js +0 -184
- package/utils/prompts.js +0 -316
- /package/{types.js → api/types.js} +0 -0
package/commands/auth.js
DELETED
|
@@ -1,593 +0,0 @@
|
|
|
1
|
-
import { CodePenClient } from '../api/client.js';
|
|
2
|
-
import { isCodePenApiError } from '../api/errors.js';
|
|
3
|
-
import { API_KEY_ENV_VAR, API_KEY_FILENAME, CODEPEN_API_SETTINGS_URL, DEFAULT_PROFILE_NAME, getKeyType, getShellExportSnippets, isRecognizedKeyFormat, isValidProfileName, maskApiKey, readAuthStore, removeApiKeyFile, removeProfileFromFile, saveProfileToFile, setDefaultProfile, validateProfileName, } from '../config/auth.js';
|
|
4
|
-
import { extractCodePenOwnerFromUrl, findProject } from '../config/project.js';
|
|
5
|
-
import { openBrowser } from '../utils/browser.js';
|
|
6
|
-
import { fmt, formatTable } from '../utils/format.js';
|
|
7
|
-
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
8
|
-
import { isInteractive, promptConfirm, promptSecret, promptSelect, promptText } from '../utils/prompts.js';
|
|
9
|
-
import { getGlobalProfileOption } from './auth-context.js';
|
|
10
|
-
async function verifyKeyForStatus(apiKey) {
|
|
11
|
-
const client = new CodePenClient({ apiKey });
|
|
12
|
-
try {
|
|
13
|
-
await client.verifyAuth();
|
|
14
|
-
return {
|
|
15
|
-
ok: true,
|
|
16
|
-
error: null,
|
|
17
|
-
detail: `${fmt.green('Valid')} ${fmt.dim('(verified with api.codepen.io/v2)')}`,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
catch (error) {
|
|
21
|
-
const msg = isCodePenApiError(error) ? error.message : error.message;
|
|
22
|
-
return {
|
|
23
|
-
ok: false,
|
|
24
|
-
error: msg,
|
|
25
|
-
detail: `${fmt.red('Invalid')} — ${msg}`,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
export function registerAuthCommands(program) {
|
|
30
|
-
const authCmd = program
|
|
31
|
-
.command('auth')
|
|
32
|
-
.description('Manage CodePen API v2 authentication and profiles');
|
|
33
|
-
authCmd
|
|
34
|
-
.command('status')
|
|
35
|
-
.description('Show loaded CodePen API keys/profiles and verify them')
|
|
36
|
-
.option('--offline', 'Skip live verification against the CodePen API')
|
|
37
|
-
.option('--json', 'Output authentication status as JSON')
|
|
38
|
-
.action(async (options, command) => {
|
|
39
|
-
const requestedProfile = getGlobalProfileOption(command);
|
|
40
|
-
const envKey = process.env[API_KEY_ENV_VAR]?.trim() || '';
|
|
41
|
-
const { store, filePath, insecurePermissions } = await readAuthStore();
|
|
42
|
-
const profileNames = Object.keys(store.profiles).sort((a, b) => a.localeCompare(b));
|
|
43
|
-
const project = await findProject(process.cwd()).catch(() => null);
|
|
44
|
-
const projectProfile = project?.config.profile ?? null;
|
|
45
|
-
if (requestedProfile) {
|
|
46
|
-
const validName = validateProfileName(requestedProfile);
|
|
47
|
-
if (!Object.hasOwn(store.profiles, validName)) {
|
|
48
|
-
if (options.json) {
|
|
49
|
-
printJsonResponse(404, `Profile "${validName}" not found in ${filePath}.`, {
|
|
50
|
-
authenticated: false,
|
|
51
|
-
offline: Boolean(options.offline),
|
|
52
|
-
requested_profile: validName,
|
|
53
|
-
file_path: filePath,
|
|
54
|
-
available_profiles: profileNames,
|
|
55
|
-
});
|
|
56
|
-
process.exitCode = 1;
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
console.log(`${fmt.red('✗')} Profile ${fmt.bold(validName)} not found in ${filePath}.`);
|
|
60
|
-
if (profileNames.length > 0) {
|
|
61
|
-
console.log(fmt.dim(` Available profiles: ${profileNames.join(', ')}`));
|
|
62
|
-
}
|
|
63
|
-
console.log(fmt.dim(` Run \`cdpn auth login ${validName}\` to add it.`));
|
|
64
|
-
process.exitCode = 1;
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
const key = store.profiles[validName];
|
|
68
|
-
const check = options.offline ? null : await verifyKeyForStatus(key);
|
|
69
|
-
if (options.json) {
|
|
70
|
-
const ok = check ? check.ok : true;
|
|
71
|
-
const code = ok ? 200 : 401;
|
|
72
|
-
const message = options.offline
|
|
73
|
-
? `Loaded profile "${validName}" (offline mode).`
|
|
74
|
-
: ok
|
|
75
|
-
? `Profile "${validName}" is valid.`
|
|
76
|
-
: `Profile "${validName}" failed verification: ${check.error}`;
|
|
77
|
-
printJsonResponse(code, message, {
|
|
78
|
-
authenticated: ok,
|
|
79
|
-
offline: Boolean(options.offline),
|
|
80
|
-
file_path: filePath,
|
|
81
|
-
insecure_permissions: insecurePermissions,
|
|
82
|
-
project_profile: projectProfile,
|
|
83
|
-
profile: {
|
|
84
|
-
name: validName,
|
|
85
|
-
key_type: getKeyType(key),
|
|
86
|
-
masked_key: maskApiKey(key),
|
|
87
|
-
is_default: validName === store.defaultProfile,
|
|
88
|
-
is_project_profile: validName === projectProfile,
|
|
89
|
-
verified: check ? check.ok : null,
|
|
90
|
-
verification_error: check ? check.error : null,
|
|
91
|
-
},
|
|
92
|
-
});
|
|
93
|
-
if (!ok)
|
|
94
|
-
process.exitCode = 1;
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
const badges = [];
|
|
98
|
-
if (validName === store.defaultProfile)
|
|
99
|
-
badges.push(fmt.cyan('default'));
|
|
100
|
-
if (validName === projectProfile)
|
|
101
|
-
badges.push(fmt.green('current folder'));
|
|
102
|
-
const badgeText = badges.length > 0 ? ` ${fmt.dim(`(${badges.join(', ')})`)}` : '';
|
|
103
|
-
console.log(`${fmt.green('✓')} Profile ${fmt.bold(validName)}${badgeText} from ${fmt.dim(filePath)}`);
|
|
104
|
-
console.log(` Key: ${fmt.cyan(maskApiKey(key))} ${fmt.dim(`(${getKeyType(key)} key)`)}`);
|
|
105
|
-
if (insecurePermissions) {
|
|
106
|
-
console.log(fmt.yellow(` Warning: ${filePath} is readable by other users. Tighten permissions with: chmod 600 ~/${API_KEY_FILENAME}`));
|
|
107
|
-
}
|
|
108
|
-
if (check) {
|
|
109
|
-
console.log(` API: ${check.detail}`);
|
|
110
|
-
if (!check.ok)
|
|
111
|
-
process.exitCode = 1;
|
|
112
|
-
}
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
if (!envKey && profileNames.length === 0) {
|
|
116
|
-
if (options.json) {
|
|
117
|
-
printJsonResponse(401, `Not authenticated. No API key found in ${API_KEY_ENV_VAR} or ~/${API_KEY_FILENAME}.`, {
|
|
118
|
-
authenticated: false,
|
|
119
|
-
offline: Boolean(options.offline),
|
|
120
|
-
file_path: filePath,
|
|
121
|
-
insecure_permissions: insecurePermissions,
|
|
122
|
-
default_profile: store.defaultProfile,
|
|
123
|
-
project_profile: projectProfile,
|
|
124
|
-
env: null,
|
|
125
|
-
profiles: [],
|
|
126
|
-
});
|
|
127
|
-
process.exitCode = 1;
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
console.log(`${fmt.red('✗')} Not authenticated.`);
|
|
131
|
-
console.log(fmt.dim(`No API key found in ${API_KEY_ENV_VAR} or ~/${API_KEY_FILENAME}. Run \`cdpn auth login\` to get started.`));
|
|
132
|
-
process.exitCode = 1;
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
let anyInvalid = false;
|
|
136
|
-
if (options.json) {
|
|
137
|
-
let envEntry = null;
|
|
138
|
-
if (envKey) {
|
|
139
|
-
const check = options.offline ? null : await verifyKeyForStatus(envKey);
|
|
140
|
-
if (check && !check.ok)
|
|
141
|
-
anyInvalid = true;
|
|
142
|
-
envEntry = {
|
|
143
|
-
env_var: API_KEY_ENV_VAR,
|
|
144
|
-
key_type: getKeyType(envKey),
|
|
145
|
-
masked_key: maskApiKey(envKey),
|
|
146
|
-
verified: check ? check.ok : null,
|
|
147
|
-
verification_error: check ? check.error : null,
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
const profileEntries = [];
|
|
151
|
-
for (const name of profileNames) {
|
|
152
|
-
const key = store.profiles[name];
|
|
153
|
-
const check = options.offline ? null : await verifyKeyForStatus(key);
|
|
154
|
-
if (check && !check.ok)
|
|
155
|
-
anyInvalid = true;
|
|
156
|
-
profileEntries.push({
|
|
157
|
-
name,
|
|
158
|
-
key_type: getKeyType(key),
|
|
159
|
-
masked_key: maskApiKey(key),
|
|
160
|
-
is_default: name === store.defaultProfile,
|
|
161
|
-
is_project_profile: name === projectProfile,
|
|
162
|
-
verified: check ? check.ok : null,
|
|
163
|
-
verification_error: check ? check.error : null,
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
const totalCount = (envEntry ? 1 : 0) + profileEntries.length;
|
|
167
|
-
const code = anyInvalid ? 401 : 200;
|
|
168
|
-
const message = options.offline
|
|
169
|
-
? `Loaded ${totalCount} authentication credential(s) (offline mode).`
|
|
170
|
-
: anyInvalid
|
|
171
|
-
? 'One or more CodePen API keys failed verification.'
|
|
172
|
-
: 'All CodePen API keys are valid.';
|
|
173
|
-
printJsonResponse(code, message, {
|
|
174
|
-
authenticated: !anyInvalid,
|
|
175
|
-
offline: Boolean(options.offline),
|
|
176
|
-
file_path: filePath,
|
|
177
|
-
insecure_permissions: insecurePermissions,
|
|
178
|
-
default_profile: store.defaultProfile,
|
|
179
|
-
project_profile: projectProfile,
|
|
180
|
-
env: envEntry,
|
|
181
|
-
profiles: profileEntries,
|
|
182
|
-
});
|
|
183
|
-
if (anyInvalid) {
|
|
184
|
-
process.exitCode = 1;
|
|
185
|
-
}
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
if (envKey) {
|
|
189
|
-
console.log(`${fmt.green('✓')} API key loaded from ${fmt.bold(`environment variable (${API_KEY_ENV_VAR})`)}`);
|
|
190
|
-
console.log(` Key: ${fmt.cyan(maskApiKey(envKey))} ${fmt.dim(`(${getKeyType(envKey)} key)`)}`);
|
|
191
|
-
if (!options.offline) {
|
|
192
|
-
const check = await verifyKeyForStatus(envKey);
|
|
193
|
-
console.log(` API: ${check.detail}`);
|
|
194
|
-
if (!check.ok)
|
|
195
|
-
anyInvalid = true;
|
|
196
|
-
}
|
|
197
|
-
if (profileNames.length > 0) {
|
|
198
|
-
console.log(fmt.dim(` Note: ${API_KEY_ENV_VAR} takes precedence over ~/${API_KEY_FILENAME} unless --profile <name> is specified.\n`));
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
if (profileNames.length > 0) {
|
|
202
|
-
console.log(`${fmt.green('✓')} Stored profiles in ${fmt.bold(filePath)} ${fmt.dim(`(${profileNames.length})`)}:`);
|
|
203
|
-
for (const name of profileNames) {
|
|
204
|
-
const key = store.profiles[name];
|
|
205
|
-
const badges = [];
|
|
206
|
-
if (name === store.defaultProfile)
|
|
207
|
-
badges.push(fmt.cyan('default'));
|
|
208
|
-
if (name === projectProfile)
|
|
209
|
-
badges.push(fmt.green('current folder'));
|
|
210
|
-
const badgeText = badges.length > 0 ? ` ${fmt.dim(`[${badges.join(', ')}]`)}` : '';
|
|
211
|
-
console.log(` • ${fmt.bold(name)}${badgeText}`);
|
|
212
|
-
console.log(` Key: ${fmt.cyan(maskApiKey(key))} ${fmt.dim(`(${getKeyType(key)} key)`)}`);
|
|
213
|
-
if (!options.offline) {
|
|
214
|
-
const check = await verifyKeyForStatus(key);
|
|
215
|
-
console.log(` API: ${check.detail}`);
|
|
216
|
-
if (!check.ok)
|
|
217
|
-
anyInvalid = true;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
if (projectProfile && !Object.hasOwn(store.profiles, projectProfile)) {
|
|
221
|
-
console.log(fmt.yellow(`\n Warning: Current folder’s .cdpn.json references profile "${projectProfile}", which is not in ~/${API_KEY_FILENAME}.`));
|
|
222
|
-
}
|
|
223
|
-
if (insecurePermissions) {
|
|
224
|
-
console.log(fmt.yellow(`\n Warning: ${filePath} is readable by other users. Tighten permissions with: chmod 600 ~/${API_KEY_FILENAME}`));
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (anyInvalid) {
|
|
228
|
-
process.exitCode = 1;
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
authCmd
|
|
232
|
-
.command('list')
|
|
233
|
-
.alias('ls')
|
|
234
|
-
.description(`List stored CodePen profiles in ~/${API_KEY_FILENAME}`)
|
|
235
|
-
.option('--json', 'Output stored profiles as JSON')
|
|
236
|
-
.action(async (options) => {
|
|
237
|
-
const { store, filePath, insecurePermissions } = await readAuthStore();
|
|
238
|
-
const profileNames = Object.keys(store.profiles).sort((a, b) => a.localeCompare(b));
|
|
239
|
-
const project = await findProject(process.cwd()).catch(() => null);
|
|
240
|
-
const projectProfile = project?.config.profile ?? null;
|
|
241
|
-
if (options.json) {
|
|
242
|
-
const message = profileNames.length === 0
|
|
243
|
-
? `No profiles stored in ~/${API_KEY_FILENAME}.`
|
|
244
|
-
: `Found ${profileNames.length} stored profile(s).`;
|
|
245
|
-
printJsonResponse(200, message, {
|
|
246
|
-
file_path: filePath,
|
|
247
|
-
default_profile: store.defaultProfile,
|
|
248
|
-
project_profile: projectProfile,
|
|
249
|
-
env_var_set: Boolean(process.env[API_KEY_ENV_VAR]?.trim()),
|
|
250
|
-
insecure_permissions: insecurePermissions,
|
|
251
|
-
profiles: profileNames.map((name) => {
|
|
252
|
-
const key = store.profiles[name];
|
|
253
|
-
return {
|
|
254
|
-
name,
|
|
255
|
-
key_type: getKeyType(key),
|
|
256
|
-
masked_key: maskApiKey(key),
|
|
257
|
-
is_default: name === store.defaultProfile,
|
|
258
|
-
is_project_profile: name === projectProfile,
|
|
259
|
-
};
|
|
260
|
-
}),
|
|
261
|
-
});
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
if (profileNames.length === 0) {
|
|
265
|
-
console.log(fmt.dim(`No profiles stored in ${filePath}. Run \`cdpn auth login\` to add one.`));
|
|
266
|
-
if (process.env[API_KEY_ENV_VAR]) {
|
|
267
|
-
console.log(fmt.dim(`(${API_KEY_ENV_VAR} is currently set in your environment.)`));
|
|
268
|
-
}
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
const rows = profileNames.map((name) => {
|
|
272
|
-
const key = store.profiles[name];
|
|
273
|
-
const markers = [];
|
|
274
|
-
if (name === store.defaultProfile)
|
|
275
|
-
markers.push(fmt.cyan('default'));
|
|
276
|
-
if (name === projectProfile)
|
|
277
|
-
markers.push(fmt.green('current folder'));
|
|
278
|
-
return [
|
|
279
|
-
fmt.bold(name),
|
|
280
|
-
getKeyType(key),
|
|
281
|
-
fmt.cyan(maskApiKey(key)),
|
|
282
|
-
markers.length > 0 ? markers.join(', ') : fmt.dim('—'),
|
|
283
|
-
];
|
|
284
|
-
});
|
|
285
|
-
console.log(formatTable(['Profile', 'Type', 'Key', 'Markers'], rows));
|
|
286
|
-
if (insecurePermissions) {
|
|
287
|
-
console.log(fmt.yellow(`\nWarning: ${filePath} is readable by other users. Tighten permissions with: chmod 600 ~/${API_KEY_FILENAME}`));
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
authCmd
|
|
291
|
-
.command('default [profile]')
|
|
292
|
-
.description(`Set the default CodePen profile in ~/${API_KEY_FILENAME}`)
|
|
293
|
-
.option('--json', 'Output result as JSON')
|
|
294
|
-
.action(async (profileArg, options, command) => {
|
|
295
|
-
const { store } = await readAuthStore();
|
|
296
|
-
const profileNames = Object.keys(store.profiles).sort((a, b) => a.localeCompare(b));
|
|
297
|
-
if (profileNames.length === 0) {
|
|
298
|
-
throw new CliError(404, `No profiles found in ~/${API_KEY_FILENAME}. Run \`cdpn auth login\` first.`);
|
|
299
|
-
}
|
|
300
|
-
let target = profileArg?.trim() || getGlobalProfileOption(command);
|
|
301
|
-
if (!target) {
|
|
302
|
-
if (options.json || !isInteractive()) {
|
|
303
|
-
throw new CliError(400, `Specify a profile name: \`cdpn auth default <profile>\`. Available: ${profileNames.join(', ')}`, { available_profiles: profileNames });
|
|
304
|
-
}
|
|
305
|
-
const defaultIdx = Math.max(0, store.defaultProfile ? profileNames.indexOf(store.defaultProfile) : 0);
|
|
306
|
-
target = await promptSelect('Select your default CodePen profile:', profileNames.map((name) => ({
|
|
307
|
-
value: name,
|
|
308
|
-
label: name,
|
|
309
|
-
description: `${getKeyType(store.profiles[name])}${name === store.defaultProfile ? ' · current default' : ''}`,
|
|
310
|
-
})), defaultIdx);
|
|
311
|
-
}
|
|
312
|
-
const validTarget = validateProfileName(target);
|
|
313
|
-
await setDefaultProfile(validTarget);
|
|
314
|
-
if (options.json) {
|
|
315
|
-
printJsonResponse(200, `Default profile set to "${validTarget}".`, {
|
|
316
|
-
default_profile: validTarget,
|
|
317
|
-
profiles: profileNames,
|
|
318
|
-
});
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
console.log(`${fmt.green('✓')} Default profile set to ${fmt.bold(validTarget)}.`);
|
|
322
|
-
});
|
|
323
|
-
authCmd
|
|
324
|
-
.command('login [profile]')
|
|
325
|
-
.description(`Open CodePen API settings, verify a key, and save a profile to ~/${API_KEY_FILENAME}`)
|
|
326
|
-
.option('--default', 'Set this profile as the default profile')
|
|
327
|
-
.option('--no-open', 'Do not automatically open the browser')
|
|
328
|
-
.action(async (profileArg, options, command) => {
|
|
329
|
-
if (!isInteractive()) {
|
|
330
|
-
throw new Error(`Interactive login requires a TTY. Set ${API_KEY_ENV_VAR} in your environment instead.`);
|
|
331
|
-
}
|
|
332
|
-
const explicitProfile = profileArg?.trim() || getGlobalProfileOption(command);
|
|
333
|
-
if (explicitProfile) {
|
|
334
|
-
validateProfileName(explicitProfile);
|
|
335
|
-
}
|
|
336
|
-
if (options.open !== false) {
|
|
337
|
-
console.log(`Opening ${fmt.cyan(CODEPEN_API_SETTINGS_URL)} in your browser…`);
|
|
338
|
-
const launched = await openBrowser(CODEPEN_API_SETTINGS_URL);
|
|
339
|
-
if (!launched) {
|
|
340
|
-
console.log(fmt.yellow(`Could not launch browser automatically. Open this URL manually:\n ${CODEPEN_API_SETTINGS_URL}`));
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
else {
|
|
344
|
-
console.log(`Generate or reveal your API key at:\n ${fmt.cyan(CODEPEN_API_SETTINGS_URL)}`);
|
|
345
|
-
}
|
|
346
|
-
console.log(fmt.dim('Click "Generate API Key" (or "Reveal") and paste it below.\n'));
|
|
347
|
-
let apiKey = '';
|
|
348
|
-
let detectedOwner = null;
|
|
349
|
-
for (;;) {
|
|
350
|
-
apiKey = await promptSecret('CodePen API key');
|
|
351
|
-
if (!apiKey) {
|
|
352
|
-
console.log(fmt.yellow('API key cannot be empty.'));
|
|
353
|
-
continue;
|
|
354
|
-
}
|
|
355
|
-
if (!isRecognizedKeyFormat(apiKey)) {
|
|
356
|
-
console.log(fmt.yellow('Heads up: CodePen API v2 keys usually start with `cp_user_` or `cp_team_`.'));
|
|
357
|
-
}
|
|
358
|
-
process.stdout.write('Verifying key with CodePen API v2… ');
|
|
359
|
-
const client = new CodePenClient({ apiKey });
|
|
360
|
-
try {
|
|
361
|
-
const samplePen = await client.verifyAuth();
|
|
362
|
-
detectedOwner = samplePen?.urls?.editor
|
|
363
|
-
? extractCodePenOwnerFromUrl(samplePen.urls.editor)
|
|
364
|
-
: null;
|
|
365
|
-
console.log(fmt.green('✓ Valid!'));
|
|
366
|
-
console.log(`Recognized ${fmt.bold(getKeyType(apiKey))} key (${fmt.cyan(maskApiKey(apiKey))})` +
|
|
367
|
-
(detectedOwner ? ` ${fmt.dim(`· account: ${detectedOwner}`)}` : '') +
|
|
368
|
-
'.\n');
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
catch (error) {
|
|
372
|
-
console.log(fmt.red('✗ Failed'));
|
|
373
|
-
const msg = isCodePenApiError(error) ? error.message : error.message;
|
|
374
|
-
console.log(fmt.red(` ${msg}`));
|
|
375
|
-
const retry = await promptConfirm('Try entering a different key?', true);
|
|
376
|
-
if (!retry) {
|
|
377
|
-
process.exitCode = 1;
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
const storageChoice = await promptSelect('How would you like to store your API key?', [
|
|
383
|
-
{
|
|
384
|
-
value: 'file',
|
|
385
|
-
label: `Save to ~/${API_KEY_FILENAME}`,
|
|
386
|
-
description: 'supports multiple profiles, written with owner-only (0600) permissions',
|
|
387
|
-
},
|
|
388
|
-
{
|
|
389
|
-
value: 'env',
|
|
390
|
-
label: 'Add to my shell environment variables myself',
|
|
391
|
-
description: 'print export lines for ~/.zshrc, ~/.bashrc, etc.',
|
|
392
|
-
},
|
|
393
|
-
], 0);
|
|
394
|
-
if (storageChoice === 'file') {
|
|
395
|
-
const { store } = await readAuthStore();
|
|
396
|
-
const existingNames = Object.keys(store.profiles);
|
|
397
|
-
let profileName = explicitProfile ?? '';
|
|
398
|
-
if (!profileName) {
|
|
399
|
-
const suggested = detectedOwner && isValidProfileName(detectedOwner)
|
|
400
|
-
? detectedOwner
|
|
401
|
-
: existingNames.length === 0
|
|
402
|
-
? DEFAULT_PROFILE_NAME
|
|
403
|
-
: undefined;
|
|
404
|
-
for (;;) {
|
|
405
|
-
const entered = await promptText('Profile name', suggested);
|
|
406
|
-
if (isValidProfileName(entered)) {
|
|
407
|
-
profileName = entered.trim();
|
|
408
|
-
break;
|
|
409
|
-
}
|
|
410
|
-
console.log(fmt.yellow('Use 1-64 characters: letters, numbers, hyphens, underscores, or dots (starting with a letter or number).'));
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
const alreadyExists = Object.hasOwn(store.profiles, profileName);
|
|
414
|
-
if (alreadyExists) {
|
|
415
|
-
const overwrite = await promptConfirm(`Profile ${fmt.bold(profileName)} already exists in ~/${API_KEY_FILENAME}. Overwrite its API key?`, true);
|
|
416
|
-
if (!overwrite) {
|
|
417
|
-
console.log('Aborted.');
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
const hasOtherProfiles = existingNames.some((n) => n !== profileName);
|
|
422
|
-
let setAsDefault = Boolean(options.default) || !hasOtherProfiles;
|
|
423
|
-
if (!setAsDefault && store.defaultProfile !== profileName) {
|
|
424
|
-
setAsDefault = await promptConfirm(`Set ${fmt.bold(profileName)} as your default profile?`, false);
|
|
425
|
-
}
|
|
426
|
-
const saved = await saveProfileToFile(profileName, apiKey, { setAsDefault });
|
|
427
|
-
const defaultNote = saved.isDefault ? ` ${fmt.cyan('(default)')}` : '';
|
|
428
|
-
console.log(`\n${fmt.green('✓')} Saved profile ${fmt.bold(saved.profile)}${defaultNote} to ${fmt.bold(saved.filePath)} ${fmt.dim('(mode 0600)')}`);
|
|
429
|
-
if (process.env[API_KEY_ENV_VAR]) {
|
|
430
|
-
console.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).`));
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
else {
|
|
434
|
-
const snippets = getShellExportSnippets(apiKey);
|
|
435
|
-
console.log('\nAdd the appropriate line to your shell configuration file:\n');
|
|
436
|
-
for (const snippet of snippets) {
|
|
437
|
-
console.log(` ${fmt.bold(snippet.shell)} ${fmt.dim(`(${snippet.rcFile})`)}:`);
|
|
438
|
-
console.log(` ${fmt.cyan(snippet.line)}\n`);
|
|
439
|
-
}
|
|
440
|
-
console.log(fmt.dim('Then reload your shell (e.g. `source ~/.zshrc`) and run `cdpn auth status` to verify.'));
|
|
441
|
-
}
|
|
442
|
-
});
|
|
443
|
-
authCmd
|
|
444
|
-
.command('logout [profile]')
|
|
445
|
-
.description(`Remove a stored profile or the entire ~/${API_KEY_FILENAME} file`)
|
|
446
|
-
.option('--all', `Remove all stored profiles (~/${API_KEY_FILENAME})`)
|
|
447
|
-
.option('--json', 'Output logout result as JSON')
|
|
448
|
-
.action(async (profileArg, options, command) => {
|
|
449
|
-
const { store, exists } = await readAuthStore();
|
|
450
|
-
const profileNames = Object.keys(store.profiles).sort((a, b) => a.localeCompare(b));
|
|
451
|
-
const envVarStillSet = Boolean(process.env[API_KEY_ENV_VAR]);
|
|
452
|
-
const explicitTarget = profileArg?.trim() || getGlobalProfileOption(command);
|
|
453
|
-
if (!exists || profileNames.length === 0) {
|
|
454
|
-
if (options.json) {
|
|
455
|
-
if (explicitTarget && !options.all) {
|
|
456
|
-
printJsonResponse(404, `Profile "${explicitTarget}" was not found in ~/${API_KEY_FILENAME}.`, {
|
|
457
|
-
removed: false,
|
|
458
|
-
removed_profiles: [],
|
|
459
|
-
remaining_profiles: [],
|
|
460
|
-
default_profile: null,
|
|
461
|
-
file_deleted: false,
|
|
462
|
-
env_var_still_set: envVarStillSet,
|
|
463
|
-
});
|
|
464
|
-
process.exitCode = 1;
|
|
465
|
-
}
|
|
466
|
-
else {
|
|
467
|
-
printJsonResponse(200, `No ~/${API_KEY_FILENAME} file found.`, {
|
|
468
|
-
removed: false,
|
|
469
|
-
removed_profiles: [],
|
|
470
|
-
remaining_profiles: [],
|
|
471
|
-
default_profile: null,
|
|
472
|
-
file_deleted: false,
|
|
473
|
-
env_var_still_set: envVarStillSet,
|
|
474
|
-
});
|
|
475
|
-
}
|
|
476
|
-
return;
|
|
477
|
-
}
|
|
478
|
-
console.log(fmt.dim(`No ~/${API_KEY_FILENAME} file found.`));
|
|
479
|
-
}
|
|
480
|
-
else if (options.all) {
|
|
481
|
-
await removeApiKeyFile();
|
|
482
|
-
if (options.json) {
|
|
483
|
-
printJsonResponse(200, `Removed ~/${API_KEY_FILENAME} (all profiles).`, {
|
|
484
|
-
removed: true,
|
|
485
|
-
removed_profiles: profileNames,
|
|
486
|
-
remaining_profiles: [],
|
|
487
|
-
default_profile: null,
|
|
488
|
-
file_deleted: true,
|
|
489
|
-
env_var_still_set: envVarStillSet,
|
|
490
|
-
});
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
493
|
-
console.log(`${fmt.green('✓')} Removed ~/${API_KEY_FILENAME} (all profiles).`);
|
|
494
|
-
}
|
|
495
|
-
else {
|
|
496
|
-
let target = explicitTarget;
|
|
497
|
-
if (!target) {
|
|
498
|
-
if (profileNames.length === 1) {
|
|
499
|
-
await removeApiKeyFile();
|
|
500
|
-
if (options.json) {
|
|
501
|
-
printJsonResponse(200, `Removed profile ${profileNames[0]} (~/${API_KEY_FILENAME}).`, {
|
|
502
|
-
removed: true,
|
|
503
|
-
removed_profiles: [profileNames[0]],
|
|
504
|
-
remaining_profiles: [],
|
|
505
|
-
default_profile: null,
|
|
506
|
-
file_deleted: true,
|
|
507
|
-
env_var_still_set: envVarStillSet,
|
|
508
|
-
});
|
|
509
|
-
return;
|
|
510
|
-
}
|
|
511
|
-
console.log(`${fmt.green('✓')} Removed profile ${fmt.bold(profileNames[0])} (~/${API_KEY_FILENAME}).`);
|
|
512
|
-
}
|
|
513
|
-
else if (!options.json && isInteractive()) {
|
|
514
|
-
const ALL_SENTINEL = '__ALL__';
|
|
515
|
-
const choice = await promptSelect(`Which profile would you like to remove from ~/${API_KEY_FILENAME}?`, [
|
|
516
|
-
...profileNames.map((name) => ({
|
|
517
|
-
value: name,
|
|
518
|
-
label: name,
|
|
519
|
-
description: `${getKeyType(store.profiles[name])}${name === store.defaultProfile ? ' · default' : ''}`,
|
|
520
|
-
})),
|
|
521
|
-
{
|
|
522
|
-
value: ALL_SENTINEL,
|
|
523
|
-
label: `Remove all profiles (~/${API_KEY_FILENAME})`,
|
|
524
|
-
},
|
|
525
|
-
], 0);
|
|
526
|
-
if (choice === ALL_SENTINEL) {
|
|
527
|
-
await removeApiKeyFile();
|
|
528
|
-
console.log(`${fmt.green('✓')} Removed ~/${API_KEY_FILENAME} (all profiles).`);
|
|
529
|
-
}
|
|
530
|
-
else {
|
|
531
|
-
target = choice;
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
else {
|
|
535
|
-
throw new CliError(400, `Multiple profiles exist (${profileNames.join(', ')}). Run \`cdpn auth logout <profile>\` or \`cdpn auth logout --all\`.`, { available_profiles: profileNames });
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
if (target) {
|
|
539
|
-
const result = await removeProfileFromFile(target);
|
|
540
|
-
const remaining = [...result.remainingProfiles].sort((a, b) => a.localeCompare(b));
|
|
541
|
-
if (!result.removed) {
|
|
542
|
-
if (options.json) {
|
|
543
|
-
printJsonResponse(404, `Profile "${target}" was not found in ~/${API_KEY_FILENAME}.`, {
|
|
544
|
-
removed: false,
|
|
545
|
-
removed_profiles: [],
|
|
546
|
-
remaining_profiles: remaining,
|
|
547
|
-
default_profile: result.newDefault,
|
|
548
|
-
file_deleted: false,
|
|
549
|
-
env_var_still_set: envVarStillSet,
|
|
550
|
-
});
|
|
551
|
-
process.exitCode = 1;
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
|
-
console.log(fmt.yellow(`Profile "${target}" was not found in ~/${API_KEY_FILENAME}.`));
|
|
555
|
-
}
|
|
556
|
-
else if (remaining.length === 0) {
|
|
557
|
-
if (options.json) {
|
|
558
|
-
printJsonResponse(200, `Removed profile ${target} and deleted ~/${API_KEY_FILENAME}.`, {
|
|
559
|
-
removed: true,
|
|
560
|
-
removed_profiles: [target],
|
|
561
|
-
remaining_profiles: [],
|
|
562
|
-
default_profile: null,
|
|
563
|
-
file_deleted: true,
|
|
564
|
-
env_var_still_set: envVarStillSet,
|
|
565
|
-
});
|
|
566
|
-
return;
|
|
567
|
-
}
|
|
568
|
-
console.log(`${fmt.green('✓')} Removed profile ${fmt.bold(target)} and deleted ~/${API_KEY_FILENAME}.`);
|
|
569
|
-
}
|
|
570
|
-
else {
|
|
571
|
-
if (options.json) {
|
|
572
|
-
printJsonResponse(200, `Removed profile ${target} from ~/${API_KEY_FILENAME}.`, {
|
|
573
|
-
removed: true,
|
|
574
|
-
removed_profiles: [target],
|
|
575
|
-
remaining_profiles: remaining,
|
|
576
|
-
default_profile: result.newDefault,
|
|
577
|
-
file_deleted: false,
|
|
578
|
-
env_var_still_set: envVarStillSet,
|
|
579
|
-
});
|
|
580
|
-
return;
|
|
581
|
-
}
|
|
582
|
-
console.log(`${fmt.green('✓')} Removed profile ${fmt.bold(target)} from ~/${API_KEY_FILENAME}.`);
|
|
583
|
-
if (result.newDefault) {
|
|
584
|
-
console.log(fmt.dim(` Default profile: ${result.newDefault}`));
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
if (envVarStillSet) {
|
|
590
|
-
console.log(fmt.yellow(`Note: ${API_KEY_ENV_VAR} is still set in your environment. Unset it in your shell config to log out completely.`));
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
}
|