-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect-tech-stack.cjs
More file actions
209 lines (188 loc) · 6.45 KB
/
Copy pathdetect-tech-stack.cjs
File metadata and controls
209 lines (188 loc) · 6.45 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env node
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const ENTERPRISE_STAGES = [
'enterprise',
'enterprise-discover',
'enterprise-brainstorm',
'enterprise-stack-review',
'enterprise-plan',
'enterprise-contract',
'enterprise-build',
'enterprise-review',
'enterprise-pr-review',
'enterprise-forge',
'enterprise-verify',
'enterprise-harness',
'enterprise-compound',
'enterprise-debug',
];
function readJsonIfExists(filePath) {
if (!fs.existsSync(filePath)) return null;
try {
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
} catch {
return null;
}
}
function exists(rootDir, relativePath) {
return fs.existsSync(path.join(rootDir, relativePath));
}
function listFiles(rootDir, relativeDir) {
const dir = path.join(rootDir, relativeDir);
if (!fs.existsSync(dir)) return [];
return fs.readdirSync(dir).map((name) => path.join(relativeDir, name));
}
function addUnique(target, value) {
if (value && !target.includes(value)) target.push(value);
}
function slugify(value) {
return String(value || 'repo')
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.slice(0, 48) || 'repo';
}
function titleCaseSlug(slug) {
return slug
.split('-')
.filter(Boolean)
.map((part) => part[0].toUpperCase() + part.slice(1))
.join(' ');
}
function detectPackageManager(rootDir) {
if (exists(rootDir, 'pnpm-lock.yaml')) return 'pnpm';
if (exists(rootDir, 'yarn.lock')) return 'yarn';
if (exists(rootDir, 'bun.lockb') || exists(rootDir, 'bun.lock')) return 'bun';
if (exists(rootDir, 'package-lock.json')) return 'npm';
if (exists(rootDir, 'package.json')) return 'npm';
return null;
}
function detectFromPackageJson(rootDir, stack) {
const packageJson = readJsonIfExists(path.join(rootDir, 'package.json'));
if (!packageJson) return null;
const deps = {
...packageJson.dependencies,
...packageJson.devDependencies,
...packageJson.peerDependencies,
};
const has = (name) => Object.prototype.hasOwnProperty.call(deps, name);
addUnique(stack.runtimes, 'node');
stack.packageManager = detectPackageManager(rootDir);
if (has('react')) addUnique(stack.frontend, 'react');
if (has('vue')) addUnique(stack.frontend, 'vue');
if (has('svelte')) addUnique(stack.frontend, 'svelte');
if (has('next')) addUnique(stack.frameworks, 'next');
if (has('express')) addUnique(stack.frameworks, 'express');
if (has('fastify')) addUnique(stack.frameworks, 'fastify');
if (has('hono')) addUnique(stack.frameworks, 'hono');
if (has('@nestjs/core')) addUnique(stack.frameworks, 'nestjs');
if (has('vite')) addUnique(stack.tooling, 'vite');
if (has('typescript')) addUnique(stack.languages, 'typescript');
if (has('pg') || has('postgres') || has('postgresql')) addUnique(stack.databases, 'postgresql');
if (has('mysql2') || has('mysql')) addUnique(stack.databases, 'mysql');
if (has('sqlite3') || has('better-sqlite3')) addUnique(stack.databases, 'sqlite');
if (has('mongodb') || has('mongoose')) addUnique(stack.databases, 'mongodb');
if (has('prisma')) addUnique(stack.dataAccess, 'prisma');
if (has('drizzle-orm')) addUnique(stack.dataAccess, 'drizzle');
if (has('knex')) addUnique(stack.dataAccess, 'knex');
if (has('jest')) addUnique(stack.testing, 'jest');
if (has('vitest')) addUnique(stack.testing, 'vitest');
if (has('@playwright/test') || has('playwright')) addUnique(stack.testing, 'playwright');
if (has('cypress')) addUnique(stack.testing, 'cypress');
return packageJson;
}
function detectTechStack(rootDir = process.cwd()) {
const resolvedRoot = path.resolve(rootDir);
const stack = {
repoRoot: resolvedRoot,
repoSlug: null,
repoDisplayName: null,
packageName: null,
packageManager: null,
languages: [],
runtimes: [],
frameworks: [],
frontend: [],
databases: [],
dataAccess: [],
testing: [],
tooling: [],
ci: [],
deployment: [],
evidence: [],
};
const packageJson = detectFromPackageJson(resolvedRoot, stack);
if (packageJson?.name) {
stack.packageName = packageJson.name;
}
if (exists(resolvedRoot, 'pyproject.toml') || exists(resolvedRoot, 'requirements.txt')) {
addUnique(stack.runtimes, 'python');
addUnique(stack.languages, 'python');
if (exists(resolvedRoot, 'pytest.ini') || exists(resolvedRoot, 'pyproject.toml')) addUnique(stack.testing, 'pytest');
}
if (exists(resolvedRoot, 'go.mod')) {
addUnique(stack.runtimes, 'go');
addUnique(stack.languages, 'go');
}
if (exists(resolvedRoot, 'Cargo.toml')) {
addUnique(stack.runtimes, 'rust');
addUnique(stack.languages, 'rust');
}
if (exists(resolvedRoot, 'composer.json')) {
addUnique(stack.runtimes, 'php');
addUnique(stack.languages, 'php');
}
if (exists(resolvedRoot, 'Gemfile')) {
addUnique(stack.runtimes, 'ruby');
addUnique(stack.languages, 'ruby');
}
if (exists(resolvedRoot, '.github/workflows')) {
addUnique(stack.ci, 'github-actions');
for (const workflow of listFiles(resolvedRoot, '.github/workflows')) {
stack.evidence.push(workflow);
}
}
if (exists(resolvedRoot, 'render.yaml')) addUnique(stack.deployment, 'render');
if (exists(resolvedRoot, 'vercel.json')) addUnique(stack.deployment, 'vercel');
if (exists(resolvedRoot, 'Dockerfile') || exists(resolvedRoot, 'docker-compose.yml')) {
addUnique(stack.deployment, 'docker');
}
const rootName = path.basename(resolvedRoot);
stack.repoSlug = slugify(stack.packageName || rootName);
stack.repoDisplayName = titleCaseSlug(stack.repoSlug);
stack.overlayFamily = `${stack.repoSlug}-enterprise`;
stack.recommendedStages = ENTERPRISE_STAGES;
stack.overlayHints = {
codeLocationFirst: true,
sourceTruthRequired: true,
headlessVerificationRequired: true,
liveDataProofWhenDataSensitive: stack.databases.length > 0,
browserProofWhenUi: stack.frontend.length > 0,
};
return stack;
}
function parseArgs(argv) {
const args = { root: process.cwd(), json: true };
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === '--root') {
args.root = argv[++index];
} else if (arg === '--pretty') {
args.json = false;
}
}
return args;
}
if (require.main === module) {
const args = parseArgs(process.argv.slice(2));
const result = detectTechStack(args.root);
console.log(JSON.stringify(result, null, 2));
}
module.exports = {
ENTERPRISE_STAGES,
detectTechStack,
slugify,
titleCaseSlug,
};