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
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { CodePenClient } from '../api/client.js';
|
|
3
|
+
import { PROJECT_CONFIG_FILENAME, requireProjectInDir, } from '../config/project.js';
|
|
4
|
+
import { fmt } from '../utils/format.js';
|
|
5
|
+
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
6
|
+
import { isInteractive, promptConfirm, promptSelect, } from '../utils/prompts.js';
|
|
7
|
+
import { getGlobalProfileOption, resolveCommandAuth } from './auth-context.js';
|
|
8
|
+
export const CODEPEN_DELETED_ITEMS_URL = 'https://codepen.io/your-work?item_type=DELETED_ITEMS';
|
|
9
|
+
/**
|
|
10
|
+
* Deletes the online Pen linked to `.cdpn.json` directly inside `options.cwd`
|
|
11
|
+
* (moves it to CodePen Deleted Items, restorable for 3 days) and removes `.cdpn.json`
|
|
12
|
+
* only after the remote deletion succeeds. Local project files are kept untouched.
|
|
13
|
+
*
|
|
14
|
+
* Returns `null` if the user cancels at the confirmation prompt.
|
|
15
|
+
*/
|
|
16
|
+
export async function deletePenCommand(options = {}) {
|
|
17
|
+
const cwd = options.cwd ?? process.cwd();
|
|
18
|
+
const interactive = (options.interactive ?? isInteractive()) && !options.json;
|
|
19
|
+
const confirmImpl = options.prompts?.confirm ?? promptConfirm;
|
|
20
|
+
const selectImpl = options.prompts?.select ?? promptSelect;
|
|
21
|
+
// Strictly require `.cdpn.json` directly inside the current folder
|
|
22
|
+
const project = await requireProjectInDir(cwd);
|
|
23
|
+
const auth = await resolveCommandAuth({
|
|
24
|
+
explicitProfile: options.explicitProfile,
|
|
25
|
+
project,
|
|
26
|
+
interactive,
|
|
27
|
+
persistProfileToProject: false,
|
|
28
|
+
env: options.env,
|
|
29
|
+
homeDir: options.homeDir,
|
|
30
|
+
selectProfile: selectImpl,
|
|
31
|
+
});
|
|
32
|
+
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
33
|
+
const pen = await client.getPen(project.config.pen_id);
|
|
34
|
+
if (!options.yes) {
|
|
35
|
+
if (!interactive) {
|
|
36
|
+
throw new CliError(400, 'Refusing to delete Pen in non-interactive mode without --yes.');
|
|
37
|
+
}
|
|
38
|
+
console.log(`\n${fmt.yellow('About to delete online Pen:')}`);
|
|
39
|
+
console.log(` Title: ${fmt.bold(pen.title || 'Untitled')} ${fmt.dim(`(${pen.id})`)}`);
|
|
40
|
+
if (auth.profile) {
|
|
41
|
+
console.log(` Profile: ${fmt.cyan(auth.profile)}`);
|
|
42
|
+
}
|
|
43
|
+
console.log(` Editor: ${fmt.cyan(pen.urls.editor)}`);
|
|
44
|
+
if (pen.deployment) {
|
|
45
|
+
console.log(` Live: ${fmt.yellow(pen.deployment.url)} ${fmt.dim('(will also be taken down)')}`);
|
|
46
|
+
}
|
|
47
|
+
console.log(fmt.dim(`\nLocal files will stay untouched; ${PROJECT_CONFIG_FILENAME} will be removed after deletion succeeds.`));
|
|
48
|
+
const ok = await confirmImpl(`Delete this Pen on CodePen (moves to Deleted Items for 3 days) and remove ${PROJECT_CONFIG_FILENAME}?`, false);
|
|
49
|
+
if (!ok) {
|
|
50
|
+
console.log('Aborted.');
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
await client.deletePen(project.config.pen_id);
|
|
55
|
+
// Remove `.cdpn.json` only after remote deletion succeeded
|
|
56
|
+
await fs.unlink(project.configPath);
|
|
57
|
+
if (options.json) {
|
|
58
|
+
printJsonResponse(200, `Deleted ${pen.title || 'Untitled'} (${pen.id}) on CodePen and removed ${PROJECT_CONFIG_FILENAME}.`, {
|
|
59
|
+
pen_id: pen.id,
|
|
60
|
+
title: pen.title,
|
|
61
|
+
profile: auth.profile,
|
|
62
|
+
editor_url: pen.urls.editor,
|
|
63
|
+
deleted: true,
|
|
64
|
+
config_removed: true,
|
|
65
|
+
restore_url: CODEPEN_DELETED_ITEMS_URL,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
console.log(`\n${fmt.green('✓')} Deleted ${fmt.bold(pen.title || 'Untitled')} ${fmt.dim(`(${pen.id})`)} on CodePen ${fmt.dim('(moved to Deleted Items; restorable for 3 days)')}`);
|
|
70
|
+
if (auth.profile) {
|
|
71
|
+
console.log(` Profile: ${fmt.cyan(auth.profile)}`);
|
|
72
|
+
}
|
|
73
|
+
console.log(` Removed: ${fmt.cyan(PROJECT_CONFIG_FILENAME)} ${fmt.dim('(local files were kept untouched)')}`);
|
|
74
|
+
console.log(` Restore: ${fmt.dim(CODEPEN_DELETED_ITEMS_URL)}`);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
pen,
|
|
78
|
+
project,
|
|
79
|
+
auth,
|
|
80
|
+
configRemoved: true,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export function registerDeleteCommand(program) {
|
|
84
|
+
program
|
|
85
|
+
.command('delete')
|
|
86
|
+
.aliases(['del', 'rm'])
|
|
87
|
+
.allowExcessArguments(false)
|
|
88
|
+
.description(`Delete the online Pen on CodePen (moves to Deleted Items) and remove ${PROJECT_CONFIG_FILENAME}`)
|
|
89
|
+
.option('-y, --yes', 'Skip confirmation prompt')
|
|
90
|
+
.option('--json', 'Output deletion result as JSON')
|
|
91
|
+
.action(async (options, command) => {
|
|
92
|
+
await deletePenCommand({
|
|
93
|
+
yes: options.yes,
|
|
94
|
+
json: options.json,
|
|
95
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { CodePenClient } from '../api/client.js';
|
|
2
|
+
import { findProject, PROJECT_CONFIG_FILENAME, requireProject } from '../config/project.js';
|
|
3
|
+
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.');
|
|
24
|
+
}
|
|
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` +
|
|
35
|
+
'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,
|
|
47
|
+
force: options.force,
|
|
48
|
+
wait: true,
|
|
49
|
+
silent: Boolean(options.json),
|
|
50
|
+
interactive: !options.json && isInteractive(),
|
|
51
|
+
});
|
|
52
|
+
if (!created) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (created.build?.status === 'Failed') {
|
|
56
|
+
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,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
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
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
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!`);
|
|
116
|
+
if (activeProfile) {
|
|
117
|
+
console.log(` Profile: ${fmt.cyan(activeProfile)}`);
|
|
118
|
+
}
|
|
119
|
+
console.log(` URL: ${fmt.cyan(deployment.url)}`);
|
|
120
|
+
console.log(` Version: ${fmt.dim(shortId(deployment.version_id))}`);
|
|
121
|
+
if (deployment.custom_domains.length > 0) {
|
|
122
|
+
console.log(` Domains: ${deployment.custom_domains.map((d) => fmt.cyan(`https://${d}`)).join(', ')}`);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
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')
|
|
130
|
+
.action(async (options, command) => {
|
|
131
|
+
const interactive = isInteractive() && !options.json;
|
|
132
|
+
const project = await requireProject();
|
|
133
|
+
const auth = await resolveCommandAuth({
|
|
134
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
135
|
+
project,
|
|
136
|
+
interactive,
|
|
137
|
+
persistProfileToProject: !options.json,
|
|
138
|
+
});
|
|
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
|
+
});
|
|
176
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { CodePenClient } from '../api/client.js';
|
|
2
|
+
import { requireProject } from '../config/project.js';
|
|
3
|
+
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) {
|
|
8
|
+
if (defaultUrl) {
|
|
9
|
+
console.log(`Default URL: ${fmt.cyan(defaultUrl)}`);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
console.log(`Default URL: ${fmt.dim('not deployed (run `cdpn deploy` to deploy)')}`);
|
|
13
|
+
}
|
|
14
|
+
console.log('');
|
|
15
|
+
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>'));
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const rows = domains.map((d) => {
|
|
21
|
+
const status = d.status === 'Verified'
|
|
22
|
+
? fmt.green(d.status)
|
|
23
|
+
: d.status === 'Misconfigured'
|
|
24
|
+
? fmt.red(d.status)
|
|
25
|
+
: fmt.yellow(d.status);
|
|
26
|
+
return [fmt.cyan(d.host), status, d.cname_record, d.a_record];
|
|
27
|
+
});
|
|
28
|
+
console.log(formatTable(['Host', 'Status', 'CNAME', 'A Record'], rows));
|
|
29
|
+
const needingDns = domains.filter((d) => d.status !== 'Verified' && d.dns.length > 0);
|
|
30
|
+
if (needingDns.length > 0) {
|
|
31
|
+
console.log('\nRequired DNS records:');
|
|
32
|
+
for (const domain of needingDns) {
|
|
33
|
+
console.log(` ${fmt.bold(domain.host)}:`);
|
|
34
|
+
for (const rec of domain.dns) {
|
|
35
|
+
console.log(` ${fmt.cyan(rec.type.padEnd(6))} ${rec.name} → ${fmt.bold(rec.value)}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export async function listPenDomains(options = {}) {
|
|
41
|
+
const cwd = options.cwd ?? process.cwd();
|
|
42
|
+
const interactive = (options.interactive ?? isInteractive()) && !options.json;
|
|
43
|
+
const project = await requireProject(cwd);
|
|
44
|
+
const auth = await resolveCommandAuth({
|
|
45
|
+
explicitProfile: options.explicitProfile,
|
|
46
|
+
project,
|
|
47
|
+
interactive,
|
|
48
|
+
persistProfileToProject: !options.json,
|
|
49
|
+
env: options.env,
|
|
50
|
+
homeDir: options.homeDir,
|
|
51
|
+
});
|
|
52
|
+
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
|
+
]);
|
|
57
|
+
const defaultUrl = pen.deployment?.url ?? null;
|
|
58
|
+
if (options.json) {
|
|
59
|
+
const message = domains.length === 0
|
|
60
|
+
? 'No custom domains configured for this Pen.'
|
|
61
|
+
: `Found ${domains.length} custom domain(s).`;
|
|
62
|
+
printJsonResponse(200, message, {
|
|
63
|
+
default_url: defaultUrl,
|
|
64
|
+
domains,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
renderDomains(domains, defaultUrl);
|
|
69
|
+
}
|
|
70
|
+
return { defaultUrl, domains, pen, project, auth };
|
|
71
|
+
}
|
|
72
|
+
export async function addPenDomain(host, options = {}) {
|
|
73
|
+
const cleanHost = host.trim().replace(/^https?:\/\//i, '').replace(/\/+$/, '');
|
|
74
|
+
if (!cleanHost || !/^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(cleanHost)) {
|
|
75
|
+
throw new CliError(400, `Invalid domain host: "${host}". Provide a hostname like www.example.com.`);
|
|
76
|
+
}
|
|
77
|
+
const cwd = options.cwd ?? process.cwd();
|
|
78
|
+
const interactive = (options.interactive ?? isInteractive()) && !options.json;
|
|
79
|
+
const project = await requireProject(cwd);
|
|
80
|
+
const auth = await resolveCommandAuth({
|
|
81
|
+
explicitProfile: options.explicitProfile,
|
|
82
|
+
project,
|
|
83
|
+
interactive,
|
|
84
|
+
persistProfileToProject: !options.json,
|
|
85
|
+
env: options.env,
|
|
86
|
+
homeDir: options.homeDir,
|
|
87
|
+
});
|
|
88
|
+
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
89
|
+
const domains = await client.addDomain(project.config.pen_id, cleanHost);
|
|
90
|
+
const pen = await client.getPen(project.config.pen_id);
|
|
91
|
+
const defaultUrl = pen.deployment?.url ?? null;
|
|
92
|
+
if (options.json) {
|
|
93
|
+
printJsonResponse(201, `Added custom domain ${cleanHost}.`, {
|
|
94
|
+
host: cleanHost,
|
|
95
|
+
default_url: defaultUrl,
|
|
96
|
+
domains,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
console.log(`${fmt.green('✓')} Added custom domain ${fmt.cyan(cleanHost)}.\n`);
|
|
101
|
+
renderDomains(domains, defaultUrl);
|
|
102
|
+
}
|
|
103
|
+
return { defaultUrl, domains, pen, project, auth };
|
|
104
|
+
}
|
|
105
|
+
export async function deletePenDomain(host, options = {}) {
|
|
106
|
+
const cleanHost = host.trim().replace(/^https?:\/\//i, '').replace(/\/+$/, '');
|
|
107
|
+
if (!cleanHost) {
|
|
108
|
+
throw new CliError(400, `Invalid domain host: "${host}". Provide a hostname like www.example.com.`);
|
|
109
|
+
}
|
|
110
|
+
const interactive = (options.interactive ?? isInteractive()) && !options.json;
|
|
111
|
+
const confirmImpl = options.confirm ?? promptConfirm;
|
|
112
|
+
if (!options.yes) {
|
|
113
|
+
if (!interactive) {
|
|
114
|
+
throw new CliError(400, 'Refusing to delete domain in non-interactive mode without --yes.');
|
|
115
|
+
}
|
|
116
|
+
const ok = await confirmImpl(`Delete custom domain ${fmt.cyan(cleanHost)}?`, false);
|
|
117
|
+
if (!ok) {
|
|
118
|
+
console.log('Aborted.');
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const cwd = options.cwd ?? process.cwd();
|
|
123
|
+
const project = await requireProject(cwd);
|
|
124
|
+
const auth = await resolveCommandAuth({
|
|
125
|
+
explicitProfile: options.explicitProfile,
|
|
126
|
+
project,
|
|
127
|
+
interactive,
|
|
128
|
+
persistProfileToProject: !options.json,
|
|
129
|
+
env: options.env,
|
|
130
|
+
homeDir: options.homeDir,
|
|
131
|
+
});
|
|
132
|
+
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
133
|
+
const remaining = await client.removeDomain(project.config.pen_id, cleanHost);
|
|
134
|
+
const pen = await client.getPen(project.config.pen_id);
|
|
135
|
+
const defaultUrl = pen.deployment?.url ?? null;
|
|
136
|
+
if (options.json) {
|
|
137
|
+
printJsonResponse(200, `Deleted custom domain ${cleanHost}.`, {
|
|
138
|
+
host: cleanHost,
|
|
139
|
+
default_url: defaultUrl,
|
|
140
|
+
domains: remaining,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
console.log(`${fmt.green('✓')} Deleted custom domain ${fmt.cyan(cleanHost)}.\n`);
|
|
145
|
+
renderDomains(remaining, defaultUrl);
|
|
146
|
+
}
|
|
147
|
+
return { defaultUrl, domains: remaining, pen, project, auth };
|
|
148
|
+
}
|
|
149
|
+
function resolveDomainsJsonOption(options, command) {
|
|
150
|
+
return Boolean(options.json || command.parent?.opts().json);
|
|
151
|
+
}
|
|
152
|
+
export function registerDomainsCommand(program) {
|
|
153
|
+
const domainsCmd = program
|
|
154
|
+
.command('domains')
|
|
155
|
+
.description('Show the default deployed URL and manage custom domains for the Pen’s deployment')
|
|
156
|
+
.option('--json', 'Output default deployed URL and domains as JSON')
|
|
157
|
+
.action(async (options, command) => {
|
|
158
|
+
await listPenDomains({
|
|
159
|
+
json: options.json,
|
|
160
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
domainsCmd
|
|
164
|
+
.command('list')
|
|
165
|
+
.alias('ls')
|
|
166
|
+
.description('Show the default deployed URL, custom domains, and required DNS records')
|
|
167
|
+
.option('--json', 'Output default deployed URL and domains as JSON')
|
|
168
|
+
.action(async (options, command) => {
|
|
169
|
+
await listPenDomains({
|
|
170
|
+
json: resolveDomainsJsonOption(options, command),
|
|
171
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
domainsCmd
|
|
175
|
+
.command('add <host>')
|
|
176
|
+
.description('Attach a custom domain (e.g. www.example.com) to this Pen’s deployment')
|
|
177
|
+
.option('--json', 'Output default deployed URL and updated domains as JSON')
|
|
178
|
+
.action(async (host, options, command) => {
|
|
179
|
+
await addPenDomain(host, {
|
|
180
|
+
json: resolveDomainsJsonOption(options, command),
|
|
181
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
domainsCmd
|
|
185
|
+
.command('delete <host>')
|
|
186
|
+
.aliases(['del', 'rm'])
|
|
187
|
+
.description('Delete a custom domain from this Pen’s deployment')
|
|
188
|
+
.option('-y, --yes', 'Skip confirmation prompt')
|
|
189
|
+
.option('--json', 'Output default deployed URL and remaining domains as JSON')
|
|
190
|
+
.action(async (host, options, command) => {
|
|
191
|
+
await deletePenDomain(host, {
|
|
192
|
+
yes: options.yes,
|
|
193
|
+
json: resolveDomainsJsonOption(options, command),
|
|
194
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
}
|