ShtukbaseStandard
файл пакета→ .shtukbase/packages/std-twelve-factor/checks/tf02_lock.ts

checks/tf02_lock.ts

77 строк, 4.1 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // TF-R02 (II Dependencies): every manifest that declares dependencies has its lock file in git — next to it or in a
  2. // folder above (a workspace root); requirements.txt pins every line with `==`.
  3. import { join, posix } from 'node:path';
  4. import { files, PKG } from './tflib.ts';
  5. import { finish, norm, readTextFile } from './lib/std.ts';
  6. const n = norm(PKG, 'TF-R02', 'enforce');
  7. const out: string[] = [];
  8. const all = new Set(files());
  9. const has = (dir: string, names: string[]) => names.some((x) => all.has(dir ? `${dir}/${x}` : x));
  10. function lockAbove(file: string, names: string[]): boolean {
  11. let dir = posix.dirname(file);
  12. for (;;) {
  13. const d = dir === '.' ? '' : dir;
  14. if (has(d, names)) return true;
  15. if (!d) return false;
  16. dir = posix.dirname(dir);
  17. }
  18. }
  19. function tomlHasDeps(text: string, tables: RegExp): boolean {
  20. let inT = false;
  21. for (const raw of text.split('\n')) {
  22. const l = raw.trim();
  23. const t = /^\[([^\]]+)\]/.exec(l);
  24. if (t) {
  25. inT = tables.test(t[1].trim());
  26. continue;
  27. }
  28. if (inT && /^[A-Za-z0-9_.-]+\s*=/.test(l)) return true;
  29. }
  30. return false;
  31. }
  32. const NODE_LOCKS = ['bun.lock', 'bun.lockb', 'package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock', 'pnpm-lock.yaml'];
  33. const PY_LOCKS = ['uv.lock', 'poetry.lock', 'pdm.lock', 'Pipfile.lock', 'pylock.toml'];
  34. if (n.active)
  35. for (const f of all) {
  36. const base = posix.basename(f);
  37. const dir = posix.dirname(f) === '.' ? '' : posix.dirname(f);
  38. const need = (names: string[], what: string) => {
  39. 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`);
  40. };
  41. if (base === 'package.json') {
  42. const text = readTextFile(f);
  43. let j: Record<string, unknown> = {};
  44. try {
  45. j = JSON.parse(text ?? '{}');
  46. } catch {
  47. continue;
  48. }
  49. const deps = ['dependencies', 'devDependencies', 'optionalDependencies'].some((k) => j[k] && typeof j[k] === 'object' && Object.keys(j[k] as object).length);
  50. if (deps) need(NODE_LOCKS, `lock file (${NODE_LOCKS.join(', ')})`);
  51. } else if (base === 'Cargo.toml') {
  52. const text = readTextFile(f) ?? '';
  53. if (tomlHasDeps(text, /^(dependencies|dev-dependencies|build-dependencies|workspace\.dependencies|target\..+\.dependencies)$/)) need(['Cargo.lock'], 'Cargo.lock');
  54. } else if (base === 'pyproject.toml') {
  55. const text = readTextFile(f) ?? '';
  56. 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] ?? '');
  57. 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`);
  58. } else if (/^requirements.*\.(txt|in)$/.test(base) && base.endsWith('.txt')) {
  59. if (has(dir, PY_LOCKS) || all.has(join(dir, base.replace(/\.txt$/, '.in')))) continue; // compiled from .in or a lock rules
  60. const loose: number[] = [];
  61. (readTextFile(f) ?? '').split('\n').forEach((raw, i) => {
  62. const l = raw.replace(/\s+#.*$/, '').trim();
  63. if (!l || l.startsWith('#') || l.startsWith('-') || /^(git\+|https?:|file:|\.|\/)/.test(l)) return;
  64. if (!/===?\s*[^\s,;]+/.test(l) && !/@\s*(https?|file|git\+)/.test(l)) loose.push(i + 1);
  65. });
  66. 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)`);
  67. } else if (base === 'Pipfile') need(['Pipfile.lock'], 'Pipfile.lock');
  68. else if (base === 'Gemfile') need(['Gemfile.lock'], 'Gemfile.lock');
  69. else if (base === 'go.mod') {
  70. if (/^\s*require\b/m.test(readTextFile(f) ?? '')) need(['go.sum'], 'go.sum');
  71. } else if (base === 'composer.json') {
  72. const t = readTextFile(f) ?? '';
  73. if (/"require(-dev)?"\s*:\s*\{\s*"/.test(t)) need(['composer.lock'], 'composer.lock');
  74. }
  75. }
  76. finish(n, out);