@timefoldai/viz-cli 0.0.0-stage → 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.
package/dist/vite.mjs ADDED
@@ -0,0 +1,89 @@
1
+ import { declaredLayerOrder } from "./layer-order.mjs";
2
+ import path from "node:path";
3
+ import tailwindcss from "@tailwindcss/vite";
4
+ import react from "@vitejs/plugin-react";
5
+ import { createLogger } from "vite";
6
+ // src/vite.ts
7
+ function vendorChunkName(id) {
8
+ if (!id.includes("node_modules")) return;
9
+ if (id.includes("@timefoldai/viz-")) return;
10
+ if (id.includes("@js-joda")) return "js-joda";
11
+ if (id.includes("vis-timeline")) return "vis-timeline";
12
+ if (id.includes("vis-data")) return "vis-data";
13
+ if (id.includes("maplibre-gl")) return "maplibre-gl";
14
+ return "vendor";
15
+ }
16
+ function declareLayerOrderFirst(appDir, themeStylesheet) {
17
+ const statement = declaredLayerOrder(appDir, themeStylesheet);
18
+ return {
19
+ name: "declare-layer-order-first",
20
+ transformIndexHtml: () => [{
21
+ tag: "style",
22
+ children: statement,
23
+ injectTo: "head-prepend"
24
+ }]
25
+ };
26
+ }
27
+ const VITE_QUERY_IMPORT = /\?(?:.*&)?(?:worker|sharedworker|url|raw|inline)(?:&|$)/;
28
+ function keepViteQueryImportsExternal() {
29
+ return {
30
+ name: "keep-vite-query-imports-external",
31
+ resolveId: {
32
+ filter: { id: VITE_QUERY_IMPORT },
33
+ async handler(id, importer) {
34
+ const queryStart = id.indexOf("?");
35
+ const resolved = await this.resolve(id.slice(0, queryStart), importer);
36
+ if (!resolved) return null;
37
+ return {
38
+ id: `${resolved.id}${id.slice(queryStart)}`,
39
+ external: "absolute"
40
+ };
41
+ }
42
+ }
43
+ };
44
+ }
45
+ function stdoutWarningLogger() {
46
+ return createLogger("info", { console: {
47
+ ...console,
48
+ warn: (...message) => console.log(...message)
49
+ } });
50
+ }
51
+ /**
52
+ * The shared Vite config every visualisation app builds on.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * export default appViteConfig({ appDir: import.meta.dirname, port: 5173 });
57
+ * ```
58
+ */
59
+ function appViteConfig({ appDir, port, plugins = [], alias = {}, themeStylesheet }) {
60
+ return {
61
+ base: "./",
62
+ customLogger: stdoutWarningLogger(),
63
+ server: { port },
64
+ preview: { port },
65
+ optimizeDeps: { rolldownOptions: { plugins: [keepViteQueryImportsExternal()] } },
66
+ build: {
67
+ chunkSizeWarningLimit: 1200,
68
+ rolldownOptions: {
69
+ input: "index.html",
70
+ output: { codeSplitting: { groups: [{
71
+ name: vendorChunkName,
72
+ debugName: "vendors"
73
+ }] } }
74
+ }
75
+ },
76
+ plugins: [
77
+ declareLayerOrderFirst(appDir, themeStylesheet),
78
+ react(),
79
+ tailwindcss(),
80
+ ...plugins
81
+ ],
82
+ css: { devSourcemap: true },
83
+ resolve: { alias: {
84
+ "@": path.join(appDir, "src"),
85
+ ...alias
86
+ } }
87
+ };
88
+ }
89
+ export { appViteConfig };
@@ -0,0 +1,20 @@
1
+ import { ViteUserConfig } from "vitest/config";
2
+ import { CoverageOptions } from "vitest/node";
3
+ /** The coverage block every workspace reports against, with the caller's deviations applied. */
4
+ export declare function coverageConfig(overrides?: CoverageOptions): CoverageOptions;
5
+ /** The options of the shared Vitest config for a Node package. */
6
+ export interface NodeVitestOptions {
7
+ workspaceDir: string;
8
+ alias?: Record<string, string>;
9
+ coverage?: CoverageOptions;
10
+ include?: string[];
11
+ }
12
+ /**
13
+ * The shared Vitest config for a workspace's node suite.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * export default defineConfig(nodeVitestConfig({ workspaceDir: import.meta.dirname }));
18
+ * ```
19
+ */
20
+ export declare function nodeVitestConfig({ workspaceDir, alias, coverage, include }: NodeVitestOptions): ViteUserConfig;
@@ -0,0 +1,61 @@
1
+ import path from "node:path";
2
+ import react from "@vitejs/plugin-react";
3
+ import "vitest/config";
4
+ import "vitest/node";
5
+ // src/vitest.ts
6
+ const sharedCoverage = {
7
+ enabled: false,
8
+ provider: "istanbul",
9
+ thresholds: {
10
+ statements: 80,
11
+ branches: 80,
12
+ functions: 80,
13
+ lines: 80
14
+ },
15
+ reporter: [
16
+ "text",
17
+ "json-summary",
18
+ "json",
19
+ "html"
20
+ ],
21
+ reportsDirectory: "coverage",
22
+ include: ["src/**/*.{ts,tsx}"],
23
+ exclude: [
24
+ "src/**/*.test.{ts,tsx}",
25
+ "src/**/*.spec.{ts,tsx}",
26
+ "src/**/*.stories.{ts,tsx}",
27
+ "src/test/**",
28
+ "src/**/__tests__/**"
29
+ ]
30
+ };
31
+ /** The coverage block every workspace reports against, with the caller's deviations applied. */
32
+ function coverageConfig(overrides = {}) {
33
+ return {
34
+ ...sharedCoverage,
35
+ ...overrides
36
+ };
37
+ }
38
+ /**
39
+ * The shared Vitest config for a workspace's node suite.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * export default defineConfig(nodeVitestConfig({ workspaceDir: import.meta.dirname }));
44
+ * ```
45
+ */
46
+ function nodeVitestConfig({ workspaceDir, alias = {}, coverage = {}, include = ["src/**/*.test.{ts,tsx}"] }) {
47
+ return {
48
+ plugins: [react()],
49
+ resolve: { alias: {
50
+ "@": path.join(workspaceDir, "src"),
51
+ ...alias
52
+ } },
53
+ test: {
54
+ environment: "node",
55
+ include,
56
+ server: { deps: { inline: [/@timefoldai\/viz-/] } },
57
+ coverage: coverageConfig(coverage)
58
+ }
59
+ };
60
+ }
61
+ export { coverageConfig, nodeVitestConfig };
@@ -0,0 +1,175 @@
1
+ const MAX_PROSE_LINES = 2;
2
+ const MAX_PROSE_LINES_CLASS = 4;
3
+ const MAX_PROSE_CHARS = 120;
4
+ const MAX_TAG_LINES = 4;
5
+
6
+ const DIRECTIVE =
7
+ /^(@ts-|@vite-ignore|@vitest-environment|eslint-|oxlint-|\/ <reference|v8 ignore|c8 ignore|istanbul ignore|tokens-allow|#__PURE__|@__PURE__)/;
8
+ const WHY_COMMENT = /^why:\s*\S/;
9
+
10
+ function proseKind(text) {
11
+ if (text === "") return "blank";
12
+ if (text.startsWith("@") || DIRECTIVE.test(text)) return "tag";
13
+ return "prose";
14
+ }
15
+
16
+ function blockLines(lines) {
17
+ const prose = [];
18
+ const tagParagraphs = [];
19
+ let longLine = false;
20
+ let inTag = false;
21
+ let inFence = false;
22
+ for (const raw of lines) {
23
+ const text = raw
24
+ .trim()
25
+ .replace(/^\{?\/\*\*?\s?/, "")
26
+ .replace(/\s*\*\/\}?\s*$/, "")
27
+ .replace(/^\*\s?/, "")
28
+ .trim();
29
+ if (text.startsWith("```")) {
30
+ inFence = !inFence;
31
+ continue;
32
+ }
33
+ if (inFence) continue;
34
+ if (text.length > MAX_PROSE_CHARS) longLine = true;
35
+ const kind = proseKind(text);
36
+ if (kind === "blank") inTag = false;
37
+ else if (kind === "tag") {
38
+ inTag = true;
39
+ tagParagraphs.push(1);
40
+ } else if (inTag) tagParagraphs[tagParagraphs.length - 1]++;
41
+ else prose.push(text);
42
+ }
43
+ return { prose, longLine, longestTag: Math.max(0, ...tagParagraphs) };
44
+ }
45
+
46
+ function collectJsxComments(sourceCode, node, into) {
47
+ if (node.expression.type !== "JSXEmptyExpression") return;
48
+ for (const comment of sourceCode.getCommentsInside(node))
49
+ into.set(comment.start, node);
50
+ }
51
+
52
+ function lineCommentOffence(text, length) {
53
+ if (DIRECTIVE.test(text))
54
+ return length > MAX_PROSE_CHARS
55
+ ? `a directive line beyond ${MAX_PROSE_CHARS} characters`
56
+ : null;
57
+ if (WHY_COMMENT.test(text))
58
+ return length > MAX_PROSE_CHARS
59
+ ? `a why-comment beyond ${MAX_PROSE_CHARS} characters`
60
+ : null;
61
+ return "inline comment";
62
+ }
63
+
64
+ const noInlineComment = {
65
+ meta: {
66
+ type: "suggestion",
67
+ docs: {
68
+ description:
69
+ "A script carries no inline comment but a one-line `// why:`.",
70
+ },
71
+ },
72
+ create(context) {
73
+ const { sourceCode } = context;
74
+ const jsxComments = new Map();
75
+ return {
76
+ JSXExpressionContainer(node) {
77
+ collectJsxComments(sourceCode, node, jsxComments);
78
+ },
79
+ "Program:exit": () => {
80
+ const reported = new Set();
81
+ for (const comment of sourceCode.getAllComments()) {
82
+ const container = jsxComments.get(comment.start);
83
+ if (container) {
84
+ if (reported.has(container.start)) continue;
85
+ reported.add(container.start);
86
+ context.report({
87
+ message: "jsx comment",
88
+ loc: sourceCode.getLoc(container),
89
+ });
90
+ continue;
91
+ }
92
+ if (comment.type !== "Line") continue;
93
+ const offence = lineCommentOffence(
94
+ comment.value.replace(/^\s?/, ""),
95
+ comment.end - comment.start,
96
+ );
97
+ if (offence)
98
+ context.report({
99
+ message: offence,
100
+ loc: sourceCode.getLoc(comment),
101
+ });
102
+ }
103
+ },
104
+ };
105
+ },
106
+ };
107
+
108
+ function docblockOffence(source, cap) {
109
+ const { prose, longLine, longestTag } = blockLines(source.split("\n"));
110
+ if (longLine) return `a docblock line beyond ${MAX_PROSE_CHARS} characters`;
111
+ if (longestTag > MAX_TAG_LINES) return `${longestTag} lines under one tag`;
112
+ if (prose.length === 0) return null;
113
+ if (cap === undefined) return "prose on a non-exported declaration";
114
+ return prose.length > cap ? `${prose.length} lines of prose` : null;
115
+ }
116
+
117
+ const docblockProse = {
118
+ meta: {
119
+ type: "suggestion",
120
+ docs: {
121
+ description:
122
+ "Docblock prose (2 lines, 4 on a class) sits on an exported declaration only.",
123
+ },
124
+ },
125
+ create(context) {
126
+ const { sourceCode } = context;
127
+ const jsxComments = new Map();
128
+ const capByComment = new Map();
129
+ const documents = (node, declaration) => {
130
+ const cap =
131
+ declaration?.type === "ClassDeclaration"
132
+ ? MAX_PROSE_LINES_CLASS
133
+ : MAX_PROSE_LINES;
134
+ for (const comment of sourceCode.getCommentsBefore(node))
135
+ capByComment.set(comment.start, cap);
136
+ };
137
+ return {
138
+ JSXExpressionContainer(node) {
139
+ collectJsxComments(sourceCode, node, jsxComments);
140
+ },
141
+ ExportNamedDeclaration(node) {
142
+ documents(node, node.declaration);
143
+ },
144
+ ExportDefaultDeclaration(node) {
145
+ documents(node, node.declaration);
146
+ },
147
+ ExportAllDeclaration(node) {
148
+ documents(node, null);
149
+ },
150
+ "Program:exit": () => {
151
+ for (const comment of sourceCode.getAllComments()) {
152
+ if (comment.type !== "Block") continue;
153
+ if (jsxComments.has(comment.start)) continue;
154
+ const offence = docblockOffence(
155
+ sourceCode.text.slice(comment.start, comment.end),
156
+ capByComment.get(comment.start),
157
+ );
158
+ if (offence)
159
+ context.report({
160
+ message: offence,
161
+ loc: sourceCode.getLoc(comment),
162
+ });
163
+ }
164
+ },
165
+ };
166
+ },
167
+ };
168
+
169
+ export default {
170
+ meta: { name: "comments" },
171
+ rules: {
172
+ "no-inline-comment": noInlineComment,
173
+ "docblock-prose": docblockProse,
174
+ },
175
+ };
@@ -0,0 +1,50 @@
1
+ import { readdirSync } from "node:fs";
2
+ import path from "node:path";
3
+
4
+ const TEST_FILE = /\.test\.[cm]?[jt]sx?$/;
5
+ const NOT_A_SOURCE = /\.(test|stories)\.[cm]?[jt]sx?$/;
6
+
7
+ /** Why a test file breaks the one-test-file-per-source rule, or null when it keeps it. */
8
+ export function testFileOffence(testPath, siblingNames) {
9
+ const name = path.basename(testPath);
10
+ const stem = name.replace(TEST_FILE, "");
11
+ const isSource = (sibling) =>
12
+ sibling.startsWith(`${stem}.`) && !NOT_A_SOURCE.test(sibling);
13
+ if (!siblingNames.some(isSource))
14
+ return `${name} has no source file ${stem}.* beside it: put its tests in that source's one test file`;
15
+ const testsOfStem = siblingNames.filter(
16
+ (sibling) =>
17
+ TEST_FILE.test(sibling) && sibling.replace(TEST_FILE, "") === stem,
18
+ );
19
+ if (testsOfStem.length > 1)
20
+ return `${stem} has ${testsOfStem.length} test files: keep one`;
21
+ return null;
22
+ }
23
+
24
+ const oneTestFilePerSource = {
25
+ meta: {
26
+ type: "suggestion",
27
+ docs: {
28
+ description:
29
+ "A source file has exactly one test file, named after it and beside it.",
30
+ },
31
+ },
32
+ create(context) {
33
+ const file = context.physicalFilename;
34
+ if (!TEST_FILE.test(file)) return {};
35
+ return {
36
+ Program(node) {
37
+ const offence = testFileOffence(
38
+ file,
39
+ readdirSync(path.dirname(file)),
40
+ );
41
+ if (offence) context.report({ node, message: offence });
42
+ },
43
+ };
44
+ },
45
+ };
46
+
47
+ export default {
48
+ meta: { name: "tests" },
49
+ rules: { "one-test-file-per-source": oneTestFilePerSource },
50
+ };
package/package.json CHANGED
@@ -1,6 +1,90 @@
1
1
  {
2
2
  "name": "@timefoldai/viz-cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Build tools for a Timefold model visualization app: shared Vite and Vitest configs, lint plugins and build guards.",
5
+ "keywords": [
6
+ "oxlint",
7
+ "stylelint",
8
+ "timefold",
9
+ "visualization",
10
+ "vite",
11
+ "vitest"
12
+ ],
13
+ "homepage": "https://timefold.ai",
14
+ "bugs": {
15
+ "url": "https://docs.timefold.ai/timefold-platform/latest/support/contacting-support"
16
+ },
17
+ "license": "Apache-2.0",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/TimefoldAI/timefold-visualisations.git",
21
+ "directory": "packages/cli"
22
+ },
23
+ "bin": {
24
+ "viz-check-css-layer-order": "./bin/viz-check-css-layer-order.mjs",
25
+ "viz-check-leaked-env": "./bin/viz-check-leaked-env.mjs",
26
+ "viz-codegen": "./bin/viz-codegen.mjs"
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "!dist/index.*",
31
+ "bin",
32
+ "oxlint",
33
+ "stylelint",
34
+ "!**/*.test.mjs"
35
+ ],
36
+ "type": "module",
37
+ "sideEffects": false,
38
+ "exports": {
39
+ "./vite": {
40
+ "types": "./dist/vite.d.mts",
41
+ "default": "./dist/vite.mjs"
42
+ },
43
+ "./vitest": {
44
+ "types": "./dist/vitest.d.mts",
45
+ "default": "./dist/vitest.mjs"
46
+ },
47
+ "./layer-order": {
48
+ "types": "./dist/layer-order.d.mts",
49
+ "default": "./dist/layer-order.mjs"
50
+ },
51
+ "./oxlint/*": "./oxlint/*.mjs",
52
+ "./stylelint/*": "./stylelint/*.mjs"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public",
56
+ "registry": "https://registry.npmjs.org"
57
+ },
58
+ "peerDependencies": {
59
+ "@tailwindcss/vite": "^4",
60
+ "@vitejs/plugin-react": "^6",
61
+ "@vitest/coverage-istanbul": "^5",
62
+ "openapi-typescript": "^7",
63
+ "stylelint": "^18",
64
+ "vite": "^8",
65
+ "vitest": "^5"
66
+ },
67
+ "peerDependenciesMeta": {
68
+ "@tailwindcss/vite": {
69
+ "optional": true
70
+ },
71
+ "@vitejs/plugin-react": {
72
+ "optional": true
73
+ },
74
+ "@vitest/coverage-istanbul": {
75
+ "optional": true
76
+ },
77
+ "openapi-typescript": {
78
+ "optional": true
79
+ },
80
+ "stylelint": {
81
+ "optional": true
82
+ },
83
+ "vitest": {
84
+ "optional": true
85
+ }
86
+ },
87
+ "engines": {
88
+ "node": ">=26"
89
+ }
6
90
  }
