@mlightcad/cad-simple-viewer-cli 1.6.3 → 1.7.1

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.
@@ -0,0 +1,236 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Recursively scan a directory for .dwg / .dxf files, export each to a
4
+ * multi-file ACEX zip + zoom-extents JPEG via export-html-multi-preview.scr,
5
+ * then arrange outputs in the demo-drawings package layout:
6
+ *
7
+ * <outputDir>/<folder>/
8
+ * drawing.acex.json
9
+ * chunks/
10
+ * preview.jpg
11
+ * drawing.dwg | drawing.dxf
12
+ *
13
+ * Folder names match demo-drawings: the path relative to <inputDir> without
14
+ * extension, with path separators turned into underscores. Same-stem
15
+ * `.dwg`/`.dxf` pairs get a `_dwg` / `_dxf` suffix so they do not collide.
16
+ *
17
+ * Usage (from packages/cad-simple-viewer-cli after build):
18
+ * node examples/batch-export-html-multi-preview.mjs <inputDir> <outputDir>
19
+ */
20
+ import { spawn, spawnSync } from 'node:child_process'
21
+ import { existsSync } from 'node:fs'
22
+ import {
23
+ copyFile,
24
+ mkdir,
25
+ mkdtemp,
26
+ readdir,
27
+ rename,
28
+ rm
29
+ } from 'node:fs/promises'
30
+ import os from 'node:os'
31
+ import path from 'node:path'
32
+ import { fileURLToPath } from 'node:url'
33
+
34
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
35
+ const packageRoot = path.resolve(__dirname, '..')
36
+ const cliJs = path.join(packageRoot, 'dist', 'cli.js')
37
+ const scriptPath = path.join(__dirname, 'export-html-multi-preview.scr')
38
+
39
+ const DRAWING_EXT = new Set(['.dwg', '.dxf'])
40
+
41
+ /** True when `child` is `parent` or a path under it. */
42
+ function isPathInside(parent, child) {
43
+ const rel = path.relative(parent, child)
44
+ return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel))
45
+ }
46
+
47
+ async function collectDrawings(dir, out = [], excludeDir) {
48
+ if (excludeDir && isPathInside(excludeDir, dir)) {
49
+ return out
50
+ }
51
+ const entries = await readdir(dir, { withFileTypes: true })
52
+ for (const entry of entries) {
53
+ const full = path.join(dir, entry.name)
54
+ if (entry.isDirectory()) {
55
+ await collectDrawings(full, out, excludeDir)
56
+ } else if (DRAWING_EXT.has(path.extname(entry.name).toLowerCase())) {
57
+ if (!(excludeDir && isPathInside(excludeDir, full))) {
58
+ out.push(full)
59
+ }
60
+ }
61
+ }
62
+ return out
63
+ }
64
+
65
+ /**
66
+ * demo-drawings-style folder name from a path relative to inputDir.
67
+ * `used` tracks claimed names so same-stem DWG/DXF pairs stay unique.
68
+ */
69
+ function drawingFolderName(inputDir, drawingPath, used) {
70
+ const rel = path.relative(inputDir, drawingPath)
71
+ const ext = path.extname(rel).toLowerCase()
72
+ const withoutExt = ext ? rel.slice(0, -ext.length) : rel
73
+ const base = withoutExt
74
+ .split(/[/\\]/)
75
+ .filter(Boolean)
76
+ .join('_')
77
+ .replace(/[^\p{L}\p{N}._-]+/gu, '_')
78
+ const extTag = ext.replace(/^\./, '') || 'bin'
79
+
80
+ let name = base
81
+ if (used.has(name)) {
82
+ name = `${base}_${extTag}`
83
+ }
84
+ let n = 2
85
+ while (used.has(name)) {
86
+ name = `${base}_${extTag}_${n++}`
87
+ }
88
+ used.add(name)
89
+ return name
90
+ }
91
+
92
+ function runCli(input, outputDir) {
93
+ return new Promise((resolve, reject) => {
94
+ const child = spawn(
95
+ process.execPath,
96
+ [
97
+ cliJs,
98
+ '-i',
99
+ input,
100
+ '-s',
101
+ scriptPath,
102
+ '-o',
103
+ outputDir,
104
+ '--mode',
105
+ 'read'
106
+ ],
107
+ { stdio: 'inherit' }
108
+ )
109
+ child.on('error', reject)
110
+ child.on('exit', code => {
111
+ if (code === 0) resolve()
112
+ else
113
+ reject(
114
+ new Error(
115
+ `cad-simple-viewer-cli exited with code ${code} for ${input}`
116
+ )
117
+ )
118
+ })
119
+ })
120
+ }
121
+
122
+ function unzipWithTar(zipPath, destDir) {
123
+ const result = spawnSync(
124
+ 'tar',
125
+ ['-xf', zipPath, '-C', destDir],
126
+ { encoding: 'utf8' }
127
+ )
128
+ if (result.status !== 0) {
129
+ throw new Error(
130
+ `Failed to unzip ${zipPath}: ${result.stderr || result.stdout || 'tar error'}`
131
+ )
132
+ }
133
+ }
134
+
135
+ async function packageDrawing(workDir, targetDir, sourceDrawing) {
136
+ const entries = await readdir(workDir)
137
+ const zipName = entries.find(name => name.toLowerCase().endsWith('.zip'))
138
+ const jpegName = entries.find(name => {
139
+ const lower = name.toLowerCase()
140
+ return lower.endsWith('.jpg') || lower.endsWith('.jpeg')
141
+ })
142
+ if (!zipName) {
143
+ throw new Error(`No .zip produced in ${workDir}`)
144
+ }
145
+ if (!jpegName) {
146
+ throw new Error(`No .jpg preview produced in ${workDir}`)
147
+ }
148
+
149
+ // Stage under the same parent as targetDir so rename stays on one volume.
150
+ const stageDir = await mkdtemp(
151
+ path.join(path.dirname(targetDir), '.cad-cli-pack-')
152
+ )
153
+ try {
154
+ unzipWithTar(path.join(workDir, zipName), stageDir)
155
+
156
+ await copyFile(
157
+ path.join(workDir, jpegName),
158
+ path.join(stageDir, 'preview.jpg')
159
+ )
160
+
161
+ const ext = path.extname(sourceDrawing).toLowerCase()
162
+ const destName = ext === '.dxf' ? 'drawing.dxf' : 'drawing.dwg'
163
+ await copyFile(sourceDrawing, path.join(stageDir, destName))
164
+
165
+ await rm(targetDir, { recursive: true, force: true })
166
+ await rename(stageDir, targetDir)
167
+ } catch (error) {
168
+ await rm(stageDir, { recursive: true, force: true })
169
+ throw error
170
+ }
171
+ }
172
+
173
+ async function main() {
174
+ const inputDir = path.resolve(process.argv[2] ?? '')
175
+ const outputDir = path.resolve(process.argv[3] ?? '')
176
+
177
+ if (!process.argv[2] || !process.argv[3] || !existsSync(inputDir)) {
178
+ console.error(
179
+ 'Usage: node examples/batch-export-html-multi-preview.mjs <inputDir> <outputDir>'
180
+ )
181
+ process.exitCode = 1
182
+ return
183
+ }
184
+ if (!existsSync(cliJs)) {
185
+ console.error(
186
+ 'CLI not built. Run: pnpm --filter @mlightcad/cad-simple-viewer-cli build'
187
+ )
188
+ process.exitCode = 1
189
+ return
190
+ }
191
+ if (!existsSync(scriptPath)) {
192
+ console.error(`Missing script: ${scriptPath}`)
193
+ process.exitCode = 1
194
+ return
195
+ }
196
+
197
+ await mkdir(outputDir, { recursive: true })
198
+ // Skip anything already under outputDir so a nested out/ does not re-scan
199
+ // packaged drawing.dwg / drawing.dxf copies on later runs.
200
+ const drawings = await collectDrawings(inputDir, [], outputDir)
201
+ if (!drawings.length) {
202
+ console.error(`No .dwg/.dxf files found under ${inputDir}`)
203
+ process.exitCode = 1
204
+ return
205
+ }
206
+
207
+ console.log(
208
+ `Found ${drawings.length} drawing(s). Output (demo-drawings layout): ${outputDir}`
209
+ )
210
+
211
+ const usedFolders = new Set()
212
+ let failed = 0
213
+
214
+ for (const drawing of drawings) {
215
+ const folder = drawingFolderName(inputDir, drawing, usedFolders)
216
+ const targetDir = path.join(outputDir, folder)
217
+ console.log(`\n=== ${drawing} → ${folder}/ ===`)
218
+
219
+ const workDir = await mkdtemp(path.join(os.tmpdir(), 'cad-cli-demo-'))
220
+ try {
221
+ await runCli(drawing, workDir)
222
+ await packageDrawing(workDir, targetDir, drawing)
223
+ console.log(`Packaged ${targetDir}`)
224
+ } catch (error) {
225
+ failed++
226
+ console.error(error instanceof Error ? error.message : String(error))
227
+ } finally {
228
+ await rm(workDir, { recursive: true, force: true })
229
+ }
230
+ }
231
+
232
+ console.log(`\nDone. success=${drawings.length - failed} failed=${failed}`)
233
+ if (failed) process.exitCode = 1
234
+ }
235
+
236
+ await main()
@@ -0,0 +1,18 @@
1
+ ; Zoom extents, export a multi-file ACEX package zip (-chtml Multi),
2
+ ; then export a JPEG preview of the extents view.
3
+ ; Prompts for -chtml: export format / export invisible layers / export layouts /
4
+ ; initial view / viewer mode.
5
+ ; Usage:
6
+ ; cad-simple-viewer-cli -i drawing.dwg -s examples/export-html-multi-preview.scr -o ./out
7
+ zoom
8
+ e
9
+ -chtml
10
+ Multi
11
+ Yes
12
+ Yes
13
+ Extents
14
+ Measure
15
+ jpgout
16
+
17
+ 1024
18
+ quit
@@ -0,0 +1,13 @@
1
+ ; Export a multi-file ACEX package as a zip (-chtml Multi).
2
+ ; Unzip before hosting so the offline viewer can fetch chunks progressively.
3
+ ; Prompts: export format / export invisible layers / export layouts /
4
+ ; initial view / viewer mode.
5
+ ; Usage:
6
+ ; cad-simple-viewer-cli -i drawing.dwg -s examples/export-html-multi.scr -o ./out
7
+ -chtml
8
+ Multi
9
+ Yes
10
+ Yes
11
+ Extents
12
+ Measure
13
+ quit
@@ -1,8 +1,10 @@
1
1
  ; Export a self-contained offline HTML viewer (-chtml).
