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
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { CodePenClient } from '../../api/client.js';
|
|
2
|
+
import { isCodePenApiError } from '../../api/errors.js';
|
|
3
|
+
import { API_KEY_ENV_VAR, API_KEY_FILENAME, getKeyType, maskApiKey, validateProfileName, } from '../../config/auth.js';
|
|
4
|
+
import { findProject } from '../../config/project.js';
|
|
5
|
+
import { readAuthStore } from '../../config/user-store.js';
|
|
6
|
+
import { fmt } from '../../utils/format.js';
|
|
7
|
+
import { printJsonResponse } from '../../utils/json.js';
|
|
8
|
+
import { defaultOutput } from '../../utils/output.js';
|
|
9
|
+
async function verifyKeyForStatus(apiKey, baseUrl) {
|
|
10
|
+
const client = new CodePenClient({ apiKey, baseUrl });
|
|
11
|
+
try {
|
|
12
|
+
await client.verifyAuth();
|
|
13
|
+
return {
|
|
14
|
+
ok: true,
|
|
15
|
+
error: null,
|
|
16
|
+
detail: `${fmt.green('Valid')} ${fmt.dim('(verified with api.codepen.io/v2)')}`,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
const msg = isCodePenApiError(error) ? error.message : error.message;
|
|
21
|
+
return {
|
|
22
|
+
ok: false,
|
|
23
|
+
error: msg,
|
|
24
|
+
detail: `${fmt.red('Invalid')} — ${msg}`,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export async function runAuthStatusCommand(options = {}) {
|
|
29
|
+
const output = options.output ?? defaultOutput;
|
|
30
|
+
const env = options.env ?? process.env;
|
|
31
|
+
const cwd = options.cwd ?? process.cwd();
|
|
32
|
+
const requestedProfile = options.requestedProfile;
|
|
33
|
+
const envKey = env[API_KEY_ENV_VAR]?.trim() || '';
|
|
34
|
+
const { store, filePath, insecurePermissions } = await readAuthStore({
|
|
35
|
+
homeDir: options.homeDir,
|
|
36
|
+
});
|
|
37
|
+
const profileNames = Object.keys(store.profiles).sort((a, b) => a.localeCompare(b));
|
|
38
|
+
const project = await findProject(cwd).catch(() => null);
|
|
39
|
+
const projectProfile = project?.config.profile ?? null;
|
|
40
|
+
if (requestedProfile) {
|
|
41
|
+
const validName = validateProfileName(requestedProfile);
|
|
42
|
+
if (!Object.hasOwn(store.profiles, validName)) {
|
|
43
|
+
if (options.json) {
|
|
44
|
+
printJsonResponse(404, `Profile "${validName}" not found in ${filePath}.`, {
|
|
45
|
+
authenticated: false,
|
|
46
|
+
offline: Boolean(options.offline),
|
|
47
|
+
requested_profile: validName,
|
|
48
|
+
file_path: filePath,
|
|
49
|
+
available_profiles: profileNames,
|
|
50
|
+
}, output);
|
|
51
|
+
return { authenticated: false, exitCode: 1 };
|
|
52
|
+
}
|
|
53
|
+
output.log(`${fmt.red('✗')} Profile ${fmt.bold(validName)} not found in ${filePath}.`);
|
|
54
|
+
if (profileNames.length > 0) {
|
|
55
|
+
output.log(fmt.dim(` Available profiles: ${profileNames.join(', ')}`));
|
|
56
|
+
}
|
|
57
|
+
output.log(fmt.dim(` Run \`cdpn auth login ${validName}\` to add it.`));
|
|
58
|
+
return { authenticated: false, exitCode: 1 };
|
|
59
|
+
}
|
|
60
|
+
const key = store.profiles[validName];
|
|
61
|
+
const check = options.offline ? null : await verifyKeyForStatus(key, options.baseUrl);
|
|
62
|
+
const ok = check ? check.ok : true;
|
|
63
|
+
if (options.json) {
|
|
64
|
+
const code = ok ? 200 : 401;
|
|
65
|
+
const message = options.offline
|
|
66
|
+
? `Loaded profile "${validName}" (offline mode).`
|
|
67
|
+
: ok
|
|
68
|
+
? `Profile "${validName}" is valid.`
|
|
69
|
+
: `Profile "${validName}" failed verification: ${check.error}`;
|
|
70
|
+
printJsonResponse(code, message, {
|
|
71
|
+
authenticated: ok,
|
|
72
|
+
offline: Boolean(options.offline),
|
|
73
|
+
file_path: filePath,
|
|
74
|
+
insecure_permissions: insecurePermissions,
|
|
75
|
+
project_profile: projectProfile,
|
|
76
|
+
profile: {
|
|
77
|
+
name: validName,
|
|
78
|
+
key_type: getKeyType(key),
|
|
79
|
+
masked_key: maskApiKey(key),
|
|
80
|
+
is_default: validName === store.defaultProfile,
|
|
81
|
+
is_project_profile: validName === projectProfile,
|
|
82
|
+
verified: check ? check.ok : null,
|
|
83
|
+
verification_error: check ? check.error : null,
|
|
84
|
+
},
|
|
85
|
+
}, output);
|
|
86
|
+
return ok ? { authenticated: true } : { authenticated: false, exitCode: 1 };
|
|
87
|
+
}
|
|
88
|
+
const badges = [];
|
|
89
|
+
if (validName === store.defaultProfile)
|
|
90
|
+
badges.push(fmt.cyan('default'));
|
|
91
|
+
if (validName === projectProfile)
|
|
92
|
+
badges.push(fmt.green('current folder'));
|
|
93
|
+
const badgeText = badges.length > 0 ? ` ${fmt.dim(`(${badges.join(', ')})`)}` : '';
|
|
94
|
+
output.log(`${fmt.green('✓')} Profile ${fmt.bold(validName)}${badgeText} from ${fmt.dim(filePath)}`);
|
|
95
|
+
output.log(` Key: ${fmt.cyan(maskApiKey(key))} ${fmt.dim(`(${getKeyType(key)} key)`)}`);
|
|
96
|
+
if (insecurePermissions) {
|
|
97
|
+
output.log(fmt.yellow(` Warning: ${filePath} is readable by other users. Tighten permissions with: chmod 600 ~/${API_KEY_FILENAME}`));
|
|
98
|
+
}
|
|
99
|
+
if (check) {
|
|
100
|
+
output.log(` API: ${check.detail}`);
|
|
101
|
+
}
|
|
102
|
+
return ok ? { authenticated: true } : { authenticated: false, exitCode: 1 };
|
|
103
|
+
}
|
|
104
|
+
if (!envKey && profileNames.length === 0) {
|
|
105
|
+
if (options.json) {
|
|
106
|
+
printJsonResponse(401, `Not authenticated. No API key found in ${API_KEY_ENV_VAR} or ~/${API_KEY_FILENAME}.`, {
|
|
107
|
+
authenticated: false,
|
|
108
|
+
offline: Boolean(options.offline),
|
|
109
|
+
file_path: filePath,
|
|
110
|
+
insecure_permissions: insecurePermissions,
|
|
111
|
+
default_profile: store.defaultProfile,
|
|
112
|
+
project_profile: projectProfile,
|
|
113
|
+
env: null,
|
|
114
|
+
profiles: [],
|
|
115
|
+
}, output);
|
|
116
|
+
return { authenticated: false, exitCode: 1 };
|
|
117
|
+
}
|
|
118
|
+
output.log(`${fmt.red('✗')} Not authenticated.`);
|
|
119
|
+
output.log(fmt.dim(`No API key found in ${API_KEY_ENV_VAR} or ~/${API_KEY_FILENAME}. Run \`cdpn auth login\` to get started.`));
|
|
120
|
+
return { authenticated: false, exitCode: 1 };
|
|
121
|
+
}
|
|
122
|
+
let anyInvalid = false;
|
|
123
|
+
if (options.json) {
|
|
124
|
+
let envEntry = null;
|
|
125
|
+
if (envKey) {
|
|
126
|
+
const check = options.offline ? null : await verifyKeyForStatus(envKey, options.baseUrl);
|
|
127
|
+
if (check && !check.ok)
|
|
128
|
+
anyInvalid = true;
|
|
129
|
+
envEntry = {
|
|
130
|
+
env_var: API_KEY_ENV_VAR,
|
|
131
|
+
key_type: getKeyType(envKey),
|
|
132
|
+
masked_key: maskApiKey(envKey),
|
|
133
|
+
verified: check ? check.ok : null,
|
|
134
|
+
verification_error: check ? check.error : null,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const profileEntries = [];
|
|
138
|
+
for (const name of profileNames) {
|
|
139
|
+
const key = store.profiles[name];
|
|
140
|
+
const check = options.offline ? null : await verifyKeyForStatus(key, options.baseUrl);
|
|
141
|
+
if (check && !check.ok)
|
|
142
|
+
anyInvalid = true;
|
|
143
|
+
profileEntries.push({
|
|
144
|
+
name,
|
|
145
|
+
key_type: getKeyType(key),
|
|
146
|
+
masked_key: maskApiKey(key),
|
|
147
|
+
is_default: name === store.defaultProfile,
|
|
148
|
+
is_project_profile: name === projectProfile,
|
|
149
|
+
verified: check ? check.ok : null,
|
|
150
|
+
verification_error: check ? check.error : null,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
const totalCount = (envEntry ? 1 : 0) + profileEntries.length;
|
|
154
|
+
const code = anyInvalid ? 401 : 200;
|
|
155
|
+
const message = options.offline
|
|
156
|
+
? `Loaded ${totalCount} authentication credential(s) (offline mode).`
|
|
157
|
+
: anyInvalid
|
|
158
|
+
? 'One or more CodePen API keys failed verification.'
|
|
159
|
+
: 'All CodePen API keys are valid.';
|
|
160
|
+
printJsonResponse(code, message, {
|
|
161
|
+
authenticated: !anyInvalid,
|
|
162
|
+
offline: Boolean(options.offline),
|
|
163
|
+
file_path: filePath,
|
|
164
|
+
insecure_permissions: insecurePermissions,
|
|
165
|
+
default_profile: store.defaultProfile,
|
|
166
|
+
project_profile: projectProfile,
|
|
167
|
+
env: envEntry,
|
|
168
|
+
profiles: profileEntries,
|
|
169
|
+
}, output);
|
|
170
|
+
return anyInvalid ? { authenticated: false, exitCode: 1 } : { authenticated: true };
|
|
171
|
+
}
|
|
172
|
+
if (envKey) {
|
|
173
|
+
output.log(`${fmt.green('✓')} API key loaded from ${fmt.bold(`environment variable (${API_KEY_ENV_VAR})`)}`);
|
|
174
|
+
output.log(` Key: ${fmt.cyan(maskApiKey(envKey))} ${fmt.dim(`(${getKeyType(envKey)} key)`)}`);
|
|
175
|
+
if (!options.offline) {
|
|
176
|
+
const check = await verifyKeyForStatus(envKey, options.baseUrl);
|
|
177
|
+
output.log(` API: ${check.detail}`);
|
|
178
|
+
if (!check.ok)
|
|
179
|
+
anyInvalid = true;
|
|
180
|
+
}
|
|
181
|
+
if (profileNames.length > 0) {
|
|
182
|
+
output.log(fmt.dim(` Note: ${API_KEY_ENV_VAR} takes precedence over ~/${API_KEY_FILENAME} unless --profile <name> is specified.\n`));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (profileNames.length > 0) {
|
|
186
|
+
output.log(`${fmt.green('✓')} Stored profiles in ${fmt.bold(filePath)} ${fmt.dim(`(${profileNames.length})`)}:`);
|
|
187
|
+
for (const name of profileNames) {
|
|
188
|
+
const key = store.profiles[name];
|
|
189
|
+
const badges = [];
|
|
190
|
+
if (name === store.defaultProfile)
|
|
191
|
+
badges.push(fmt.cyan('default'));
|
|
192
|
+
if (name === projectProfile)
|
|
193
|
+
badges.push(fmt.green('current folder'));
|
|
194
|
+
const badgeText = badges.length > 0 ? ` ${fmt.dim(`[${badges.join(', ')}]`)}` : '';
|
|
195
|
+
output.log(` • ${fmt.bold(name)}${badgeText}`);
|
|
196
|
+
output.log(` Key: ${fmt.cyan(maskApiKey(key))} ${fmt.dim(`(${getKeyType(key)} key)`)}`);
|
|
197
|
+
if (!options.offline) {
|
|
198
|
+
const check = await verifyKeyForStatus(key, options.baseUrl);
|
|
199
|
+
output.log(` API: ${check.detail}`);
|
|
200
|
+
if (!check.ok)
|
|
201
|
+
anyInvalid = true;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (projectProfile && !Object.hasOwn(store.profiles, projectProfile)) {
|
|
205
|
+
output.log(fmt.yellow(`\n Warning: Current folder’s .cdpn.json references profile "${projectProfile}", which is not in ~/${API_KEY_FILENAME}.`));
|
|
206
|
+
}
|
|
207
|
+
if (insecurePermissions) {
|
|
208
|
+
output.log(fmt.yellow(`\n Warning: ${filePath} is readable by other users. Tighten permissions with: chmod 600 ~/${API_KEY_FILENAME}`));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return anyInvalid ? { authenticated: false, exitCode: 1 } : { authenticated: true };
|
|
212
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { CliError } from '../errors.js';
|
|
4
|
+
import { DEFAULT_BUILD_DIR, buildProject, resolveBuildTargetDir, watchBuild, } from '../site/builder.js';
|
|
5
|
+
import { inspectTargetDirectory } from '../site/detect.js';
|
|
6
|
+
import { ensureIgnoredInCdpnIgnore, inspectGitIgnoreStatus, updateGitIgnore, } from '../site/ignore-files.js';
|
|
7
|
+
import { fmt } from '../utils/format.js';
|
|
8
|
+
import { printJsonResponse } from '../utils/json.js';
|
|
9
|
+
import { defaultOutput } from '../utils/output.js';
|
|
10
|
+
import { isInteractive, promptConfirm, promptValidatedTextWithIO, } from '../utils/prompts/index.js';
|
|
11
|
+
export async function runBuildCommand(options = {}) {
|
|
12
|
+
const output = options.output ?? defaultOutput;
|
|
13
|
+
const jsonMode = Boolean(options.json);
|
|
14
|
+
const watchMode = Boolean(options.watch);
|
|
15
|
+
if (watchMode && jsonMode) {
|
|
16
|
+
throw new CliError(400, 'Cannot combine --watch with --json.');
|
|
17
|
+
}
|
|
18
|
+
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
19
|
+
const sourceDirArg = options.dirArg ?? '.';
|
|
20
|
+
const resolvedSource = path.resolve(cwd, sourceDirArg);
|
|
21
|
+
let sourceStat;
|
|
22
|
+
try {
|
|
23
|
+
sourceStat = await fs.stat(resolvedSource);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (error.code === 'ENOENT') {
|
|
27
|
+
throw new CliError(404, `Source directory not found: ${resolvedSource}`);
|
|
28
|
+
}
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
if (!sourceStat.isDirectory()) {
|
|
32
|
+
throw new CliError(400, `Source path is not a directory: ${resolvedSource}`);
|
|
33
|
+
}
|
|
34
|
+
const inspection = await inspectTargetDirectory(resolvedSource);
|
|
35
|
+
const interactive = !jsonMode && (options.interactive ?? isInteractive());
|
|
36
|
+
const confirmImpl = options.prompts?.confirm ?? promptConfirm;
|
|
37
|
+
const askOutDirImpl = options.prompts?.askOutDir ??
|
|
38
|
+
((question, defaultValue, validate) => promptValidatedTextWithIO(question, {
|
|
39
|
+
defaultValue,
|
|
40
|
+
validate,
|
|
41
|
+
}));
|
|
42
|
+
// 1. Resolve output directory (explicit --out, interactive prompt, or default "build")
|
|
43
|
+
let target;
|
|
44
|
+
if (options.out !== undefined) {
|
|
45
|
+
const validated = await resolveBuildTargetDir(options.out, inspection.rootRealpath, {
|
|
46
|
+
homeDir: options.homeDir,
|
|
47
|
+
});
|
|
48
|
+
if (!validated.ok) {
|
|
49
|
+
throw new CliError(400, validated.error);
|
|
50
|
+
}
|
|
51
|
+
target = validated.target;
|
|
52
|
+
}
|
|
53
|
+
else if (interactive) {
|
|
54
|
+
output.log('');
|
|
55
|
+
target = await askOutDirImpl('Where would you like to build into?', DEFAULT_BUILD_DIR, async (raw) => {
|
|
56
|
+
const candidate = raw.trim() || DEFAULT_BUILD_DIR;
|
|
57
|
+
const validated = await resolveBuildTargetDir(candidate, inspection.rootRealpath, {
|
|
58
|
+
homeDir: options.homeDir,
|
|
59
|
+
});
|
|
60
|
+
if (!validated.ok) {
|
|
61
|
+
return { ok: false, error: validated.error };
|
|
62
|
+
}
|
|
63
|
+
return { ok: true, value: validated.target, displayValue: candidate };
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const validated = await resolveBuildTargetDir(DEFAULT_BUILD_DIR, inspection.rootRealpath, {
|
|
68
|
+
homeDir: options.homeDir,
|
|
69
|
+
});
|
|
70
|
+
if (!validated.ok) {
|
|
71
|
+
throw new CliError(400, validated.error);
|
|
72
|
+
}
|
|
73
|
+
target = validated.target;
|
|
74
|
+
}
|
|
75
|
+
// 2. Confirm overwrite if target directory already exists
|
|
76
|
+
const autoConfirmOverwrite = Boolean(options.yes || options.force);
|
|
77
|
+
if (target.exists && !autoConfirmOverwrite) {
|
|
78
|
+
if (!interactive) {
|
|
79
|
+
throw new CliError(409, `Build directory "${target.displayPath}" already exists. Pass --yes (or --force) to overwrite it.`, { out_dir: target.outDir });
|
|
80
|
+
}
|
|
81
|
+
output.log('');
|
|
82
|
+
const overwrite = await confirmImpl(`Directory "${target.displayPath}" already exists. Overwrite it?`, false);
|
|
83
|
+
if (!overwrite) {
|
|
84
|
+
output.log(fmt.dim('Build aborted.'));
|
|
85
|
+
return {
|
|
86
|
+
built: false,
|
|
87
|
+
aborted: true,
|
|
88
|
+
result: null,
|
|
89
|
+
target,
|
|
90
|
+
cdpnIgnoreUpdated: false,
|
|
91
|
+
cdpnIgnoreEntry: null,
|
|
92
|
+
gitIgnoreUpdated: false,
|
|
93
|
+
gitIgnoreCreated: false,
|
|
94
|
+
gitIgnoreEntry: null,
|
|
95
|
+
watcher: null,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const buildOptions = {
|
|
100
|
+
sourceDir: inspection.rootRealpath,
|
|
101
|
+
outDir: target.outDir,
|
|
102
|
+
explicitShell: options.shell,
|
|
103
|
+
site: {
|
|
104
|
+
title: options.title,
|
|
105
|
+
subtitle: options.subtitle,
|
|
106
|
+
footer: options.footer,
|
|
107
|
+
},
|
|
108
|
+
homeDir: options.homeDir,
|
|
109
|
+
cwd,
|
|
110
|
+
};
|
|
111
|
+
// 3. Execute build (passing preResolved inspection & target to avoid duplicate stat work)
|
|
112
|
+
const result = await buildProject({
|
|
113
|
+
...buildOptions,
|
|
114
|
+
preResolved: { inspection, target },
|
|
115
|
+
});
|
|
116
|
+
// 4. For a Pen built into a subfolder of itself, update .cdpnignore and offer .gitignore
|
|
117
|
+
let cdpnIgnoreUpdated = false;
|
|
118
|
+
let cdpnIgnoreEntry = null;
|
|
119
|
+
let gitIgnoreUpdated = false;
|
|
120
|
+
let gitIgnoreCreated = false;
|
|
121
|
+
let gitIgnoreEntry = null;
|
|
122
|
+
if (result.mode === 'pen' && target.isInsideSource && target.relInsideSource) {
|
|
123
|
+
const ignoreRes = await ensureIgnoredInCdpnIgnore(inspection.rootRealpath, target.relInsideSource);
|
|
124
|
+
cdpnIgnoreUpdated = ignoreRes.updated;
|
|
125
|
+
cdpnIgnoreEntry = ignoreRes.entry;
|
|
126
|
+
if (!jsonMode) {
|
|
127
|
+
output.log('');
|
|
128
|
+
if (ignoreRes.updated) {
|
|
129
|
+
const verb = ignoreRes.created ? 'Created' : 'Updated';
|
|
130
|
+
output.log(` ${fmt.boldGreen('+')} ${verb} ${fmt.magenta('.cdpnignore')} ${fmt.dim(`(added ${ignoreRes.entry})`)}`);
|
|
131
|
+
}
|
|
132
|
+
else if (ignoreRes.alreadyIgnored) {
|
|
133
|
+
output.log(` ${fmt.dim('·')} ${fmt.dim(`.cdpnignore already ignores ${ignoreRes.entry}`)}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const gitStatus = await inspectGitIgnoreStatus(inspection.rootRealpath, target.relInsideSource);
|
|
137
|
+
if (!gitStatus.alreadyIgnored && interactive) {
|
|
138
|
+
output.log('');
|
|
139
|
+
const gitQuestion = gitStatus.exists
|
|
140
|
+
? `Add "${gitStatus.entry}" to .gitignore?`
|
|
141
|
+
: `Generate a .gitignore ignoring "${gitStatus.entry}"?`;
|
|
142
|
+
const shouldUpdateGit = await confirmImpl(gitQuestion, true);
|
|
143
|
+
if (shouldUpdateGit) {
|
|
144
|
+
const gitRes = await updateGitIgnore(inspection.rootRealpath, target.relInsideSource);
|
|
145
|
+
gitIgnoreUpdated = gitRes.updated;
|
|
146
|
+
gitIgnoreCreated = gitRes.created;
|
|
147
|
+
gitIgnoreEntry = gitRes.entry;
|
|
148
|
+
if (gitRes.updated) {
|
|
149
|
+
const verb = gitRes.created ? 'Created' : 'Updated';
|
|
150
|
+
output.log(` ${fmt.boldGreen('+')} ${verb} ${fmt.magenta('.gitignore')} ${fmt.dim(`(added ${gitRes.entry})`)}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// 5. Output summary
|
|
156
|
+
const relOutDisplay = path.relative(cwd, result.outDir) || result.outDir;
|
|
157
|
+
if (jsonMode) {
|
|
158
|
+
const message = result.mode === 'collection'
|
|
159
|
+
? `Built ${relOutDisplay} (${result.demoCount} ${result.demoCount === 1 ? 'demo' : 'demos'}).`
|
|
160
|
+
: `Built ${relOutDisplay}.`;
|
|
161
|
+
printJsonResponse(200, message, {
|
|
162
|
+
mode: result.mode,
|
|
163
|
+
source_dir: result.sourceDir,
|
|
164
|
+
out_dir: result.outDir,
|
|
165
|
+
demo_count: result.demoCount,
|
|
166
|
+
shell_source: result.shell?.source ?? null,
|
|
167
|
+
shell_dir: result.shell?.dir ?? null,
|
|
168
|
+
unsupported_demos: result.unsupportedDemos,
|
|
169
|
+
cdpnignore_updated: cdpnIgnoreUpdated,
|
|
170
|
+
cdpnignore_entry: cdpnIgnoreEntry,
|
|
171
|
+
gitignore_updated: gitIgnoreUpdated,
|
|
172
|
+
gitignore_created: gitIgnoreCreated,
|
|
173
|
+
gitignore_entry: gitIgnoreEntry,
|
|
174
|
+
}, output);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
output.log('');
|
|
178
|
+
if (result.mode === 'collection') {
|
|
179
|
+
const countLabel = `${result.demoCount} ${result.demoCount === 1 ? 'demo' : 'demos'}`;
|
|
180
|
+
output.log(`${fmt.boldGreen('✓')} ${fmt.bold('Built')} ${fmt.magenta(relOutDisplay)} ${fmt.dim(`(${countLabel})`)}`);
|
|
181
|
+
if (result.shell && result.shell.source !== 'builtin') {
|
|
182
|
+
const sourceLabel = result.shell.source === 'flag' ? '--shell' : '~/.cdpn';
|
|
183
|
+
output.log(` ${fmt.dim('Shell:')} ${fmt.magenta(result.shell.dir)} ${fmt.dim(`(${sourceLabel})`)}`);
|
|
184
|
+
}
|
|
185
|
+
if (result.unsupportedDemos.length > 0) {
|
|
186
|
+
output.log(`\n ${fmt.boldYellow('!')} ${fmt.yellow(`${result.unsupportedDemos.length} demo(s) use unsupported CodePen blocks (rendered info pages):`)}`);
|
|
187
|
+
for (const item of result.unsupportedDemos) {
|
|
188
|
+
output.log(` ${fmt.yellow('•')} ${item.path} ${fmt.dim(`(${item.blocks.join(', ')})`)}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
output.log(`${fmt.boldGreen('✓')} ${fmt.bold('Built Pen into')} ${fmt.magenta(relOutDisplay)}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// 6. Optional watch mode
|
|
197
|
+
let watcher = null;
|
|
198
|
+
if (watchMode) {
|
|
199
|
+
output.log(`\n${fmt.dim('Watching for changes… (Press Ctrl+C to stop)')}`);
|
|
200
|
+
watcher = watchBuild({
|
|
201
|
+
buildOptions,
|
|
202
|
+
canonicalOutDir: target.canonicalOutDir,
|
|
203
|
+
onRebuild(updated) {
|
|
204
|
+
if (updated.mode === 'collection') {
|
|
205
|
+
output.log(`${fmt.cyan('↻')} Rebuilt ${fmt.magenta(relOutDisplay)} ${fmt.dim(`(${updated.demoCount} ${updated.demoCount === 1 ? 'demo' : 'demos'})`)}`);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
output.log(`${fmt.cyan('↻')} Rebuilt ${fmt.magenta(relOutDisplay)}`);
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
onError(error) {
|
|
212
|
+
output.error(`${fmt.red('✗')} Rebuild error: ${error.message}`);
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
built: true,
|
|
218
|
+
aborted: false,
|
|
219
|
+
result,
|
|
220
|
+
target,
|
|
221
|
+
cdpnIgnoreUpdated,
|
|
222
|
+
cdpnIgnoreEntry,
|
|
223
|
+
gitIgnoreUpdated,
|
|
224
|
+
gitIgnoreCreated,
|
|
225
|
+
gitIgnoreEntry,
|
|
226
|
+
watcher,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
export function registerBuildCommand(program) {
|
|
230
|
+
program
|
|
231
|
+
.command('build [dir]')
|
|
232
|
+
.description('Build a local Pen or directory of Pens into a static deployable folder')
|
|
233
|
+
.option('-o, --out <dir>', `Target output directory (default: prompts interactively, or "${DEFAULT_BUILD_DIR}")`)
|
|
234
|
+
.option('-y, --yes', 'Overwrite existing output directory without confirmation')
|
|
235
|
+
.option('-f, --force', 'Alias for --yes (overwrite existing output directory without confirmation)')
|
|
236
|
+
.option('-w, --watch', 'Watch source files and rebuild on changes')
|
|
237
|
+
.option('--shell <dir>', 'Custom shell directory for the demo index page (overrides ~/.cdpn)')
|
|
238
|
+
.option('--title <text>', 'Custom title for the demo index page (defaults to folder name)')
|
|
239
|
+
.option('--subtitle <text>', 'Custom subtitle for the demo index page')
|
|
240
|
+
.option('--footer <text>', 'Custom footer for the demo index page')
|
|
241
|
+
.option('--json', 'Output build result as JSON')
|
|
242
|
+
.action(async (dirArg, options) => {
|
|
243
|
+
const outcome = await runBuildCommand({
|
|
244
|
+
dirArg,
|
|
245
|
+
...options,
|
|
246
|
+
});
|
|
247
|
+
if (outcome.watcher) {
|
|
248
|
+
const shutdown = () => {
|
|
249
|
+
outcome.watcher?.close();
|
|
250
|
+
process.exit(0);
|
|
251
|
+
};
|
|
252
|
+
process.on('SIGINT', shutdown);
|
|
253
|
+
process.on('SIGTERM', shutdown);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|