Skip to content

Commit 777c804

Browse files
committed
Support preview from stdin input
1 parent 15c0f74 commit 777c804

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

bin/handler.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ export async function handlePreview(source, options) {
5353
console.error(error.message);
5454
process.exit(1);
5555
} else {
56-
let title = path.basename(source);
57-
preview(source, title, options);
56+
if (source === undefined) {
57+
options.fromStdin = true;
58+
preview(content, 'css-doodle', options);
59+
} else {
60+
let title = path.basename(source);
61+
preview(source, title, options);
62+
}
5863
}
5964
}
6065

bin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ program
4949
program
5050
.command('preview')
5151
.description('Open a window to preview the css-doodle file')
52-
.argument('<source>', 'css-doodle source file to preview')
52+
.argument('[source]', 'css-doodle source file to preview')
5353
.option('--fullscreen', 'open the preview in fullscreen mode')
5454
.action(handlePreview);
5555

src/preview/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import crypto from 'node:crypto';
2+
import { tmpdir } from 'node:os';
3+
import { join } from 'node:path';
4+
import { writeFileSync } from 'node:fs';
25
import { spawn } from 'node:child_process';
36

47
import puppeteer from 'puppeteer';
@@ -8,6 +11,17 @@ import { previewServerPath, getBrowserPath, defaultAppArgs, config } from '../st
811
const SIZE = '600,628';
912

1013
export function preview(sourceFile, title, options = {}) {
14+
let source = sourceFile;
15+
if (options.fromStdin) {
16+
sourceFile = join(tmpdir(), crypto.randomUUID());
17+
try {
18+
writeFileSync(sourceFile, source);
19+
} catch (e) {
20+
console.error('error: failed to create a temporary file for preview.');
21+
process.exit(1);
22+
}
23+
}
24+
1125
const serverProcess = spawn('node', [previewServerPath, sourceFile], {
1226
detached: true,
1327
stdio: ['pipe', 'pipe', 'pipe', 'ipc']

0 commit comments

Comments
 (0)