2
- ; Prompts: export invisible layers / export layouts / initial view / viewer mode.
2
+ ; Prompts: export format / export invisible layers / export layouts /
3
+ ; initial view / viewer mode.
3
4
  ; Usage:
4
5
  ; cad-simple-viewer-cli -i drawing.dwg -s examples/export-html.scr -o ./out
5
6
  -chtml
7
+ Single
6
8
  Yes
7
9
  Yes
8
10
  Extents
@@ -0,0 +1,9 @@
1
+ ; export-pdf.scr
2
+ zoom
3
+ e
4
+ -cpdf
5
+ Extents
6
+ Yes
7
+ Text
8
+
9
+ quit
@@ -47,6 +47,24 @@
47
47
  "inputKind": "file",
48
48
  "runnable": true
49
49
  },
50
+ {
51
+ "id": "export-html-multi",
52
+ "title": "Export HTML (multi-file)",
53
+ "description": "Export a multi-file ACEX package zip for progressive loading (-chtml Multi).",
54
+ "script": "export-html-multi.scr",
55
+ "mode": "read",
56
+ "inputKind": "file",
57
+ "runnable": true
58
+ },
59
+ {
60
+ "id": "export-html-multi-preview",
61
+ "title": "Export HTML multi + preview",
62
+ "description": "Zoom extents, export multi-file ACEX zip (-chtml Multi), then JPEG preview.",
63
+ "script": "export-html-multi-preview.scr",
64
+ "mode": "read",
65
+ "inputKind": "file",
66
+ "runnable": true
67
+ },
50
68
  {
51
69
  "id": "export-dxf",
52
70
  "title": "Export DXF",
@@ -82,6 +100,15 @@
82
100
  "kind": "batch",
83
101
  "inputKind": "directory",
84
102
  "runnable": true
103
+ },
104
+ {
105
+ "id": "batch-export-html-multi-preview",
106
+ "title": "Batch export HTML multi + preview",
107
+ "description": "Scan a folder, export multi ACEX zip + extents preview, package like demo-drawings.",
108
+ "script": "batch-export-html-multi-preview.mjs",
109
+ "kind": "batch",
110
+ "inputKind": "directory",
111
+ "runnable": true
85
112
  }
