package file→ .shtukbase/packages/std-twelve-factor/checks/tf02_lock.ts
checks/tf02_lock.ts
77 lines, 4.1 KB.
- // TF-R02 (II Dependencies): every manifest that declares dependencies has its lock file in git — next to it or in a
- // folder above (a workspace root); requirements.txt pins every line with `==`.
- import { join, posix } from 'node:path';
- import { files, PKG } from './tflib.ts';
- import { finish, norm, readTextFile } from './lib/std.ts';
- const n = norm(PKG, 'TF-R02', 'enforce');
- const out: string[] = [];
- const all = new Set(files());
- const has = (dir: string, names: string[]) => names.some((x) => all.has(dir ? `${dir}/${x}` : x));
- function lockAbove(file: string, names: string[]): boolean {
- let dir = posix.dirname(file);
- for (;;) {
- const d = dir === '.' ? '' : dir;
- if (has(d, names)) return true;
- if (!d) return false;
- dir = posix.dirname(dir);
- }
- }
- function tomlHasDeps(text: string, tables: RegExp): boolean {
- let inT = false;
- for (const raw of text.split('\n')) {
- const l = raw.trim();
- const t = /^\[([^\]]+)\]/.exec(l);
- if (t) {
- inT = tables.test(t[1].trim());
- continue;
- }
- if (inT && /^[A-Za-z0-9_.-]+\s*=/.test(l)) return true;
- }
- return false;
- }
- const NODE_LOCKS = ['bun.lock', 'bun.lockb', 'package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock', 'pnpm-lock.yaml'];
- const PY_LOCKS = ['uv.lock', 'poetry.lock', 'pdm.lock', 'Pipfile.lock', 'pylock.toml'];
- if (n.active)
- for (const f of all) {
- const base = posix.basename(f);
- const dir = posix.dirname(f) === '.' ? '' : posix.dirname(f);
- const need = (names: string[], what: string) => {
- if (!lockAbove(f, names)) out.push(`${f}: dependencies are declared, but there is no ${what} next to it or above → install the dependencies and commit the lock file`);
- };
- if (base === 'package.json') {
- const text = readTextFile(f);
- let j: Record<string, unknown> = {};
- try {
- j = JSON.parse(text ?? '{}');
- } catch {
- continue;
- }
- const deps = ['dependencies', 'devDependencies', 'optionalDependencies'].some((k) => j[k] && typeof j[k] === 'object' && Object.keys(j[k] as object).length);
- if (deps) need(NODE_LOCKS, `lock file (${NODE_LOCKS.join(', ')})`);
- } else if (base === 'Cargo.toml') {
- const text = readTextFile(f) ?? '';
- if (tomlHasDeps(text, /^(dependencies|dev-dependencies|build-dependencies|workspace\.dependencies|target\..+\.dependencies)$/)) need(['Cargo.lock'], 'Cargo.lock');
- } else if (base === 'pyproject.toml') {
- const text = readTextFile(f) ?? '';
- const deps = /^\s*dependencies\s*=\s*\[\s*["']/m.test(text) || tomlHasDeps(text, /^tool\.poetry\.dependencies$/) && /^\s*(?!python\b)[A-Za-z0-9_.-]+\s*=/m.test(text.split('[tool.poetry.dependencies]')[1]?.split(/\n\[/)[0] ?? '');
- if (deps && !lockAbove(f, PY_LOCKS) && !has(dir, ['requirements.txt', 'requirements.lock'])) out.push(`${f}: dependencies are declared, but there is no lock file (${PY_LOCKS.join(', ')}) → pin the versions (uv lock, poetry lock) and commit it`);
- } else if (/^requirements.*\.(txt|in)$/.test(base) && base.endsWith('.txt')) {
- if (has(dir, PY_LOCKS) || all.has(join(dir, base.replace(/\.txt$/, '.in')))) continue; // compiled from .in or a lock rules
- const loose: number[] = [];
- (readTextFile(f) ?? '').split('\n').forEach((raw, i) => {
- const l = raw.replace(/\s+#.*$/, '').trim();
- if (!l || l.startsWith('#') || l.startsWith('-') || /^(git\+|https?:|file:|\.|\/)/.test(l)) return;
- if (!/===?\s*[^\s,;]+/.test(l) && !/@\s*(https?|file|git\+)/.test(l)) loose.push(i + 1);
- });
- if (loose.length) out.push(`${f}:${loose[0]}: the version is not pinned with "==" (lines: ${loose.length}) → pin the versions or build the file from requirements.in (pip-compile, uv pip compile)`);
- } else if (base === 'Pipfile') need(['Pipfile.lock'], 'Pipfile.lock');
- else if (base === 'Gemfile') need(['Gemfile.lock'], 'Gemfile.lock');
- else if (base === 'go.mod') {
- if (/^\s*require\b/m.test(readTextFile(f) ?? '')) need(['go.sum'], 'go.sum');
- } else if (base === 'composer.json') {
- const t = readTextFile(f) ?? '';
- if (/"require(-dev)?"\s*:\s*\{\s*"/.test(t)) need(['composer.lock'], 'composer.lock');
- }
- }
- finish(n, out);