@coding-with-hassan/devkit 1.0.0-next.1 → 1.0.0-next.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coding-with-hassan/devkit",
3
- "version": "1.0.0-next.1",
3
+ "version": "1.0.0-next.3",
4
4
  "description": "CLI for the developer workflow shared across Hassan's client projects: docker compose orchestration, db helpers, and husky hook installation.",
5
5
  "license": "MIT",
6
6
  "author": "Hassan Khousheish",
@@ -47,9 +47,22 @@ export function declaredDevkitRange(pkg) {
47
47
  return pkg.devDependencies?.[DEVKIT_PACKAGE] ?? pkg.dependencies?.[DEVKIT_PACKAGE] ?? null;
48
48
  }
49
49
 
50
- /** Pure: the workflow drift check. Returns the diff lines (empty = in sync) or null when absent. */
50
+ export const SCAFFOLD_MARKER = '# Scaffolded by hassan-devkit init';
51
+
52
+ /** Pure: was this workflow written by `init` (as opposed to a hand-written one not yet adopted)? */
53
+ export function isScaffoldedWorkflow(text) {
54
+ return typeof text === 'string' && text.includes(SCAFFOLD_MARKER);
55
+ }
56
+
57
+ /**
58
+ * Pure: the workflow drift check. Returns the diff lines (empty = in sync), `null` when the
59
+ * file is absent, or `'not-adopted'` for a hand-written workflow (no scaffold marker) — a
60
+ * repo adopting the way of working piece by piece is not drifting, it just has not taken
61
+ * the `ci` part yet.
62
+ */
51
63
  export function workflowDrift(current, config) {
52
64
  if (current === null) return null;
65
+ if (!isScaffoldedWorkflow(current)) return 'not-adopted';
53
66
  const expected = renderWorkflow({ nodeVersion: config.ci.nodeVersion, baseBranch: config.ci.baseBranch, exemptAuthors: config.commits.exemptAuthors });
54
67
  return current === expected ? [] : lineDiff(current, expected);
55
68
  }
@@ -69,7 +82,8 @@ export function doctor({ cwd = process.cwd(), log = console.log } = {}) {
69
82
  if (loaded.config) {
70
83
  const file = join(loaded.root, WORKFLOW_PATH);
71
84
  const drift = workflowDrift(existsSync(file) ? readFileSync(file, 'utf8') : null, loaded.config);
72
- if (drift && drift.length) {
85
+ if (drift === 'not-adopted') log(`doctor: ${WORKFLOW_PATH} is hand-written (ci part not adopted yet) — \`hassan-devkit init --only ci --force\` replaces it with the devkit workflow`);
86
+ else if (drift && drift.length) {
73
87
  problems.push(`${WORKFLOW_PATH} differs from the devkit ${installed} template — run \`hassan-devkit init --force\` (diff below)`);
74
88
  for (const line of drift) log(` ${line}`);
75
89
  }
@@ -8,26 +8,37 @@ const HOOK_SOURCE = resolve(here, '../../hooks/pre-commit.sh');
8
8
 
9
9
  /**
10
10
  * `hooks:install` runs from the consumer's `prepare` script, i.e. on every `pnpm install`
11
- * — in the main checkout AND in every worktree. Husky's shim directory (`.husky/_`) is
12
- * per-checkout and `husky init` only runs when `.husky` is absent, so a fresh worktree
13
- * would have the committed hook but no shims — and git SILENTLY skips every hook when
14
- * `core.hooksPath` points at a missing directory. `pnpm exec husky` (re)creates the shims.
11
+ * — in the main checkout, in every worktree, AND inside container image builds (the dev
12
+ * Dockerfiles install with the root package.json present). Two rules follow:
13
+ * - no git checkout (no `.git` file/dir, or `HUSKY=0` as the Dockerfiles set) → there is
14
+ * nothing to hook; log and return without failing the install;
15
+ * - in a real checkout, husky's shim directory (`.husky/_`) is per checkout and git
16
+ * SILENTLY skips every hook when `core.hooksPath` points at a missing directory, so the
17
+ * shims are (re)created with `pnpm exec husky` and their absence afterwards is a hard error.
18
+ * `husky init` is never used: it rewrites the consumer's `prepare` script to plain `husky`.
15
19
  */
16
- export async function installHooks({ cwd = process.cwd(), log = console.log } = {}) {
20
+ export async function installHooks({ cwd = process.cwd(), env = process.env, log = console.log } = {}) {
21
+ if (env.HUSKY === '0') {
22
+ log('hooks: HUSKY=0 — skipping the pre-commit hook install');
23
+ return false;
24
+ }
25
+ if (!existsSync(resolve(cwd, '.git'))) {
26
+ log('hooks: no git checkout here (container build or export) — skipping the pre-commit hook install');
27
+ return false;
28
+ }
17
29
  const husky = resolve(cwd, '.husky');
18
- if (!existsSync(husky)) {
19
- await runCommand('pnpm', ['exec', 'husky', 'init'], { cwd });
20
- } else if (!existsSync(resolve(husky, '_'))) {
30
+ mkdirSync(husky, { recursive: true });
31
+ if (!existsSync(resolve(husky, '_', 'pre-commit'))) {
21
32
  await runCommand('pnpm', ['exec', 'husky'], { cwd });
22
33
  }
23
34
  const target = resolve(husky, 'pre-commit');
24
- mkdirSync(dirname(target), { recursive: true });
25
35
  copyFileSync(HOOK_SOURCE, target);
26
36
  chmodSync(target, 0o755);
27
37
  log(`Wrote ${target}`);
28
38
  if (!existsSync(resolve(husky, '_', 'pre-commit'))) {
29
39
  throw new Error(`husky shims missing in ${husky}/_ — git would silently skip all hooks; run \`pnpm exec husky\``);
30
40
  }
41
+ return true;
31
42
  }
32
43
 
33
44
  export function registerHooks(cli) {
@@ -1,8 +1,13 @@
1
- import { join } from 'node:path';
1
+ import { join, resolve } from 'node:path';
2
2
  import { requireConfig, requireSection } from '../core/config.js';
3
3
  import { generate } from '../core/test-cases/generate.js';
4
4
  import { exitOnFailure } from '../util/run.js';
5
5
 
6
+ /** Pure: `--testers` accepts a path relative to the cwd or an absolute one. */
7
+ export function resolveTestersPath(cwd, arg) {
8
+ return resolve(cwd, arg);
9
+ }
10
+
6
11
  /**
7
12
  * `test-cases:generate` — the acceptance-case workbooks from `<testCases.dir>/tc-data-*.ts`.
8
13
  * Languages come from `testCases.languages`, the title from `testCases.title` (default: the
@@ -23,7 +28,7 @@ export function registerTestCases(cli) {
23
28
  languages: tc.languages,
24
29
  project: tc.title ?? loaded.pkg.name ?? loaded.rootName,
25
30
  onlyLang: opts.lang ?? null,
26
- testersFile: opts.testers ? join(process.cwd(), opts.testers) : null,
31
+ testersFile: opts.testers ? resolveTestersPath(process.cwd(), opts.testers) : null,
27
32
  });
28
33
  }),
29
34
  ),