@assemblyline/al-orc 0.0.0-stage → 0.2.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/CHANGELOG.md +78 -0
- package/LICENSE +201 -0
- package/NOTICE +5 -0
- package/README.md +98 -2
- package/bin/cli.js +5 -0
- package/package.json +59 -4
- package/src/cli/_helpers.js +68 -0
- package/src/cli/config/add.js +11 -0
- package/src/cli/config/current.js +21 -0
- package/src/cli/config/index.js +64 -0
- package/src/cli/config/list.js +23 -0
- package/src/cli/config/remove.js +7 -0
- package/src/cli/config/use.js +7 -0
- package/src/cli/config-store.js +128 -0
- package/src/cli/factory-sessions/index.js +36 -0
- package/src/cli/factory-sessions/list.js +94 -0
- package/src/cli/factory-sessions/show.js +84 -0
- package/src/cli/index.js +62 -0
- package/src/cli/lines/index.js +54 -0
- package/src/cli/lines/list.js +40 -0
- package/src/cli/lines/pull.js +47 -0
- package/src/cli/lines/push.js +83 -0
- package/src/cli/lines/show.js +37 -0
- package/src/cli/shared/edit-tags.js +44 -0
- package/src/cli/stations/archive.js +25 -0
- package/src/cli/stations/delete.js +9 -0
- package/src/cli/stations/edit.js +31 -0
- package/src/cli/stations/index.js +105 -0
- package/src/cli/stations/list.js +38 -0
- package/src/cli/stations/pull.js +74 -0
- package/src/cli/stations/push.js +213 -0
- package/src/cli/stations/run.js +94 -0
- package/src/cli/stations/show.js +75 -0
- package/src/cli/status.js +24 -0
- package/src/cli/supporting-docs/index.js +22 -0
- package/src/cli/supporting-docs/show.js +50 -0
- package/src/cli/tags/create.js +22 -0
- package/src/cli/tags/delete.js +9 -0
- package/src/cli/tags/edit.js +28 -0
- package/src/cli/tags/index.js +50 -0
- package/src/cli/tags/list.js +25 -0
- package/src/cli/utils/format.js +171 -0
- package/src/cli/utils/line-files.js +85 -0
- package/src/cli/utils/station-files.js +152 -0
- package/src/cli/utils/station-labels.js +49 -0
- package/src/cli/work-records/cancel.js +25 -0
- package/src/cli/work-records/edit.js +23 -0
- package/src/cli/work-records/index.js +61 -0
- package/src/cli/work-records/list.js +49 -0
- package/src/cli/work-records/show.js +104 -0
- package/src/http.js +254 -0
- package/src/index.js +104 -0
- package/src/resources/factory-sessions.js +86 -0
- package/src/resources/lines.js +57 -0
- package/src/resources/stations.js +90 -0
- package/src/resources/supporting-docs.js +37 -0
- package/src/resources/tags.js +68 -0
- package/src/resources/work-records.js +78 -0
- package/src/types/api/Line.types.js +23 -0
- package/src/types/api/Station.types.js +36 -0
- package/src/types/api/SupportingDoc.types.js +20 -0
- package/src/types/api/Tag.types.js +12 -0
- package/src/types/api/WorkRecord.types.js +34 -0
- package/src/types/general/Credentials.types.js +18 -0
- package/src/types/general/Transport.types.js +22 -0
- package/src/types/general/index.js +9 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { clientFor } from '../_helpers.js';
|
|
3
|
+
import { buildStationLabelMap, stationLabel } from '../utils/station-labels.js';
|
|
4
|
+
import { formatHeader, formatField, formatTable, formatSection, formatDate, formatDuration, localTzLabel, truncate } from '../utils/format.js';
|
|
5
|
+
|
|
6
|
+
/** `al-orc stations show <id>` — one station's definition, optionally with recent work records. */
|
|
7
|
+
export async function showStationHandler(argv) {
|
|
8
|
+
const { id, workRecords, all, json } = argv;
|
|
9
|
+
|
|
10
|
+
const orc = clientFor();
|
|
11
|
+
const station = await orc.stations.get(id);
|
|
12
|
+
|
|
13
|
+
if (json) {
|
|
14
|
+
console.log(JSON.stringify(station, null, 2));
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!station) {
|
|
19
|
+
console.error(`Station not found: ${id}`);
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
console.log(formatHeader('Station', station.short_code || station.id));
|
|
24
|
+
const lw = 14;
|
|
25
|
+
console.log(formatField('Name', station.name, lw));
|
|
26
|
+
console.log(formatField('ID', station.id, lw));
|
|
27
|
+
if (station.short_code) console.log(formatField('Short Code', station.short_code, lw));
|
|
28
|
+
if (station.line) console.log(formatField('Line', station.line, lw));
|
|
29
|
+
|
|
30
|
+
const tags = station.tags?.map((t) => (typeof t === 'string' ? t : t.name)).join(', ');
|
|
31
|
+
if (tags) console.log(formatField('Tags', tags, lw));
|
|
32
|
+
|
|
33
|
+
// `dependencies` comes back as bare station ids, so each needs resolving to a
|
|
34
|
+
// short_code — printing them verbatim shows opaque ids where the reader
|
|
35
|
+
// expects `EM0`. Best-effort: an unresolvable ref falls through unchanged.
|
|
36
|
+
let deps = 'none';
|
|
37
|
+
if (station.dependencies?.length) {
|
|
38
|
+
const labels = await buildStationLabelMap(orc);
|
|
39
|
+
deps = station.dependencies
|
|
40
|
+
.map((d) => (typeof d === 'string' ? stationLabel(d, labels) : d.short_code || stationLabel(d.id, labels)))
|
|
41
|
+
.join(', ');
|
|
42
|
+
}
|
|
43
|
+
console.log(formatField('Dependencies', deps, lw));
|
|
44
|
+
|
|
45
|
+
if (station.steps?.length) {
|
|
46
|
+
console.log(`\nSteps (${station.steps.length}):`);
|
|
47
|
+
station.steps.forEach((s, i) => console.log(` ${i + 1}. ${s.slug}`));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (workRecords || all) {
|
|
51
|
+
const records = await orc.workRecords.list({ station: station.id, limit: 10 }).then((r) => r.data || []);
|
|
52
|
+
console.log(formatSection(`Recent Work Records (${records.length})`));
|
|
53
|
+
if (records.length > 0) {
|
|
54
|
+
// No STATION column — this section is already scoped to one station.
|
|
55
|
+
const rows = records.map((r) => [
|
|
56
|
+
r.id,
|
|
57
|
+
r.status || '—',
|
|
58
|
+
formatDate(r.created_at),
|
|
59
|
+
formatDuration(r.started_at, r.completed_at),
|
|
60
|
+
r.performed_by || '—',
|
|
61
|
+
truncate(r.error, 40),
|
|
62
|
+
]);
|
|
63
|
+
console.log(formatTable(
|
|
64
|
+
['ID', 'STATUS', `CREATED (${localTzLabel()})`, 'TOOK', 'BY', 'ERROR'],
|
|
65
|
+
rows,
|
|
66
|
+
));
|
|
67
|
+
// `--station` takes an id or a short_code — suggest the readable form.
|
|
68
|
+
console.log(`\nUse \`al-orc work-records list --station ${station.short_code || station.id}\` for the full list.`);
|
|
69
|
+
} else {
|
|
70
|
+
console.log('No work records found.');
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
console.log('\nUse --work-records or --all for linked entities.');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { clientFor } from './_helpers.js';
|
|
3
|
+
|
|
4
|
+
/** `al-orc status` — probe connectivity to the orchestrator with the active profile. */
|
|
5
|
+
export async function statusHandler(argv) {
|
|
6
|
+
const { json } = argv || {};
|
|
7
|
+
const result = await clientFor().checkConnection();
|
|
8
|
+
|
|
9
|
+
if (json) {
|
|
10
|
+
console.log(JSON.stringify(result, null, 2));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (result.connected) {
|
|
15
|
+
console.log('Status: Connected');
|
|
16
|
+
if (result.org) console.log(`Org: ${result.org.name} (${result.org.id})`);
|
|
17
|
+
if (result.api_key) console.log(`Key: ${result.api_key.name} (${result.api_key.key})`);
|
|
18
|
+
} else {
|
|
19
|
+
console.log('Status: Not connected');
|
|
20
|
+
if (result.error) console.log(`Error: ${result.error}`);
|
|
21
|
+
else if (result.status) console.log(`HTTP: ${result.status}`);
|
|
22
|
+
process.exitCode = 1;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Subtree builder for `al-orc supporting-docs <action>`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { safe, localOptions } from '../_helpers.js';
|
|
7
|
+
import { showSupportingDocHandler } from './show.js';
|
|
8
|
+
|
|
9
|
+
export function buildSupportingDocsSubcommands(yargs) {
|
|
10
|
+
return yargs
|
|
11
|
+
.usage('$0 supporting-docs <action> [options]')
|
|
12
|
+
.command(
|
|
13
|
+
'show <id>',
|
|
14
|
+
'Show a supporting document (--save <path> to download file docs)',
|
|
15
|
+
(y) =>
|
|
16
|
+
localOptions(y.positional('id', { describe: 'Document ID', type: 'string' }))
|
|
17
|
+
.option('save', { describe: 'Download the file to this path', type: 'string' })
|
|
18
|
+
.option('json', { describe: 'Output raw JSON', type: 'boolean' }),
|
|
19
|
+
safe(showSupportingDocHandler),
|
|
20
|
+
)
|
|
21
|
+
.demandCommand(1, 'Specify an action: show');
|
|
22
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { writeFileSync } from 'node:fs';
|
|
3
|
+
import { clientFor } from '../_helpers.js';
|
|
4
|
+
import { formatHeader, formatField, formatBytes, formatDate } from '../utils/format.js';
|
|
5
|
+
|
|
6
|
+
/** `al-orc supporting-docs show <id>` — metadata (+ markdown content); --save <path> downloads files. */
|
|
7
|
+
export async function showSupportingDocHandler(argv) {
|
|
8
|
+
const { id, save, json } = argv;
|
|
9
|
+
|
|
10
|
+
const orc = clientFor();
|
|
11
|
+
const doc = await orc.supportingDocs.get(id);
|
|
12
|
+
|
|
13
|
+
if (json) {
|
|
14
|
+
console.log(JSON.stringify(doc, null, 2));
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!doc) {
|
|
19
|
+
console.error(`Supporting document not found: ${id}`);
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
console.log(formatHeader('Supporting Document', doc.id));
|
|
24
|
+
console.log('');
|
|
25
|
+
const lw = 16;
|
|
26
|
+
console.log(formatField('Title', doc.title ?? doc.name, lw));
|
|
27
|
+
console.log(formatField('Type', doc.type, lw));
|
|
28
|
+
if (doc.step_slug) console.log(formatField('Step', doc.step_slug, lw));
|
|
29
|
+
if (doc.filename) console.log(formatField('Filename', doc.filename, lw));
|
|
30
|
+
if (doc.mime_type) console.log(formatField('MIME Type', doc.mime_type, lw));
|
|
31
|
+
if (doc.size_bytes) console.log(formatField('Size', formatBytes(doc.size_bytes), lw));
|
|
32
|
+
if (doc.work_record_id) console.log(formatField('Work Record', doc.work_record_id, lw));
|
|
33
|
+
console.log(formatField('Created', formatDate(doc.created_at), lw));
|
|
34
|
+
|
|
35
|
+
if (doc.type === 'markdown' && doc.content) {
|
|
36
|
+
console.log('\n--- Content ---\n');
|
|
37
|
+
console.log(doc.content);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (doc.type === 'file') {
|
|
41
|
+
if (save) {
|
|
42
|
+
const { response } = await orc.supportingDocs.downloadStream(id);
|
|
43
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
44
|
+
writeFileSync(save, buffer);
|
|
45
|
+
console.log(`\nSaved to: ${save}`);
|
|
46
|
+
} else {
|
|
47
|
+
console.log('\nUse --save <path> to download this file.');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { clientFor } from '../_helpers.js';
|
|
3
|
+
|
|
4
|
+
/** `al-orc tags create <name>` — create a tag. */
|
|
5
|
+
export async function createTagHandler(argv) {
|
|
6
|
+
const { name, color, description, json } = argv;
|
|
7
|
+
|
|
8
|
+
const data = { name };
|
|
9
|
+
if (color) data.color = color;
|
|
10
|
+
if (description) data.description = description;
|
|
11
|
+
|
|
12
|
+
const tag = await clientFor().tags.create(data);
|
|
13
|
+
|
|
14
|
+
if (json) {
|
|
15
|
+
console.log(JSON.stringify(tag, null, 2));
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
console.log(`Created tag: ${tag.name} (${tag.id})`);
|
|
20
|
+
if (tag.color) console.log(` Color: ${tag.color}`);
|
|
21
|
+
if (tag.description) console.log(` Description: ${tag.description}`);
|
|
22
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { clientFor } from '../_helpers.js';
|
|
3
|
+
|
|
4
|
+
/** `al-orc tags edit <id>` — update a tag's fields. */
|
|
5
|
+
export async function editTagHandler(argv) {
|
|
6
|
+
const { id, name, color, description, json } = argv;
|
|
7
|
+
|
|
8
|
+
if (!name && !color && !description) {
|
|
9
|
+
console.error('At least one of --name, --color, or --description is required');
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const data = {};
|
|
14
|
+
if (name) data.name = name;
|
|
15
|
+
if (color) data.color = color;
|
|
16
|
+
if (description) data.description = description;
|
|
17
|
+
|
|
18
|
+
const tag = await clientFor().tags.update(id, data);
|
|
19
|
+
|
|
20
|
+
if (json) {
|
|
21
|
+
console.log(JSON.stringify(tag, null, 2));
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
console.log(`Updated tag: ${tag.name} (${tag.id})`);
|
|
26
|
+
if (tag.color) console.log(` Color: ${tag.color}`);
|
|
27
|
+
if (tag.description) console.log(` Description: ${tag.description}`);
|
|
28
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Subtree builder for `al-orc tags <action>`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { safe, localOptions } from '../_helpers.js';
|
|
7
|
+
|
|
8
|
+
import { listTagsHandler } from './list.js';
|
|
9
|
+
import { createTagHandler } from './create.js';
|
|
10
|
+
import { editTagHandler } from './edit.js';
|
|
11
|
+
import { deleteTagHandler } from './delete.js';
|
|
12
|
+
|
|
13
|
+
export function buildTagsSubcommands(yargs) {
|
|
14
|
+
return yargs
|
|
15
|
+
.usage('$0 tags <action> [options]')
|
|
16
|
+
.command(
|
|
17
|
+
'list',
|
|
18
|
+
'List all tags',
|
|
19
|
+
(y) => localOptions(y).option('json', { describe: 'Output raw JSON', type: 'boolean' }),
|
|
20
|
+
safe(listTagsHandler),
|
|
21
|
+
)
|
|
22
|
+
.command(
|
|
23
|
+
'create <name>',
|
|
24
|
+
'Create a tag',
|
|
25
|
+
(y) =>
|
|
26
|
+
localOptions(y.positional('name', { describe: 'Tag name', type: 'string' }))
|
|
27
|
+
.option('color', { describe: 'Display color (hex)', type: 'string' })
|
|
28
|
+
.option('description', { describe: 'Tag description', type: 'string' })
|
|
29
|
+
.option('json', { describe: 'Output raw JSON', type: 'boolean' }),
|
|
30
|
+
safe(createTagHandler),
|
|
31
|
+
)
|
|
32
|
+
.command(
|
|
33
|
+
'edit <id>',
|
|
34
|
+
"Update a tag's fields",
|
|
35
|
+
(y) =>
|
|
36
|
+
localOptions(y.positional('id', { describe: 'Tag ID', type: 'string' }))
|
|
37
|
+
.option('name', { describe: 'New name', type: 'string' })
|
|
38
|
+
.option('color', { describe: 'New color (hex)', type: 'string' })
|
|
39
|
+
.option('description', { describe: 'New description', type: 'string' })
|
|
40
|
+
.option('json', { describe: 'Output raw JSON', type: 'boolean' }),
|
|
41
|
+
safe(editTagHandler),
|
|
42
|
+
)
|
|
43
|
+
.command(
|
|
44
|
+
['delete <id>', 'rm <id>'],
|
|
45
|
+
'Delete a tag',
|
|
46
|
+
(y) => y.positional('id', { describe: 'Tag ID', type: 'string' }),
|
|
47
|
+
safe(deleteTagHandler),
|
|
48
|
+
)
|
|
49
|
+
.demandCommand(1, 'Specify an action: list, create, edit, delete');
|
|
50
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { clientFor } from '../_helpers.js';
|
|
3
|
+
import { formatTable } from '../utils/format.js';
|
|
4
|
+
|
|
5
|
+
/** `al-orc tags list` — all tags. */
|
|
6
|
+
export async function listTagsHandler(argv) {
|
|
7
|
+
const { json } = argv || {};
|
|
8
|
+
const { data } = await clientFor().tags.list();
|
|
9
|
+
const tags = data || [];
|
|
10
|
+
|
|
11
|
+
if (json) {
|
|
12
|
+
console.log(JSON.stringify(tags, null, 2));
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (tags.length === 0) {
|
|
17
|
+
console.log('No tags found');
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const rows = tags.map((t) => [t.id, t.name, t.color || '—', String(t.usage?.total ?? 0)]);
|
|
22
|
+
console.log(formatTable(['ID', 'NAME', 'COLOR', 'USAGE'], rows));
|
|
23
|
+
console.log('');
|
|
24
|
+
console.log(`Total: ${tags.length} tags`);
|
|
25
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Shared formatting helpers for CLI output.
|
|
4
|
+
* Structured text readable by both humans and LLMs.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Format a label: value field with aligned label.
|
|
9
|
+
* "Name: Acme Corp"
|
|
10
|
+
*/
|
|
11
|
+
export function formatField(label, value, labelWidth = 0) {
|
|
12
|
+
const padded = (label + ':').padEnd(labelWidth || label.length + 1);
|
|
13
|
+
return `${padded} ${value ?? '—'}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Format a table with dynamic column widths.
|
|
18
|
+
* @param {string[]} headers - Column header names
|
|
19
|
+
* @param {string[][]} rows - Array of row arrays (strings)
|
|
20
|
+
* @param {{ align?: Array<'left'|'right'> }} [options] - Per-column alignment; defaults to all 'left'
|
|
21
|
+
* @returns {string} Formatted table string
|
|
22
|
+
*/
|
|
23
|
+
export function formatTable(headers, rows, options = {}) {
|
|
24
|
+
if (rows.length === 0) {
|
|
25
|
+
return headers.join(' ') + '\n(none)';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const align = options.align ?? [];
|
|
29
|
+
const widths = headers.map((h, i) =>
|
|
30
|
+
Math.max(h.length, ...rows.map((r) => (r[i] || '').length)),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const pad = (text, width, i) =>
|
|
34
|
+
align[i] === 'right' ? text.padStart(width) : text.padEnd(width);
|
|
35
|
+
|
|
36
|
+
const headerLine = headers.map((h, i) => pad(h, widths[i], i)).join(' ');
|
|
37
|
+
const separator = '-'.repeat(headerLine.length);
|
|
38
|
+
const dataLines = rows.map((row) =>
|
|
39
|
+
row.map((cell, i) => pad(cell || '—', widths[i], i)).join(' '),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
return [headerLine, separator, ...dataLines].join('\n');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Format a byte count as a human-readable size. Returns '' for null/undefined.
|
|
47
|
+
* "812 B", "1.5 KB", "3.2 MB".
|
|
48
|
+
*/
|
|
49
|
+
export function formatBytes(bytes) {
|
|
50
|
+
if (bytes == null || bytes === '') return '';
|
|
51
|
+
const n = Number(bytes);
|
|
52
|
+
if (!Number.isFinite(n)) return '';
|
|
53
|
+
if (n < 1024) return `${n} B`;
|
|
54
|
+
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
|
|
55
|
+
return `${(n / (1024 * 1024)).toFixed(1)} MB`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Format duration between two ISO timestamps.
|
|
60
|
+
* "1m" / "2h 15m" / "3d 4h"
|
|
61
|
+
*/
|
|
62
|
+
export function formatDuration(start, end) {
|
|
63
|
+
if (!start || !end) return '—';
|
|
64
|
+
const ms = new Date(end).getTime() - new Date(start).getTime();
|
|
65
|
+
if (ms < 0) return '—';
|
|
66
|
+
const seconds = Math.floor(ms / 1000);
|
|
67
|
+
if (seconds < 60) return `${seconds}s`;
|
|
68
|
+
const minutes = Math.floor(seconds / 60);
|
|
69
|
+
if (minutes < 60) return `${minutes}m`;
|
|
70
|
+
const hours = Math.floor(minutes / 60);
|
|
71
|
+
const remMin = minutes % 60;
|
|
72
|
+
if (hours < 24) return remMin > 0 ? `${hours}h ${remMin}m` : `${hours}h`;
|
|
73
|
+
const days = Math.floor(hours / 24);
|
|
74
|
+
const remHr = hours % 24;
|
|
75
|
+
return remHr > 0 ? `${days}d ${remHr}h` : `${days}d`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The zone timestamps render in. Resolved once, so a long listing cannot straddle a DST change mid-table. */
|
|
79
|
+
const LOCAL_TZ = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Short zone label, e.g. "PDT" or "Asia/Calcutta". Computed once — it labels a
|
|
83
|
+
* header, not a value.
|
|
84
|
+
*
|
|
85
|
+
* `timeZoneName: 'short'` only yields a real abbreviation for zones ICU has one
|
|
86
|
+
* for; elsewhere it returns "GMT+5:30", which is correct but noisy in a column
|
|
87
|
+
* header and tells you less than the zone's own name. So: take the abbreviation
|
|
88
|
+
* when it is one, otherwise use the IANA name.
|
|
89
|
+
*/
|
|
90
|
+
const LOCAL_TZ_LABEL = (() => {
|
|
91
|
+
const parts = new Intl.DateTimeFormat('en-US', { timeZoneName: 'short' }).formatToParts(new Date());
|
|
92
|
+
const short = parts.find((p) => p.type === 'timeZoneName')?.value;
|
|
93
|
+
if (short && !/^(GMT|UTC)[+-]/.test(short)) return short;
|
|
94
|
+
return LOCAL_TZ || short || 'local';
|
|
95
|
+
})();
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Short name of the local zone, e.g. "PDT" or "Asia/Calcutta".
|
|
99
|
+
*
|
|
100
|
+
* Use it to name the zone ONCE per table or detail block — in the column header
|
|
101
|
+
* (`CREATED (${localTzLabel()})`) or a section heading — never per row.
|
|
102
|
+
*/
|
|
103
|
+
export function localTzLabel() {
|
|
104
|
+
return LOCAL_TZ_LABEL;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Format an ISO timestamp in the operator's local timezone: "2026-03-15 15:53:01".
|
|
109
|
+
*
|
|
110
|
+
* **Local, not UTC.** The orchestrator stores and returns UTC, and this used to
|
|
111
|
+
* print `toISOString()` verbatim — so an operator in IST read every timestamp
|
|
112
|
+
* 5.5 hours early with nothing on screen saying so. That quietly breaks the
|
|
113
|
+
* main thing these timestamps are for: correlating against `pm2 logs`, file
|
|
114
|
+
* mtimes and `al-control` output, all of which are local.
|
|
115
|
+
*
|
|
116
|
+
* Pair with `localTzLabel()` in the surrounding header so the zone is stated
|
|
117
|
+
* once. sv-SE is used because its locale format is already `YYYY-MM-DD HH:MM:SS`.
|
|
118
|
+
*/
|
|
119
|
+
export function formatDate(iso) {
|
|
120
|
+
if (!iso) return '—';
|
|
121
|
+
const d = new Date(iso);
|
|
122
|
+
if (Number.isNaN(d.getTime())) return '—';
|
|
123
|
+
return d.toLocaleString('sv-SE', { timeZone: LOCAL_TZ }).replace(',', '');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Collapse whitespace and cap a free-text value for table display.
|
|
128
|
+
*
|
|
129
|
+
* For fields with no length bound — error messages, titles, descriptions. An
|
|
130
|
+
* error containing a newline would otherwise break the table's row alignment
|
|
131
|
+
* entirely, so newlines collapse to spaces before truncating. Returns the
|
|
132
|
+
* standard '—' placeholder for empty input, so callers need no `|| '—'`.
|
|
133
|
+
*
|
|
134
|
+
* @param {unknown} value
|
|
135
|
+
* @param {number} max Total width including the ellipsis.
|
|
136
|
+
*/
|
|
137
|
+
export function truncate(value, max) {
|
|
138
|
+
if (value === null || value === undefined || value === '') return '—';
|
|
139
|
+
const flat = String(value).replace(/\s+/g, ' ').trim();
|
|
140
|
+
if (!flat) return '—';
|
|
141
|
+
return flat.length > max ? `${flat.slice(0, max - 1)}…` : flat;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* One-line hint shown under a list when there are more pages.
|
|
146
|
+
* Returns null when the response fits on one page.
|
|
147
|
+
*/
|
|
148
|
+
export function formatPaginationHint(pageContext) {
|
|
149
|
+
if (!pageContext?.has_more) return null;
|
|
150
|
+
return `(showing ${pageContext.count} of ${pageContext.total} — page ${pageContext.page}; use --page ${pageContext.page + 1} for more)`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Format a header line with right-aligned status.
|
|
155
|
+
* "Account: abc123 active"
|
|
156
|
+
*/
|
|
157
|
+
export function formatHeader(label, id, status) {
|
|
158
|
+
const left = `${label}: ${id}`;
|
|
159
|
+
if (!status) return left;
|
|
160
|
+
const minGap = 4;
|
|
161
|
+
const width = Math.max(left.length + minGap + status.length, 60);
|
|
162
|
+
return left + ' '.repeat(width - left.length - status.length) + status;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Format a section divider.
|
|
167
|
+
* "--- Title ---"
|
|
168
|
+
*/
|
|
169
|
+
export function formatSection(title) {
|
|
170
|
+
return `\n--- ${title} ---\n`;
|
|
171
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Local line file cache — `.orchestrator/lines/<CODE>.json`, one per line.
|
|
4
|
+
*
|
|
5
|
+
* A line file carries what the factory needs to route (`code`, `location`) and
|
|
6
|
+
* what people need to recognise it (`name`, `description`). Station files
|
|
7
|
+
* reference their line by `code`; `@assemblyline/lib-factory` joins the two to decide
|
|
8
|
+
* which stations this factory session serves.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
const LINES_DIR = '.orchestrator/lines';
|
|
15
|
+
|
|
16
|
+
/** Fields worth persisting locally, in a stable order. */
|
|
17
|
+
const LOCAL_FIELDS = ['id', 'code', 'name', 'description', 'location'];
|
|
18
|
+
|
|
19
|
+
/** Path of the local lines directory (shown in pull/push output). */
|
|
20
|
+
export function getLinesDir() {
|
|
21
|
+
return LINES_DIR;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** All line JSON file paths under `.orchestrator/lines/`. */
|
|
25
|
+
export function listAllLineFiles() {
|
|
26
|
+
if (!fs.existsSync(LINES_DIR)) return [];
|
|
27
|
+
return fs
|
|
28
|
+
.readdirSync(LINES_DIR)
|
|
29
|
+
.filter((f) => f.endsWith('.json'))
|
|
30
|
+
.map((f) => path.join(LINES_DIR, f));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Every local line, parsed. Malformed files are skipped. */
|
|
34
|
+
export function listLocalLines() {
|
|
35
|
+
const lines = [];
|
|
36
|
+
for (const filepath of listAllLineFiles()) {
|
|
37
|
+
try {
|
|
38
|
+
lines.push({ filepath, data: JSON.parse(fs.readFileSync(filepath, 'utf8')) });
|
|
39
|
+
} catch {
|
|
40
|
+
// skip malformed files
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return lines;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Find a line file by code or id. */
|
|
47
|
+
export function findLineFile(identifier) {
|
|
48
|
+
const direct = path.join(LINES_DIR, `${identifier}.json`);
|
|
49
|
+
if (fs.existsSync(direct)) return direct;
|
|
50
|
+
for (const { filepath, data } of listLocalLines()) {
|
|
51
|
+
if (data.id === identifier || data.code === identifier) return filepath;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Load a line file by code or id. */
|
|
57
|
+
export function loadLine(identifier) {
|
|
58
|
+
const filepath = findLineFile(identifier);
|
|
59
|
+
return filepath ? JSON.parse(fs.readFileSync(filepath, 'utf8')) : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Load a line from a bare filename or a relative path. */
|
|
63
|
+
export function loadLineByFilename(arg) {
|
|
64
|
+
const filepath = arg.includes('/') || arg.includes(path.sep) ? arg : path.join(LINES_DIR, arg);
|
|
65
|
+
return fs.existsSync(filepath) ? JSON.parse(fs.readFileSync(filepath, 'utf8')) : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Save a line locally as `<code>.json`, removing any stale file for the same
|
|
70
|
+
* line (a renamed code leaves its old filename behind otherwise).
|
|
71
|
+
*/
|
|
72
|
+
export function saveLine(line) {
|
|
73
|
+
if (!fs.existsSync(LINES_DIR)) fs.mkdirSync(LINES_DIR, { recursive: true });
|
|
74
|
+
|
|
75
|
+
const stale = findLineFile(line.id);
|
|
76
|
+
const filepath = path.join(LINES_DIR, `${line.code}.json`);
|
|
77
|
+
if (stale && stale !== filepath) fs.unlinkSync(stale);
|
|
78
|
+
|
|
79
|
+
const local = {};
|
|
80
|
+
for (const field of LOCAL_FIELDS) {
|
|
81
|
+
if (line[field] !== undefined) local[field] = line[field];
|
|
82
|
+
}
|
|
83
|
+
fs.writeFileSync(filepath, JSON.stringify(local, null, 2));
|
|
84
|
+
return filepath;
|
|
85
|
+
}
|