-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-structure.ts
More file actions
135 lines (116 loc) · 3.89 KB
/
Copy pathvalidate-structure.ts
File metadata and controls
135 lines (116 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { existsSync, readFileSync } from "fs";
import { glob } from "glob";
import { load } from "js-yaml";
import path from "path";
import { fileURLToPath } from "url";
interface FrameworkManifest {
framework: string;
env_file?: string;
scenarios: Record<string, unknown>;
}
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const SAMPLES = path.join(ROOT, "samples");
const TAG_START = /@snippet:step(\d+):start/g;
const TAG_END = /@snippet:step(\d+):end/g;
let errors = 0;
function error(msg: string) {
console.error(`ERROR: ${msg}`);
errors++;
}
function exampleFileFor(envFile: string): string {
// Special case: .env (a dotfile with no stem) → .env.example.
if (path.basename(envFile) === ".env") {
return envFile + ".example";
}
// General case: insert ".example" before the final extension.
const ext = path.extname(envFile);
if (ext) {
return envFile.slice(0, -ext.length) + ".example" + ext;
}
return envFile + ".example";
}
async function main() {
const manifestFiles = await glob("*/manifest.yaml", { cwd: SAMPLES });
for (const manifestFile of manifestFiles) {
const frameworkDir = path.join(SAMPLES, path.dirname(manifestFile));
const content = readFileSync(path.join(SAMPLES, manifestFile), "utf-8");
const manifest = load(content) as FrameworkManifest;
for (const scenarioId of Object.keys(manifest.scenarios)) {
const dirName = scenarioId.replace(/^[^_]+_/, "").replaceAll("_", "-");
const scenarioDir = path.join(frameworkDir, dirName);
if (!existsSync(scenarioDir)) {
error(
`Directory not found: samples/${manifest.framework}/${dirName}/ (scenario: ${scenarioId})`,
);
continue;
}
const envFile = manifest.env_file || ".env";
const exampleFile = exampleFileFor(envFile);
if (!existsSync(path.join(scenarioDir, exampleFile))) {
error(
`Missing ${exampleFile} in samples/${manifest.framework}/${dirName}/`,
);
}
if (!existsSync(path.join(scenarioDir, "README.md"))) {
error(`Missing README.md in samples/${manifest.framework}/${dirName}/`);
}
const sourceFiles = await glob(
"**/*.{ts,tsx,js,jsx,vue,cs,java,yml,yaml,swift,kt,kts,dart,gradle,xcconfig}",
{
cwd: scenarioDir,
ignore: [
"**/node_modules/**",
"**/.yarn/**",
"**/dist/**",
"**/build/**",
"**/target/**",
"**/bin/**",
"**/obj/**",
"**/.gradle/**",
"**/.dart_tool/**",
"**/Pods/**",
"**/DerivedData/**",
"**/.build/**",
"**/coverage/**",
],
},
);
let hasSnippetTag = false;
for (const file of sourceFiles) {
const fileContent = readFileSync(path.join(scenarioDir, file), "utf-8");
const starts = [...fileContent.matchAll(TAG_START)].map((m) =>
parseInt(m[1], 10),
);
const ends = [...fileContent.matchAll(TAG_END)].map((m) =>
parseInt(m[1], 10),
);
if (starts.length > 0) hasSnippetTag = true;
for (const step of starts) {
if (!ends.includes(step)) {
error(
`Missing @snippet:step${step}:end in samples/${manifest.framework}/${dirName}/${file}`,
);
}
}
for (const step of ends) {
if (!starts.includes(step)) {
error(
`Missing @snippet:step${step}:start in samples/${manifest.framework}/${dirName}/${file}`,
);
}
}
}
if (!hasSnippetTag) {
error(
`No @snippet tags found in samples/${manifest.framework}/${dirName}/`,
);
}
}
}
if (errors > 0) {
console.error(`\n${errors} error(s) found`);
process.exit(1);
}
console.log("All structure checks passed");
}
main();