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.
Files changed (93) hide show
  1. package/README.md +104 -10
  2. package/api/client.js +129 -106
  3. package/cli.js +34 -29
  4. package/commands/auth/default.js +40 -0
  5. package/commands/auth/index.js +83 -0
  6. package/commands/auth/list.js +78 -0
  7. package/commands/auth/login.js +136 -0
  8. package/commands/auth/logout.js +186 -0
  9. package/commands/auth/status.js +212 -0
  10. package/commands/build.js +256 -0
  11. package/commands/clone.js +13 -581
  12. package/commands/delete.js +553 -37
  13. package/commands/deploy.js +164 -154
  14. package/commands/domains.js +83 -39
  15. package/commands/fork.js +13 -317
  16. package/commands/info.js +152 -0
  17. package/commands/init.js +375 -0
  18. package/commands/list.js +95 -78
  19. package/commands/new.js +155 -83
  20. package/commands/open.js +29 -19
  21. package/commands/privacy.js +52 -36
  22. package/commands/pull.js +123 -110
  23. package/commands/push.js +222 -365
  24. package/commands/restore.js +251 -0
  25. package/commands/rollback.js +141 -0
  26. package/commands/serve.js +119 -0
  27. package/commands/settings.js +126 -0
  28. package/commands/shell.js +305 -0
  29. package/commands/status.js +133 -39
  30. package/commands/sync.js +330 -0
  31. package/commands/undeploy.js +102 -0
  32. package/commands/unlink.js +191 -0
  33. package/commands/versions.js +100 -163
  34. package/config/auth.js +4 -374
  35. package/config/pen-blocks.js +166 -0
  36. package/config/project.js +179 -240
  37. package/config/user-store.js +364 -0
  38. package/constants.js +42 -0
  39. package/errors.js +17 -0
  40. package/package.json +1 -1
  41. package/shell/index.html +79 -0
  42. package/shell/scripts.js +276 -0
  43. package/shell/styles.css +492 -0
  44. package/site/blocks/classic.js +270 -0
  45. package/site/blocks/context.js +63 -0
  46. package/site/blocks/index.js +4 -0
  47. package/site/blocks/packages.js +242 -0
  48. package/site/blocks/unsupported-page.js +328 -0
  49. package/site/builder.js +475 -0
  50. package/site/detect.js +65 -0
  51. package/site/ignore-files.js +90 -0
  52. package/site/manifest.js +227 -0
  53. package/site/server.js +483 -0
  54. package/site/shell.js +363 -0
  55. package/sync/fs.js +19 -17
  56. package/sync/info.js +6 -81
  57. package/utils/browser.js +17 -10
  58. package/utils/cli-options.js +79 -0
  59. package/utils/codepen-url.js +183 -0
  60. package/utils/format.js +39 -30
  61. package/utils/fs-safe.js +95 -0
  62. package/utils/html.js +40 -1
  63. package/utils/json.js +4 -19
  64. package/utils/output.js +37 -0
  65. package/utils/package-paths.js +51 -0
  66. package/utils/prompts/index.js +5 -0
  67. package/utils/prompts/multi-select.js +185 -0
  68. package/utils/prompts/secret.js +65 -0
  69. package/utils/prompts/select.js +141 -0
  70. package/utils/prompts/text.js +99 -0
  71. package/utils/prompts/tty-list.js +165 -0
  72. package/utils/prompts/types.js +35 -0
  73. package/utils/strings.js +98 -0
  74. package/utils/watcher.js +82 -0
  75. package/{commands → workflows}/auth-context.js +13 -7
  76. package/workflows/batch-pens.js +1647 -0
  77. package/workflows/batch-target.js +184 -0
  78. package/workflows/classic-convert.js +241 -0
  79. package/workflows/clone-pen.js +381 -0
  80. package/workflows/create-pen.js +254 -0
  81. package/workflows/discover-pens.js +91 -0
  82. package/workflows/fork-pen.js +314 -0
  83. package/workflows/missing-pen.js +212 -0
  84. package/workflows/pull-pen.js +204 -0
  85. package/workflows/push-pen.js +318 -0
  86. package/{commands → workflows}/scaffold.js +4 -3
  87. package/workflows/sync-pen.js +227 -0
  88. package/{commands → workflows}/unowned-pen.js +16 -12
  89. package/commands/auth.js +0 -593
  90. package/commands/create-pen.js +0 -251
  91. package/commands/meta.js +0 -184
  92. package/utils/prompts.js +0 -316
  93. /package/{types.js → api/types.js} +0 -0
