@projektemacher/vivliostyle-batch-cli 0.1.1 → 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/dist/vivliostyle-batch-cli.js +38 -28
- package/package.json +1 -1
- package/src/vivliostyle-cli.ts +46 -28
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Command } from 'commander';
|
|
2
2
|
import { build, preview } from '@vivliostyle/cli';
|
|
3
3
|
import { resolve, dirname, posix, join } from 'node:path';
|
|
4
|
-
import { existsSync, readFileSync, mkdtempSync, writeFileSync, rmSync, statSync } from 'node:fs';
|
|
4
|
+
import { realpathSync, existsSync, readFileSync, mkdtempSync, writeFileSync, rmSync, statSync } from 'node:fs';
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import { tmpdir } from 'node:os';
|
|
7
7
|
import { lookup } from 'mime-types';
|
|
@@ -75,7 +75,10 @@ function applyStaticMounts(app, staticMap, assetBases, dbg) {
|
|
|
75
75
|
}
|
|
76
76
|
else {
|
|
77
77
|
const mountPath = virtual.endsWith("/") ? virtual : `${virtual}/`;
|
|
78
|
-
app.use(mountPath, serveStatic(absLocal, {
|
|
78
|
+
app.use(mountPath, serveStatic(absLocal, {
|
|
79
|
+
fallthrough: true,
|
|
80
|
+
setHeaders: makeMimeHeaders
|
|
81
|
+
}));
|
|
79
82
|
console.log(`[static-server] Dir mount: ${mountPath} → ${absLocal}`);
|
|
80
83
|
}
|
|
81
84
|
dbg("[static-server] mount", { virtual, absLocal });
|
|
@@ -272,7 +275,10 @@ function buildPreviewHtml(inputAbs, assetBases, dbg) {
|
|
|
272
275
|
console.warn(`[buildPreviewHtml] Duplicate assetBase virtual prefix "${mapKey}" — overwriting "${extraStatic[mapKey]}" with "${resolve(ab.localBase)}"`);
|
|
273
276
|
}
|
|
274
277
|
extraStatic[mapKey] = resolve(ab.localBase);
|
|
275
|
-
dbg("buildPreviewHtml: assetBase extra static mount", {
|
|
278
|
+
dbg("buildPreviewHtml: assetBase extra static mount", {
|
|
279
|
+
mapKey,
|
|
280
|
+
local: ab.localBase
|
|
281
|
+
});
|
|
276
282
|
}
|
|
277
283
|
const dom = new JSDOM(content);
|
|
278
284
|
const document = dom.window.document;
|
|
@@ -403,7 +409,10 @@ function urlToStaticMapping(url, htmlDir, assetBases, ignoredAssets, dbg) {
|
|
|
403
409
|
const absoluteMapped = mapAbsoluteUrlToLocal(trimmed, assetBases);
|
|
404
410
|
if (absoluteMapped) {
|
|
405
411
|
if (shouldIgnoreVirtualPath(absoluteMapped.virtualPath, ignoredAssets)) {
|
|
406
|
-
dbg("urlToStaticMapping: ignored (asset-base match)", {
|
|
412
|
+
dbg("urlToStaticMapping: ignored (asset-base match)", {
|
|
413
|
+
url,
|
|
414
|
+
...absoluteMapped
|
|
415
|
+
});
|
|
407
416
|
return {
|
|
408
417
|
kind: "skipped",
|
|
409
418
|
url,
|
|
@@ -422,7 +431,11 @@ function urlToStaticMapping(url, htmlDir, assetBases, ignoredAssets, dbg) {
|
|
|
422
431
|
};
|
|
423
432
|
}
|
|
424
433
|
if (trimmed.startsWith("//")) {
|
|
425
|
-
return {
|
|
434
|
+
return {
|
|
435
|
+
kind: "skipped",
|
|
436
|
+
url,
|
|
437
|
+
reason: "protocol-relative external URL"
|
|
438
|
+
};
|
|
426
439
|
}
|
|
427
440
|
if (/^[a-zA-Z][a-zA-Z0-9+\-.]*:/.test(trimmed)) {
|
|
428
441
|
const protocol = trimmed.slice(0, trimmed.indexOf(":"));
|
|
@@ -434,12 +447,19 @@ function urlToStaticMapping(url, htmlDir, assetBases, ignoredAssets, dbg) {
|
|
|
434
447
|
}
|
|
435
448
|
const [cleanUrl] = trimmed.split(/[?#]/);
|
|
436
449
|
if (!cleanUrl) {
|
|
437
|
-
return {
|
|
450
|
+
return {
|
|
451
|
+
kind: "skipped",
|
|
452
|
+
url,
|
|
453
|
+
reason: "URL is empty after stripping query/fragment"
|
|
454
|
+
};
|
|
438
455
|
}
|
|
439
456
|
const localPath = resolve(htmlDir, cleanUrl);
|
|
440
457
|
const virtualPath = cleanUrl.startsWith("/") ? posix.normalize(cleanUrl) : posix.resolve("/", posix.relative(htmlDir, localPath));
|
|
441
458
|
if (shouldIgnoreVirtualPath(virtualPath, ignoredAssets)) {
|
|
442
|
-
dbg("urlToStaticMapping: ignored (ignore-asset match)", {
|
|
459
|
+
dbg("urlToStaticMapping: ignored (ignore-asset match)", {
|
|
460
|
+
url,
|
|
461
|
+
virtualPath
|
|
462
|
+
});
|
|
443
463
|
return {
|
|
444
464
|
kind: "skipped",
|
|
445
465
|
url,
|
|
@@ -610,7 +630,7 @@ function buildProgram() {
|
|
|
610
630
|
].join("\n"));
|
|
611
631
|
}
|
|
612
632
|
function printHelp() {
|
|
613
|
-
buildProgram().help();
|
|
633
|
+
buildProgram().help({ error: false });
|
|
614
634
|
}
|
|
615
635
|
function parseArgs(argv) {
|
|
616
636
|
const { cliArgv, extraArgs } = splitArgsAtDoubleDash(argv);
|
|
@@ -777,10 +797,7 @@ async function execute(options, extraArgs = []) {
|
|
|
777
797
|
console.log(`[preview] AssetBase static mount: ${virtual} → ${local}`);
|
|
778
798
|
}
|
|
779
799
|
else {
|
|
780
|
-
dbg("preview: skipping assetBase mount shadowed by explicit --static", {
|
|
781
|
-
virtual,
|
|
782
|
-
local
|
|
783
|
-
});
|
|
800
|
+
dbg("preview: skipping assetBase mount shadowed by explicit --static", { virtual, local });
|
|
784
801
|
}
|
|
785
802
|
}
|
|
786
803
|
if (htmlPath !== inputAbs) {
|
|
@@ -841,19 +858,14 @@ async function execute(options, extraArgs = []) {
|
|
|
841
858
|
// ---------------------------------------------------------------------------
|
|
842
859
|
// Direct execution
|
|
843
860
|
// ---------------------------------------------------------------------------
|
|
844
|
-
let _resolvedEntry = null;
|
|
845
|
-
try {
|
|
846
|
-
_resolvedEntry = resolve(fileURLToPath(import.meta.url));
|
|
847
|
-
}
|
|
848
|
-
catch {
|
|
849
|
-
// ESM import.meta.url not available — not a direct invocation
|
|
850
|
-
}
|
|
851
861
|
function isDirectExecution() {
|
|
852
862
|
const entry = process.argv[1];
|
|
853
|
-
if (!entry
|
|
863
|
+
if (!entry)
|
|
854
864
|
return false;
|
|
855
865
|
try {
|
|
856
|
-
|
|
866
|
+
const realEntry = realpathSync(entry);
|
|
867
|
+
const realSelf = realpathSync(fileURLToPath(import.meta.url));
|
|
868
|
+
return realEntry === realSelf;
|
|
857
869
|
}
|
|
858
870
|
catch {
|
|
859
871
|
return false;
|
|
@@ -865,14 +877,12 @@ if (isDirectExecution()) {
|
|
|
865
877
|
}
|
|
866
878
|
const parsed = parseArgs(process.argv);
|
|
867
879
|
if (!parsed) {
|
|
868
|
-
|
|
869
|
-
}
|
|
870
|
-
else {
|
|
871
|
-
execute(parsed.options, parsed.extraArgs).catch((err) => {
|
|
872
|
-
console.error(err instanceof Error ? err.message : err);
|
|
873
|
-
process.exit(1);
|
|
874
|
-
});
|
|
880
|
+
process.exit(0);
|
|
875
881
|
}
|
|
882
|
+
execute(parsed.options, parsed.extraArgs).catch((err) => {
|
|
883
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
884
|
+
process.exit(1);
|
|
885
|
+
});
|
|
876
886
|
}
|
|
877
887
|
|
|
878
888
|
export { buildPreviewHtml, execute, extractUrlsFromHtml, isHtmlInput, mapAbsoluteUrlToLocal, normalizeIgnoreAssetPath, normalizeUrlBase, parseArgs, parseAssetBaseMapping, parseExtraArgs, parseStaticMapping, prepareInputHtmlForBuild, printHelp, rewriteAbsoluteUrls, rewriteAbsoluteUrlsInDom, rewriteVirtualPathsToServer, rewriteVirtualPathsToServerInDom, shouldIgnoreVirtualPath, splitArgsAtDoubleDash, startStaticServer, urlToStaticMapping };
|
package/package.json
CHANGED
package/src/vivliostyle-cli.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
import { build, preview } from "@vivliostyle/cli";
|
|
3
3
|
import { resolve, dirname, posix, join } from "node:path";
|
|
4
|
-
import { readFileSync, existsSync, mkdtempSync, writeFileSync, rmSync, statSync } from "node:fs";
|
|
4
|
+
import { readFileSync, existsSync, mkdtempSync, writeFileSync, rmSync, statSync, realpathSync } from "node:fs";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
7
7
|
import { AddressInfo } from "node:net";
|
|
@@ -128,7 +128,13 @@ function applyStaticMounts(app: express.Express, staticMap: Record<string, strin
|
|
|
128
128
|
console.log(`[static-server] File route: GET ${virtual} → ${absLocal}`);
|
|
129
129
|
} else {
|
|
130
130
|
const mountPath = virtual.endsWith("/") ? virtual : `${virtual}/`;
|
|
131
|
-
app.use(
|
|
131
|
+
app.use(
|
|
132
|
+
mountPath,
|
|
133
|
+
serveStatic(absLocal, {
|
|
134
|
+
fallthrough: true,
|
|
135
|
+
setHeaders: makeMimeHeaders
|
|
136
|
+
})
|
|
137
|
+
);
|
|
132
138
|
console.log(`[static-server] Dir mount: ${mountPath} → ${absLocal}`);
|
|
133
139
|
}
|
|
134
140
|
dbg("[static-server] mount", { virtual, absLocal });
|
|
@@ -356,7 +362,10 @@ export function buildPreviewHtml(
|
|
|
356
362
|
);
|
|
357
363
|
}
|
|
358
364
|
extraStatic[mapKey] = resolve(ab.localBase);
|
|
359
|
-
dbg("buildPreviewHtml: assetBase extra static mount", {
|
|
365
|
+
dbg("buildPreviewHtml: assetBase extra static mount", {
|
|
366
|
+
mapKey,
|
|
367
|
+
local: ab.localBase
|
|
368
|
+
});
|
|
360
369
|
}
|
|
361
370
|
|
|
362
371
|
const dom = new JSDOM(content);
|
|
@@ -519,7 +528,10 @@ export function urlToStaticMapping(
|
|
|
519
528
|
const absoluteMapped = mapAbsoluteUrlToLocal(trimmed, assetBases);
|
|
520
529
|
if (absoluteMapped) {
|
|
521
530
|
if (shouldIgnoreVirtualPath(absoluteMapped.virtualPath, ignoredAssets)) {
|
|
522
|
-
dbg("urlToStaticMapping: ignored (asset-base match)", {
|
|
531
|
+
dbg("urlToStaticMapping: ignored (asset-base match)", {
|
|
532
|
+
url,
|
|
533
|
+
...absoluteMapped
|
|
534
|
+
});
|
|
523
535
|
return {
|
|
524
536
|
kind: "skipped",
|
|
525
537
|
url,
|
|
@@ -541,7 +553,11 @@ export function urlToStaticMapping(
|
|
|
541
553
|
}
|
|
542
554
|
|
|
543
555
|
if (trimmed.startsWith("//")) {
|
|
544
|
-
return {
|
|
556
|
+
return {
|
|
557
|
+
kind: "skipped",
|
|
558
|
+
url,
|
|
559
|
+
reason: "protocol-relative external URL"
|
|
560
|
+
};
|
|
545
561
|
}
|
|
546
562
|
|
|
547
563
|
if (/^[a-zA-Z][a-zA-Z0-9+\-.]*:/.test(trimmed)) {
|
|
@@ -555,14 +571,21 @@ export function urlToStaticMapping(
|
|
|
555
571
|
|
|
556
572
|
const [cleanUrl] = trimmed.split(/[?#]/);
|
|
557
573
|
if (!cleanUrl) {
|
|
558
|
-
return {
|
|
574
|
+
return {
|
|
575
|
+
kind: "skipped",
|
|
576
|
+
url,
|
|
577
|
+
reason: "URL is empty after stripping query/fragment"
|
|
578
|
+
};
|
|
559
579
|
}
|
|
560
580
|
|
|
561
581
|
const localPath = resolve(htmlDir, cleanUrl);
|
|
562
582
|
const virtualPath = cleanUrl.startsWith("/") ? posix.normalize(cleanUrl) : posix.resolve("/", posix.relative(htmlDir, localPath));
|
|
563
583
|
|
|
564
584
|
if (shouldIgnoreVirtualPath(virtualPath, ignoredAssets)) {
|
|
565
|
-
dbg("urlToStaticMapping: ignored (ignore-asset match)", {
|
|
585
|
+
dbg("urlToStaticMapping: ignored (ignore-asset match)", {
|
|
586
|
+
url,
|
|
587
|
+
virtualPath
|
|
588
|
+
});
|
|
566
589
|
return {
|
|
567
590
|
kind: "skipped",
|
|
568
591
|
url,
|
|
@@ -582,7 +605,10 @@ export function urlToStaticMapping(
|
|
|
582
605
|
return { kind: "mapped", mapping: `${virtualPath}:${localPath}` };
|
|
583
606
|
}
|
|
584
607
|
|
|
585
|
-
export function parseStaticMapping(mapping: string): {
|
|
608
|
+
export function parseStaticMapping(mapping: string): {
|
|
609
|
+
virtual: string;
|
|
610
|
+
local: string;
|
|
611
|
+
} {
|
|
586
612
|
if (!mapping.startsWith("/")) {
|
|
587
613
|
throw new Error(`Invalid --static mapping: "${mapping}"\nExpected format: /virtual/path:/local/path`);
|
|
588
614
|
}
|
|
@@ -773,7 +799,7 @@ function buildProgram(): Command {
|
|
|
773
799
|
}
|
|
774
800
|
|
|
775
801
|
export function printHelp(): void {
|
|
776
|
-
buildProgram().help();
|
|
802
|
+
buildProgram().help({ error: false });
|
|
777
803
|
}
|
|
778
804
|
|
|
779
805
|
export function parseArgs(argv: string[]): {
|
|
@@ -975,10 +1001,7 @@ export async function execute(options: CliOptions, extraArgs: string[] = []): Pr
|
|
|
975
1001
|
previewStaticMap[virtual] = local;
|
|
976
1002
|
console.log(`[preview] AssetBase static mount: ${virtual} → ${local}`);
|
|
977
1003
|
} else {
|
|
978
|
-
dbg("preview: skipping assetBase mount shadowed by explicit --static", {
|
|
979
|
-
virtual,
|
|
980
|
-
local
|
|
981
|
-
});
|
|
1004
|
+
dbg("preview: skipping assetBase mount shadowed by explicit --static", { virtual, local });
|
|
982
1005
|
}
|
|
983
1006
|
}
|
|
984
1007
|
|
|
@@ -1050,18 +1073,13 @@ export async function execute(options: CliOptions, extraArgs: string[] = []): Pr
|
|
|
1050
1073
|
// Direct execution
|
|
1051
1074
|
// ---------------------------------------------------------------------------
|
|
1052
1075
|
|
|
1053
|
-
let _resolvedEntry: string | null = null;
|
|
1054
|
-
try {
|
|
1055
|
-
_resolvedEntry = resolve(fileURLToPath(import.meta.url));
|
|
1056
|
-
} catch {
|
|
1057
|
-
// ESM import.meta.url not available — not a direct invocation
|
|
1058
|
-
}
|
|
1059
|
-
|
|
1060
1076
|
function isDirectExecution(): boolean {
|
|
1061
1077
|
const entry = process.argv[1];
|
|
1062
|
-
if (!entry
|
|
1078
|
+
if (!entry) return false;
|
|
1063
1079
|
try {
|
|
1064
|
-
|
|
1080
|
+
const realEntry = realpathSync(entry);
|
|
1081
|
+
const realSelf = realpathSync(fileURLToPath(import.meta.url));
|
|
1082
|
+
return realEntry === realSelf;
|
|
1065
1083
|
} catch {
|
|
1066
1084
|
return false;
|
|
1067
1085
|
}
|
|
@@ -1075,11 +1093,11 @@ if (isDirectExecution()) {
|
|
|
1075
1093
|
const parsed = parseArgs(process.argv);
|
|
1076
1094
|
|
|
1077
1095
|
if (!parsed) {
|
|
1078
|
-
|
|
1079
|
-
} else {
|
|
1080
|
-
execute(parsed.options, parsed.extraArgs).catch((err) => {
|
|
1081
|
-
console.error(err instanceof Error ? err.message : err);
|
|
1082
|
-
process.exit(1);
|
|
1083
|
-
});
|
|
1096
|
+
process.exit(0);
|
|
1084
1097
|
}
|
|
1098
|
+
|
|
1099
|
+
execute(parsed.options, parsed.extraArgs).catch((err: unknown) => {
|
|
1100
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
1101
|
+
process.exit(1);
|
|
1102
|
+
});
|
|
1085
1103
|
}
|