PluginProbe
404 Solution / 4.3.3
404 Solution v4.3.3
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / contracts / contractFileIO.js

contractFileIO.js in 404 Solution 4.3.3, at contracts/contractFileIO.js

65 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * contractFileIO: filesystem-access layer for the contract validation gate.
3 *
4 * Pure data-access concern. Reads files and directories from disk and parses
5 * their contents. Contains no business/validation logic; callers in
6 * validate-contracts.js decide what the read data means for contract validity.
7 *
8 * Exposes:
9 * fileExists(p) -> boolean
10 * loadJson(p) -> parsed JSON (throws on bad JSON)
11 * findJsonSchemaFiles(dir) -> string[] of *.schema.json paths
12 * fileContainsAnnotation(filePath, id, name?) -> boolean
13 * fileContainsParityAnnotation(filePath, id) -> boolean
14 */
15 // allow-no-test-found: file-IO layer of the contract-validation CLI gate; it is the data-access half of validate-contracts.js (its only consumer) and is exercised whenever that gate runs over the real contracts/schemas tree. There is no isolated JS unit spec because it only wraps fs/path reads, with no business logic to assert in isolation.
16
17 const fs = require("fs");
18 const path = require("path");
19
20 function fileExists(p) {
21 try {
22 return fs.statSync(p).isFile();
23 } catch {
24 return false; // allow-silent-catch: stat failure means file does not exist
25 }
26 }
27
28 function loadJson(p) {
29 return JSON.parse(fs.readFileSync(p, "utf8"));
30 }
31
32 function findJsonSchemaFiles(dir) {
33 const results = [];
34 if (!fs.existsSync(dir)) return results;
35 for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
36 const full = path.join(dir, entry.name);
37 if (entry.isDirectory()) {
38 results.push(...findJsonSchemaFiles(full));
39 } else if (entry.name.endsWith(".schema.json")) {
40 results.push(full);
41 }
42 }
43 return results;
44 }
45
46 function fileContainsAnnotation(filePath, contractId, annotationName = "contract") {
47 const content = fs.readFileSync(filePath, "utf8");
48 const pattern = new RegExp(`@${annotationName}\\s+${contractId.replace(/-/g, "\\-")}\\b`);
49 return pattern.test(content);
50 }
51
52 function fileContainsParityAnnotation(filePath, contractId) {
53 const content = fs.readFileSync(filePath, "utf8");
54 const pattern = new RegExp(`@parityTest\\s+${contractId.replace(/-/g, "\\-")}\\b`);
55 return pattern.test(content);
56 }
57
58 module.exports = {
59 fileExists,
60 loadJson,
61 findJsonSchemaFiles,
62 fileContainsAnnotation,
63 fileContainsParityAnnotation,
64 };
65