@coding-with-hassan/devkit 0.3.0 → 1.0.0-next.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/README.md +80 -1
- package/package.json +6 -2
- package/src/commands/config.js +117 -0
- package/src/commands/docker.js +22 -2
- package/src/commands/init.js +127 -0
- package/src/commands/worktree.js +37 -0
- package/src/core/agents-docs.js +225 -0
- package/src/core/compose.js +69 -0
- package/src/core/config.js +316 -0
- package/src/core/derive.js +130 -0
- package/src/core/env-block.js +76 -0
- package/src/core/exec.js +45 -0
- package/src/core/git.js +77 -0
- package/src/core/locks.js +60 -0
- package/src/core/root.js +30 -0
- package/src/core/scaffold.js +131 -0
- package/src/core/stack.js +63 -0
- package/src/core/tracker/github.js +29 -0
- package/src/core/tracker/index.js +37 -0
- package/src/core/tracker/jira.js +101 -0
- package/src/core/workspace.js +105 -0
- package/src/core/worktrees.js +564 -0
- package/src/index.js +6 -0
- package/src/util/config.js +14 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { parseEnv } from '../env-block.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Jira adapter. On `worktree:create`: whoever starts the work owns the ticket, so the
|
|
8
|
+
* issue is assigned to the current user regardless of status, and a `To Do` issue moves
|
|
9
|
+
* to `In Progress`. Best effort throughout — every failure is a `note:`.
|
|
10
|
+
*
|
|
11
|
+
* Credentials: `JIRA_EMAIL`, `JIRA_API_TOKEN` and optionally `JIRA_URL` (or `JIRA_BASE_URL`)
|
|
12
|
+
* from the environment, else from `~/.config/jira/env` (KEY=value lines).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const JIRA_ENV_FILE = join(homedir(), '.config', 'jira', 'env');
|
|
16
|
+
|
|
17
|
+
/** Pure: pick credentials from the process env, falling back to the env file's text. */
|
|
18
|
+
export function resolveJiraCredentials(env, fileText, baseUrlFromConfig) {
|
|
19
|
+
const file = fileText ? unquote(parseEnv(fileText)) : {};
|
|
20
|
+
const pick = (k) => env[k] ?? file[k] ?? null;
|
|
21
|
+
return {
|
|
22
|
+
email: pick('JIRA_EMAIL'),
|
|
23
|
+
token: pick('JIRA_API_TOKEN'),
|
|
24
|
+
baseUrl: (baseUrlFromConfig ?? pick('JIRA_URL') ?? pick('JIRA_BASE_URL') ?? null)?.replace(/\/+$/, '') ?? null,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function unquote(obj) {
|
|
29
|
+
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, v.replace(/^(['"])(.*)\1$/, '$2')]));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function loadJiraCredentials(tracker) {
|
|
33
|
+
const text = existsSync(JIRA_ENV_FILE) ? readFileSync(JIRA_ENV_FILE, 'utf8') : null;
|
|
34
|
+
return resolveJiraCredentials(process.env, text, tracker.baseUrl);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function api(creds, method, path, body) {
|
|
38
|
+
const res = await fetch(`${creds.baseUrl}/rest/api/3${path}`, {
|
|
39
|
+
method,
|
|
40
|
+
headers: {
|
|
41
|
+
Authorization: `Basic ${Buffer.from(`${creds.email}:${creds.token}`).toString('base64')}`,
|
|
42
|
+
Accept: 'application/json',
|
|
43
|
+
...(body ? { 'Content-Type': 'application/json' } : {}),
|
|
44
|
+
},
|
|
45
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
46
|
+
signal: AbortSignal.timeout(15000),
|
|
47
|
+
});
|
|
48
|
+
if (!res.ok) throw new Error(`${method} ${path} → HTTP ${res.status}`);
|
|
49
|
+
const text = await res.text();
|
|
50
|
+
return text ? JSON.parse(text) : null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const jiraAdapter = {
|
|
54
|
+
async startTicket({ key, tracker, log, note }) {
|
|
55
|
+
const creds = loadJiraCredentials(tracker);
|
|
56
|
+
if (!creds.email || !creds.token || !creds.baseUrl) {
|
|
57
|
+
note(`Jira credentials unavailable (set JIRA_EMAIL/JIRA_API_TOKEN/JIRA_URL or ${JIRA_ENV_FILE}) — assign ${key} to yourself and move it to In Progress`);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
let me = null;
|
|
61
|
+
try {
|
|
62
|
+
me = (await api(creds, 'GET', '/myself'))?.accountId ?? null;
|
|
63
|
+
} catch (e) {
|
|
64
|
+
note(`Jira unreachable (${e.message}) — assign ${key} to yourself and move it to In Progress`);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
await api(creds, 'PUT', `/issue/${key}/assignee`, { accountId: me });
|
|
69
|
+
log(`Jira: ${key} assigned to you`);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
note(`assigning ${key} failed (${e.message}) — assign it to yourself in Jira`);
|
|
72
|
+
}
|
|
73
|
+
let status = null;
|
|
74
|
+
try {
|
|
75
|
+
status = (await api(creds, 'GET', `/issue/${key}?fields=status`))?.fields?.status?.name ?? null;
|
|
76
|
+
} catch {
|
|
77
|
+
status = null;
|
|
78
|
+
}
|
|
79
|
+
if (status !== 'To Do') {
|
|
80
|
+
log(`Jira: ${key} is '${status ?? 'unreachable'}' — leaving its status alone`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
let transitionId = null;
|
|
84
|
+
try {
|
|
85
|
+
const { transitions = [] } = (await api(creds, 'GET', `/issue/${key}/transitions`)) ?? {};
|
|
86
|
+
transitionId = transitions.find((t) => t.to?.name === 'In Progress')?.id ?? null;
|
|
87
|
+
} catch {
|
|
88
|
+
transitionId = null;
|
|
89
|
+
}
|
|
90
|
+
if (!transitionId) {
|
|
91
|
+
note(`${key} has no To Do → In Progress transition — move it yourself`);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
await api(creds, 'POST', `/issue/${key}/transitions`, { transition: { id: transitionId } });
|
|
96
|
+
log(`Jira: ${key} → In Progress`);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
note(`transitioning ${key} failed (${e.message}) — move it to In Progress yourself`);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { basename, join, relative } from 'node:path';
|
|
3
|
+
import { parse as parseYaml } from 'yaml';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Workspace projects are derived from `pnpm-workspace.yaml`, never declared: every
|
|
7
|
+
* directory matched by a `packages:` glob that holds a package.json is a project.
|
|
8
|
+
*
|
|
9
|
+
* name — the directory basename (`apps/web-admin` → `web-admin`); this is what commit
|
|
10
|
+
* scopes, lint-staged targets and Nx project names are keyed on
|
|
11
|
+
* kind — `api` (has nest-cli.json), `spa` (has angular.json), else `lib`
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', '.nx', '.angular', 'www']);
|
|
15
|
+
|
|
16
|
+
export function parseWorkspaceGlobs(yamlText) {
|
|
17
|
+
const doc = parseYaml(yamlText) ?? {};
|
|
18
|
+
const globs = Array.isArray(doc.packages) ? doc.packages : [];
|
|
19
|
+
return globs.filter((g) => typeof g === 'string' && g.trim()).map((g) => g.trim().replace(/\/+$/, ''));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function subdirs(dir) {
|
|
23
|
+
let entries;
|
|
24
|
+
try {
|
|
25
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
26
|
+
} catch {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
return entries
|
|
30
|
+
.filter((e) => e.isDirectory() && !SKIP_DIRS.has(e.name) && !e.name.startsWith('.'))
|
|
31
|
+
.map((e) => join(dir, e.name));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function segmentMatcher(segment) {
|
|
35
|
+
const re = new RegExp(`^${segment.split('*').map((s) => s.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('[^/]*')}$`);
|
|
36
|
+
return (name) => re.test(name);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Expand one pnpm-style glob (`apps/*`, `packages/**`, `tools/cli`) to existing dirs. */
|
|
40
|
+
export function expandGlob(root, pattern) {
|
|
41
|
+
const segments = pattern.split('/').filter((s) => s && s !== '.');
|
|
42
|
+
const results = new Set();
|
|
43
|
+
const walk = (dir, segs) => {
|
|
44
|
+
if (segs.length === 0) {
|
|
45
|
+
if (existsSync(dir) && statSync(dir).isDirectory() && dir !== root) results.add(dir);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const [seg, ...rest] = segs;
|
|
49
|
+
if (seg === '**') {
|
|
50
|
+
walk(dir, rest);
|
|
51
|
+
for (const sub of subdirs(dir)) walk(sub, segs);
|
|
52
|
+
} else if (seg.includes('*')) {
|
|
53
|
+
const matches = segmentMatcher(seg);
|
|
54
|
+
for (const sub of subdirs(dir)) if (matches(basename(sub))) walk(sub, rest);
|
|
55
|
+
} else {
|
|
56
|
+
walk(join(dir, seg), rest);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
walk(root, segments);
|
|
60
|
+
return [...results];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function detectKind(dir) {
|
|
64
|
+
if (existsSync(join(dir, 'nest-cli.json'))) return 'api';
|
|
65
|
+
if (existsSync(join(dir, 'angular.json'))) return 'spa';
|
|
66
|
+
return 'lib';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* List the workspace projects of `root`, sorted by relative directory.
|
|
71
|
+
* Negated globs (`!apps/legacy`) remove matches. Directories without a package.json are
|
|
72
|
+
* ignored, like pnpm does.
|
|
73
|
+
*/
|
|
74
|
+
export function listProjects(root) {
|
|
75
|
+
const wsFile = join(root, 'pnpm-workspace.yaml');
|
|
76
|
+
if (!existsSync(wsFile)) return [];
|
|
77
|
+
const globs = parseWorkspaceGlobs(readFileSync(wsFile, 'utf8'));
|
|
78
|
+
const included = new Set();
|
|
79
|
+
const excluded = new Set();
|
|
80
|
+
for (const glob of globs) {
|
|
81
|
+
const negated = glob.startsWith('!');
|
|
82
|
+
const dirs = expandGlob(root, negated ? glob.slice(1) : glob);
|
|
83
|
+
for (const d of dirs) (negated ? excluded : included).add(d);
|
|
84
|
+
}
|
|
85
|
+
const projects = [];
|
|
86
|
+
for (const dir of included) {
|
|
87
|
+
if (excluded.has(dir)) continue;
|
|
88
|
+
const pkgFile = join(dir, 'package.json');
|
|
89
|
+
if (!existsSync(pkgFile)) continue;
|
|
90
|
+
let packageName = null;
|
|
91
|
+
try {
|
|
92
|
+
packageName = JSON.parse(readFileSync(pkgFile, 'utf8')).name ?? null;
|
|
93
|
+
} catch {
|
|
94
|
+
packageName = null;
|
|
95
|
+
}
|
|
96
|
+
projects.push({
|
|
97
|
+
name: basename(dir),
|
|
98
|
+
dir: relative(root, dir).split('\\').join('/'),
|
|
99
|
+
kind: detectKind(dir),
|
|
100
|
+
packageName,
|
|
101
|
+
hasEslint: readdirSync(dir).some((f) => /^eslint\.config\.(c|m)?js$/.test(f)),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return projects.sort((a, b) => a.dir.localeCompare(b.dir));
|
|
105
|
+
}
|