@specatlas/core 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 SpecAtlas contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,7 @@
1
+ import {
2
+ signApproval
3
+ } from "./chunk-2BFXQ65Q.js";
4
+ import "./chunk-5RKNY2ZC.js";
5
+ export {
6
+ signApproval
7
+ };
@@ -0,0 +1,239 @@
1
+ import {
2
+ readTextIfExists,
3
+ toPosix,
4
+ writeText
5
+ } from "./chunk-5RKNY2ZC.js";
6
+
7
+ // src/approvals.ts
8
+ import path from "path";
9
+ import { stringify as stringifyYaml } from "yaml";
10
+
11
+ // src/diagnostics.ts
12
+ function diag(code, severity, message, opts = {}) {
13
+ const d = { code, severity, message };
14
+ if (opts.path !== void 0) d.path = opts.path;
15
+ if (opts.line !== void 0) d.line = opts.line;
16
+ if (opts.suggestion !== void 0) d.suggestion = opts.suggestion;
17
+ return d;
18
+ }
19
+ function countBySeverity(diags) {
20
+ let errors = 0;
21
+ let warnings = 0;
22
+ let infos = 0;
23
+ for (const d of diags) {
24
+ if (d.severity === "error") errors += 1;
25
+ else if (d.severity === "warning") warnings += 1;
26
+ else infos += 1;
27
+ }
28
+ return { errors, warnings, infos };
29
+ }
30
+ function hasErrors(diags) {
31
+ return diags.some((d) => d.severity === "error");
32
+ }
33
+
34
+ // src/hash.ts
35
+ import { createHash } from "crypto";
36
+ function canonicalizeMarkdown(md) {
37
+ let text = md;
38
+ if (text.charCodeAt(0) === 65279) text = text.slice(1);
39
+ const lines = text.replace(/\r\n?/g, "\n").split("\n");
40
+ const out = [];
41
+ let blankRun = 0;
42
+ for (const line of lines) {
43
+ const trimmedEnd = line.replace(/[ \t]+$/g, "");
44
+ if (trimmedEnd.trim() === "") {
45
+ blankRun += 1;
46
+ if (blankRun > 1) continue;
47
+ out.push("");
48
+ } else {
49
+ blankRun = 0;
50
+ out.push(trimmedEnd);
51
+ }
52
+ }
53
+ while (out.length > 0 && out[0] === "") out.shift();
54
+ while (out.length > 0 && out[out.length - 1] === "") out.pop();
55
+ return out.join("\n") + "\n";
56
+ }
57
+ function sha256(content) {
58
+ const h = createHash("sha256");
59
+ h.update(content);
60
+ return `sha256:${h.digest("hex")}`;
61
+ }
62
+ function artifactHash(markdown) {
63
+ return sha256(canonicalizeMarkdown(markdown));
64
+ }
65
+ function shortHash(hash) {
66
+ return hash.replace(/^sha256:/, "").slice(0, 8);
67
+ }
68
+
69
+ // src/parse/meta.ts
70
+ import { parse as parseYaml } from "yaml";
71
+ import { z } from "zod";
72
+ var changeMetaSchema = z.object({
73
+ schema_version: z.number().int().positive().default(1),
74
+ slug: z.string().min(1),
75
+ title: z.string().optional(),
76
+ domain: z.string().optional(),
77
+ lane: z.enum(["fix", "standard", "full"]).default("standard"),
78
+ risk: z.enum(["low", "medium", "high"]).optional(),
79
+ created: z.string().optional(),
80
+ owner: z.string().optional(),
81
+ tracker: z.object({ provider: z.string(), id: z.string() }).optional(),
82
+ paused: z.object({ reason: z.string(), at: z.string(), by: z.string() }).optional(),
83
+ lane_history: z.array(z.object({ from: z.enum(["fix", "standard", "full"]), to: z.enum(["fix", "standard", "full"]), at: z.string(), by: z.string() })).optional(),
84
+ diagram_exceptions: z.array(z.string()).optional(),
85
+ overrides: z.array(z.object({ gate: z.string(), reason: z.string(), by: z.string(), at: z.string() })).optional()
86
+ });
87
+ function parseChangeMeta(raw, filePath) {
88
+ const diagnostics = [];
89
+ let data;
90
+ try {
91
+ data = parseYaml(raw);
92
+ } catch (err) {
93
+ return { diagnostics: [diag("ATLAS-META-001", "error", `meta.yaml inv\xE1lido: ${err.message}`, { path: filePath })] };
94
+ }
95
+ const parsed = changeMetaSchema.safeParse(data);
96
+ if (!parsed.success) {
97
+ return {
98
+ diagnostics: parsed.error.issues.map(
99
+ (issue) => diag("ATLAS-META-002", "error", `meta.yaml (${issue.path.join(".") || "ra\xEDz"}): ${issue.message}`, { path: filePath })
100
+ )
101
+ };
102
+ }
103
+ const v = parsed.data;
104
+ const meta = { schemaVersion: v.schema_version, slug: v.slug, lane: v.lane };
105
+ if (v.title !== void 0) meta.title = v.title;
106
+ if (v.domain !== void 0) meta.domain = v.domain;
107
+ if (v.risk !== void 0) meta.risk = v.risk;
108
+ if (v.created !== void 0) meta.created = v.created;
109
+ if (v.owner !== void 0) meta.owner = v.owner;
110
+ if (v.tracker !== void 0) meta.tracker = v.tracker;
111
+ if (v.paused !== void 0) meta.paused = v.paused;
112
+ if (v.lane_history !== void 0) meta.laneHistory = v.lane_history;
113
+ if (v.diagram_exceptions !== void 0) meta.diagramExceptions = v.diagram_exceptions;
114
+ if (v.overrides !== void 0) meta.overrides = v.overrides;
115
+ for (const override of meta.overrides ?? []) {
116
+ if (!override.reason || !override.by) {
117
+ diagnostics.push(diag("ATLAS-LIFECYCLE-004", "error", `Override del gate "${override.gate}" sin motivo o autor`, { path: filePath }));
118
+ }
119
+ }
120
+ return { meta, diagnostics };
121
+ }
122
+ var approvalsSchema = z.object({
123
+ schema_version: z.number().int().positive().default(1),
124
+ approvals: z.array(
125
+ z.object({
126
+ artifact: z.string(),
127
+ artifact_hash: z.string(),
128
+ approved_by: z.string(),
129
+ approved_at: z.string(),
130
+ channel: z.enum(["presentation", "editor", "pr", "tracker", "cli"]).default("cli"),
131
+ note: z.string().optional()
132
+ })
133
+ ).default([])
134
+ });
135
+ function parseApprovals(raw, filePath) {
136
+ let data;
137
+ try {
138
+ data = parseYaml(raw);
139
+ } catch (err) {
140
+ return { diagnostics: [diag("ATLAS-APPROVALS-001", "error", `approvals.yaml inv\xE1lido: ${err.message}`, { path: filePath })] };
141
+ }
142
+ const parsed = approvalsSchema.safeParse(data);
143
+ if (!parsed.success) {
144
+ return {
145
+ diagnostics: parsed.error.issues.map(
146
+ (issue) => diag("ATLAS-APPROVALS-002", "error", `approvals.yaml (${issue.path.join(".") || "ra\xEDz"}): ${issue.message}`, { path: filePath })
147
+ )
148
+ };
149
+ }
150
+ const file = {
151
+ schemaVersion: parsed.data.schema_version,
152
+ approvals: parsed.data.approvals.map((a) => {
153
+ const approval = {
154
+ artifact: a.artifact,
155
+ artifactHash: a.artifact_hash,
156
+ approvedBy: a.approved_by,
157
+ approvedAt: a.approved_at,
158
+ channel: a.channel
159
+ };
160
+ if (a.note !== void 0) approval.note = a.note;
161
+ return approval;
162
+ })
163
+ };
164
+ return { approvals: file, diagnostics: [] };
165
+ }
166
+
167
+ // src/approvals.ts
168
+ function normalizeArtifact(sddDir, artifact) {
169
+ const abs = path.isAbsolute(artifact) ? artifact : path.resolve(sddDir, artifact);
170
+ const rel = toPosix(path.relative(sddDir, abs));
171
+ return { rel, abs };
172
+ }
173
+ async function signApproval(opts) {
174
+ const root = path.resolve(opts.root);
175
+ const sddDir = path.join(root, ".sdd");
176
+ const file = path.join(sddDir, "approvals.yaml");
177
+ const diagnostics = [];
178
+ const { rel, abs } = normalizeArtifact(sddDir, opts.artifact);
179
+ if (!opts.by.trim()) {
180
+ diagnostics.push(diag("ATLAS-APPROVE-001", "error", "La aprobaci\xF3n requiere el nombre de quien aprueba (--by)", { suggestion: 'Ejemplo: satlas approve changes/x/spec.md --by "Mar\xEDa P\xE9rez"`' }));
181
+ return { file, diagnostics };
182
+ }
183
+ const content = await readTextIfExists(abs);
184
+ if (content === void 0) {
185
+ diagnostics.push(diag("ATLAS-APPROVE-002", "error", `No existe el artefacto a aprobar: ${rel}`, { path: abs }));
186
+ return { file, diagnostics };
187
+ }
188
+ const existing = await readTextIfExists(file);
189
+ let approvals = [];
190
+ if (existing !== void 0) {
191
+ const parsed = parseApprovals(existing, file);
192
+ diagnostics.push(...parsed.diagnostics);
193
+ approvals = parsed.approvals?.approvals ?? [];
194
+ }
195
+ const now = opts.now ?? /* @__PURE__ */ new Date();
196
+ const approval = {
197
+ artifact: rel,
198
+ artifactHash: artifactHash(content),
199
+ approvedBy: opts.by.trim(),
200
+ approvedAt: now.toISOString(),
201
+ channel: opts.channel ?? "cli"
202
+ };
203
+ if (opts.note) approval.note = opts.note;
204
+ approvals = approvals.filter((a) => a.artifact !== rel);
205
+ approvals.push(approval);
206
+ const doc = {
207
+ schema_version: 1,
208
+ approvals: approvals.map((a) => {
209
+ const row = {
210
+ artifact: a.artifact,
211
+ artifact_hash: a.artifactHash,
212
+ approved_by: a.approvedBy,
213
+ approved_at: a.approvedAt,
214
+ channel: a.channel
215
+ };
216
+ if (a.note) row["note"] = a.note;
217
+ return row;
218
+ })
219
+ };
220
+ if (!opts.dryRun) {
221
+ await writeText(file, stringifyYaml(doc, { lineWidth: 120 }));
222
+ }
223
+ return { approval, file, diagnostics };
224
+ }
225
+
226
+ export {
227
+ diag,
228
+ countBySeverity,
229
+ hasErrors,
230
+ canonicalizeMarkdown,
231
+ sha256,
232
+ artifactHash,
233
+ shortHash,
234
+ changeMetaSchema,
235
+ parseChangeMeta,
236
+ approvalsSchema,
237
+ parseApprovals,
238
+ signApproval
239
+ };
@@ -0,0 +1,94 @@
1
+ // src/fsx.ts
2
+ import { promises as fs } from "fs";
3
+ import path from "path";
4
+ async function exists(p) {
5
+ try {
6
+ await fs.access(p);
7
+ return true;
8
+ } catch {
9
+ return false;
10
+ }
11
+ }
12
+ async function isDirectory(p) {
13
+ try {
14
+ const st = await fs.stat(p);
15
+ return st.isDirectory();
16
+ } catch {
17
+ return false;
18
+ }
19
+ }
20
+ async function readText(p) {
21
+ return fs.readFile(p, "utf8");
22
+ }
23
+ async function readTextIfExists(p) {
24
+ try {
25
+ return await fs.readFile(p, "utf8");
26
+ } catch {
27
+ return void 0;
28
+ }
29
+ }
30
+ async function writeText(p, content) {
31
+ await ensureDir(path.dirname(p));
32
+ await fs.writeFile(p, content, "utf8");
33
+ }
34
+ async function ensureDir(p) {
35
+ await fs.mkdir(p, { recursive: true });
36
+ }
37
+ async function copyFile(from, to) {
38
+ await ensureDir(path.dirname(to));
39
+ await fs.copyFile(from, to);
40
+ }
41
+ async function listDir(p) {
42
+ try {
43
+ return await fs.readdir(p);
44
+ } catch {
45
+ return [];
46
+ }
47
+ }
48
+ async function listDirs(p) {
49
+ const entries = await listDir(p);
50
+ const out = [];
51
+ for (const e of entries) {
52
+ if (await isDirectory(path.join(p, e))) out.push(e);
53
+ }
54
+ return out.sort();
55
+ }
56
+ async function walkFiles(root, opts = {}) {
57
+ const skip = new Set(opts.skipDirs ?? ["node_modules", ".git", "dist", "coverage", ".sdd"]);
58
+ const out = [];
59
+ async function rec(dir) {
60
+ let entries;
61
+ try {
62
+ entries = await fs.readdir(dir, { withFileTypes: true });
63
+ } catch {
64
+ return;
65
+ }
66
+ for (const e of entries) {
67
+ if (e.isDirectory()) {
68
+ if (skip.has(e.name)) continue;
69
+ await rec(path.join(dir, e.name));
70
+ } else if (e.isFile()) {
71
+ out.push({ path: path.join(dir, e.name), name: e.name });
72
+ }
73
+ }
74
+ }
75
+ await rec(root);
76
+ return out;
77
+ }
78
+ function toPosix(p) {
79
+ return p.split(path.sep).join("/");
80
+ }
81
+
82
+ export {
83
+ exists,
84
+ isDirectory,
85
+ readText,
86
+ readTextIfExists,
87
+ writeText,
88
+ ensureDir,
89
+ copyFile,
90
+ listDir,
91
+ listDirs,
92
+ walkFiles,
93
+ toPosix
94
+ };
@@ -0,0 +1,26 @@
1
+ import {
2
+ copyFile,
3
+ ensureDir,
4
+ exists,
5
+ isDirectory,
6
+ listDir,
7
+ listDirs,
8
+ readText,
9
+ readTextIfExists,
10
+ toPosix,
11
+ walkFiles,
12
+ writeText
13
+ } from "./chunk-5RKNY2ZC.js";
14
+ export {
15
+ copyFile,
16
+ ensureDir,
17
+ exists,
18
+ isDirectory,
19
+ listDir,
20
+ listDirs,
21
+ readText,
22
+ readTextIfExists,
23
+ toPosix,
24
+ walkFiles,
25
+ writeText
26
+ };