@assemblyline/al-orc 0.9.0 → 0.11.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.
@@ -1,141 +0,0 @@
1
- // @ts-check
2
- /**
3
- * Local station file cache — the bridge between the orchestrator (remote truth)
4
- * and the working tree.
5
- *
6
- * Station files live in: .orchestrator/stations/<short_code>__<name>.json
7
- *
8
- * Line membership is encoded by the `line` field inside each JSON, not by folder
9
- * hierarchy. (Ported from cli-fob for the al-orc pull/push bridges.)
10
- */
11
-
12
- import fs from 'node:fs';
13
- import path from 'node:path';
14
-
15
- const STATIONS_DIR = '.orchestrator/stations';
16
-
17
- /** All station JSON file paths under `.orchestrator/stations/`. */
18
- function getAllStationFilePaths() {
19
- if (!fs.existsSync(STATIONS_DIR)) return [];
20
- const paths = [];
21
- for (const f of fs.readdirSync(STATIONS_DIR)) {
22
- if (f.endsWith('.json')) paths.push(path.join(STATIONS_DIR, f));
23
- }
24
- return paths;
25
- }
26
-
27
- /** "Update Rules" → "update_rules" */
28
- function nameToSnakeCase(name) {
29
- return name.toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '');
30
- }
31
-
32
- /** Filename: short_code (or id) prefix + snake_case name. */
33
- function buildFilename(station) {
34
- const snakeName = nameToSnakeCase(station.name || 'unnamed');
35
- const prefix = station.short_code || station.id;
36
- return `${prefix}__${snakeName}.json`;
37
- }
38
-
39
- /** Find a station file by ID or short_code (filename prefix, then JSON `id`). */
40
- export function findStationFile(identifier) {
41
- const all = getAllStationFilePaths();
42
- for (const p of all) {
43
- if (path.basename(p).startsWith(`${identifier}__`)) return p;
44
- }
45
- for (const p of all) {
46
- try {
47
- if (JSON.parse(fs.readFileSync(p, 'utf8')).id === identifier) return p;
48
- } catch {
49
- // skip malformed files
50
- }
51
- }
52
- return null;
53
- }
54
-
55
- /**
56
- * The station as its local file holds it:
57
- * - a station with no conveyor has no `conveyor` key, rather than `"conveyor": null`;
58
- *
59
- * @param {any} station - As the API returns it
60
- */
61
- function forLocalFile(station) {
62
- if (station.conveyor == null) delete station.conveyor;
63
- return station;
64
- }
65
-
66
- /** Save a station locally, removing any existing file(s) for it first. */
67
- export function saveStation(station) {
68
- const toRemove = new Set();
69
- const existingById = findStationFile(station.id);
70
- if (existingById) toRemove.add(existingById);
71
- if (station.short_code) {
72
- const existingByCode = findStationFile(station.short_code);
73
- if (existingByCode) toRemove.add(existingByCode);
74
- }
75
- for (const f of toRemove) fs.unlinkSync(f);
76
-
77
- if (!fs.existsSync(STATIONS_DIR)) fs.mkdirSync(STATIONS_DIR, { recursive: true });
78
-
79
- const filepath = path.join(STATIONS_DIR, buildFilename(station));
80
- fs.writeFileSync(filepath, JSON.stringify(forLocalFile(station), null, 2));
81
- return filepath;
82
- }
83
-
84
- /** Load a station file by ID or short_code. */
85
- export function loadStation(identifier) {
86
- const filepath = findStationFile(identifier);
87
- return filepath ? JSON.parse(fs.readFileSync(filepath, 'utf8')) : null;
88
- }
89
-
90
- /** All station JSON file paths. */
91
- export function listAllStationFiles() {
92
- return getAllStationFilePaths();
93
- }
94
-
95
- /** IDs from every local station file that carries one. */
96
- export function listLocalStationIds() {
97
- const ids = [];
98
- for (const p of getAllStationFilePaths()) {
99
- try {
100
- const content = JSON.parse(fs.readFileSync(p, 'utf8'));
101
- if (content.id) ids.push(content.id);
102
- } catch {
103
- // skip malformed files
104
- }
105
- }
106
- return ids;
107
- }
108
-
109
- /** Load a station from a bare filename or a relative path. */
110
- export function loadStationByFilename(arg) {
111
- if (arg.includes(path.sep) || arg.includes('/')) {
112
- return fs.existsSync(arg) ? JSON.parse(fs.readFileSync(arg, 'utf8')) : null;
113
- }
114
- for (const filepath of getAllStationFilePaths()) {
115
- if (path.basename(filepath) === arg) return JSON.parse(fs.readFileSync(filepath, 'utf8'));
116
- }
117
- return null;
118
- }
119
-
120
- /** Write the server-assigned id into a new station file and rename to standard form. */
121
- export function finalizeNewStationFile(originalArg, station) {
122
- let originalPath;
123
- if (originalArg.includes(path.sep) || originalArg.includes('/')) {
124
- originalPath = originalArg;
125
- } else {
126
- originalPath =
127
- getAllStationFilePaths().find((p) => path.basename(p) === originalArg) ||
128
- path.join(STATIONS_DIR, originalArg);
129
- }
130
- if (fs.existsSync(originalPath)) fs.unlinkSync(originalPath);
131
- if (!fs.existsSync(STATIONS_DIR)) fs.mkdirSync(STATIONS_DIR, { recursive: true });
132
-
133
- const newPath = path.join(STATIONS_DIR, buildFilename(station));
134
- fs.writeFileSync(newPath, JSON.stringify(forLocalFile(station), null, 2));
135
- return newPath;
136
- }
137
-
138
- /** Path of the local stations directory (shown in pull/push output). */
139
- export function getStationsDir() {
140
- return STATIONS_DIR;
141
- }