-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathdocs.ts
More file actions
190 lines (159 loc) · 5.62 KB
/
docs.ts
File metadata and controls
190 lines (159 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import * as p from '@clack/prompts';
import { validateInstallationOptions } from '@/utils/installation/codegen';
import type { InstallMethod, Renderer, UseCase } from '@/utils/installation/types';
import type { Framework } from '../utils/config.js';
import { getConfigValue } from '../utils/config.js';
import { docExistsInAnyFramework, readBundledDoc, readLlmsTxt } from '../utils/docs.js';
import { formatInstallationCode } from '../utils/format.js';
import { mapRawSkin, type PartialInstallFlags, promptFramework, promptInstallOptions } from '../utils/prompts.js';
import { replaceMarker, stripOmitMarkers } from '../utils/replace.js';
interface ParsedFlags {
framework?: string;
list?: boolean;
help?: boolean;
preset?: string;
skin?: string;
media?: string;
'source-url'?: string;
'install-method'?: string;
}
function printVersionHeader(): void {
console.log(`@videojs/cli v${__CLI_VERSION__}\n`);
}
async function resolveFramework(flags: ParsedFlags): Promise<Framework> {
if (flags.framework === 'html' || flags.framework === 'react') {
return flags.framework;
}
if (flags.framework) {
console.error(`Invalid framework: "${flags.framework}". Must be "html" or "react".`);
process.exit(1);
}
const saved = getConfigValue('framework');
if (saved === 'html' || saved === 'react') return saved;
return promptFramework();
}
function mapPresetToUseCase(preset: string): UseCase {
const map: Record<string, UseCase> = {
video: 'default-video',
audio: 'default-audio',
'background-video': 'background-video',
};
const result = map[preset];
if (!result) {
console.error(`Invalid preset: "${preset}". Must be "video", "audio", or "background-video".`);
process.exit(1);
}
return result;
}
const ALL_RENDERERS: Renderer[] = ['html5-video', 'html5-audio', 'hls', 'background-video'];
function validateMedia(media: string): Renderer {
if (!ALL_RENDERERS.includes(media as Renderer)) {
console.error(`Invalid media type: "${media}". Valid options: ${ALL_RENDERERS.join(', ')}`);
process.exit(1);
}
return media as Renderer;
}
function validateInstallMethod(method: string, framework: Framework): InstallMethod {
const valid = framework === 'html' ? ['cdn', 'npm', 'pnpm', 'yarn', 'bun'] : ['npm', 'pnpm', 'yarn', 'bun'];
if (!valid.includes(method)) {
console.error(`Invalid install method: "${method}". Valid options: ${valid.join(', ')}`);
process.exit(1);
}
return method as InstallMethod;
}
function buildPartialFlags(flags: ParsedFlags, framework: Framework): PartialInstallFlags {
const partial: PartialInstallFlags = {};
if (flags.preset) {
partial.preset = mapPresetToUseCase(flags.preset);
}
if (flags.skin) {
if (partial.preset) {
partial.skin = mapRawSkin(flags.skin, partial.preset);
} else {
partial.rawSkin = flags.skin;
}
}
if (flags['source-url'] !== undefined) {
partial.sourceUrl = flags['source-url'];
}
if (flags.media) {
partial.media = validateMedia(flags.media);
}
if (flags['install-method'] !== undefined) {
partial.installMethod = validateInstallMethod(flags['install-method'], framework);
}
return partial;
}
const DOCS_HELP = `Usage: @videojs/cli docs <slug> [--framework <html|react>]
@videojs/cli docs --list [--framework <html|react>]
Installation flags (for docs how-to/installation):
--preset <video|audio|background-video>
--skin <default|minimal>
--source-url <url>
--media <html5-video|html5-audio|hls|background-video>
--install-method <cdn|npm|pnpm|yarn|bun>`;
export async function handleDocs(flags: ParsedFlags, positionals: string[]): Promise<void> {
if (flags.help) {
console.log(DOCS_HELP);
process.exit(0);
}
// --list: print llms.txt
if (flags.list) {
const framework = await resolveFramework(flags);
const content = readLlmsTxt(framework);
if (!content) {
console.error(`No documentation index found for framework "${framework}".`);
process.exit(1);
}
console.log(content);
return;
}
const slug = positionals[0];
if (!slug) {
console.error(DOCS_HELP);
process.exit(1);
}
// Bail early if the doc doesn't exist in either framework
if (!docExistsInAnyFramework(slug)) {
console.error(`Doc not found: "${slug}".`);
console.error('Run `@videojs/cli docs --list` to see available pages.');
process.exit(1);
}
const framework = await resolveFramework(flags);
const markdown = readBundledDoc(framework, slug);
if (!markdown) {
console.error(`Doc not found: "${slug}" for framework "${framework}".`);
console.error('Run `@videojs/cli docs --list` to see available pages.');
process.exit(1);
}
// Installation page: generate code and replace markers
if (slug === 'how-to/installation') {
const partial = buildPartialFlags(flags, framework);
const needsPrompting =
!partial.preset ||
(!partial.skin && !partial.rawSkin) ||
partial.sourceUrl === undefined ||
!partial.media ||
!partial.installMethod;
if (needsPrompting) {
p.intro('Video.js Installation');
}
const opts = await promptInstallOptions(framework, partial);
if (needsPrompting) {
p.outro('');
}
const validation = validateInstallationOptions(opts);
if (!validation.valid) {
console.error(`Error: ${validation.reason}`);
process.exit(1);
}
const generated = formatInstallationCode(opts);
const output = stripOmitMarkers(replaceMarker(markdown, 'installation', generated));
printVersionHeader();
console.log(output);
return;
}
// Regular doc: print as-is
printVersionHeader();
console.log(stripOmitMarkers(markdown));
}