-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
329 lines (286 loc) · 11.5 KB
/
Copy pathcli.js
File metadata and controls
329 lines (286 loc) · 11.5 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// ── Load ontology ──────────────────────────────────────────────────────
const ONTOLOGY = JSON.parse(
fs.readFileSync(path.join(__dirname, 'ontology.json'), 'utf8')
);
const { elements, families } = ONTOLOGY;
// Build lookup indices
const byId = {};
const bySymbol = {};
const byName = {};
const byFamily = {};
for (const el of elements) {
byId[el.id] = el;
bySymbol[el.symbol.toLowerCase()] = el;
byName[el.name.toLowerCase()] = el;
if (!byFamily[el.family]) byFamily[el.family] = [];
byFamily[el.family].push(el);
}
// ── Formatting helpers ────────────────────────────────────────────────
const BOLD = (s) => `\x1b[1m${s}\x1b[22m`;
const DIM = (s) => `\x1b[2m${s}\x1b[22m`;
const CYAN = (s) => `\x1b[36m${s}\x1b[39m`;
const GREEN = (s) => `\x1b[32m${s}\x1b[39m`;
const YELLOW = (s) => `\x1b[33m${s}\x1b[39m`;
const MAGENTA = (s) => `\x1b[35m${s}\x1b[39m`;
const RED = (s) => `\x1b[31m${s}\x1b[39m`;
const FAMILY_COLORS = {
objects: CYAN,
properties: GREEN,
actions: YELLOW,
interfaces: MAGENTA,
intelligence: BOLD,
rules: RED,
};
function formatElement(el, index = false) {
const color = FAMILY_COLORS[el.family] || ((s) => s);
const sym = CYAN(el.symbol.padEnd(3));
const name = BOLD(el.name.padEnd(14));
const family = DIM(`(${el.family})`).padEnd(14);
const desc = el.description;
const idx = index ? `${String(el.id).padEnd(4)}` : '';
return ` ${idx}${sym} ${name} ${family} ${desc}`;
}
function formatFamily(el) {
return FAMILY_COLORS[el.family] ? FAMILY_COLORS[el.family](el.family) : el.family;
}
// ── Commands ──────────────────────────────────────────────────────────
function cmdList(args) {
const familyFilter = args[0] ? args[0].toLowerCase() : null;
let items = elements;
if (familyFilter) {
const family = families.find(
(f) => f.id === familyFilter || f.name.toLowerCase().startsWith(familyFilter)
);
if (!family) {
console.log(`Unknown family: "${args[0]}". Try: ${families.map((f) => f.id).join(', ')}`);
process.exit(1);
}
items = byFamily[family.id];
}
for (const el of items) {
console.log(formatElement(el, true));
}
console.log(DIM(`\n ${items.length} elements`));
console.log(DIM(` Source: github.com/NullLabTests/software-periodic-table`));
}
function cmdSearch(args) {
const query = args.join(' ').toLowerCase();
if (!query) {
console.log('Usage: periodic search <query>');
process.exit(1);
}
const results = elements.filter(
(el) =>
el.name.toLowerCase().includes(query) ||
el.symbol.toLowerCase().includes(query) ||
el.description.toLowerCase().includes(query) ||
el.family.toLowerCase().includes(query)
);
if (results.length === 0) {
console.log(`No elements match "${query}"`);
return;
}
for (const el of results) {
console.log(formatElement(el, true));
}
console.log(DIM(`\n ${results.length} matches`));
}
function cmdFamily(args) {
if (args.length === 0) {
console.log(BOLD('\n Families\n'));
for (const f of families) {
const count = byFamily[f.id].length;
const color = FAMILY_COLORS[f.id] || ((s) => s);
console.log(` ${color(f.name.padEnd(22))} ${DIM(`${count} elements`)}`);
}
return;
}
const familyName = args[0].toLowerCase();
const family = families.find(
(f) => f.id === familyName || f.name.toLowerCase().startsWith(familyName)
);
if (!family) {
console.log(`Unknown family: "${args[0]}". Try: ${families.map((f) => f.id).join(', ')}`);
process.exit(1);
}
console.log(BOLD(`\n ${family.name}`));
console.log(DIM(` ${family.description}\n`));
for (const el of byFamily[family.id]) {
console.log(formatElement(el, true));
}
console.log(DIM(`\n ${byFamily[family.id].length} elements`));
}
function cmdShow(args) {
const query = args.join(' ').toLowerCase().trim();
if (!query) {
console.log('Usage: periodic show <name-or-symbol>');
process.exit(1);
}
const el =
bySymbol[query] ||
byName[query] ||
elements.find(
(e) => e.symbol.toLowerCase() === query || e.name.toLowerCase() === query
);
if (!el) {
console.log(`Element not found: "${query}"`);
process.exit(1);
}
const color = FAMILY_COLORS[el.family] || ((s) => s);
console.log(BOLD(`\n ${el.symbol} — ${el.name}`));
console.log(` ${DIM('ID:')} ${el.id}`);
console.log(` ${DIM('Family:')} ${color(el.family)}`);
console.log(` ${DIM('Desc:')} ${el.description}`);
}
function cmdCompose(args) {
const feature = args.join(' ').trim();
if (!feature) {
console.log('Usage: periodic compose <feature description>');
console.log(DIM('\n Suggests Periodic Table atoms for a feature request.'));
process.exit(1);
}
const query = feature.toLowerCase();
// Score elements by keyword match
const scored = elements.map((el) => {
let score = 0;
const words = query.split(/\s+/);
for (const word of words) {
if (el.name.toLowerCase().includes(word)) score += 3;
if (el.description.toLowerCase().includes(word)) score += 2;
if (el.symbol.toLowerCase() === word) score += 5;
if (el.family === 'objects' && ['user', 'task', 'project', 'item', 'entity'].includes(word)) score += 1;
}
// Boost common patterns
if (el.family === 'objects' && ['manage', 'track', 'handle', 'organize'].some((w) => query.includes(w))) score += 1;
if (el.family === 'properties' && ['field', 'attribute', 'detail', 'info'].some((w) => query.includes(w))) score += 1;
if (el.family === 'actions' && ['do', 'perform', 'execute', 'operate', 'action'].some((w) => query.includes(w))) score += 1;
return { el, score };
});
const relevant = scored.filter((s) => s.score > 0).sort((a, b) => b.score - a.score);
if (relevant.length === 0) {
console.log(`No relevant atoms found for "${feature}"`);
console.log(DIM(' Try different keywords, or browse all with: periodic list'));
return;
}
console.log(BOLD(`\n Composition suggestion for: "${feature}"\n`));
// Group by family, sorted by total score per family
const byFam = {};
for (const { el, score } of relevant) {
if (!byFam[el.family]) byFam[el.family] = { elements: [], totalScore: 0 };
byFam[el.family].elements.push({ el, score });
byFam[el.family].totalScore += score;
}
const familyOrder = ['objects', 'properties', 'actions', 'interfaces', 'intelligence', 'rules'];
for (const famId of familyOrder) {
if (!byFam[famId]) continue;
const fam = families.find((f) => f.id === famId);
const color = FAMILY_COLORS[famId] || ((s) => s);
byFam[famId].elements.sort((a, b) => b.score - a.score);
console.log(color(BOLD(` ${fam ? fam.name : famId}`)));
for (const { el, score } of byFam[famId].elements.slice(0, 8)) {
const bar = DIM('·'.repeat(Math.min(score, 10)));
console.log(` ${CYAN(el.symbol.padEnd(3))} ${BOLD(el.name.padEnd(14))} ${DIM(el.description)} ${bar}`);
}
console.log('');
}
console.log(DIM(' Compose, don\'t regenerate — wire these atoms together.'));
console.log(DIM(' Source: github.com/NullLabTests/software-periodic-table'));
}
function cmdPlan() {
console.log(BOLD('\n Composition Plan Schema'));
console.log(DIM(' When building a feature, emit a plan with these fields:\n'));
console.log(' objects: Nouns/entities involved (e.g. User, Task, Invoice)');
console.log(' properties: Attributes needed (e.g. Status, Owner, CreatedAt)');
console.log(' actions: Operations required (e.g. Create, Update, Notify)');
console.log(' interfaces: Views to present (e.g. Table, Kanban, Form)');
console.log(' intelligence: AI primitives (optional) (e.g. Search, Summarize)');
console.log(' rules: Governance (optional) (e.g. Permission, Trigger)');
console.log('');
console.log(DIM(' Core principle: prefer selecting and wiring existing atoms'));
console.log(DIM(' over reimplementing the same patterns from scratch.'));
}
function cmdStats() {
console.log(BOLD('\n Periodic Table Statistics\n'));
console.log(` ${'Total elements:'.padEnd(20)} ${BOLD(String(elements.length))}`);
console.log(` ${'Families:'.padEnd(20)} ${BOLD(String(families.length))}`);
const famOrder = ['objects', 'properties', 'actions', 'interfaces', 'intelligence', 'rules'];
for (const famId of famOrder) {
const fam = families.find((f) => f.id === famId);
const count = byFamily[famId] ? byFamily[famId].length : 0;
const color = FAMILY_COLORS[famId] || ((s) => s);
const pct = ((count / elements.length) * 100).toFixed(1);
console.log(` ${color(fam ? fam.name.padEnd(16) : famId)} ${String(count).padStart(3)} elements (${pct}%)`);
}
// Find longest descriptions
const byDescLen = [...elements].sort((a, b) => b.description.length - a.description.length);
console.log(DIM(`\n Longest description: ${byDescLen[0].symbol} — ${byDescLen[0].name}`));
// Source
console.log(DIM('\n Source: github.com/NullLabTests/software-periodic-table'));
}
function cmdVersion() {
console.log('periodic v1.0.0 (@shift-zero/periodic-table-cli)');
console.log(DIM('Software Periodic Table — 115 elements, 6 families'));
console.log(DIM('Based on NullLabTests/software-periodic-table'));
}
function cmdHelp() {
console.log(BOLD('\n periodic — CLI for the Software Periodic Table\n'));
console.log(' Usage: periodic <command> [args]\n');
console.log(' Commands:');
console.log(' list [family] List all elements, or by family');
console.log(' search <query> Search elements by name, symbol, or description');
console.log(' family [name] Show family info or elements in a family');
console.log(' show <id> Show detailed info about an element');
console.log(' compose <desc> Suggest atoms for a feature description');
console.log(' plan Show the composition plan schema');
console.log(' families List all families with counts');
console.log(' stats Show ontology statistics');
console.log(' version / -v Show version info');
console.log(' help / --help Show this help\n');
console.log(' Examples:');
console.log(' periodic search task');
console.log(' periodic family objects');
console.log(' periodic compose "Build a task board with assignees"');
console.log(' periodic show User\n');
console.log(DIM(' Based on the Software Periodic Table by NullLabTests'));
}
// ── Main ──────────────────────────────────────────────────────────────
const args = process.argv.slice(2);
const cmd = args[0] || 'help';
switch (cmd) {
case 'list':
cmdList(args.slice(1));
break;
case 'search':
cmdSearch(args.slice(1));
break;
case 'family':
case 'families':
cmdFamily(args.slice(1));
break;
case 'stats':
cmdStats();
break;
case 'show':
cmdShow(args.slice(1));
break;
case 'compose':
cmdCompose(args.slice(1));
break;
case 'plan':
cmdPlan();
break;
case 'version':
case '-v':
case '--version':
cmdVersion();
break;
case 'help':
case '--help':
case '-h':
default:
cmdHelp();
break;
}