Skip to content

Commit 213f19b

Browse files
Divyapahuja31DIVYA PAHUJA
andauthored
feat(cli): add stdin support and list commands (#1241)
Co-authored-by: DIVYA PAHUJA <divyapahuja@DIVYAs-MacBook-Pro.local>
1 parent 142d55c commit 213f19b

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

packages/cli/src/cli.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,59 @@ export async function run(
4747
.option('--theme <theme>', 'Color theme to use', { default: 'vitesse-dark' })
4848
.option('--lang <lang>', 'Programming language')
4949
.option('--format <format>', 'Output format (ansi, html)', { default: 'ansi' })
50+
.option('--list-themes', 'List all available themes')
51+
.option('--list-langs', 'List all available languages')
5052
.help()
5153
.version(version)
5254

5355
const { options, args } = cli.parse(argv)
56+
57+
if (options.listThemes) {
58+
const { bundledThemes } = await import('shiki')
59+
for (const theme of Object.keys(bundledThemes))
60+
log(theme)
61+
return
62+
}
63+
64+
if (options.listLangs) {
65+
const { bundledLanguages } = await import('shiki')
66+
for (const lang of Object.keys(bundledLanguages))
67+
log(lang)
68+
return
69+
}
70+
5471
const files = args
5572

73+
if (files.length === 0) {
74+
// If no files provided, verify if we are in a TTY environment
75+
// If NOT in TTY (piped), read from stdin
76+
// If in TTY, show help
77+
if (!process.stdin.isTTY) {
78+
const content = await new Promise<string>((resolve, reject) => {
79+
let data = ''
80+
process.stdin.on('data', chunk => data += chunk)
81+
process.stdin.on('end', () => resolve(data))
82+
process.stdin.on('error', reject)
83+
})
84+
85+
const lang = options.lang || 'text'
86+
if (options.format === 'html') {
87+
const { codeToHtml } = await import('shiki')
88+
log(await codeToHtml(content, {
89+
lang: lang as BundledLanguage,
90+
theme: options.theme,
91+
}))
92+
}
93+
else {
94+
log(await codeToANSI(content, lang as BundledLanguage, options.theme))
95+
}
96+
return
97+
}
98+
99+
cli.outputHelp()
100+
return
101+
}
102+
56103
const codes = await Promise.all(files.map(async (path) => {
57104
const { content, ext } = await readSource(path)
58105
const lang = options.lang || ext

packages/cli/test/cli.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,22 @@ describe('run', () => {
143143

144144
vi.unstubAllGlobals()
145145
})
146+
147+
it('--list-themes', async () => {
148+
const output: string[] = []
149+
await run(['node', 'shiki', '--list-themes'], msg => output.push(msg))
150+
151+
expect(output.length).toBeGreaterThan(0)
152+
expect(output).toContain('vitesse-dark')
153+
expect(output).toContain('nord')
154+
})
155+
156+
it('--list-langs', async () => {
157+
const output: string[] = []
158+
await run(['node', 'shiki', '--list-langs'], msg => output.push(msg))
159+
160+
expect(output.length).toBeGreaterThan(0)
161+
expect(output).toContain('javascript')
162+
expect(output).toContain('python')
163+
})
146164
})

0 commit comments

Comments
 (0)