Skip to content

Commit 15e6afc

Browse files
committed
Improve reading browserPath and css-doodle.
1 parent c398fc1 commit 15e6afc

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

src/preview/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { spawn } from 'node:child_process';
33

44
import puppeteer from 'puppeteer';
55

6-
import { previewServerPath, defaultAppArgs, config } from '../static.js';
6+
import { previewServerPath, getBrowserPath, defaultAppArgs, config } from '../static.js';
77

88
const SIZE = '600,628';
99

@@ -47,13 +47,14 @@ export function preview(sourceFile, title, options = {}) {
4747
],
4848
};
4949

50-
if (config.browserPath) {
51-
settings.executablePath = config.browserPath;
50+
let browserPath = getBrowserPath();
51+
if (browserPath) {
52+
settings.executablePath = browserPath;
5253
}
5354

5455
puppeteer.launch(settings)
5556
.catch(e => {
56-
console.log(e.message);
57+
console.error(e.message);
5758
serverProcess.kill();
5859
process.exit(1);
5960
});

src/preview/server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { readFileSync } from 'node:fs';
22
import { createServer } from 'http';
33
import { WebSocketServer } from 'ws';
44
import watch from 'node-watch';
5-
import { previewClient, cssDoodleLib } from '../static.js';
5+
import { previewClient, getCssDoodleLib } from '../static.js';
66

77
const sourceFile = process.argv[2];
8+
const cssDoodleLib = getCssDoodleLib();
89
const timers = {};
910

1011
if (!sourceFile) {

src/render.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import puppeteer from 'puppeteer';
2-
import { config, cssDoodleLib, defaultAppArgs } from './static.js';
2+
import { config, getCssDoodleLib, getBrowserPath, defaultAppArgs } from './static.js';
33

44
export async function render(code, options = {}) {
55
options.selector ??= 'css-doodle';
@@ -8,14 +8,15 @@ export async function render(code, options = {}) {
88
args: defaultAppArgs
99
};
1010

11-
if (config.browserPath) {
12-
settings.executablePath = config.browserPath;
11+
let browserPath = getBrowserPath();
12+
if (browserPath) {
13+
settings.executablePath = browserPath;
1314
}
1415

1516
const browser = await puppeteer.launch(settings);
1617

1718
const page = await browser.newPage();
18-
await page.setContent(buildHTML(code, cssDoodleLib, options), { waitUntil: 'networkidle0', timeout: 5000 });
19+
await page.setContent(buildHTML(code, getCssDoodleLib(), options), { waitUntil: 'networkidle0', timeout: 5000 });
1920

2021
const data = await page.$eval(options.selector, el => {
2122
return {

src/static.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,38 @@ function getDefaultAppArgs() {
4040
return args;
4141
}
4242

43+
export function getCssDoodleLib() {
44+
const libPath = config['css-doodle'];
45+
if (libPath) {
46+
if (fs.existsSync(libPath)) {
47+
return fs.readFileSync(libPath, 'utf8');
48+
} else {
49+
console.warn(`[warn] css-doodle not found: ${libPath}. Use default css-doodle instead.`);
50+
console.info('[info] Please check it with `config` command.\n');
51+
}
52+
}
53+
return read('../node_modules/css-doodle/css-doodle.min.js');
54+
}
55+
56+
export function getBrowserPath() {
57+
const browserPath = config['browserPath']
58+
|| config['browser-path']
59+
|| config['executablePath']
60+
|| config['executable-path'];
61+
if (browserPath) {
62+
if (fs.existsSync(browserPath)) {
63+
return browserPath;
64+
} else {
65+
console.warn(`[warn] Browser not found: ${browserPath}. Use default browser instead.`);
66+
console.info('[info] Please check it with `config` command.\n');
67+
}
68+
}
69+
return '';
70+
}
71+
4372
export const config = JSON.parse(fs.readFileSync(configFilePath), 'utf8');
4473
export const configPath = configFilePath;
45-
4674
export const pkg = JSON.parse(read('../package.json'));
47-
export const cssDoodleLib = read('../node_modules/css-doodle/css-doodle.min.js');
4875
export const previewClient = read('./preview/client.html');
4976
export const previewServerPath = join(root, './preview/server.js');
5077
export const defaultAppArgs = getDefaultAppArgs();

0 commit comments

Comments
 (0)