@@ -1,176 +1,186 @@
1
+ import path from 'node:path';
1
2
  import { CodePenClient } from '../api/client.js';
2
- import { findProject, PROJECT_CONFIG_FILENAME, requireProject } from '../config/project.js';
3
+ import { findNearestConfig } from '../config/project.js';
4
+ import { PROJECT_CONFIG_FILENAME } from '../constants.js';
5
+ import { CliError } from '../errors.js';
6
+ import { assertValidVersionLabel, resolveAccessOrVisibility } from '../utils/cli-options.js';
3
7
  import { fmt, shortId } from '../utils/format.js';
4
- import { CliError, printJsonResponse } from '../utils/json.js';
5
- import { isInteractive, promptConfirm } from '../utils/prompts.js';
6
- import { getGlobalProfileOption, resolveCommandAuth } from './auth-context.js';
7
- import { createPenFromDirectory } from './create-pen.js';
8
- import { executePush } from './push.js';
9
- export function registerDeployCommands(program) {
10
- program
11
- .command('deploy')
12
- .description('Push local changes and deploy the Pen to CodePen Hosting')
13
- .option('--no-push', 'Deploy the current remote saved head without pushing local files first')
14
- .option('--version <version-id>', 'Deploy a specific saved version ID instead of pushing')
15
- .option('-f, --force', 'Overwrite remote head if moved (or skip missing index.html prompt on first push)')
16
- .option('-m, --label <label>', 'Label for the saved version created by push')
17
- .option('-t, --title <title>', 'Title for a new Pen when deploying an unlinked folder for the first time')
18
- .option('--access <level>', 'Privacy for a new Pen: Public, Private, Protected, or Collaborators')
19
- .option('--password <password>', 'Password for a new Protected Pen (min 8 chars)')
20
- .option('--json', 'Output deployment result as JSON')
21
- .action(async (options, command) => {
22
- if (options.label && options.label.length > 80) {
23
- throw new CliError(400, 'Version label must be at most 80 characters.');
8
+ import { printJsonResponse } from '../utils/json.js';
9
+ import { defaultOutput } from '../utils/output.js';
10
+ import { isInteractive } from '../utils/prompts/index.js';
11
+ import { getGlobalProfileOption, resolveCommandAuth } from '../workflows/auth-context.js';
12
+ import { createPenFromDirectory } from '../workflows/create-pen.js';
13
+ import { fetchLinkedPenOrHandleMissing } from '../workflows/missing-pen.js';
14
+ import { executePush } from '../workflows/push-pen.js';
15
+ export async function runDeployCommand(options = {}) {
16
+ const output = options.output ?? defaultOutput;
17
+ const cwd = path.resolve(options.cwd ?? process.cwd());
18
+ const jsonMode = Boolean(options.json);
19
+ const interactive = !jsonMode && (options.interactive ?? isInteractive());
20
+ assertValidVersionLabel(options.label);
21
+ const resolvedAccess = resolveAccessOrVisibility(options.access, options.visibility);
22
+ const nearest = await findNearestConfig(cwd);
23
+ let project = nearest?.linked
24
+ ? {
25
+ rootDir: nearest.rootDir,
26
+ configPath: nearest.configPath,
27
+ config: nearest.config,
24
28
  }
25
- const explicitProfile = getGlobalProfileOption(command);
26
- let project = await findProject(process.cwd());
27
- let versionIdToDeploy = options.version;
28
- let client;
29
- let activeProfile;
30
- let createdPen = false;
31
- let pushedChanges = false;
32
- if (!project) {
33
- if (options.push === false || options.version) {
34
- throw new CliError(404, `No ${PROJECT_CONFIG_FILENAME} found in this directory or any parent directory.\n` +
29
+ : null;
30
+ let versionIdToDeploy = options.version;
31
+ let client;
32
+ let activeProfile;
33
+ let createdPen = false;
34
+ let pushedChanges = false;
35
+ if (!project) {
36
+ if (options.push === false || options.version) {
37
+ throw new CliError(404, nearest
38
+ ? `${PROJECT_CONFIG_FILENAME} in ${nearest.rootDir} only contains Pen info and is not linked to an online Pen yet.\n` +
39
+ 'Run `cdpn deploy` without --no-push/--version to create and link it first.'
40
+ : `No ${PROJECT_CONFIG_FILENAME} found in this directory or any parent directory.\n` +
35
41
  'Run `cdpn clone <url>` to clone a Pen or run `cdpn deploy` without --no-push/--version to create one first.');
36
- }
37
- if (!options.json) {
38
- console.log(fmt.dim(`No ${PROJECT_CONFIG_FILENAME} found — pushing this local folder as a new CodePen 2.0 Pen before deploying.`));
39
- }
40
- const created = await createPenFromDirectory({
41
- targetDir: process.cwd(),
42
- folderDisplay: '.',
43
- explicitProfile,
44
- title: options.title,
45
- access: options.access,
46
- password: options.password,
42
+ }
43
+ const targetDir = nearest ? nearest.rootDir : cwd;
44
+ const folderDisplay = path.relative(cwd, targetDir) || '.';
45
+ if (!jsonMode) {
46
+ output.log(fmt.dim(nearest
47
+ ? `Found unlinked ${PROJECT_CONFIG_FILENAME} with saved Pen info — pushing this local folder as a new CodePen 2.0 Pen before deploying.`
48
+ : `No ${PROJECT_CONFIG_FILENAME} found — pushing this local folder as a new CodePen 2.0 Pen before deploying.`));
49
+ }
50
+ const created = await createPenFromDirectory({
51
+ targetDir,
52
+ folderDisplay,
53
+ explicitProfile: options.explicitProfile,
54
+ title: options.title,
55
+ access: resolvedAccess,
56
+ password: options.password,
57
+ force: options.force,
58
+ wait: true,
59
+ silent: jsonMode,
60
+ output,
61
+ baseUrl: options.baseUrl,
62
+ interactive,
63
+ env: options.env,
64
+ homeDir: options.homeDir,
65
+ prompts: options.prompts,
66
+ });
67
+ if (!created) {
68
+ return null;
69
+ }
70
+ if (created.build?.status === 'Failed') {
71
+ throw new CliError(422, 'Aborting deploy because the build failed. Fix build errors and try again.', {
72
+ pen_id: created.pen.id,
73
+ version_id: created.versionId,
74
+ build: created.build,
75
+ });
76
+ }
77
+ createdPen = true;
78
+ pushedChanges = true;
79
+ project = created.project;
80
+ versionIdToDeploy = created.versionId ?? undefined;
81
+ activeProfile = created.auth.profile;
82
+ client = new CodePenClient({ apiKey: created.auth.apiKey, baseUrl: options.baseUrl });
83
+ }
84
+ else {
85
+ const auth = await resolveCommandAuth({
86
+ explicitProfile: options.explicitProfile,
87
+ project,
88
+ interactive,
89
+ persistProfileToProject: !jsonMode,
90
+ output,
91
+ env: options.env,
92
+ homeDir: options.homeDir,
93
+ selectProfile: options.prompts?.select,
94
+ });
95
+ activeProfile = auth.profile;
96
+ client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
97
+ const penLookup = await fetchLinkedPenOrHandleMissing({
98
+ client,
99
+ project,
100
+ auth,
101
+ commandName: 'deploy',
102
+ interactive,
103
+ json: jsonMode,
104
+ output,
105
+ prompts: options.prompts,
106
+ });
107
+ if (!penLookup.found) {
108
+ return null;
109
+ }
110
+ if (!versionIdToDeploy && options.push !== false) {
111
+ const pushResult = await executePush(client, project, {
47
112
  force: options.force,
113
+ label: options.label,
48
114
  wait: true,
49
- silent: Boolean(options.json),
50
- interactive: !options.json && isInteractive(),
115
+ prefetchedPen: penLookup.pen,
116
+ silent: jsonMode,
117
+ output,
51
118
  });
52
- if (!created) {
53
- return;
54
- }
55
- if (created.build?.status === 'Failed') {
119
+ pushedChanges = pushResult.pushed;
120
+ if (pushResult.build?.status === 'Failed') {
56
121
  throw new CliError(422, 'Aborting deploy because the build failed. Fix build errors and try again.', {
57
- pen_id: created.pen.id,
58
- version_id: created.versionId,
59
- build: created.build,
122
+ pen_id: project.config.pen_id,
123
+ version_id: pushResult.versionId,
124
+ build: pushResult.build,
60
125
  });
61
126
  }
62
- createdPen = true;
63
- pushedChanges = true;
64
- project = created.project;
65
- versionIdToDeploy = created.versionId ?? undefined;
66
- activeProfile = created.auth.profile;
67
- client = new CodePenClient({ apiKey: created.auth.apiKey });
68
- }
69
- else {
70
- const auth = await resolveCommandAuth({
71
- explicitProfile,
72
- project,
73
- interactive: !options.json && isInteractive(),
74
- persistProfileToProject: !options.json,
75
- });
76
- activeProfile = auth.profile;
77
- client = new CodePenClient({ apiKey: auth.apiKey });
78
- if (!versionIdToDeploy && options.push !== false) {
79
- const pushResult = await executePush(client, project, {
80
- force: options.force,
81
- label: options.label,
82
- wait: true,
83
- silent: Boolean(options.json),
84
- });
85
- pushedChanges = pushResult.pushed;
86
- if (pushResult.build?.status === 'Failed') {
87
- throw new CliError(422, 'Aborting deploy because the build failed. Fix build errors and try again.', {
88
- pen_id: project.config.pen_id,
89
- version_id: pushResult.versionId,
90
- build: pushResult.build,
91
- });
92
- }
93
- if (pushResult.versionId) {
94
- versionIdToDeploy = pushResult.versionId;
95
- }
127
+ if (pushResult.versionId) {
128
+ versionIdToDeploy = pushResult.versionId;
96
129
  }
97
130
  }
98
- if (!options.json) {
99
- console.log(versionIdToDeploy
100
- ? `\nDeploying version ${fmt.dim(shortId(versionIdToDeploy))}…`
101
- : '\nDeploying saved head…');
102
- }
103
- const deployment = await client.deployPen(project.config.pen_id, versionIdToDeploy);
104
- if (options.json) {
105
- printJsonResponse(200, 'Deployed successfully.', {
106
- pen_id: project.config.pen_id,
107
- profile: activeProfile,
108
- created: createdPen,
109
- pushed: pushedChanges,
110
- version_id: deployment.version_id,
111
- deployment,
112
- });
113
- return;
114
- }
115
- console.log(`${fmt.green('✓')} Deployed successfully!`);
131
+ }
132
+ if (!jsonMode) {
133
+ output.log(versionIdToDeploy
134
+ ? `\nDeploying version ${fmt.dim(shortId(versionIdToDeploy))}…`
135
+ : '\nDeploying saved head…');
136
+ }
137
+ const deployment = await client.deployPen(project.config.pen_id, versionIdToDeploy);
138
+ if (jsonMode) {
139
+ printJsonResponse(200, 'Deployed successfully.', {
140
+ pen_id: project.config.pen_id,
141
+ profile: activeProfile,
142
+ created: createdPen,
143
+ pushed: pushedChanges,
144
+ version_id: deployment.version_id,
145
+ deployment,
146
+ }, output);
147
+ }
148
+ else {
149
+ output.log(`${fmt.green('✓')} Deployed successfully!`);
116
150
  if (activeProfile) {
117
- console.log(` Profile: ${fmt.cyan(activeProfile)}`);
151
+ output.log(` Profile: ${fmt.cyan(activeProfile)}`);
118
152
  }
119
- console.log(` URL: ${fmt.cyan(deployment.url)}`);
120
- console.log(` Version: ${fmt.dim(shortId(deployment.version_id))}`);
153
+ output.log(` URL: ${fmt.cyan(deployment.url)}`);
154
+ output.log(` Version: ${fmt.dim(shortId(deployment.version_id))}`);
121
155
  if (deployment.custom_domains.length > 0) {
122
- console.log(` Domains: ${deployment.custom_domains.map((d) => fmt.cyan(`https://${d}`)).join(', ')}`);
156
+ output.log(` Domains: ${deployment.custom_domains.map((d) => fmt.cyan(`https://${d}`)).join(', ')}`);
123
157
  }
124
- });
158
+ }
159
+ return {
160
+ penId: project.config.pen_id,
161
+ profile: activeProfile,
162
+ created: createdPen,
163
+ pushed: pushedChanges,
164
+ deployment,
165
+ };
166
+ }
167
+ export function registerDeployCommand(program) {
125
168
  program
126
- .command('undeploy')
127
- .description('Take down the Pen’s live deployment (Pen and versions stay intact)')
128
- .option('-y, --yes', 'Skip confirmation prompt')
129
- .option('--json', 'Output undeploy result as JSON')
169
+ .command('deploy')
170
+ .description('Push local changes and deploy the Pen to CodePen Hosting')
171
+ .option('--no-push', 'Deploy the current remote saved head without pushing local files first')
172
+ .option('--version <version-id>', 'Deploy a specific saved version ID instead of pushing')
173
+ .option('-f, --force', 'Overwrite remote head if moved (or skip missing index.html prompt on first push)')
174
+ .option('-m, --label <label>', 'Label for the saved version created by push')
175
+ .option('-t, --title <title>', 'Title for a new Pen when deploying an unlinked folder for the first time')
176
+ .option('--access <level>', 'Privacy for a new Pen: Public, Private, Protected, or Collaborators')
177
+ .option('--visibility <level>', 'Alias for --access')
178
+ .option('--password <password>', 'Password for a new Protected Pen (min 8 chars)')
179
+ .option('--json', 'Output deployment result as JSON')
130
180
  .action(async (options, command) => {
131
- const interactive = isInteractive() && !options.json;
132
- const project = await requireProject();
133
- const auth = await resolveCommandAuth({
181
+ await runDeployCommand({
182
+ ...options,
134
183
  explicitProfile: getGlobalProfileOption(command),
135
- project,
136
- interactive,
137
- persistProfileToProject: !options.json,
138
184
  });
139
- const client = new CodePenClient({ apiKey: auth.apiKey });
140
- const pen = await client.getPen(project.config.pen_id);
141
- if (!pen.deployment) {
142
- if (options.json) {
143
- printJsonResponse(200, 'This Pen is not currently deployed.', {
144
- pen_id: project.config.pen_id,
145
- profile: auth.profile,
146
- undeployed: false,
147
- previous_deployment: null,
148
- });
149
- return;
150
- }
151
- console.log(fmt.dim('This Pen is not currently deployed.'));
152
- return;
153
- }
154
- if (!options.yes) {
155
- if (!interactive) {
156
- throw new CliError(400, 'Refusing to undeploy in non-interactive mode without --yes.');
157
- }
158
- const ok = await promptConfirm(`Take down live deployment at ${fmt.cyan(pen.deployment.url)}?`, false);
159
- if (!ok) {
160
- console.log('Aborted.');
161
- return;
162
- }
163
- }
164
- await client.undeployPen(project.config.pen_id);
165
- if (options.json) {
166
- printJsonResponse(200, 'Deployment taken down. Your Pen and saved versions remain intact.', {
167
- pen_id: project.config.pen_id,
168
- profile: auth.profile,
169
- undeployed: true,
170
- previous_deployment: pen.deployment,
171
- });
172
- return;
173
- }
174
- console.log(`${fmt.green('✓')} Deployment taken down. Your Pen and saved versions remain intact.`);
175
185
  });
176
186
  }
@@ -1,20 +1,24 @@
1
1
  import { CodePenClient } from '../api/client.js';
2
2
  import { requireProject } from '../config/project.js';
3
+ import { CliError } from '../errors.js';
4
+ import { resolveInheritedJsonOption } from '../utils/cli-options.js';
3
5
  import { fmt, formatTable } from '../utils/format.js';
4
- import { CliError, printJsonResponse } from '../utils/json.js';
5
- import { isInteractive, promptConfirm } from '../utils/prompts.js';
6
- import { getGlobalProfileOption, resolveCommandAuth } from './auth-context.js';
7
- export function renderDomains(domains, defaultUrl) {
6
+ import { printJsonResponse } from '../utils/json.js';
7
+ import { defaultOutput } from '../utils/output.js';
8
+ import { isInteractive, promptConfirm } from '../utils/prompts/index.js';
9
+ import { getGlobalProfileOption, resolveCommandAuth } from '../workflows/auth-context.js';
10
+ import { fetchLinkedPenOrHandleMissing, } from '../workflows/missing-pen.js';
11
+ export function renderDomains(domains, defaultUrl, output = defaultOutput) {
8
12
  if (defaultUrl) {
9
- console.log(`Default URL: ${fmt.cyan(defaultUrl)}`);
13
+ output.log(`Default URL: ${fmt.cyan(defaultUrl)}`);
10
14
  }
11
15
  else {
12
- console.log(`Default URL: ${fmt.dim('not deployed (run `cdpn deploy` to deploy)')}`);
16
+ output.log(`Default URL: ${fmt.dim('not deployed (run `cdpn deploy` to deploy)')}`);
13
17
  }
14
- console.log('');
18
+ output.log('');
15
19
  if (domains.length === 0) {
16
- console.log(fmt.dim('No custom domains configured for this Pen.'));
17
- console.log(fmt.dim('Add one with: cdpn domains add <host>'));
20
+ output.log(fmt.dim('No custom domains configured for this Pen.'));
21
+ output.log(fmt.dim('Add one with: cdpn domains add <host>'));
18
22
  return;
19
23
  }
20
24
  const rows = domains.map((d) => {
@@ -25,19 +29,20 @@ export function renderDomains(domains, defaultUrl) {
25
29
  : fmt.yellow(d.status);
26
30
  return [fmt.cyan(d.host), status, d.cname_record, d.a_record];
27
31
  });
28
- console.log(formatTable(['Host', 'Status', 'CNAME', 'A Record'], rows));
32
+ output.log(formatTable(['Host', 'Status', 'CNAME', 'A Record'], rows));
29
33
  const needingDns = domains.filter((d) => d.status !== 'Verified' && d.dns.length > 0);
30
34
  if (needingDns.length > 0) {
31
- console.log('\nRequired DNS records:');
35
+ output.log('\nRequired DNS records:');
32
36
  for (const domain of needingDns) {
33
- console.log(` ${fmt.bold(domain.host)}:`);
37
+ output.log(` ${fmt.bold(domain.host)}:`);
34
38
  for (const rec of domain.dns) {
35
- console.log(` ${fmt.cyan(rec.type.padEnd(6))} ${rec.name} → ${fmt.bold(rec.value)}`);
39
+ output.log(` ${fmt.cyan(rec.type.padEnd(6))} ${rec.name} → ${fmt.bold(rec.value)}`);
36
40
  }
37
41
  }
38
42
  }
39
43
  }
40
- export async function listPenDomains(options = {}) {
44
+ export async function runDomainsListCommand(options = {}) {
45
+ const output = options.output ?? defaultOutput;
41
46
  const cwd = options.cwd ?? process.cwd();
42
47
  const interactive = (options.interactive ?? isInteractive()) && !options.json;
43
48
  const project = await requireProject(cwd);
@@ -46,14 +51,26 @@ export async function listPenDomains(options = {}) {
46
51
  project,
47
52
  interactive,
48
53
  persistProfileToProject: !options.json,
54
+ output,
49
55
  env: options.env,
50
56
  homeDir: options.homeDir,
51
57
  });
52
58
  const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
53
- const [pen, domains] = await Promise.all([
54
- client.getPen(project.config.pen_id),
55
- client.listDomains(project.config.pen_id),
56
- ]);
59
+ const penLookup = await fetchLinkedPenOrHandleMissing({
60
+ client,
61
+ project,
62
+ auth,
63
+ commandName: 'domains',
64
+ interactive,
65
+ json: options.json,
66
+ output,
67
+ prompts: options.prompts,
68
+ });
69
+ if (!penLookup.found) {
70
+ return null;
71
+ }
72
+ const pen = penLookup.pen;
73
+ const domains = await client.listDomains(project.config.pen_id);
57
74
  const defaultUrl = pen.deployment?.url ?? null;
58
75
  if (options.json) {
59
76
  const message = domains.length === 0
@@ -62,14 +79,15 @@ export async function listPenDomains(options = {}) {
62
79
  printJsonResponse(200, message, {
63
80
  default_url: defaultUrl,
64
81
  domains,
65
- });
82
+ }, output);
66
83
  }
67
84
  else {
68
- renderDomains(domains, defaultUrl);
85
+ renderDomains(domains, defaultUrl, output);
69
86
  }
70
87
  return { defaultUrl, domains, pen, project, auth };
71
88
  }
72
- export async function addPenDomain(host, options = {}) {
89
+ export async function runDomainsAddCommand(host, options = {}) {
90
+ const output = options.output ?? defaultOutput;
73
91
  const cleanHost = host.trim().replace(/^https?:\/\//i, '').replace(/\/+$/, '');
74
92
  if (!cleanHost || !/^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(cleanHost)) {
75
93
  throw new CliError(400, `Invalid domain host: "${host}". Provide a hostname like www.example.com.`);
@@ -82,10 +100,24 @@ export async function addPenDomain(host, options = {}) {
82
100
  project,
83
101
  interactive,
84
102
  persistProfileToProject: !options.json,
103
+ output,
85
104
  env: options.env,
86
105
  homeDir: options.homeDir,
87
106
  });
88
107
  const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
108
+ const penLookup = await fetchLinkedPenOrHandleMissing({
109
+ client,
110
+ project,
111
+ auth,
112
+ commandName: 'domains add',
113
+ interactive,
114
+ json: options.json,
115
+ output,
116
+ prompts: options.prompts,
117
+ });
118
+ if (!penLookup.found) {
119
+ return null;
120
+ }
89
121
  const domains = await client.addDomain(project.config.pen_id, cleanHost);
90
122
  const pen = await client.getPen(project.config.pen_id);
91
123
  const defaultUrl = pen.deployment?.url ?? null;
@@ -94,15 +126,16 @@ export async function addPenDomain(host, options = {}) {
94
126
  host: cleanHost,
95
127
  default_url: defaultUrl,
96
128
  domains,
97
- });
129
+ }, output);
98
130
  }
99
131
  else {
100
- console.log(`${fmt.green('✓')} Added custom domain ${fmt.cyan(cleanHost)}.\n`);
101
- renderDomains(domains, defaultUrl);
132
+ output.log(`${fmt.green('✓')} Added custom domain ${fmt.cyan(cleanHost)}.\n`);
133
+ renderDomains(domains, defaultUrl, output);
102
134
  }
103
135
  return { defaultUrl, domains, pen, project, auth };
104
136
  }
105
- export async function deletePenDomain(host, options = {}) {
137
+ export async function runDomainsDeleteCommand(host, options = {}) {
138
+ const output = options.output ?? defaultOutput;
106
139
  const cleanHost = host.trim().replace(/^https?:\/\//i, '').replace(/\/+$/, '');
107
140
  if (!cleanHost) {
108
141
  throw new CliError(400, `Invalid domain host: "${host}". Provide a hostname like www.example.com.`);
@@ -115,7 +148,7 @@ export async function deletePenDomain(host, options = {}) {
115
148
  }
116
149
  const ok = await confirmImpl(`Delete custom domain ${fmt.cyan(cleanHost)}?`, false);
117
150
  if (!ok) {
118
- console.log('Aborted.');
151
+ output.log('Aborted.');
119
152
  return null;
120
153
  }
121
154
  }
@@ -126,10 +159,24 @@ export async function deletePenDomain(host, options = {}) {
126
159
  project,
127
160
  interactive,
128
161
  persistProfileToProject: !options.json,
162
+ output,
129
163
  env: options.env,
130
164
  homeDir: options.homeDir,
131
165
  });
132
166
  const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
167
+ const penLookup = await fetchLinkedPenOrHandleMissing({
168
+ client,
169
+ project,
170
+ auth,
171
+ commandName: 'domains delete',
172
+ interactive,
173
+ json: options.json,
174
+ output,
175
+ prompts: options.prompts,
176
+ });
177
+ if (!penLookup.found) {
178
+ return null;
179
+ }
133
180
  const remaining = await client.removeDomain(project.config.pen_id, cleanHost);
134
181
  const pen = await client.getPen(project.config.pen_id);
135
182
  const defaultUrl = pen.deployment?.url ?? null;
@@ -138,24 +185,21 @@ export async function deletePenDomain(host, options = {}) {
138
185
  host: cleanHost,
139
186
  default_url: defaultUrl,
140
187
  domains: remaining,
141
- });
188
+ }, output);
142
189
  }
143
190
  else {
144
- console.log(`${fmt.green('✓')} Deleted custom domain ${fmt.cyan(cleanHost)}.\n`);
145
- renderDomains(remaining, defaultUrl);
191
+ output.log(`${fmt.green('✓')} Deleted custom domain ${fmt.cyan(cleanHost)}.\n`);
192
+ renderDomains(remaining, defaultUrl, output);
146
193
  }
147
194
  return { defaultUrl, domains: remaining, pen, project, auth };
148
195
  }
149
- function resolveDomainsJsonOption(options, command) {
150
- return Boolean(options.json || command.parent?.opts().json);
151
- }
152
196
  export function registerDomainsCommand(program) {
153
197
  const domainsCmd = program
154
198
  .command('domains')
155
199
  .description('Show the default deployed URL and manage custom domains for the Pen’s deployment')
156
200
  .option('--json', 'Output default deployed URL and domains as JSON')
157
201
  .action(async (options, command) => {
158
- await listPenDomains({
202
+ await runDomainsListCommand({
159
203
  json: options.json,
160
204
  explicitProfile: getGlobalProfileOption(command),
161
205
  });
@@ -166,8 +210,8 @@ export function registerDomainsCommand(program) {
166
210
  .description('Show the default deployed URL, custom domains, and required DNS records')
167
211
  .option('--json', 'Output default deployed URL and domains as JSON')
168
212
  .action(async (options, command) => {
169
- await listPenDomains({
170
- json: resolveDomainsJsonOption(options, command),
213
+ await runDomainsListCommand({
214
+ json: resolveInheritedJsonOption(options, command),
171
215
  explicitProfile: getGlobalProfileOption(command),
172
216
  });
173
217
  });
@@ -176,8 +220,8 @@ export function registerDomainsCommand(program) {
176
220
  .description('Attach a custom domain (e.g. www.example.com) to this Pen’s deployment')
177
221
  .option('--json', 'Output default deployed URL and updated domains as JSON')
178
222
  .action(async (host, options, command) => {
179
- await addPenDomain(host, {
180
- json: resolveDomainsJsonOption(options, command),
223
+ await runDomainsAddCommand(host, {
224
+ json: resolveInheritedJsonOption(options, command),
181
225
  explicitProfile: getGlobalProfileOption(command),
182
226
  });
183
227
  });
@@ -188,9 +232,9 @@ export function registerDomainsCommand(program) {
188
232
  .option('-y, --yes', 'Skip confirmation prompt')
189
233
  .option('--json', 'Output default deployed URL and remaining domains as JSON')
190
234
  .action(async (host, options, command) => {
191
- await deletePenDomain(host, {
235
+ await runDomainsDeleteCommand(host, {
192
236
  yes: options.yes,
193
- json: resolveDomainsJsonOption(options, command),
237
+ json: resolveInheritedJsonOption(options, command),
194
238
  explicitProfile: getGlobalProfileOption(command),
195
239
  });
196
240
  });