86
113
  ]
87
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlightcad/cad-simple-viewer-cli",
3
- "version": "1.6.3",
3
+ "version": "1.7.1",
4
4
  "description": "AcCoreConsole-style headless CLI: run .scr command scripts against DXF/DWG drawings",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -42,15 +42,16 @@
42
42
  "script"
43
43
  ],
44
44
  "dependencies": {
45
- "@mlightcad/data-model": "^1.14.2",
46
- "@mlightcad/libredwg-converter": "^3.14.2",
47
- "@mlightcad/mtext-renderer": "^0.12.4",
45
+ "@mlightcad/data-model": "^1.14.8",
46
+ "@mlightcad/libredwg-converter": "^3.14.8",
47
+ "@mlightcad/mtext-renderer": "^0.12.12",
48
48
  "commander": "^12.1.0",
49
49
  "playwright": "^1.49.1",
50
- "@mlightcad/cad-pdf-plugin": "1.6.3",
51
- "@mlightcad/cad-svg-plugin": "1.6.3",
52
- "@mlightcad/cad-simple-viewer": "1.6.3",
53
- "@mlightcad/cad-html-plugin": "1.6.3"
50
+ "@mlightcad/cad-html-plugin": "1.7.1",
51
+ "@mlightcad/cad-simple-viewer": "1.7.1",
52
+ "@mlightcad/cad-pdf-plugin": "1.7.1",
53
+ "@mlightcad/pdf-renderer": "1.7.1",
54
+ "@mlightcad/cad-svg-plugin": "1.7.1"
54
55
  },
55
56
  "devDependencies": {
56
57
  "@types/node": "^20.14.9",