@coding-with-hassan/devkit 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Hassan Khousheish
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # @coding-with-hassan/devkit
2
+
3
+ CLI that wraps the developer workflow shared across Hassan's client projects: docker compose orchestration, postgres helpers, and the husky pre-commit hook.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add -D @coding-with-hassan/devkit
9
+ ```
10
+
11
+ ## Configure
12
+
13
+ Add a `hassan-devkit` block to your client repo's root `package.json` so the db commands know which container to talk to:
14
+
15
+ ```json
16
+ {
17
+ "hassan-devkit": {
18
+ "db": {
19
+ "container": "de-autozaak-db",
20
+ "user": "postgres",
21
+ "name": "de_autozaak"
22
+ }
23
+ }
24
+ }
25
+ ```
26
+
27
+ The docker commands assume `docker/docker-compose.dev.yml` and a `.env` file at the repo root (the convention the template repo ships with).
28
+
29
+ ## Wire it into your scripts
30
+
31
+ ```json
32
+ {
33
+ "scripts": {
34
+ "docker:dev:up": "hassan-devkit docker:up",
35
+ "docker:dev:down": "hassan-devkit docker:down",
36
+ "docker:dev:logs": "hassan-devkit docker:logs",
37
+ "db:list-tables": "hassan-devkit db:list-tables",
38
+ "db:psql": "hassan-devkit db:psql",
39
+ "prepare": "hassan-devkit hooks:install"
40
+ }
41
+ }
42
+ ```
43
+
44
+ ## Commands
45
+
46
+ ### Docker
47
+
48
+ - `hassan-devkit docker:up` — `docker compose up -d --build`
49
+ - `hassan-devkit docker:down` — `docker compose down`
50
+ - `hassan-devkit docker:logs` — tail logs
51
+ - `hassan-devkit docker:restart <service>` — restart a single container by name
52
+
53
+ ### Database
54
+
55
+ - `hassan-devkit db:list-tables` — `\dt`
56
+ - `hassan-devkit db:describe <table>` — `\d <table>`
57
+ - `hassan-devkit db:count <table>` — `SELECT COUNT(*) FROM <table>`
58
+ - `hassan-devkit db:truncate <table>` — `TRUNCATE TABLE <table> CASCADE`
59
+ - `hassan-devkit db:psql` — interactive psql shell
60
+
61
+ ### Husky hooks
62
+
63
+ - `hassan-devkit hooks:install` — installs husky if needed, then writes the shared `pre-commit` hook content into `.husky/pre-commit`. Re-run after bumping `@coding-with-hassan/devkit` to pick up changes to the hook.
64
+ - `hassan-devkit pre-commit` — runs the hook body directly. Used internally; the installed `.husky/pre-commit` file invokes this.
65
+ - `hassan-devkit hooks:print` — print the hook content to stdout (for inspection / diffing).
66
+
67
+ ### Per-project extras
68
+
69
+ If a project needs to run something extra before the standard lint-staged step (e.g., the de-autozaak SPA's translation consistency check), drop a `scripts/pre-commit-extra.sh` in the repo root. It runs first, must `exit 0` to continue.
package/bin/cli.js ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { run } from '../src/index.js';
3
+
4
+ run(process.argv);
@@ -0,0 +1,10 @@
1
+ # Generated by @coding-with-hassan/devkit — do not edit by hand.
2
+ # Re-run `hassan-devkit hooks:install` to refresh after a devkit bump.
3
+
4
+ # Run project pre-commit body (optional, only if it exists)
5
+ if [ -f "scripts/pre-commit-extra.sh" ]; then
6
+ sh scripts/pre-commit-extra.sh || exit 1
7
+ fi
8
+
9
+ # Project-aware lint/format on staged files
10
+ pnpm exec lint-staged
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@coding-with-hassan/devkit",
3
+ "version": "0.1.0",
4
+ "description": "CLI for the developer workflow shared across Hassan's client projects: docker compose orchestration, db helpers, and husky hook installation.",
5
+ "license": "MIT",
6
+ "author": "Hassan Khousheish",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/CodiNG-With-Hassan/hassan-devkit.git",
10
+ "directory": "packages/devkit"
11
+ },
12
+ "keywords": [
13
+ "cli",
14
+ "docker",
15
+ "husky",
16
+ "lint-staged",
17
+ "monorepo"
18
+ ],
19
+ "type": "module",
20
+ "bin": {
21
+ "hassan-devkit": "./bin/cli.js"
22
+ },
23
+ "files": [
24
+ "bin",
25
+ "src",
26
+ "hooks",
27
+ "README.md"
28
+ ],
29
+ "dependencies": {
30
+ "cac": "^6.7.14"
31
+ },
32
+ "engines": {
33
+ "node": ">=20"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ }
38
+ }
@@ -0,0 +1,48 @@
1
+ import { runCommand, exitOnFailure } from '../util/run.js';
2
+ import { requireDbConfig } from '../util/config.js';
3
+
4
+ function psqlExec(sql) {
5
+ const db = requireDbConfig();
6
+ return runCommand('docker', [
7
+ 'exec',
8
+ db.container,
9
+ 'psql',
10
+ '-U',
11
+ db.user,
12
+ '-d',
13
+ db.name,
14
+ '-c',
15
+ sql,
16
+ ]);
17
+ }
18
+
19
+ function psqlShell() {
20
+ const db = requireDbConfig();
21
+ return runCommand(
22
+ 'docker',
23
+ ['exec', '-it', db.container, 'psql', '-U', db.user, '-d', db.name],
24
+ { stdio: 'inherit' },
25
+ );
26
+ }
27
+
28
+ export function registerDb(cli) {
29
+ cli
30
+ .command('db:list-tables', 'List tables in the dev database')
31
+ .action(() => exitOnFailure(psqlExec('\\dt')));
32
+
33
+ cli
34
+ .command('db:describe <table>', 'Describe a table')
35
+ .action((table) => exitOnFailure(psqlExec(`\\d ${table}`)));
36
+
37
+ cli
38
+ .command('db:count <table>', 'Count rows in a table')
39
+ .action((table) => exitOnFailure(psqlExec(`SELECT COUNT(*) FROM ${table};`)));
40
+
41
+ cli
42
+ .command('db:truncate <table>', 'Truncate a table (with CASCADE)')
43
+ .action((table) => exitOnFailure(psqlExec(`TRUNCATE TABLE ${table} CASCADE;`)));
44
+
45
+ cli
46
+ .command('db:psql', 'Open an interactive psql shell against the dev database')
47
+ .action(() => exitOnFailure(psqlShell()));
48
+ }
@@ -0,0 +1,26 @@
1
+ import { runCommand, exitOnFailure } from '../util/run.js';
2
+
3
+ const COMPOSE_FILE = 'docker/docker-compose.dev.yml';
4
+ const ENV_FILE = '.env';
5
+
6
+ function compose(args) {
7
+ return runCommand('docker', ['compose', '--env-file', ENV_FILE, '-f', COMPOSE_FILE, ...args]);
8
+ }
9
+
10
+ export function registerDocker(cli) {
11
+ cli
12
+ .command('docker:up', 'Build and start the dev docker compose stack')
13
+ .action(() => exitOnFailure(compose(['up', '-d', '--build'])));
14
+
15
+ cli
16
+ .command('docker:down', 'Stop the dev docker compose stack')
17
+ .action(() => exitOnFailure(compose(['down'])));
18
+
19
+ cli
20
+ .command('docker:logs', 'Tail logs for the dev docker compose stack')
21
+ .action(() => exitOnFailure(compose(['logs', '-f'])));
22
+
23
+ cli
24
+ .command('docker:restart <service>', 'Restart a single container')
25
+ .action((service) => exitOnFailure(runCommand('docker', ['restart', service])));
26
+ }
@@ -0,0 +1,40 @@
1
+ import { copyFileSync, mkdirSync, chmodSync, existsSync, readFileSync } from 'node:fs';
2
+ import { resolve, dirname } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { runCommand, exitOnFailure } from '../util/run.js';
5
+
6
+ const here = dirname(fileURLToPath(import.meta.url));
7
+ const HOOK_SOURCE = resolve(here, '../../hooks/pre-commit.sh');
8
+
9
+ export function registerHooks(cli) {
10
+ cli
11
+ .command('hooks:install', 'Install husky and the shared pre-commit hook into the current repo')
12
+ .action(() =>
13
+ exitOnFailure(
14
+ (async () => {
15
+ if (!existsSync('.husky')) {
16
+ await runCommand('pnpm', ['exec', 'husky', 'init']);
17
+ }
18
+ const target = resolve(process.cwd(), '.husky/pre-commit');
19
+ mkdirSync(dirname(target), { recursive: true });
20
+ copyFileSync(HOOK_SOURCE, target);
21
+ chmodSync(target, 0o755);
22
+ console.log(`Wrote ${target}`);
23
+ })(),
24
+ ),
25
+ );
26
+
27
+ cli
28
+ .command('pre-commit', 'Body of the shared pre-commit hook (called from .husky/pre-commit)')
29
+ .action(() =>
30
+ exitOnFailure(
31
+ runCommand('sh', [HOOK_SOURCE]),
32
+ ),
33
+ );
34
+
35
+ cli
36
+ .command('hooks:print', 'Print the shared pre-commit hook content to stdout')
37
+ .action(() => {
38
+ process.stdout.write(readFileSync(HOOK_SOURCE, 'utf8'));
39
+ });
40
+ }
package/src/index.js ADDED
@@ -0,0 +1,20 @@
1
+ import { cac } from 'cac';
2
+ import { registerDocker } from './commands/docker.js';
3
+ import { registerDb } from './commands/db.js';
4
+ import { registerHooks } from './commands/hooks.js';
5
+
6
+ export function run(argv) {
7
+ const cli = cac('hassan-devkit');
8
+
9
+ registerDocker(cli);
10
+ registerDb(cli);
11
+ registerHooks(cli);
12
+
13
+ cli.help();
14
+ cli.version(readVersion());
15
+ cli.parse(argv);
16
+ }
17
+
18
+ function readVersion() {
19
+ return process.env.npm_package_version ?? '0.0.0';
20
+ }
@@ -0,0 +1,31 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { resolve } from 'node:path';
3
+
4
+ /**
5
+ * Read the consumer's `hassan-devkit` config block from their package.json.
6
+ *
7
+ * {
8
+ * "hassan-devkit": {
9
+ * "db": { "container": "...", "user": "postgres", "name": "..." }
10
+ * }
11
+ * }
12
+ */
13
+ export function readDevkitConfig() {
14
+ const pkgPath = resolve(process.cwd(), 'package.json');
15
+ try {
16
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
17
+ return pkg['hassan-devkit'] ?? {};
18
+ } catch {
19
+ return {};
20
+ }
21
+ }
22
+
23
+ export function requireDbConfig() {
24
+ const cfg = readDevkitConfig().db;
25
+ if (!cfg?.container || !cfg?.user || !cfg?.name) {
26
+ throw new Error(
27
+ 'Missing db config. Add a "hassan-devkit": { "db": { "container": "...", "user": "...", "name": "..." } } block to package.json.',
28
+ );
29
+ }
30
+ return cfg;
31
+ }
@@ -0,0 +1,19 @@
1
+ import { spawn } from 'node:child_process';
2
+
3
+ export function runCommand(cmd, args, opts = {}) {
4
+ return new Promise((resolve, reject) => {
5
+ const child = spawn(cmd, args, { stdio: 'inherit', ...opts });
6
+ child.on('error', reject);
7
+ child.on('exit', (code) => {
8
+ if (code === 0) resolve();
9
+ else reject(new Error(`${cmd} ${args.join(' ')} exited with code ${code}`));
10
+ });
11
+ });
12
+ }
13
+
14
+ export function exitOnFailure(promise) {
15
+ promise.catch((err) => {
16
+ console.error(err.message ?? err);
17
+ process.exit(1);
18
+ });
19
+ }