Skip to content

Commit a4d931a

Browse files
author
elonz
committed
script: watch typora themes
Change-Id: I14d573c268388174c63b4cb402ce7d8f4dafdc4f
1 parent adc95c9 commit a4d931a

File tree

10 files changed

+4828
-4
lines changed

10 files changed

+4828
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,4 @@ out
119119
.yarn/build-state.yml
120120
.yarn/install-state.gz
121121
.pnp.*
122+
static/typora.css

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"watch": "npm run clean && node scripts/watch.js",
8-
"clean": "node scripts/clean.js",
8+
"typora-watch": "npm run clean && node scripts/typora/watch.js",
9+
"clean": "rm -rf static/*.css static/*.map",
910
"compile": "node scripts/compile.js",
1011
"gallery-watch": "rm -rf dist/gallery && node scripts/gallery/index.js watch",
1112
"gallery-build": "rm -rf dist/gallery && node scripts/gallery/index.js build"

scripts/clean.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// 已经没用了
2+
3+
14
const path = require('path');
25
const fs = require('fs')
36

scripts/compile.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ const platformConfig = {
2222
},
2323
juejin: {
2424
namer: (filename) =>
25-
`${path.parse(filename).name.replace(/^(mweb-)/, "")}.css`, // 替换 mweb- 前缀
26-
postcss: (css) => setContainerSelector({ css, selector: ".markdown-body" }),
25+
`${path.parse(filename).name.replace(/^mweb/, "juejin")}.css`, // 替换 mweb- 前缀为 juejin-
2726
},
2827
typora: {
2928
namer: (filename) =>
30-
`${path.parse(filename).name.replace(/^(mweb-)/, "")}.css`,
29+
`${path.parse(filename).name.replace(/^mweb/, "typora")}.css`, // 替换 mweb- 前缀为 typora-
3130
postcss: async (css) =>
3231
wrapSelector({
3332
css: await setContainerSelector({ css, selector: "#write" }),

scripts/typora/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const path = require("path")
2+
3+
const toRootPrefix = "../../";
4+
const fromRoot = (pathFromRoot) => toRootPrefix + pathFromRoot;
5+
const filePath = (file) => path.join(__dirname, `${file}`);
6+
const isExisted = async function (path) {
7+
return new Promise((resolve, reject) => {
8+
fs.access(path, (err) => {
9+
err ? reject(false) : resolve(true);
10+
});
11+
}).catch(err => err);
12+
};
13+
14+
module.exports = {
15+
fromRoot,
16+
filePath,
17+
isExisted
18+
};

scripts/typora/watch.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env node
2+
3+
const koa = require('koa');
4+
const koaRouter = require('koa-router');
5+
const views = require('koa-views');
6+
const statics = require('koa-static');
7+
const http = require('http');
8+
const socketio = require('socket.io');
9+
const path = require('path');
10+
const child_process = require('child_process');
11+
const chokidar = require('chokidar');
12+
const { fromRoot, filePath, isExisted } = require('./utils')
13+
14+
const defaultThemePath = fromRoot('src/themes/typora-lark.scss')
15+
const outputPath = fromRoot('static/typora.css')
16+
const watchPath = fromRoot('static/*.css')
17+
const staticDir = fromRoot('static')
18+
const indexPath = fromRoot("src/views/typora")
19+
20+
// 实例化 koa 和路由
21+
const app = new koa();
22+
const router = new koaRouter();
23+
24+
let socketItem;
25+
26+
async function run(args) {
27+
let fileName
28+
let tempFilePath = '';
29+
if (args.length > 0) {
30+
fileName = fromRoot(args[0])
31+
console.log(`当前使用主题:${fileName}`);
32+
} else {
33+
fileName = defaultThemePath
34+
console.log(`当前使用主题:${fileName}`);
35+
console.log('如需指定主题文件,请执行:\x1B[96mnpm run dev <theme_file_path>\x1B[39m\n');
36+
}
37+
38+
tempFilePath = path.resolve(__dirname, fileName);
39+
if (!await isExisted(tempFilePath)) {
40+
console.error('未能读取正确的样式文件,请检查文件名和路径是否正确');
41+
return;
42+
}
43+
44+
watchFile(tempFilePath);
45+
koaServer();
46+
47+
console.log('\x1B[32m监听主题文件更改中...\x1B[39m\n');
48+
49+
const server = http.createServer(app.callback());
50+
const io = socketio(server);
51+
io.on('connection', socket => { socketItem = socket; });
52+
53+
server.listen(3000, () => { console.log('在浏览器中打开: \x1B[96mhttp://localhost:3000\x1B[39m'); });
54+
}
55+
56+
function watchFile(file) {
57+
58+
// 启用子线程执行 scss 监听
59+
const execCmd = `npx sass --watch ${file}:${path.resolve(__dirname, outputPath)}`;
60+
console.log(execCmd)
61+
child_process.exec(execCmd);
62+
63+
// 监听模板文件变化
64+
chokidar
65+
.watch([filePath(watchPath)], {})
66+
.on('change', _ => { app.emit('change'); });
67+
}
68+
69+
async function koaServer() {
70+
let changing = false;
71+
72+
// 静态文件服务 & 模板服务
73+
app.use(statics(filePath(staticDir)));
74+
app.use(views(filePath('./'), { map: { html: 'ejs' } }));
75+
76+
// 路由
77+
router.get('/', async ctx => ctx.render(indexPath));
78+
79+
app.on('change', async () => {
80+
if (!changing) {
81+
socketItem && socketItem.emit('reload');
82+
changing = true;
83+
let st = setTimeout(() => {
84+
changing = false;
85+
clearTimeout(st);
86+
}, 2000);
87+
}
88+
});
89+
90+
app.use(router.routes());
91+
app.use(router.allowedMethods());
92+
}
93+
94+
run(process.argv.slice(2));

src/views/typora.html

Lines changed: 25 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)