@cs-object/cli 0.1.0

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,166 @@
1
+ import {
2
+ UsageError,
3
+ parseCliArgs
4
+ } from "./chunk-OYQDZWT7.js";
5
+ import {
6
+ stringifyJson
7
+ } from "./chunk-MOM6Q2VG.js";
8
+
9
+ // src/development.ts
10
+ import { spawnSync } from "child_process";
11
+ import {
12
+ existsSync,
13
+ mkdirSync,
14
+ readFileSync,
15
+ writeFileSync,
16
+ renameSync,
17
+ rmSync
18
+ } from "fs";
19
+ import { randomUUID } from "crypto";
20
+ import { createRequire } from "module";
21
+ import { dirname, join, basename } from "path";
22
+ import { chromium } from "playwright";
23
+ import * as React from "react";
24
+ import { renderToStaticMarkup } from "react-dom/server";
25
+ import { FormulaSheet } from "@cs-object/react";
26
+ import {
27
+ CalculationSourceObjectSchema,
28
+ createSheetFromCalculationSourceObject
29
+ } from "@cs-object/core";
30
+ var defaultCssPath = createRequire(import.meta.url).resolve(
31
+ "@cs-object/react/style.css"
32
+ );
33
+ var escapeHtml = (value) => value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
34
+ function isolatedEnvironment() {
35
+ const environment = { ...process.env };
36
+ Reflect.deleteProperty(environment, "NODE_PATH");
37
+ Reflect.deleteProperty(environment, "PYTHONPATH");
38
+ Reflect.deleteProperty(environment, "PYTHONHOME");
39
+ return environment;
40
+ }
41
+ function writeAtomically(outPath, bytes) {
42
+ mkdirSync(dirname(outPath), { recursive: true });
43
+ const temporary = join(
44
+ dirname(outPath),
45
+ `.${basename(outPath)}.${randomUUID()}.tmp`
46
+ );
47
+ try {
48
+ writeFileSync(temporary, bytes, { flag: "wx" });
49
+ renameSync(temporary, outPath);
50
+ } finally {
51
+ rmSync(temporary, { force: true });
52
+ }
53
+ }
54
+ var runPythonExporter = ({
55
+ functionName,
56
+ inputs,
57
+ sourcePath
58
+ }) => {
59
+ const result = spawnSync(
60
+ process.env.PYTHON ?? "python3",
61
+ [
62
+ "-I",
63
+ "-m",
64
+ "cso_python",
65
+ "export",
66
+ sourcePath,
67
+ "--function",
68
+ functionName,
69
+ "--inputs-json",
70
+ stringifyJson(inputs)
71
+ ],
72
+ {
73
+ encoding: "utf8",
74
+ env: isolatedEnvironment(),
75
+ maxBuffer: 32 * 1024 * 1024
76
+ }
77
+ );
78
+ if (result.error) {
79
+ throw result.error;
80
+ }
81
+ if (result.status !== 0) {
82
+ const message = result.stderr.trim() || "Python exporter failed";
83
+ if (result.status === 2) throw new UsageError(message);
84
+ throw new Error(message);
85
+ }
86
+ if (result.stderr) {
87
+ process.stderr.write(result.stderr);
88
+ }
89
+ return CalculationSourceObjectSchema.parse(JSON.parse(result.stdout));
90
+ };
91
+ var renderFormulaSheetHtml = (sheet, { css, title }) => {
92
+ const sheetMarkup = renderToStaticMarkup(
93
+ React.createElement(FormulaSheet, { sheet })
94
+ );
95
+ const safeTitle = escapeHtml(title);
96
+ return [
97
+ "<!doctype html>",
98
+ '<html lang="en">',
99
+ "<head>",
100
+ '<meta charset="utf-8" />',
101
+ `<title>${safeTitle}</title>`,
102
+ "<style>",
103
+ css,
104
+ "</style>",
105
+ "</head>",
106
+ '<body class="formula-sheet-printing">',
107
+ '<div data-formula-sheet-print-root="true">',
108
+ sheetMarkup,
109
+ "</div>",
110
+ "</body>",
111
+ "</html>"
112
+ ].join("");
113
+ };
114
+ var renderPdfFromHtml = async (html, outPath) => {
115
+ mkdirSync(dirname(outPath), { recursive: true });
116
+ const browser = await chromium.launch({ headless: true });
117
+ try {
118
+ const page = await browser.newPage();
119
+ await page.setContent(html, { waitUntil: "networkidle" });
120
+ await page.emulateMedia({ media: "print" });
121
+ await page.evaluate(() => document.fonts.ready);
122
+ const pdf = await page.pdf({
123
+ format: "A4",
124
+ margin: {
125
+ top: "0",
126
+ right: "0",
127
+ bottom: "0",
128
+ left: "0"
129
+ },
130
+ printBackground: true
131
+ });
132
+ writeAtomically(outPath, pdf);
133
+ } finally {
134
+ await browser.close();
135
+ }
136
+ };
137
+ var readCompiledFormulaSheetCss = () => {
138
+ if (!existsSync(defaultCssPath)) {
139
+ throw new Error(
140
+ `Compiled stylesheet does not exist at ${defaultCssPath}. Run npm run build:css first.`
141
+ );
142
+ }
143
+ return readFileSync(defaultCssPath, "utf8");
144
+ };
145
+ var renderAnnotatedPythonPdf = async (options) => {
146
+ const source = runPythonExporter(options);
147
+ const calculation = createSheetFromCalculationSourceObject(source, {
148
+ id: source.source.id,
149
+ label: source.title
150
+ });
151
+ const html = renderFormulaSheetHtml(calculation.sheet, {
152
+ css: readCompiledFormulaSheetCss(),
153
+ title: source.title
154
+ });
155
+ await renderPdfFromHtml(html, options.outPath);
156
+ };
157
+ export {
158
+ isolatedEnvironment,
159
+ parseCliArgs,
160
+ readCompiledFormulaSheetCss,
161
+ renderAnnotatedPythonPdf,
162
+ renderFormulaSheetHtml,
163
+ renderPdfFromHtml,
164
+ runPythonExporter,
165
+ writeAtomically
166
+ };
@@ -0,0 +1,9 @@
1
+ import {
2
+ documentCommand
3
+ } from "./chunk-UCPKZ67Z.js";
4
+ import "./chunk-7XCJ6AF3.js";
5
+ import "./chunk-KYWIVB2V.js";
6
+ import "./chunk-MOM6Q2VG.js";
7
+ export {
8
+ documentCommand as htmlCommand
9
+ };
@@ -0,0 +1,9 @@
1
+ import {
2
+ documentCommand
3
+ } from "./chunk-UCPKZ67Z.js";
4
+ import "./chunk-7XCJ6AF3.js";
5
+ import "./chunk-KYWIVB2V.js";
6
+ import "./chunk-MOM6Q2VG.js";
7
+ export {
8
+ documentCommand as pdfCommand
9
+ };
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@cs-object/cli",
3
+ "version": "0.1.0",
4
+ "license": "Apache-2.0",
5
+ "type": "module",
6
+ "description": "Installed calculation verification and development export/render commands.",
7
+ "bin": {
8
+ "cso": "./dist/cli.js"
9
+ },
10
+ "exports": {
11
+ ".": "./dist/development.js",
12
+ "./package.json": "./package.json"
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md",
17
+ "LICENSE",
18
+ "NOTICE"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup",
22
+ "prepack": "npm run build",
23
+ "typecheck": "tsc --noEmit --pretty false",
24
+ "lint": "biome lint src tests",
25
+ "test": "vitest run --config vitest.config.ts",
26
+ "pretest": "npm run build"
27
+ },
28
+ "dependencies": {
29
+ "@cs-object/core": "0.1.0",
30
+ "@cs-object/react": "0.1.0",
31
+ "playwright": "^1.63.0",
32
+ "react": "^19.3.0",
33
+ "react-dom": "^19.3.0"
34
+ },
35
+ "overrides": {
36
+ "esbuild": "0.28.2"
37
+ },
38
+ "devDependencies": {
39
+ "tsup": "^8.5.1",
40
+ "typescript": "^5",
41
+ "vitest": "^5.0.1",
42
+ "@types/node": "^26",
43
+ "@types/react": "^19",
44
+ "@types/react-dom": "^19",
45
+ "@biomejs/biome": "2.5.14"
46
+ },
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/viktar-b/CalculationSourceObject.git",
50
+ "directory": "packages/cso-cli"
51
+ },
52
+ "homepage": "https://github.com/viktar-b/CalculationSourceObject#readme",
53
+ "bugs": {
54
+ "url": "https://github.com/viktar-b/CalculationSourceObject/issues"
55
+ },
56
+ "publishConfig": {
57
+ "access": "public",
58
+ "registry": "https://registry.npmjs.org/"
59
+ }
60
+ }