@@ -0,0 +1,71 @@
1
+ import stylelint from "stylelint";
2
+
3
+ const ruleName = "viz/no-stylesheet-comment";
4
+ const messages = stylelint.utils.ruleMessages(ruleName, {
5
+ rejected:
6
+ "A stylesheet carries no comment but a single-line `tokens-allow` directive",
7
+ });
8
+
9
+ const ABSORBED_COMMENT = /\/\/|\/\*/;
10
+ const strippedOfUrls = (value) => value.replace(/url\([^)]*\)/g, "");
11
+ const isAllowedText = (text) =>
12
+ !text.includes("\n") && /tokens-allow/.test(text);
13
+
14
+ const rawText = (raws, key) => raws[key] ?? "";
15
+ const rawOrOwnText = (raws, key, ownText) => raws[key]?.raw ?? ownText;
16
+
17
+ const declRaw = (decl) =>
18
+ rawText(decl.raws, "between") +
19
+ rawOrOwnText(decl.raws, "value", decl.value) +
20
+ rawText(decl.raws, "important");
21
+ const ruleRaw = (styleRule) =>
22
+ rawOrOwnText(styleRule.raws, "selector", styleRule.selector) +
23
+ rawText(styleRule.raws, "between");
24
+ const atRuleRaw = (atRule) =>
25
+ rawText(atRule.raws, "afterName") +
26
+ rawOrOwnText(atRule.raws, "params", atRule.params) +
27
+ rawText(atRule.raws, "between");
28
+
29
+ const rule = (primary) => (root, result) => {
30
+ if (
31
+ !stylelint.utils.validateOptions(result, ruleName, {
32
+ actual: primary,
33
+ possible: [true],
34
+ })
35
+ )
36
+ return;
37
+
38
+ const reportUnlessAllowed = (node, raw) => {
39
+ const stripped = strippedOfUrls(raw);
40
+ if (!ABSORBED_COMMENT.test(stripped)) return;
41
+ if (isAllowedText(stripped)) return;
42
+ stylelint.utils.report({
43
+ ruleName,
44
+ result,
45
+ message: messages.rejected,
46
+ node,
47
+ });
48
+ };
49
+
50
+ root.walkComments((comment) => {
51
+ if (isAllowedText(comment.text)) return;
52
+ stylelint.utils.report({
53
+ ruleName,
54
+ result,
55
+ message: messages.rejected,
56
+ node: comment,
57
+ });
58
+ });
59
+ root.walkDecls((decl) => reportUnlessAllowed(decl, declRaw(decl)));
60
+ root.walkRules((styleRule) =>
61
+ reportUnlessAllowed(styleRule, ruleRaw(styleRule)),
62
+ );
63
+ root.walkAtRules((atRule) =>
64
+ reportUnlessAllowed(atRule, atRuleRaw(atRule)),
65
+ );
66
+ };
67
+
68
+ rule.ruleName = ruleName;
69
+ rule.messages = messages;
70
+
71
+ export default stylelint.createPlugin(ruleName, rule);