@assemblyline/al-orc 0.2.0 → 0.3.1
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 +16 -0
- package/README.md +6 -1
- package/package.json +1 -1
- package/src/cli/_helpers.js +3 -3
- package/src/cli/config-store.js +6 -6
- package/src/cli/index.js +17 -1
- package/src/cli/lines/list.js +4 -4
- package/src/cli/login.js +96 -0
- package/src/cli/shared/edit-tags.js +9 -9
- package/src/cli/stations/edit.js +10 -10
- package/src/cli/stations/list.js +5 -5
- package/src/cli/stations/pull.js +9 -9
- package/src/cli/stations/push.js +8 -8
- package/src/http.js +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.3.1] - 2026-10-26
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
- `al-orc --version` shows al-orc's own version when installed from npm. It printed the
|
|
14
|
+
version of whatever `package.json` was nearest to the install (`unknown`, or e.g. `1.0.0`),
|
|
15
|
+
because yargs looked it up from the bin link instead of from this package.
|
|
16
|
+
|
|
17
|
+
## [0.3.0] - 2026-10-26
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
- `al-orc login`: sign in through the browser instead of pasting a key and secret. Opens
|
|
21
|
+
the Orchestrator's approve page (`--no-browser` only prints the URL, e.g. over SSH), where
|
|
22
|
+
you pick one or more orgs; saves one profile per org, named after it (an existing name gets
|
|
23
|
+
the org id appended). Orgs you already have a profile for show as connected. The current
|
|
24
|
+
profile changes only when there was none.
|
|
25
|
+
|
|
10
26
|
## [0.2.0] - 2026-10-19
|
|
11
27
|
|
|
12
28
|
### Fixed
|
package/README.md
CHANGED
|
@@ -6,13 +6,18 @@ records.
|
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install -g @assemblyline/al-orc
|
|
9
|
-
al-orc
|
|
9
|
+
al-orc login # pick orgs in the browser; one profile per org
|
|
10
10
|
al-orc status # org and key the profile belongs to
|
|
11
11
|
al-orc lines push --all && al-orc stations push --all # from a factory repo root
|
|
12
12
|
al-orc stations run EX0
|
|
13
13
|
al-orc work-records list --station EX0
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
+
`al-orc login` opens the Orchestrator's approve page (over SSH, add `--no-browser` and open the
|
|
17
|
+
printed link on any device). Orgs you already have a profile for show as connected; the current
|
|
18
|
+
profile changes only when there was none. To add a key by hand instead:
|
|
19
|
+
`al-orc config profiles add acme --api-key <key> --api-secret <secret>`.
|
|
20
|
+
|
|
16
21
|
Profiles live in `~/.assemblyline/al-orc/config.yml` (mode 0600; override the folder with
|
|
17
22
|
`AL_ORC_CONFIG_DIR`). The CLI never reads `.env`.
|
|
18
23
|
|
package/package.json
CHANGED
package/src/cli/_helpers.js
CHANGED
|
@@ -42,7 +42,7 @@ export function paginationOptions(yargs) {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
// Print the credential-source hint at most once per invocation.
|
|
45
|
-
let
|
|
45
|
+
let is_hint_printed = false;
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Resolve the current command's credentials from the CLI config, printing a
|
|
@@ -51,9 +51,9 @@ let hintPrinted = false;
|
|
|
51
51
|
*/
|
|
52
52
|
export function requireCreds() {
|
|
53
53
|
const creds = resolveCredentials();
|
|
54
|
-
if (!
|
|
54
|
+
if (!is_hint_printed) {
|
|
55
55
|
console.error(`(using orchestrator credentials from ${creds.source})`);
|
|
56
|
-
|
|
56
|
+
is_hint_printed = true;
|
|
57
57
|
}
|
|
58
58
|
return creds;
|
|
59
59
|
}
|
package/src/cli/config-store.js
CHANGED
|
@@ -20,11 +20,11 @@ import yaml from 'js-yaml';
|
|
|
20
20
|
const CONFIG_DIR = process.env.AL_ORC_CONFIG_DIR || join(homedir(), '.assemblyline', 'al-orc');
|
|
21
21
|
const CONFIG_PATH = join(CONFIG_DIR, 'config.yml');
|
|
22
22
|
|
|
23
|
-
let
|
|
23
|
+
let org_override = null;
|
|
24
24
|
|
|
25
25
|
/** Set by the `--profile <name>` global flag (alias `--org`; yargs middleware). Beats current_org. */
|
|
26
26
|
export function setOrgOverride(name) {
|
|
27
|
-
|
|
27
|
+
org_override = name;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
export function loadConfig() {
|
|
@@ -95,10 +95,10 @@ export function listProfiles() {
|
|
|
95
95
|
export function resolveCredentials() {
|
|
96
96
|
const cfg = loadConfig();
|
|
97
97
|
|
|
98
|
-
if (
|
|
99
|
-
const org = cfg.orgs[
|
|
100
|
-
if (!org) throw new Error(`Profile '${
|
|
101
|
-
return { ...org, name:
|
|
98
|
+
if (org_override) {
|
|
99
|
+
const org = cfg.orgs[org_override];
|
|
100
|
+
if (!org) throw new Error(`Profile '${org_override}' is not configured. Run \`al-orc config profiles add ${org_override}\`.`);
|
|
101
|
+
return { ...org, name: org_override, source: `profile '${org_override}'` };
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
if (cfg.current_org && cfg.orgs[cfg.current_org]) {
|
package/src/cli/index.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* is lib-factory's `factory` command. Not here.)
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import { readFileSync } from 'node:fs';
|
|
13
14
|
import yargs from 'yargs';
|
|
14
15
|
|
|
15
16
|
import { setOrgOverride } from './config-store.js';
|
|
@@ -22,6 +23,12 @@ import { buildSupportingDocsSubcommands } from './supporting-docs/index.js';
|
|
|
22
23
|
import { buildFactorySessionsSubcommands } from './factory-sessions/index.js';
|
|
23
24
|
import { safe, localOptions } from './_helpers.js';
|
|
24
25
|
import { statusHandler } from './status.js';
|
|
26
|
+
import { loginHandler } from './login.js';
|
|
27
|
+
import { DEFAULT_API_URL } from '../http.js';
|
|
28
|
+
|
|
29
|
+
// Read from this package, not found by yargs: an installed `al-orc` runs through a bin link,
|
|
30
|
+
// and the walk up from there never reaches this package.json.
|
|
31
|
+
const { version } = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8'));
|
|
25
32
|
|
|
26
33
|
export function run(argv) {
|
|
27
34
|
return yargs(argv)
|
|
@@ -35,6 +42,15 @@ export function run(argv) {
|
|
|
35
42
|
.middleware((argv) => {
|
|
36
43
|
if (argv.profile) setOrgOverride(argv.profile);
|
|
37
44
|
})
|
|
45
|
+
.command(
|
|
46
|
+
'login',
|
|
47
|
+
'Sign in through the browser, pick orgs, and save one profile per org',
|
|
48
|
+
(y) =>
|
|
49
|
+
localOptions(y)
|
|
50
|
+
.option('api-url', { describe: 'Orchestrator URL (origin only)', type: 'string', default: DEFAULT_API_URL })
|
|
51
|
+
.option('browser', { describe: 'Open the approve page; --no-browser only prints its URL', type: 'boolean', default: true }),
|
|
52
|
+
safe(loginHandler),
|
|
53
|
+
)
|
|
38
54
|
.command('config <resource>', 'Manage credential profiles (alias: orgs)', buildConfigSubcommands)
|
|
39
55
|
.command('lines <action>', 'Manage lines (a line owns the location its stations run at)', buildLinesSubcommands)
|
|
40
56
|
.command('stations <action>', 'Query and run stations (orchestrator definitions)', buildStationsSubcommands)
|
|
@@ -52,7 +68,7 @@ export function run(argv) {
|
|
|
52
68
|
.strict()
|
|
53
69
|
.help()
|
|
54
70
|
.alias('h', 'help')
|
|
55
|
-
.version()
|
|
71
|
+
.version(version)
|
|
56
72
|
.alias('v', 'version')
|
|
57
73
|
// Global options (inherited by every command) render under their own
|
|
58
74
|
// heading; each command's own options stay under "Options:", shown first
|
package/src/cli/lines/list.js
CHANGED
|
@@ -4,10 +4,10 @@ import { formatTable, formatPaginationHint } from '../utils/format.js';
|
|
|
4
4
|
|
|
5
5
|
/** `al-orc lines list` — lines from the orchestrator, with their station codes. */
|
|
6
6
|
export async function listLinesHandler(argv) {
|
|
7
|
-
const { location, json, includeArchived, page, limit } = argv || {};
|
|
7
|
+
const { location, json, includeArchived: include_archived, page, limit } = argv || {};
|
|
8
8
|
|
|
9
9
|
const orc = clientFor();
|
|
10
|
-
const { data, page_context } = await orc.lines.list({ location, include_archived
|
|
10
|
+
const { data, page_context } = await orc.lines.list({ location, include_archived, page, limit });
|
|
11
11
|
const lines = data || [];
|
|
12
12
|
|
|
13
13
|
if (json) {
|
|
@@ -26,9 +26,9 @@ export async function listLinesHandler(argv) {
|
|
|
26
26
|
l.location || '—',
|
|
27
27
|
String(l.stations?.length ?? 0),
|
|
28
28
|
(l.stations || []).map((s) => s.short_code || s.id).join(', ') || '—',
|
|
29
|
-
...(
|
|
29
|
+
...(include_archived ? [l.archived_at ? 'yes' : '—'] : []),
|
|
30
30
|
]);
|
|
31
|
-
const headers = ['CODE', 'NAME', 'LOCATION', 'STATIONS', 'MEMBERS', ...(
|
|
31
|
+
const headers = ['CODE', 'NAME', 'LOCATION', 'STATIONS', 'MEMBERS', ...(include_archived ? ['ARCHIVED'] : [])];
|
|
32
32
|
|
|
33
33
|
console.log(formatTable(headers, rows));
|
|
34
34
|
console.log('');
|
package/src/cli/login.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* `al-orc login` — get API keys without copying them from the settings page. Starts a login
|
|
4
|
+
* request on the Orchestrator (sending the key ids of the existing profiles, so the approve
|
|
5
|
+
* page shows those orgs as already connected), opens the approve page in the browser (or
|
|
6
|
+
* prints its URL), polls until the user picks orgs and approves, then saves one profile per
|
|
7
|
+
* org. The current profile changes only when there was none.
|
|
8
|
+
*/
|
|
9
|
+
import { hostname } from 'node:os';
|
|
10
|
+
import { spawn } from 'node:child_process';
|
|
11
|
+
import { addOrg, loadConfig } from './config-store.js';
|
|
12
|
+
|
|
13
|
+
const POLL_INTERVAL_MS = 2000;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {string} url
|
|
17
|
+
* @param {Object} body
|
|
18
|
+
*/
|
|
19
|
+
async function postJson(url, body) {
|
|
20
|
+
const res = await fetch(url, {
|
|
21
|
+
method: 'POST',
|
|
22
|
+
headers: { 'Content-Type': 'application/json' },
|
|
23
|
+
body: JSON.stringify(body),
|
|
24
|
+
});
|
|
25
|
+
const json = await res.json().catch(() => ({}));
|
|
26
|
+
return { status: res.status, data: json.data, error: json.error };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Best effort: the URL is always printed too, for SSH sessions and headless machines. */
|
|
30
|
+
function openBrowser(url) {
|
|
31
|
+
const cmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'explorer' : 'xdg-open';
|
|
32
|
+
try {
|
|
33
|
+
spawn(cmd, [url], { stdio: 'ignore', detached: true }).on('error', () => {}).unref();
|
|
34
|
+
} catch {
|
|
35
|
+
// printed URL is the fallback
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Poll /api/cli/token until the request is approved or expires.
|
|
41
|
+
* @param {{ api_url: string, device_code: string, expires_in: number, interval_ms?: number }} args
|
|
42
|
+
* @returns {Promise<{ keys: Array<{ org: { id: string, name: string }, api_key: string, api_secret: string }> }>}
|
|
43
|
+
*/
|
|
44
|
+
export async function pollForKey({ api_url, device_code, expires_in, interval_ms = POLL_INTERVAL_MS }) {
|
|
45
|
+
const deadline = Date.now() + expires_in * 1000;
|
|
46
|
+
while (Date.now() < deadline) {
|
|
47
|
+
const { status, data, error } = await postJson(`${api_url}/api/cli/token`, { device_code });
|
|
48
|
+
if (status === 200 && data?.status === 'approved') return data;
|
|
49
|
+
if (status !== 200) throw new Error(error?.message || `Login failed (HTTP ${status}). Run \`al-orc login\` again.`);
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, interval_ms));
|
|
51
|
+
}
|
|
52
|
+
throw new Error('Login request expired. Run `al-orc login` again.');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Profile name from the org name ("Alex's org" → "alex-s-org"). A name already used by a profile
|
|
57
|
+
* gets the org id appended, so an existing profile is never overwritten.
|
|
58
|
+
* @param {{ id: string, name: string }} org
|
|
59
|
+
* @param {Set<string>} taken
|
|
60
|
+
*/
|
|
61
|
+
export function profileName(org, taken) {
|
|
62
|
+
const base = (org.name || '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') || org.id;
|
|
63
|
+
return taken.has(base) ? `${base}-${org.id}` : base;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function loginHandler(argv) {
|
|
67
|
+
const api_url = argv.apiUrl.replace(/\/+$/, '');
|
|
68
|
+
const cfg = loadConfig();
|
|
69
|
+
const known_keys = Object.values(cfg.orgs).map((o) => o.api_key).filter(Boolean);
|
|
70
|
+
const start = await postJson(`${api_url}/api/cli/login`, { name: `al-orc on ${hostname()}`, known_keys });
|
|
71
|
+
if (start.status !== 201) {
|
|
72
|
+
throw new Error(start.error?.message || `Could not start a login on ${api_url} (HTTP ${start.status}).`);
|
|
73
|
+
}
|
|
74
|
+
const { device_code, verification_url, expires_in } = start.data;
|
|
75
|
+
|
|
76
|
+
console.log('Pick the orgs to add and approve in your browser:');
|
|
77
|
+
console.log(` ${verification_url}`);
|
|
78
|
+
if (argv.browser) openBrowser(verification_url);
|
|
79
|
+
console.log('Waiting for approval…');
|
|
80
|
+
|
|
81
|
+
const { keys } = await pollForKey({ api_url, device_code, expires_in });
|
|
82
|
+
const taken = new Set(Object.keys(cfg.orgs));
|
|
83
|
+
for (const { org, api_key, api_secret } of keys) {
|
|
84
|
+
const name = profileName(org, taken);
|
|
85
|
+
taken.add(name);
|
|
86
|
+
addOrg(name, { api_url, api_key, api_secret });
|
|
87
|
+
console.log(`Added profile '${name}' for ${org.name}.`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const { current_org } = loadConfig();
|
|
91
|
+
if (cfg.current_org) {
|
|
92
|
+
console.log(`Current profile is still '${current_org}'. Switch with \`al-orc config profiles use <name>\`.`);
|
|
93
|
+
} else {
|
|
94
|
+
console.log(`Current profile: '${current_org}'.`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -7,15 +7,15 @@
|
|
|
7
7
|
* @param {{ getTags: (id: string) => Promise<any[]>, setTags: (id: string, tagIds: string[]) => Promise<any> }} ns
|
|
8
8
|
* @param {{ ensure: (name: string) => Promise<{id: string, name: string}>, resolveNames: (names: string[]) => Promise<string[]> }} tags
|
|
9
9
|
* @param {string} id
|
|
10
|
-
* @param {string[]}
|
|
11
|
-
* @param {string[]}
|
|
10
|
+
* @param {string[]} add_tags
|
|
11
|
+
* @param {string[]} remove_tags
|
|
12
12
|
*/
|
|
13
|
-
export async function editEntityTags(ns, tags, id,
|
|
13
|
+
export async function editEntityTags(ns, tags, id, add_tags, remove_tags) {
|
|
14
14
|
const current = await ns.getTags(id);
|
|
15
15
|
let ids = current.map((t) => t.id);
|
|
16
16
|
const changes = [];
|
|
17
17
|
|
|
18
|
-
for (const name of
|
|
18
|
+
for (const name of add_tags) {
|
|
19
19
|
const tag = await tags.ensure(name);
|
|
20
20
|
if (!ids.includes(tag.id)) {
|
|
21
21
|
ids.push(tag.id);
|
|
@@ -25,14 +25,14 @@ export async function editEntityTags(ns, tags, id, addTags, removeTags) {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
for (const name of
|
|
29
|
-
const [
|
|
30
|
-
if (!
|
|
28
|
+
for (const name of remove_tags) {
|
|
29
|
+
const [resolved_id] = await tags.resolveNames([name]).catch(() => [null]);
|
|
30
|
+
if (!resolved_id) {
|
|
31
31
|
console.error(` Warning: tag "${name}" not found, skipping removal`);
|
|
32
32
|
continue;
|
|
33
33
|
}
|
|
34
|
-
if (ids.includes(
|
|
35
|
-
ids = ids.filter((x) => x !==
|
|
34
|
+
if (ids.includes(resolved_id)) {
|
|
35
|
+
ids = ids.filter((x) => x !== resolved_id);
|
|
36
36
|
changes.push(` - ${name}`);
|
|
37
37
|
} else {
|
|
38
38
|
changes.push(` ${name} (not present)`);
|
package/src/cli/stations/edit.js
CHANGED
|
@@ -4,13 +4,13 @@ import { editEntityTags } from '../shared/edit-tags.js';
|
|
|
4
4
|
|
|
5
5
|
/** `al-orc stations edit <id>` — set short_code and/or add/remove tags. */
|
|
6
6
|
export async function editStationHandler(argv) {
|
|
7
|
-
const { id, shortCode, addTag, removeTag } = argv;
|
|
7
|
+
const { id, shortCode: short_code, addTag: add_tag, removeTag: remove_tag } = argv;
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
9
|
+
const add_tags = add_tag ? [].concat(add_tag) : [];
|
|
10
|
+
const remove_tags = remove_tag ? [].concat(remove_tag) : [];
|
|
11
|
+
const has_tag_changes = add_tags.length > 0 || remove_tags.length > 0;
|
|
12
12
|
|
|
13
|
-
if (!
|
|
13
|
+
if (!has_tag_changes && short_code === undefined) {
|
|
14
14
|
console.error('Usage: al-orc stations edit <id|short_code> --short-code P1 --add-tag <name> [--remove-tag <name>]');
|
|
15
15
|
console.error('At least one of --short-code, --add-tag, or --remove-tag is required');
|
|
16
16
|
process.exit(1);
|
|
@@ -19,13 +19,13 @@ export async function editStationHandler(argv) {
|
|
|
19
19
|
console.log(`Station: ${id}`);
|
|
20
20
|
const orc = clientFor();
|
|
21
21
|
|
|
22
|
-
if (
|
|
23
|
-
await orc.stations.update(id, { short_code:
|
|
24
|
-
console.log(`Short code set: ${
|
|
22
|
+
if (short_code !== undefined) {
|
|
23
|
+
await orc.stations.update(id, { short_code: short_code || null });
|
|
24
|
+
console.log(`Short code set: ${short_code || '(cleared)'}`);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
if (
|
|
28
|
-
await editEntityTags(orc.stations, orc.tags, id,
|
|
27
|
+
if (has_tag_changes) {
|
|
28
|
+
await editEntityTags(orc.stations, orc.tags, id, add_tags, remove_tags);
|
|
29
29
|
console.log('Tags updated');
|
|
30
30
|
}
|
|
31
31
|
}
|
package/src/cli/stations/list.js
CHANGED
|
@@ -4,10 +4,10 @@ import { formatTable, formatPaginationHint } from '../utils/format.js';
|
|
|
4
4
|
|
|
5
5
|
/** `al-orc stations list` — canonical station definitions from the orchestrator. */
|
|
6
6
|
export async function listStationsHandler(argv) {
|
|
7
|
-
const { tag, line, json, includeArchived, page, limit } = argv || {};
|
|
7
|
+
const { tag, line, json, includeArchived: include_archived, page, limit } = argv || {};
|
|
8
8
|
|
|
9
9
|
const orc = clientFor();
|
|
10
|
-
const { data, page_context } = await orc.stations.list({ tag, line, include_archived
|
|
10
|
+
const { data, page_context } = await orc.stations.list({ tag, line, include_archived, page, limit });
|
|
11
11
|
const stations = data || [];
|
|
12
12
|
|
|
13
13
|
if (json) {
|
|
@@ -25,13 +25,13 @@ export async function listStationsHandler(argv) {
|
|
|
25
25
|
s.id,
|
|
26
26
|
s.name || '—',
|
|
27
27
|
String(s.steps ? s.steps.length : 0),
|
|
28
|
-
...(
|
|
28
|
+
...(include_archived ? [s.archived || s.archived_at ? 'yes' : '—'] : []),
|
|
29
29
|
]);
|
|
30
|
-
const headers = ['CODE', 'ID', 'NAME', 'STEPS', ...(
|
|
30
|
+
const headers = ['CODE', 'ID', 'NAME', 'STEPS', ...(include_archived ? ['ARCHIVED'] : [])];
|
|
31
31
|
|
|
32
32
|
console.log(formatTable(headers, rows));
|
|
33
33
|
console.log('');
|
|
34
|
-
console.log(`Total: ${stations.length} stations${
|
|
34
|
+
console.log(`Total: ${stations.length} stations${include_archived ? ' (including archived)' : ''}`);
|
|
35
35
|
|
|
36
36
|
const hint = formatPaginationHint(page_context);
|
|
37
37
|
if (hint) console.log(hint);
|
package/src/cli/stations/pull.js
CHANGED
|
@@ -8,15 +8,15 @@ import { getLinesDir } from '../utils/line-files.js';
|
|
|
8
8
|
* Convert dependency IDs to short_codes using an id→short_code map.
|
|
9
9
|
* Values without a match pass through unchanged.
|
|
10
10
|
*/
|
|
11
|
-
function dependenciesToShortCodes(dependencies,
|
|
11
|
+
function dependenciesToShortCodes(dependencies, id_to_short_code) {
|
|
12
12
|
if (!dependencies?.length) return dependencies;
|
|
13
|
-
return dependencies.map((dep) =>
|
|
13
|
+
return dependencies.map((dep) => id_to_short_code.get(dep) || dep);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/** Normalize a fetched station for local storage: tags → names, deps → short_codes. */
|
|
17
|
-
function normalizeForLocal(station,
|
|
17
|
+
function normalizeForLocal(station, id_to_short_code) {
|
|
18
18
|
if (station.tags) station.tags = station.tags.map((t) => (typeof t === 'string' ? t : t.name));
|
|
19
|
-
station.dependencies = dependenciesToShortCodes(station.dependencies,
|
|
19
|
+
station.dependencies = dependenciesToShortCodes(station.dependencies, id_to_short_code);
|
|
20
20
|
return station;
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -37,13 +37,13 @@ export async function pullStationsHandler(argv) {
|
|
|
37
37
|
if (id) {
|
|
38
38
|
console.log(`Saving to: ${getStationsDir()}/`);
|
|
39
39
|
// Single station — also fetch the list for dependency resolution.
|
|
40
|
-
const [station,
|
|
40
|
+
const [station, all_stations] =await Promise.all([orc.stations.get(id), orc.stations.getAll()]);
|
|
41
41
|
if (!station) {
|
|
42
42
|
console.error(`Station not found: ${id}`);
|
|
43
43
|
process.exit(1);
|
|
44
44
|
}
|
|
45
|
-
const
|
|
46
|
-
const filepath = saveStation(normalizeForLocal(station,
|
|
45
|
+
const id_to_short_code = new Map(all_stations.filter((s) => s.short_code).map((s) => [s.id, s.short_code]));
|
|
46
|
+
const filepath = saveStation(normalizeForLocal(station, id_to_short_code));
|
|
47
47
|
console.log(`Saved: ${filepath}`);
|
|
48
48
|
return;
|
|
49
49
|
}
|
|
@@ -61,12 +61,12 @@ export async function pullStationsHandler(argv) {
|
|
|
61
61
|
console.log('No stations found');
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
|
-
const
|
|
64
|
+
const id_to_short_code = new Map(stations.filter((s) => s.short_code).map((s) => [s.id, s.short_code]));
|
|
65
65
|
|
|
66
66
|
for (const summary of stations) {
|
|
67
67
|
// Fetch full details (the list may omit fields).
|
|
68
68
|
const full = await orc.stations.get(summary.id);
|
|
69
|
-
const filepath = saveStation(normalizeForLocal(full,
|
|
69
|
+
const filepath = saveStation(normalizeForLocal(full, id_to_short_code));
|
|
70
70
|
console.log(`Saved: ${filepath}`);
|
|
71
71
|
}
|
|
72
72
|
console.log('');
|
package/src/cli/stations/push.js
CHANGED
|
@@ -47,15 +47,15 @@ async function resolveDependencies(orc, cache, dependencies) {
|
|
|
47
47
|
/** Map tag names → ids, creating any that don't exist. Returns { ids, created }. */
|
|
48
48
|
async function resolveTagNamesCreating(orc, names) {
|
|
49
49
|
const { data = [] } = await orc.tags.list();
|
|
50
|
-
const
|
|
50
|
+
const by_name = new Map(data.map((t) => [t.name, t.id]));
|
|
51
51
|
const ids = [];
|
|
52
52
|
const created = [];
|
|
53
53
|
for (const name of names) {
|
|
54
|
-
let id =
|
|
54
|
+
let id = by_name.get(name);
|
|
55
55
|
if (!id) {
|
|
56
56
|
const tag = await orc.tags.create({ name });
|
|
57
57
|
id = tag.id;
|
|
58
|
-
|
|
58
|
+
by_name.set(name, id);
|
|
59
59
|
created.push(name);
|
|
60
60
|
}
|
|
61
61
|
ids.push(id);
|
|
@@ -67,15 +67,15 @@ async function resolveTagNamesCreating(orc, names) {
|
|
|
67
67
|
* Sync tags after push. `undefined` = leave untouched; `[]` = clear all;
|
|
68
68
|
* otherwise resolve (auto-creating missing) and replace.
|
|
69
69
|
*/
|
|
70
|
-
async function syncTags(orc,
|
|
70
|
+
async function syncTags(orc, station_id, tags) {
|
|
71
71
|
if (tags === undefined) return;
|
|
72
72
|
if (tags.length === 0) {
|
|
73
|
-
await orc.stations.setTags(
|
|
73
|
+
await orc.stations.setTags(station_id, []);
|
|
74
74
|
console.log(' Tags cleared');
|
|
75
75
|
return;
|
|
76
76
|
}
|
|
77
77
|
const { ids, created } = await resolveTagNamesCreating(orc, tags);
|
|
78
|
-
await orc.stations.setTags(
|
|
78
|
+
await orc.stations.setTags(station_id, ids);
|
|
79
79
|
if (created.length > 0) console.log(` Auto-created tags: ${created.join(', ')}`);
|
|
80
80
|
console.log(` Tags synced: ${tags.join(', ')}`);
|
|
81
81
|
}
|
|
@@ -199,9 +199,9 @@ export async function pushStationsHandler(argv) {
|
|
|
199
199
|
let updatedCount = 0;
|
|
200
200
|
let createdCount = 0;
|
|
201
201
|
for (const filepath of allPaths) {
|
|
202
|
-
const
|
|
202
|
+
const has_id =!!loadStationByFilename(filepath).id;
|
|
203
203
|
await pushByFilename(orc, cache, filepath, { force });
|
|
204
|
-
if (
|
|
204
|
+
if (has_id)updatedCount++;
|
|
205
205
|
else createdCount++;
|
|
206
206
|
}
|
|
207
207
|
|
package/src/http.js
CHANGED
|
@@ -207,7 +207,7 @@ export async function apiGetAll(path, options = {}, { maxRows = MAX_ALL_ROWS, pa
|
|
|
207
207
|
const all = [];
|
|
208
208
|
let page = 1;
|
|
209
209
|
let pageContext = null;
|
|
210
|
-
let
|
|
210
|
+
let is_truncated = false;
|
|
211
211
|
|
|
212
212
|
while (true) {
|
|
213
213
|
const result = await apiGet(path, {
|
|
@@ -219,7 +219,7 @@ export async function apiGetAll(path, options = {}, { maxRows = MAX_ALL_ROWS, pa
|
|
|
219
219
|
|
|
220
220
|
if (all.length + rows.length > maxRows) {
|
|
221
221
|
all.push(...rows.slice(0, maxRows - all.length));
|
|
222
|
-
|
|
222
|
+
is_truncated = true;
|
|
223
223
|
break;
|
|
224
224
|
}
|
|
225
225
|
all.push(...rows);
|
|
@@ -228,7 +228,7 @@ export async function apiGetAll(path, options = {}, { maxRows = MAX_ALL_ROWS, pa
|
|
|
228
228
|
page++;
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
-
return { data: all, page_context: pageContext, truncated };
|
|
231
|
+
return { data: all, page_context: pageContext, truncated: is_truncated };
|
|
232
232
|
}
|
|
233
233
|
|
|
234
234
|
/**
|