codepen-commander 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +179 -2
- package/api/client.js +364 -0
- package/api/errors.js +49 -0
- package/cli.js +125 -0
- package/commands/auth-context.js +139 -0
- package/commands/auth.js +593 -0
- package/commands/clone.js +371 -0
- package/commands/create-pen.js +249 -0
- package/commands/delete.js +98 -0
- package/commands/deploy.js +176 -0
- package/commands/domains.js +197 -0
- package/commands/fork.js +342 -0
- package/commands/list.js +103 -0
- package/commands/meta.js +164 -0
- package/commands/new.js +294 -0
- package/commands/open.js +174 -0
- package/commands/privacy.js +234 -0
- package/commands/pull.js +128 -0
- package/commands/push.js +294 -0
- package/commands/scaffold.js +127 -0
- package/commands/status.js +131 -0
- package/commands/unowned-pen.js +153 -0
- package/commands/versions.js +172 -0
- package/config/auth.js +470 -0
- package/config/project.js +365 -0
- package/package.json +21 -4
- package/sync/diff.js +105 -0
- package/sync/fs.js +253 -0
- package/sync/materialize.js +81 -0
- package/types.js +1 -0
- package/utils/browser.js +47 -0
- package/utils/format.js +107 -0
- package/utils/html.js +51 -0
- package/utils/json.js +122 -0
- package/utils/prompts.js +316 -0
package/cli.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { Command, CommanderError } from 'commander';
|
|
6
|
+
import { isCodePenApiError } from './api/errors.js';
|
|
7
|
+
import { registerAuthCommands } from './commands/auth.js';
|
|
8
|
+
import { registerCloneCommand } from './commands/clone.js';
|
|
9
|
+
import { registerDeleteCommand } from './commands/delete.js';
|
|
10
|
+
import { registerDeployCommands } from './commands/deploy.js';
|
|
11
|
+
import { registerDomainsCommand } from './commands/domains.js';
|
|
12
|
+
import { registerForkCommand } from './commands/fork.js';
|
|
13
|
+
import { registerListCommand } from './commands/list.js';
|
|
14
|
+
import { registerMetaCommands } from './commands/meta.js';
|
|
15
|
+
import { registerNewCommand } from './commands/new.js';
|
|
16
|
+
import { registerOpenCommand } from './commands/open.js';
|
|
17
|
+
import { registerPrivacyCommand } from './commands/privacy.js';
|
|
18
|
+
import { registerPullCommand } from './commands/pull.js';
|
|
19
|
+
import { registerPushCommand } from './commands/push.js';
|
|
20
|
+
import { registerStatusCommand } from './commands/status.js';
|
|
21
|
+
import { registerVersionCommands } from './commands/versions.js';
|
|
22
|
+
import { fmt } from './utils/format.js';
|
|
23
|
+
import { errorToJsonEnvelope, wantsJsonOutput } from './utils/json.js';
|
|
24
|
+
function readPackageVersion() {
|
|
25
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
26
|
+
const __dirname = path.dirname(__filename);
|
|
27
|
+
// Works both in published dist/ (package.json beside cli.js) and local dev (../package.json)
|
|
28
|
+
const candidates = [
|
|
29
|
+
path.join(__dirname, 'package.json'),
|
|
30
|
+
path.join(__dirname, '..', 'package.json'),
|
|
31
|
+
];
|
|
32
|
+
for (const candidate of candidates) {
|
|
33
|
+
try {
|
|
34
|
+
const pkg = JSON.parse(fs.readFileSync(candidate, 'utf8'));
|
|
35
|
+
if (pkg.version)
|
|
36
|
+
return pkg.version;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// Try next candidate
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return '0.0.0';
|
|
43
|
+
}
|
|
44
|
+
export function createProgram() {
|
|
45
|
+
const program = new Command();
|
|
46
|
+
program
|
|
47
|
+
.name('cdpn')
|
|
48
|
+
.description('Sync local folders with CodePen 2.0 Pens via the CodePen API v2')
|
|
49
|
+
.version(readPackageVersion(), '-v, --version')
|
|
50
|
+
.option('--profile <name>', 'Use a specific API key profile from ~/.cdpn');
|
|
51
|
+
registerAuthCommands(program);
|
|
52
|
+
registerCloneCommand(program);
|
|
53
|
+
registerForkCommand(program);
|
|
54
|
+
registerPullCommand(program);
|
|
55
|
+
registerStatusCommand(program);
|
|
56
|
+
registerPushCommand(program);
|
|
57
|
+
registerDeployCommands(program);
|
|
58
|
+
registerDomainsCommand(program);
|
|
59
|
+
registerNewCommand(program);
|
|
60
|
+
registerPrivacyCommand(program);
|
|
61
|
+
registerDeleteCommand(program);
|
|
62
|
+
registerOpenCommand(program);
|
|
63
|
+
registerListCommand(program);
|
|
64
|
+
registerVersionCommands(program);
|
|
65
|
+
registerMetaCommands(program);
|
|
66
|
+
return program;
|
|
67
|
+
}
|
|
68
|
+
function configureJsonErrorMode(cmd) {
|
|
69
|
+
cmd.exitOverride();
|
|
70
|
+
cmd.configureOutput({
|
|
71
|
+
writeErr: () => {
|
|
72
|
+
// Suppress Commander's plain-text stderr errors so --json mode emits a single JSON envelope
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
for (const sub of cmd.commands) {
|
|
76
|
+
configureJsonErrorMode(sub);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function main() {
|
|
80
|
+
const program = createProgram();
|
|
81
|
+
const jsonMode = wantsJsonOutput(process.argv);
|
|
82
|
+
if (jsonMode) {
|
|
83
|
+
configureJsonErrorMode(program);
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
await program.parseAsync(process.argv);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (error instanceof CommanderError && error.exitCode === 0) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (jsonMode) {
|
|
93
|
+
const envelope = errorToJsonEnvelope(error);
|
|
94
|
+
console.log(JSON.stringify(envelope, null, 2));
|
|
95
|
+
process.exitCode = 1;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (isCodePenApiError(error)) {
|
|
99
|
+
console.error(`\n${fmt.red('Error:')} ${error.formatForUser()}`);
|
|
100
|
+
}
|
|
101
|
+
else if (error instanceof Error) {
|
|
102
|
+
console.error(`\n${fmt.red('Error:')} ${error.message}`);
|
|
103
|
+
if (process.env.DEBUG && error.stack) {
|
|
104
|
+
console.error(fmt.dim(error.stack));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
console.error(`\n${fmt.red('Error:')} ${String(error)}`);
|
|
109
|
+
}
|
|
110
|
+
process.exitCode = 1;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function isExecutedDirectly() {
|
|
114
|
+
if (!process.argv[1])
|
|
115
|
+
return false;
|
|
116
|
+
try {
|
|
117
|
+
return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url));
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (isExecutedDirectly()) {
|
|
124
|
+
void main();
|
|
125
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { API_KEY_ENV_VAR, API_KEY_FILENAME, findMatchingProfile, getKeyType, readAuthStore, validateProfileName, } from '../config/auth.js';
|
|
2
|
+
import { PROJECT_CONFIG_FILENAME, writeProjectConfig, } from '../config/project.js';
|
|
3
|
+
import { fmt } from '../utils/format.js';
|
|
4
|
+
import { CliError } from '../utils/json.js';
|
|
5
|
+
import { isInteractive, promptSelect } from '../utils/prompts.js';
|
|
6
|
+
/**
|
|
7
|
+
* Extracts the global `--profile <name>` option from any Commander command instance.
|
|
8
|
+
*/
|
|
9
|
+
export function getGlobalProfileOption(command) {
|
|
10
|
+
if (!command)
|
|
11
|
+
return undefined;
|
|
12
|
+
const globals = command.optsWithGlobals();
|
|
13
|
+
const value = globals.profile?.trim();
|
|
14
|
+
return value || undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Resolves authentication for CLI commands with full multi-profile support:
|
|
18
|
+
*
|
|
19
|
+
* 1. Explicit `--profile <name>` -> loads that profile from `~/.cdpn` (errors if missing).
|
|
20
|
+
* 2. `CODEPEN_API_KEY` env var -> uses env key (CI / shell override).
|
|
21
|
+
* 3. `project.config.profile` (from `.cdpn.json`) -> loads that profile from `~/.cdpn` (errors if missing).
|
|
22
|
+
* 4. If `~/.cdpn` has 1 profile -> uses it automatically.
|
|
23
|
+
* 5. If `~/.cdpn` has 2+ profiles -> prompts interactively (pre-selecting URL owner match or default),
|
|
24
|
+
* or falls back to the default profile in non-interactive mode.
|
|
25
|
+
*/
|
|
26
|
+
export async function resolveCommandAuth(options = {}) {
|
|
27
|
+
const env = options.env ?? process.env;
|
|
28
|
+
const envKey = env[API_KEY_ENV_VAR]?.trim() || '';
|
|
29
|
+
const { store, filePath, insecurePermissions } = await readAuthStore({
|
|
30
|
+
homeDir: options.homeDir,
|
|
31
|
+
});
|
|
32
|
+
const availableProfiles = Object.keys(store.profiles).sort((a, b) => a.localeCompare(b));
|
|
33
|
+
const hasEnv = Boolean(envKey);
|
|
34
|
+
const hasFile = availableProfiles.length > 0;
|
|
35
|
+
const interactive = options.interactive ?? isInteractive();
|
|
36
|
+
const selectImpl = options.selectProfile ?? promptSelect;
|
|
37
|
+
const makeFileAuth = (profileName) => ({
|
|
38
|
+
apiKey: store.profiles[profileName],
|
|
39
|
+
source: 'file',
|
|
40
|
+
profile: profileName,
|
|
41
|
+
filePath,
|
|
42
|
+
hasEnv,
|
|
43
|
+
hasFile,
|
|
44
|
+
availableProfiles,
|
|
45
|
+
defaultProfile: store.defaultProfile,
|
|
46
|
+
insecureFilePermissions: insecurePermissions,
|
|
47
|
+
});
|
|
48
|
+
// 1. Explicit --profile flag takes highest precedence
|
|
49
|
+
if (options.explicitProfile) {
|
|
50
|
+
const requested = validateProfileName(options.explicitProfile);
|
|
51
|
+
if (!Object.hasOwn(store.profiles, requested)) {
|
|
52
|
+
const availableNote = availableProfiles.length > 0
|
|
53
|
+
? ` Available profiles: ${availableProfiles.join(', ')}.`
|
|
54
|
+
: '';
|
|
55
|
+
throw new CliError(404, `Profile "${requested}" not found in ~/${API_KEY_FILENAME}.${availableNote}\n` +
|
|
56
|
+
`Run \`cdpn auth login ${requested}\` to add it.`, { requested_profile: requested, available_profiles: availableProfiles });
|
|
57
|
+
}
|
|
58
|
+
return makeFileAuth(requested);
|
|
59
|
+
}
|
|
60
|
+
// 2. Environment variable override (e.g. CI or shell export)
|
|
61
|
+
if (hasEnv) {
|
|
62
|
+
return {
|
|
63
|
+
apiKey: envKey,
|
|
64
|
+
source: 'env',
|
|
65
|
+
profile: null,
|
|
66
|
+
filePath,
|
|
67
|
+
hasEnv,
|
|
68
|
+
hasFile,
|
|
69
|
+
availableProfiles,
|
|
70
|
+
defaultProfile: store.defaultProfile,
|
|
71
|
+
insecureFilePermissions: insecurePermissions,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
// 3. No stored profiles at all
|
|
75
|
+
if (!hasFile) {
|
|
76
|
+
throw new CliError(401, `No CodePen API key found. Run \`cdpn auth login\` or set the ${API_KEY_ENV_VAR} environment variable.`);
|
|
77
|
+
}
|
|
78
|
+
// 4. Pen folder is already associated with a profile in .cdpn.json
|
|
79
|
+
const projectProfile = options.project?.config.profile?.trim();
|
|
80
|
+
if (projectProfile) {
|
|
81
|
+
const validProjectProfile = validateProfileName(projectProfile);
|
|
82
|
+
if (!Object.hasOwn(store.profiles, validProjectProfile)) {
|
|
83
|
+
const availableNote = ` Available profiles: ${availableProfiles.join(', ')}.`;
|
|
84
|
+
throw new CliError(404, `Profile "${validProjectProfile}" (configured in ${PROJECT_CONFIG_FILENAME}) was not found in ~/${API_KEY_FILENAME}.${availableNote}\n` +
|
|
85
|
+
`Run \`cdpn auth login ${validProjectProfile}\` to add it, or pass \`--profile <name>\` to override.`, { requested_profile: validProjectProfile, available_profiles: availableProfiles });
|
|
86
|
+
}
|
|
87
|
+
return makeFileAuth(validProjectProfile);
|
|
88
|
+
}
|
|
89
|
+
// 5. Pen is not associated with a profile (or command isn't tied to a Pen)
|
|
90
|
+
let chosenProfile;
|
|
91
|
+
if (availableProfiles.length === 1) {
|
|
92
|
+
chosenProfile = availableProfiles[0];
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
const effectiveUrlHint = options.urlHint ?? options.project?.config.url;
|
|
96
|
+
const matchedProfile = findMatchingProfile(availableProfiles, effectiveUrlHint);
|
|
97
|
+
if (matchedProfile && (options.autoSelectUrlMatch || !interactive)) {
|
|
98
|
+
chosenProfile = matchedProfile;
|
|
99
|
+
}
|
|
100
|
+
else if (interactive) {
|
|
101
|
+
const preselected = matchedProfile ?? store.defaultProfile ?? availableProfiles[0];
|
|
102
|
+
const defaultIndex = Math.max(0, availableProfiles.indexOf(preselected));
|
|
103
|
+
const selectOptions = availableProfiles.map((name) => {
|
|
104
|
+
const key = store.profiles[name];
|
|
105
|
+
const parts = [getKeyType(key)];
|
|
106
|
+
if (name === store.defaultProfile)
|
|
107
|
+
parts.push('default');
|
|
108
|
+
if (name === matchedProfile)
|
|
109
|
+
parts.push('matches URL');
|
|
110
|
+
return {
|
|
111
|
+
value: name,
|
|
112
|
+
label: name,
|
|
113
|
+
description: parts.join(' · '),
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
const question = options.promptMessage ??
|
|
117
|
+
(options.project
|
|
118
|
+
? `This Pen is not associated with a profile yet. Which profile should be used?`
|
|
119
|
+
: 'Which CodePen profile should be used?');
|
|
120
|
+
chosenProfile = await selectImpl(question, selectOptions, defaultIndex);
|
|
121
|
+
}
|
|
122
|
+
else if (store.defaultProfile && Object.hasOwn(store.profiles, store.defaultProfile)) {
|
|
123
|
+
chosenProfile = store.defaultProfile;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
throw new CliError(400, `Multiple profiles exist in ~/${API_KEY_FILENAME} (${availableProfiles.join(', ')}).\n` +
|
|
127
|
+
'Pass `--profile <name>` or set a default profile with `cdpn auth default <name>`.', { available_profiles: availableProfiles });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Persist chosen profile into an existing .cdpn.json that didn't have one yet
|
|
131
|
+
if (options.project && !options.project.config.profile && options.persistProfileToProject !== false) {
|
|
132
|
+
options.project.config.profile = chosenProfile;
|
|
133
|
+
await writeProjectConfig(options.project.rootDir, options.project.config);
|
|
134
|
+
if (interactive) {
|
|
135
|
+
console.log(fmt.dim(`Associated this Pen with profile "${chosenProfile}" in ${PROJECT_CONFIG_FILENAME}.`));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return makeFileAuth(chosenProfile);
|
|
139
|
+
}
|