1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180import { spawnSync } from 'child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync, statSync } from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as crypto from 'crypto';
import { ProjectMetadata } from '../types/index.js';
/**
* Project Identifier
*
* Generates unique project IDs based on Git remote URL and manages project metadata
*/
export class ProjectIdentifier {
/**
* Generate unique project ID from git remote URL
* Uses SHA-256 hash of origin URL (first 8 characters)
*
* @param repoRoot - Absolute path to repository root
* @returns Project ID (8-character hex string)
*/
static generateId(repoRoot: string): string {
const remoteUrl = this.getRemoteUrl(repoRoot);
const hash = crypto
.createHash('sha256')
.update(remoteUrl)
.digest('hex')
.substring(0, 8); // First 8 chars
return hash;
}
/**
* Get git remote origin URL
*
* @param repoRoot - Absolute path to repository root
* @returns Remote origin URL
*/
private static getRemoteUrl(repoRoot: string): string {
const result = spawnSync('git', ['remote', 'get-url', 'origin'], {
cwd: repoRoot,
encoding: 'utf8',
stdio: 'pipe'
});
if (result.status !== 0 || !result.stdout) {
// Fallback to repo path if no remote
return repoRoot;
}
return result.stdout.trim();
}
/**
* Get project directory in ~/.workforge/projects/<project-id>
*
* @param repoRoot - Absolute path to repository root
* @returns Absolute path to project directory
*/
static getProjectDir(repoRoot: string): string {
const id = this.generateId(repoRoot);
return path.join(os.homedir(), '.workforge', 'projects', id);
}
/**
* Get backup directory for project
*
* @param repoRoot - Absolute path to repository root
* @returns Absolute path to backup directory
*/
static getBackupDir(repoRoot: string): string {
const id = this.generateId(repoRoot);
return path.join(os.homedir(), '.workforge', 'backups', id);
}
/**
* Create or update project metadata
* Creates .meta.json file in project directory
*
* @param repoRoot - Absolute path to repository root
*/
static updateMetadata(repoRoot: string): void {
const projectDir = this.getProjectDir(repoRoot);
mkdirSync(projectDir, { recursive: true });
const metaPath = path.join(projectDir, '.meta.json');
// Load existing metadata if present
let existingMeta: Partial<ProjectMetadata> = {};
if (existsSync(metaPath)) {
try {
existingMeta = JSON.parse(readFileSync(metaPath, 'utf8'));
} catch {
// Ignore parse errors, will create new metadata
}
}
const meta: ProjectMetadata = {
projectId: this.generateId(repoRoot),
remoteUrl: this.getRemoteUrl(repoRoot),
repoPath: repoRoot,
repoName: path.basename(repoRoot),
createdAt: existingMeta.createdAt || new Date().toISOString(),
lastAccessed: new Date().toISOString()
};
writeFileSync(metaPath, JSON.stringify(meta, null, 2), 'utf8');
}
/**
* Get project metadata
*
* @param repoRoot - Absolute path to repository root
* @returns Project metadata or null if not found
*/
static getMetadata(repoRoot: string): ProjectMetadata | null {
const projectDir = this.getProjectDir(repoRoot);
const metaPath = path.join(projectDir, '.meta.json');
if (!existsSync(metaPath)) {
return null;
}
try {
return JSON.parse(readFileSync(metaPath, 'utf8'));
} catch {
return null;
}
}
/**
* Get all project directories
*
* @returns Array of project directory paths
*/
static getAllProjectDirs(): string[] {
const projectsRoot = path.join(os.homedir(), '.workforge', 'projects');
if (!existsSync(projectsRoot)) {
return [];
}
const entries = readdirSync(projectsRoot);
return entries
.map((entry: string) => path.join(projectsRoot, entry))
.filter((entryPath: string) => {
try {
return statSync(entryPath).isDirectory();
} catch {
return false;
}
});
}
/**
* Get all projects metadata
*
* @returns Array of project metadata
*/
static getAllProjects(): ProjectMetadata[] {
const projectDirs = this.getAllProjectDirs();
const projects: ProjectMetadata[] = [];
for (const dir of projectDirs) {
const metaPath = path.join(dir, '.meta.json');
if (existsSync(metaPath)) {
try {
const meta = JSON.parse(readFileSync(metaPath, 'utf8'));
projects.push(meta);
} catch {
// Skip invalid metadata files
}
}
}
return projects;
}
}