@mizumi25/cli 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs-generator.js +1302 -536
- package/index.js +866 -325
- package/package.json +1 -1
- package/watcher.js +153 -40
package/package.json
CHANGED
package/watcher.js
CHANGED
|
@@ -1,56 +1,169 @@
|
|
|
1
1
|
// packages/cli/watcher.js
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import Mizumi from '../core/index.js';
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import { createRequire } from 'node:module'
|
|
6
5
|
|
|
6
|
+
// chokidar is CJS — load via createRequire in ESM
|
|
7
|
+
const require = createRequire(import.meta.url)
|
|
8
|
+
let chokidar
|
|
9
|
+
try {
|
|
10
|
+
chokidar = require('chokidar')
|
|
11
|
+
} catch {
|
|
12
|
+
chokidar = null
|
|
13
|
+
}
|
|
7
14
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
// ── DevTools injection helpers ────────────────────────────
|
|
16
|
+
const DEVTOOLS_SCRIPT_TAG = '<script src=".mizumi/mizumi-devtools.js"></script>'
|
|
17
|
+
const DEVTOOLS_COMMENT_TAG = '<!-- mizumi-devtools -->'
|
|
11
18
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Find all .html files in cwd (excluding node_modules / .mizumi)
|
|
21
|
+
*/
|
|
22
|
+
function findHTMLFiles(cwd) {
|
|
23
|
+
const results = []
|
|
24
|
+
function walk(dir) {
|
|
25
|
+
let entries
|
|
26
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }) }
|
|
27
|
+
catch { return }
|
|
28
|
+
for (const e of entries) {
|
|
29
|
+
const full = path.join(dir, e.name)
|
|
30
|
+
if (e.isDirectory()) {
|
|
31
|
+
if (!e.name.startsWith('.') && e.name !== 'node_modules') walk(full)
|
|
32
|
+
} else if (e.isFile() && e.name.endsWith('.html')) {
|
|
33
|
+
results.push(full)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
15
36
|
}
|
|
37
|
+
walk(cwd)
|
|
38
|
+
return results
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Inject the devtools <script> tag into an HTML file if not already present.
|
|
43
|
+
* Inserts just before </body>, or at end of file if </body> not found.
|
|
44
|
+
* Marks injected lines with a comment so we can remove them on exit.
|
|
45
|
+
*/
|
|
46
|
+
function injectDevToolsTag(htmlPath) {
|
|
47
|
+
let html = fs.readFileSync(htmlPath, 'utf8')
|
|
48
|
+
if (html.includes(DEVTOOLS_SCRIPT_TAG)) return // already injected
|
|
16
49
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
50
|
+
const injection = ` ${DEVTOOLS_COMMENT_TAG}\n ${DEVTOOLS_SCRIPT_TAG}\n`
|
|
51
|
+
|
|
52
|
+
if (html.includes('</body>')) {
|
|
53
|
+
html = html.replace('</body>', injection + '</body>')
|
|
54
|
+
} else {
|
|
55
|
+
html = html + '\n' + injection
|
|
56
|
+
}
|
|
20
57
|
|
|
21
|
-
|
|
22
|
-
|
|
58
|
+
fs.writeFileSync(htmlPath, html)
|
|
59
|
+
console.log(` 💉 DevTools injected → ${path.relative(process.cwd(), htmlPath)}`)
|
|
60
|
+
}
|
|
23
61
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
62
|
+
/**
|
|
63
|
+
* Remove the injected devtools tag from an HTML file on watch exit.
|
|
64
|
+
*/
|
|
65
|
+
function removeDevToolsTag(htmlPath) {
|
|
66
|
+
if (!fs.existsSync(htmlPath)) return
|
|
67
|
+
let html = fs.readFileSync(htmlPath, 'utf8')
|
|
68
|
+
if (!html.includes(DEVTOOLS_SCRIPT_TAG)) return
|
|
29
69
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
70
|
+
// Remove the comment line + script line
|
|
71
|
+
html = html
|
|
72
|
+
.replace(/[ \t]*<!-- mizumi-devtools -->\n/g, '')
|
|
73
|
+
.replace(/[ \t]*<script src="\.mizumi\/mizumi-devtools\.js"><\/script>\n?/g, '')
|
|
34
74
|
|
|
35
|
-
|
|
36
|
-
console.error('❌ Watcher error:', err);
|
|
37
|
-
});
|
|
75
|
+
fs.writeFileSync(htmlPath, html)
|
|
38
76
|
}
|
|
39
77
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
78
|
+
/**
|
|
79
|
+
* watch(loadConfig, Mizumi, generateDevToolsScript)
|
|
80
|
+
* Called from cli/index.js after an initial build.
|
|
81
|
+
*/
|
|
82
|
+
export async function watch(loadConfig, Mizumi, generateDevToolsScript) {
|
|
83
|
+
const cwd = process.cwd()
|
|
84
|
+
const configPath = path.join(cwd, 'mizumi.config.js')
|
|
85
|
+
const outDir = path.join(cwd, '.mizumi')
|
|
86
|
+
|
|
87
|
+
if (!chokidar) {
|
|
88
|
+
console.error('❌ chokidar not installed — run: npm install chokidar')
|
|
89
|
+
process.exit(1)
|
|
90
|
+
}
|
|
44
91
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
92
|
+
// ── Write devtools JS and inject into all HTML files ──
|
|
93
|
+
const writeDevTools = async () => {
|
|
94
|
+
try {
|
|
95
|
+
const config = await loadConfig()
|
|
96
|
+
const meta = {
|
|
97
|
+
tokens: config.tokens || {},
|
|
98
|
+
patterns: config.patterns || {},
|
|
99
|
+
animations:config.animations|| {},
|
|
100
|
+
}
|
|
101
|
+
const devtoolsJS = generateDevToolsScript(meta)
|
|
102
|
+
const outPath = path.join(outDir, 'mizumi-devtools.js')
|
|
103
|
+
fs.writeFileSync(outPath, devtoolsJS)
|
|
104
|
+
console.log(`✅ DevTools: ${path.relative(cwd, outPath)}`)
|
|
48
105
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
106
|
+
// Inject into all HTML files
|
|
107
|
+
const htmlFiles = findHTMLFiles(cwd)
|
|
108
|
+
htmlFiles.forEach(injectDevToolsTag)
|
|
109
|
+
if (htmlFiles.length > 0) {
|
|
110
|
+
console.log(` 💮 DevTools active on ${htmlFiles.length} HTML file(s) — open in browser\n`)
|
|
111
|
+
}
|
|
112
|
+
} catch (err) {
|
|
113
|
+
console.error('❌ DevTools build failed:', err.message)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ── Cleanup on exit — strip injected tags from HTML ──
|
|
118
|
+
const cleanup = () => {
|
|
119
|
+
console.log('\n💮 Mizumi: Stopping watch — cleaning up DevTools tags...')
|
|
120
|
+
findHTMLFiles(cwd).forEach(removeDevToolsTag)
|
|
121
|
+
console.log('✅ Done.\n')
|
|
122
|
+
process.exit(0)
|
|
123
|
+
}
|
|
124
|
+
process.on('SIGINT', cleanup)
|
|
125
|
+
process.on('SIGTERM', cleanup)
|
|
126
|
+
|
|
127
|
+
const patterns = [
|
|
128
|
+
configPath,
|
|
129
|
+
path.join(cwd, '**/*.mizu'),
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
console.log('💮 Mizumi watching for changes...')
|
|
133
|
+
console.log(' Watching: mizumi.config.js + **/*.mizu')
|
|
134
|
+
console.log(' DevTools: active in watch mode (auto-removed on Ctrl+C)')
|
|
135
|
+
console.log(' Press Ctrl+C to stop\n')
|
|
136
|
+
|
|
137
|
+
// Write devtools on start
|
|
138
|
+
await writeDevTools()
|
|
139
|
+
|
|
140
|
+
const watcher = chokidar.watch(patterns, {
|
|
141
|
+
persistent: true,
|
|
142
|
+
ignoreInitial: true,
|
|
143
|
+
ignored: /node_modules/,
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
const rebuild = async (filePath) => {
|
|
147
|
+
console.log('\n📝 Changed: ' + path.relative(cwd, filePath) + ' — rebuilding...')
|
|
148
|
+
try {
|
|
149
|
+
const config = await loadConfig()
|
|
150
|
+
const mizumi = new Mizumi(config)
|
|
151
|
+
const outDir_ = path.join(cwd, '.mizumi')
|
|
152
|
+
|
|
153
|
+
// Rebuild CSS + runtime
|
|
154
|
+
mizumi.build(outDir_)
|
|
155
|
+
|
|
156
|
+
// Rebuild devtools with fresh config
|
|
157
|
+
await writeDevTools()
|
|
158
|
+
|
|
159
|
+
console.log('⏰ ' + new Date().toLocaleTimeString() + ' — Ready!\n')
|
|
160
|
+
} catch (err) {
|
|
161
|
+
console.error('❌ Build failed:', err.message)
|
|
162
|
+
console.log(' Fix the error and save again...\n')
|
|
163
|
+
}
|
|
53
164
|
}
|
|
54
|
-
}
|
|
55
165
|
|
|
56
|
-
|
|
166
|
+
watcher.on('change', rebuild)
|
|
167
|
+
watcher.on('add', rebuild)
|
|
168
|
+
watcher.on('error', err => console.error('❌ Watcher error:', err))
|
|
169
|
+
}
|