@flowscripter/flowscripter-io-cli 2.0.11 → 3.0.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/README.md +70 -34
- package/bun.lock +59 -58
- package/functional_tests/features/commands.feature +76 -7
- package/functional_tests/features/steps/executable.py +6 -0
- package/functional_tests/features/steps/io_commands.py +5 -0
- package/package.json +12 -11
- package/src/cli.ts +19 -9
- package/src/commands/copy.ts +6 -62
- package/src/commands/delete.ts +28 -29
- package/src/commands/get-properties.ts +30 -34
- package/src/commands/hash.ts +102 -68
- package/src/commands/list.ts +76 -57
- package/src/commands/move.ts +6 -62
- package/src/commands/set-properties.ts +92 -37
- package/src/commands/transferCommand.ts +121 -0
- package/src/util/interruptSignals.ts +70 -0
- package/src/util/location/createLocationOption.ts +59 -0
- package/src/util/location/describeTarget.ts +6 -0
- package/src/util/location/targetKey.ts +9 -0
- package/src/util/location/toStructuredLocation.ts +8 -0
- package/src/util/payloadKindOption.ts +20 -0
- package/src/{pluginsDir.ts → util/pluginsDir.ts} +1 -1
- package/src/util/providerRegistry.ts +23 -0
- package/src/util/requireOperation.ts +19 -0
- package/src/util/zodToComplexOption.ts +93 -0
- package/tests/commands/copy.test.ts +80 -0
- package/tests/commands/delete.test.ts +44 -0
- package/tests/commands/get-properties.test.ts +52 -0
- package/tests/commands/hash.test.ts +76 -0
- package/tests/commands/list.test.ts +78 -0
- package/tests/commands/move.test.ts +45 -0
- package/tests/commands/set-properties.test.ts +68 -0
- package/tests/commands/transferCommand.test.ts +63 -0
- package/tests/fixtures/pluginStore.ts +39 -0
- package/tests/{testContext.ts → fixtures/testContext.ts} +17 -1
- package/tests/{byteScale_test.ts → util/byteScale.test.ts} +1 -1
- package/tests/util/interruptSignals.test.ts +80 -0
- package/tests/util/location/createLocationOption.test.ts +73 -0
- package/tests/util/location/describeTarget.test.ts +12 -0
- package/tests/util/location/targetKey.test.ts +15 -0
- package/tests/util/location/toStructuredLocation.test.ts +14 -0
- package/tests/util/payloadKindOption.test.ts +19 -0
- package/tests/util/providerRegistry.test.ts +39 -0
- package/tests/util/requireOperation.test.ts +25 -0
- package/tests/util/zodToComplexOption.test.ts +116 -0
- package/src/filesystemProvider.ts +0 -40
- package/tests/commands_test.ts +0 -186
- /package/src/{byteScale.ts → util/byteScale.ts} +0 -0
- /package/tests/{cli_test.ts → cli.test.ts} +0 -0
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
+
type ComplexOption,
|
|
3
|
+
ComplexValueTypeName,
|
|
2
4
|
Icon,
|
|
3
5
|
PRINTER_SERVICE_ID,
|
|
4
6
|
type Context,
|
|
@@ -7,45 +9,98 @@ import {
|
|
|
7
9
|
type Values,
|
|
8
10
|
ValueTypeName,
|
|
9
11
|
} from "@flowscripter/dynamic-cli-framework";
|
|
10
|
-
import {
|
|
12
|
+
import type { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
13
|
+
import type { EntryPropertyChanges } from "@flowscripter/pluggable-io-framework-api";
|
|
14
|
+
import { createLocationOption } from "../util/location/createLocationOption.ts";
|
|
15
|
+
import { targetKey } from "../util/location/targetKey.ts";
|
|
16
|
+
import { toStructuredLocation } from "../util/location/toStructuredLocation.ts";
|
|
17
|
+
import { requireOperation } from "../util/requireOperation.ts";
|
|
18
|
+
import { zodToComplexOption } from "../util/zodToComplexOption.ts";
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
20
|
+
/**
|
|
21
|
+
* The `--properties` argument: one nested group per installed protocol
|
|
22
|
+
* holding that protocol's settable properties, e.g. `--properties.file.mode=384`.
|
|
23
|
+
* Protocols without settable properties have no group.
|
|
24
|
+
*/
|
|
25
|
+
function createPropertiesOption(registry: ProviderRegistry): ComplexOption | undefined {
|
|
26
|
+
const groups = registry.getProtocols().flatMap((protocol) => {
|
|
27
|
+
const properties = zodToComplexOption(registry.getFactory(protocol)!.settablePropertySchema);
|
|
28
|
+
return properties.length === 0
|
|
29
|
+
? []
|
|
30
|
+
: [
|
|
31
|
+
{
|
|
32
|
+
name: protocol,
|
|
33
|
+
description: `Settable properties for the ${protocol} protocol`,
|
|
34
|
+
type: ComplexValueTypeName.COMPLEX as const,
|
|
35
|
+
isOptional: true,
|
|
36
|
+
properties,
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
});
|
|
40
|
+
if (groups.length === 0) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
name: "properties",
|
|
45
|
+
description: "Protocol-specific properties to set",
|
|
46
|
+
type: ComplexValueTypeName.COMPLEX,
|
|
47
|
+
isOptional: true,
|
|
48
|
+
properties: groups,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createSetPropertiesCommand(registry: ProviderRegistry): SubCommand {
|
|
53
|
+
const propertiesOption = createPropertiesOption(registry);
|
|
54
|
+
return {
|
|
55
|
+
name: "set-properties",
|
|
56
|
+
description: "Set properties of a file/folder",
|
|
57
|
+
positionals: [],
|
|
58
|
+
options: [
|
|
59
|
+
createLocationOption(registry, "location", "Location to modify"),
|
|
60
|
+
{
|
|
61
|
+
name: "last-modified",
|
|
62
|
+
description: "Last modified time to apply, as an ISO 8601 date",
|
|
63
|
+
type: ValueTypeName.STRING,
|
|
64
|
+
isOptional: true,
|
|
65
|
+
validate: (value) =>
|
|
66
|
+
Number.isNaN(Date.parse(value as string)) ? "must be an ISO 8601 date" : undefined,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: "content-type",
|
|
70
|
+
description: "Content type to apply",
|
|
71
|
+
type: ValueTypeName.STRING,
|
|
72
|
+
isOptional: true,
|
|
73
|
+
},
|
|
74
|
+
...(propertiesOption ? [propertiesOption] : []),
|
|
75
|
+
],
|
|
76
|
+
async execute(context: Context, argumentValues: Values): Promise<void> {
|
|
77
|
+
const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
|
|
78
|
+
const location = toStructuredLocation(argumentValues.location as Values);
|
|
79
|
+
const lastModified = argumentValues["last-modified"] as string | undefined;
|
|
80
|
+
const contentType = argumentValues["content-type"] as string | undefined;
|
|
81
|
+
const properties = (argumentValues.properties as Values | undefined)?.[location.protocol] as
|
|
82
|
+
| Values
|
|
83
|
+
| undefined;
|
|
84
|
+
const changes: EntryPropertyChanges = {
|
|
85
|
+
...(lastModified !== undefined ? { lastModified: new Date(lastModified) } : {}),
|
|
86
|
+
...(contentType !== undefined ? { contentType } : {}),
|
|
87
|
+
...(properties !== undefined ? { properties } : {}),
|
|
88
|
+
};
|
|
35
89
|
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
await printerService.showSpinner(`Setting properties for ${path}...`);
|
|
90
|
+
const { provider, target } = await registry.createProviderForLocation(location);
|
|
39
91
|
try {
|
|
40
|
-
|
|
92
|
+
const setProperties = requireOperation(provider, "setProperties", location.protocol);
|
|
93
|
+
const key = targetKey(target, "set-properties");
|
|
94
|
+
await printerService.showSpinner(`Setting properties for ${key}...`);
|
|
95
|
+
try {
|
|
96
|
+
await setProperties(key, changes);
|
|
97
|
+
} finally {
|
|
98
|
+
await printerService.hideSpinner();
|
|
99
|
+
}
|
|
100
|
+
await printerService.print(`Updated properties for ${key}\n`, Icon.SUCCESS);
|
|
41
101
|
} finally {
|
|
42
|
-
await
|
|
102
|
+
await provider[Symbol.asyncDispose]();
|
|
43
103
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export default setProperties;
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Icon,
|
|
3
|
+
PRINTER_SERVICE_ID,
|
|
4
|
+
SHUTDOWN_SERVICE_ID,
|
|
5
|
+
type Context,
|
|
6
|
+
type PrinterService,
|
|
7
|
+
type ShutdownService,
|
|
8
|
+
type SubCommand,
|
|
9
|
+
type Values,
|
|
10
|
+
} from "@flowscripter/dynamic-cli-framework";
|
|
11
|
+
import {
|
|
12
|
+
copy,
|
|
13
|
+
move,
|
|
14
|
+
type ProviderRegistry,
|
|
15
|
+
type TransferOptions,
|
|
16
|
+
type TransferResult,
|
|
17
|
+
} from "@flowscripter/pluggable-io-framework";
|
|
18
|
+
import { createByteScale } from "../util/byteScale.ts";
|
|
19
|
+
import { createLocationOption } from "../util/location/createLocationOption.ts";
|
|
20
|
+
import { describeTarget } from "../util/location/describeTarget.ts";
|
|
21
|
+
import { toStructuredLocation } from "../util/location/toStructuredLocation.ts";
|
|
22
|
+
import { payloadKindOption, toPayloadKind } from "../util/payloadKindOption.ts";
|
|
23
|
+
import { createInterruptSignals } from "../util/interruptSignals.ts";
|
|
24
|
+
|
|
25
|
+
const VERBS = {
|
|
26
|
+
copy: { run: copy, progress: "Copying", done: "Copied", title: "Copy" },
|
|
27
|
+
move: { run: move, progress: "Moving", done: "Moved", title: "Move" },
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Builds the `copy` or `move` command: both locations are resolved through
|
|
32
|
+
* the registry, which negotiates the providers and payload path, and the
|
|
33
|
+
* negotiated path is printed with the result. The first Ctrl-C stops the
|
|
34
|
+
* transfer gracefully, the second cancels it.
|
|
35
|
+
*/
|
|
36
|
+
export function createTransferCommand(
|
|
37
|
+
registry: ProviderRegistry,
|
|
38
|
+
type: "copy" | "move",
|
|
39
|
+
): SubCommand {
|
|
40
|
+
const verb = VERBS[type];
|
|
41
|
+
return {
|
|
42
|
+
name: type,
|
|
43
|
+
description: `${verb.title} a file, folder or pattern, using a direct provider ${type} when possible`,
|
|
44
|
+
positionals: [],
|
|
45
|
+
options: [
|
|
46
|
+
createLocationOption(registry, "source", "Source location"),
|
|
47
|
+
createLocationOption(registry, "dest", "Destination location"),
|
|
48
|
+
payloadKindOption,
|
|
49
|
+
],
|
|
50
|
+
async execute(context: Context, argumentValues: Values): Promise<void> {
|
|
51
|
+
const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
|
|
52
|
+
const shutdownService = context.getServiceById(SHUTDOWN_SERVICE_ID) as ShutdownService;
|
|
53
|
+
|
|
54
|
+
const { source, dest, options } = await registry.createProvidersForTransfer(
|
|
55
|
+
toStructuredLocation(argumentValues.source as Values),
|
|
56
|
+
toStructuredLocation(argumentValues.dest as Values),
|
|
57
|
+
{ kind: toPayloadKind(argumentValues["payload-kind"]) },
|
|
58
|
+
);
|
|
59
|
+
const sourceName = describeTarget(source.target);
|
|
60
|
+
const destName = describeTarget(dest.target);
|
|
61
|
+
try {
|
|
62
|
+
using signals = createInterruptSignals(shutdownService);
|
|
63
|
+
const transferOptions: TransferOptions = {
|
|
64
|
+
...options,
|
|
65
|
+
stop: signals.stop,
|
|
66
|
+
signal: signals.signal,
|
|
67
|
+
};
|
|
68
|
+
let result: TransferResult;
|
|
69
|
+
if (source.target.kind === "entry") {
|
|
70
|
+
const { size } = await source.provider.getProperties(source.target.key);
|
|
71
|
+
const byteScale = createByteScale(size ?? 100);
|
|
72
|
+
const handle = await printerService.showProgressBar({
|
|
73
|
+
message: `${verb.progress} ${sourceName}`,
|
|
74
|
+
total: byteScale.total,
|
|
75
|
+
format: byteScale.format,
|
|
76
|
+
formatRate: byteScale.formatRate,
|
|
77
|
+
});
|
|
78
|
+
try {
|
|
79
|
+
result = await verb.run(source.provider, source.target, dest.provider, dest.target, {
|
|
80
|
+
...transferOptions,
|
|
81
|
+
telemetry: {
|
|
82
|
+
onProgress: (event) => {
|
|
83
|
+
if (event.parentOperationId === undefined) {
|
|
84
|
+
printerService.updateProgressBar(handle, byteScale.scale(event.bytesProcessed));
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
} finally {
|
|
90
|
+
await printerService.hideProgressBar(handle);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
await printerService.showSpinner(`${verb.progress} ${sourceName}...`);
|
|
94
|
+
try {
|
|
95
|
+
result = await verb.run(
|
|
96
|
+
source.provider,
|
|
97
|
+
source.target,
|
|
98
|
+
dest.provider,
|
|
99
|
+
dest.target,
|
|
100
|
+
transferOptions,
|
|
101
|
+
);
|
|
102
|
+
} finally {
|
|
103
|
+
await printerService.hideSpinner();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (result.stopped) {
|
|
107
|
+
await printerService.print(
|
|
108
|
+
`${verb.title} of ${sourceName} to ${destName} stopped after ${result.bytes} bytes\n`,
|
|
109
|
+
Icon.ALERT,
|
|
110
|
+
);
|
|
111
|
+
} else {
|
|
112
|
+
await printerService.print(`${verb.done} ${sourceName} to ${destName}\n`, Icon.SUCCESS);
|
|
113
|
+
}
|
|
114
|
+
await printerService.print(`path: ${result.path}\n`);
|
|
115
|
+
} finally {
|
|
116
|
+
await source.provider[Symbol.asyncDispose]();
|
|
117
|
+
await dest.provider[Symbol.asyncDispose]();
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { ShutdownService } from "@flowscripter/dynamic-cli-framework";
|
|
2
|
+
|
|
3
|
+
export interface InterruptSignals {
|
|
4
|
+
/** Aborted by the first Ctrl-C: end gracefully and report the result as stopped. */
|
|
5
|
+
readonly stop: AbortSignal;
|
|
6
|
+
/** Aborted by the second Ctrl-C: cancel. */
|
|
7
|
+
readonly signal: AbortSignal;
|
|
8
|
+
[Symbol.dispose](): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Maps Ctrl-C during a transfer to `stop` on the first press and `signal`
|
|
13
|
+
* on the second. The CLI is kept in long-running mode until disposed, so
|
|
14
|
+
* the first press does not exit the process.
|
|
15
|
+
*/
|
|
16
|
+
export function createInterruptSignals(shutdownService: ShutdownService): InterruptSignals {
|
|
17
|
+
const stop = new AbortController();
|
|
18
|
+
const cancel = new AbortController();
|
|
19
|
+
const onInterrupt = () => {
|
|
20
|
+
if (!stop.signal.aborted) {
|
|
21
|
+
stop.abort();
|
|
22
|
+
} else {
|
|
23
|
+
cancel.abort();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
shutdownService.enterLongRunningMode();
|
|
27
|
+
process.on("SIGINT", onInterrupt);
|
|
28
|
+
return {
|
|
29
|
+
stop: stop.signal,
|
|
30
|
+
signal: cancel.signal,
|
|
31
|
+
[Symbol.dispose]() {
|
|
32
|
+
process.removeListener("SIGINT", onInterrupt);
|
|
33
|
+
shutdownService.leaveLongRunningMode();
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function rejectOnAbort(signal: AbortSignal): { promise: Promise<never>; release(): void } {
|
|
39
|
+
let onAbort: () => void = () => {};
|
|
40
|
+
const promise = new Promise<never>((_, reject) => {
|
|
41
|
+
onAbort = () => reject(signal.reason);
|
|
42
|
+
if (signal.aborted) onAbort();
|
|
43
|
+
else signal.addEventListener("abort", onAbort, { once: true });
|
|
44
|
+
});
|
|
45
|
+
return { promise, release: () => signal.removeEventListener("abort", onAbort) };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Yields the items of `source` until `signals.stop` is aborted, which ends
|
|
50
|
+
* the iteration gracefully. Aborting `signals.signal` rejects, even while
|
|
51
|
+
* waiting for the next item.
|
|
52
|
+
*/
|
|
53
|
+
export async function* iterateUntilInterrupted<T>(
|
|
54
|
+
source: AsyncIterable<T>,
|
|
55
|
+
signals: InterruptSignals,
|
|
56
|
+
): AsyncGenerator<T> {
|
|
57
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
58
|
+
const cancelled = rejectOnAbort(signals.signal);
|
|
59
|
+
cancelled.promise.catch(() => {});
|
|
60
|
+
try {
|
|
61
|
+
while (!signals.stop.aborted) {
|
|
62
|
+
const next = await Promise.race([iterator.next(), cancelled.promise]);
|
|
63
|
+
if (next.done) return;
|
|
64
|
+
yield next.value;
|
|
65
|
+
}
|
|
66
|
+
} finally {
|
|
67
|
+
cancelled.release();
|
|
68
|
+
iterator.return?.().catch(() => {});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ComplexOption,
|
|
3
|
+
ComplexValueTypeName,
|
|
4
|
+
type Values,
|
|
5
|
+
ValueTypeName,
|
|
6
|
+
} from "@flowscripter/dynamic-cli-framework";
|
|
7
|
+
import type { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
8
|
+
import { zodToComplexOption } from "../zodToComplexOption.ts";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Builds a location argument with a `protocol` property, restricted to the
|
|
12
|
+
* installed protocols, and one nested group per protocol holding that
|
|
13
|
+
* protocol's location fields, e.g. `--source.protocol=file
|
|
14
|
+
* --source.file.path=/data --source.file.filename=a.txt`. The selected
|
|
15
|
+
* protocol's group is validated against its factory's `locationSchema`.
|
|
16
|
+
*/
|
|
17
|
+
export function createLocationOption(
|
|
18
|
+
registry: ProviderRegistry,
|
|
19
|
+
name: string,
|
|
20
|
+
description: string,
|
|
21
|
+
): ComplexOption {
|
|
22
|
+
const protocols = registry.getProtocols();
|
|
23
|
+
return {
|
|
24
|
+
name,
|
|
25
|
+
description,
|
|
26
|
+
type: ComplexValueTypeName.COMPLEX,
|
|
27
|
+
properties: [
|
|
28
|
+
{
|
|
29
|
+
name: "protocol",
|
|
30
|
+
description: "Protocol of the location",
|
|
31
|
+
type: ValueTypeName.STRING,
|
|
32
|
+
allowableValues: protocols,
|
|
33
|
+
},
|
|
34
|
+
...protocols.map((protocol) => ({
|
|
35
|
+
name: protocol,
|
|
36
|
+
description: `Location fields for the ${protocol} protocol`,
|
|
37
|
+
type: ComplexValueTypeName.COMPLEX as const,
|
|
38
|
+
isOptional: true,
|
|
39
|
+
properties: zodToComplexOption(registry.getFactory(protocol)!.locationSchema),
|
|
40
|
+
})),
|
|
41
|
+
],
|
|
42
|
+
validate: (value) => {
|
|
43
|
+
const location = value as Values;
|
|
44
|
+
const protocol = location.protocol as string;
|
|
45
|
+
const factory = registry.getFactory(protocol);
|
|
46
|
+
if (!factory) {
|
|
47
|
+
return `No provider installed for protocol "${protocol}"`;
|
|
48
|
+
}
|
|
49
|
+
const result = factory.locationSchema.safeParse(location[protocol] ?? {});
|
|
50
|
+
if (result.success) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const issues = result.error.issues.map((issue) =>
|
|
54
|
+
issue.path.length > 0 ? `${issue.path.join(".")}: ${issue.message}` : issue.message,
|
|
55
|
+
);
|
|
56
|
+
return `Invalid ${protocol} location: ${issues.join("; ")}`;
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { LocationTarget } from "@flowscripter/pluggable-io-framework-api";
|
|
2
|
+
|
|
3
|
+
/** A short human-readable description of a `LocationTarget` for messages. */
|
|
4
|
+
export function describeTarget(target: LocationTarget): string {
|
|
5
|
+
return target.kind === "pattern" ? `${target.pattern} in ${target.containerKey}` : target.key;
|
|
6
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { LocationTarget } from "@flowscripter/pluggable-io-framework-api";
|
|
2
|
+
|
|
3
|
+
/** The key of an `entry` or `container` target; a `pattern` target is rejected. */
|
|
4
|
+
export function targetKey(target: LocationTarget, command: string): string {
|
|
5
|
+
if (target.kind === "pattern") {
|
|
6
|
+
throw new Error(`${command} does not accept a pattern location`);
|
|
7
|
+
}
|
|
8
|
+
return target.key;
|
|
9
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Values } from "@flowscripter/dynamic-cli-framework";
|
|
2
|
+
import type { StructuredLocation } from "@flowscripter/pluggable-io-framework";
|
|
3
|
+
|
|
4
|
+
/** Converts a location argument's value into the registry's `StructuredLocation`. */
|
|
5
|
+
export function toStructuredLocation(value: Values): StructuredLocation {
|
|
6
|
+
const protocol = value.protocol as string;
|
|
7
|
+
return { protocol, location: value[protocol] ?? {} };
|
|
8
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Option, ValueTypeName } from "@flowscripter/dynamic-cli-framework";
|
|
2
|
+
import type { PayloadKind } from "@flowscripter/pluggable-io-framework-api";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Selects the payload kind of the providers: `auto` lets the registry
|
|
6
|
+
* choose, `js` or `native` require that kind.
|
|
7
|
+
*/
|
|
8
|
+
export const payloadKindOption: Option = {
|
|
9
|
+
name: "payload-kind",
|
|
10
|
+
description: "Payload kind of the providers (auto lets the registry choose)",
|
|
11
|
+
type: ValueTypeName.STRING,
|
|
12
|
+
isOptional: true,
|
|
13
|
+
defaultValue: "auto",
|
|
14
|
+
allowableValues: ["auto", "js", "native"],
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** The explicit `PayloadKind` for a `--payload-kind` value, or `undefined` for `auto`. */
|
|
18
|
+
export function toPayloadKind(value: unknown): PayloadKind | undefined {
|
|
19
|
+
return value === undefined || value === "auto" ? undefined : (value as PayloadKind);
|
|
20
|
+
}
|
|
@@ -7,7 +7,7 @@ export const PLUGGABLE_IO_FRAMEWORK_PACKAGE_JSON_NAMESPACE = "pluggable-io-frame
|
|
|
7
7
|
* Local plugin install directory shared between the CLI's built-in plugin
|
|
8
8
|
* service (`plugin add`/`plugin list`, see cli.ts) and our own
|
|
9
9
|
* NpmPluginRepository scan for provider-factory extensions (see
|
|
10
|
-
*
|
|
10
|
+
* providerRegistry.ts) - both must point at the same install location.
|
|
11
11
|
*
|
|
12
12
|
* Overridable via FLOWSCRIPTER_IO_CLI_PLUGINS_PATH so tests can point at a
|
|
13
13
|
* temp directory instead of the real home directory. Read at call time (not
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DefaultPluginManager, NpmPluginRepository } from "@flowscripter/dynamic-plugin-framework";
|
|
2
|
+
import { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
3
|
+
import {
|
|
4
|
+
PLUGGABLE_IO_FRAMEWORK_PACKAGE_JSON_NAMESPACE,
|
|
5
|
+
getPluginsNodeModulesPath,
|
|
6
|
+
} from "./pluginsDir.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a `ProviderRegistry` over the local plugin store that the CLI's
|
|
10
|
+
* `plugin:add` command installs into, and discovers every installed
|
|
11
|
+
* provider factory and payload converter. This package has no dependency on
|
|
12
|
+
* any provider plugin; one must be installed first, e.g.
|
|
13
|
+
* `flowscripter-io-cli plugin:add @flowscripter/io-plugin-filesystem`.
|
|
14
|
+
*/
|
|
15
|
+
export async function createProviderRegistry(): Promise<ProviderRegistry> {
|
|
16
|
+
const repository = new NpmPluginRepository({
|
|
17
|
+
nodeModulesPath: getPluginsNodeModulesPath(),
|
|
18
|
+
packageJsonNamespace: PLUGGABLE_IO_FRAMEWORK_PACKAGE_JSON_NAMESPACE,
|
|
19
|
+
});
|
|
20
|
+
const registry = new ProviderRegistry(new DefaultPluginManager([repository]));
|
|
21
|
+
await registry.discover();
|
|
22
|
+
return registry;
|
|
23
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { IOProvider } from "@flowscripter/pluggable-io-framework-api";
|
|
2
|
+
|
|
3
|
+
type OptionalOperation = "list" | "setProperties" | "delete";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the provider's optional operation bound to it, or throws a clear
|
|
7
|
+
* error when the protocol's provider does not implement it.
|
|
8
|
+
*/
|
|
9
|
+
export function requireOperation<T extends OptionalOperation>(
|
|
10
|
+
provider: IOProvider,
|
|
11
|
+
operation: T,
|
|
12
|
+
protocol: string,
|
|
13
|
+
): NonNullable<IOProvider[T]> {
|
|
14
|
+
const method = provider[operation];
|
|
15
|
+
if (typeof method !== "function") {
|
|
16
|
+
throw new Error(`${operation} is not supported by protocol "${protocol}"`);
|
|
17
|
+
}
|
|
18
|
+
return method.bind(provider) as NonNullable<IOProvider[T]>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ComplexOption,
|
|
3
|
+
ComplexValueTypeName,
|
|
4
|
+
type Option,
|
|
5
|
+
ValueTypeName,
|
|
6
|
+
} from "@flowscripter/dynamic-cli-framework";
|
|
7
|
+
import type { ZodType } from "zod";
|
|
8
|
+
|
|
9
|
+
interface ZodNode {
|
|
10
|
+
readonly def: {
|
|
11
|
+
readonly type: string;
|
|
12
|
+
readonly innerType?: ZodNode;
|
|
13
|
+
readonly element?: ZodNode;
|
|
14
|
+
readonly entries?: Record<string, string>;
|
|
15
|
+
};
|
|
16
|
+
readonly shape?: Record<string, ZodNode>;
|
|
17
|
+
meta(): Record<string, unknown> | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface Unwrapped {
|
|
21
|
+
readonly node: ZodNode;
|
|
22
|
+
readonly isOptional: boolean;
|
|
23
|
+
readonly isSecret: boolean;
|
|
24
|
+
readonly description: string | undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function unwrap(node: ZodNode): Unwrapped {
|
|
28
|
+
let isOptional = false;
|
|
29
|
+
let isSecret = node.meta()?.secret === true;
|
|
30
|
+
let description = node.meta()?.description as string | undefined;
|
|
31
|
+
while (node.def.type === "optional" || node.def.type === "default") {
|
|
32
|
+
isOptional = true;
|
|
33
|
+
node = node.def.innerType as ZodNode;
|
|
34
|
+
isSecret ||= node.meta()?.secret === true;
|
|
35
|
+
description ??= node.meta()?.description as string | undefined;
|
|
36
|
+
}
|
|
37
|
+
return { node, isOptional, isSecret, description };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function toOption(name: string, schema: ZodNode): Option | ComplexOption {
|
|
41
|
+
const { node, isOptional, isSecret, description } = unwrap(schema);
|
|
42
|
+
const base = { name, description: description ?? name, ...(isOptional ? { isOptional } : {}) };
|
|
43
|
+
switch (node.def.type) {
|
|
44
|
+
case "string":
|
|
45
|
+
return { ...base, type: isSecret ? ValueTypeName.SECRET : ValueTypeName.STRING };
|
|
46
|
+
case "number":
|
|
47
|
+
return { ...base, type: isSecret ? ValueTypeName.SECRET : ValueTypeName.NUMBER };
|
|
48
|
+
case "boolean":
|
|
49
|
+
return { ...base, type: ValueTypeName.BOOLEAN };
|
|
50
|
+
case "enum":
|
|
51
|
+
return {
|
|
52
|
+
...base,
|
|
53
|
+
type: ValueTypeName.STRING,
|
|
54
|
+
allowableValues: Object.values(node.def.entries ?? {}),
|
|
55
|
+
};
|
|
56
|
+
case "object":
|
|
57
|
+
return { ...base, type: ComplexValueTypeName.COMPLEX, properties: toOptions(node) };
|
|
58
|
+
case "array": {
|
|
59
|
+
const element = unwrap(node.def.element as ZodNode).node;
|
|
60
|
+
if (element.def.type !== "object") {
|
|
61
|
+
throw new Error(`Unsupported array element type "${element.def.type}" for "${name}"`);
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
...base,
|
|
65
|
+
type: ComplexValueTypeName.COMPLEX,
|
|
66
|
+
isArray: true,
|
|
67
|
+
properties: toOptions(element),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
default:
|
|
71
|
+
throw new Error(`Unsupported schema type "${node.def.type}" for "${name}"`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function toOptions(node: ZodNode): ReadonlyArray<Option | ComplexOption> {
|
|
76
|
+
return Object.entries(node.shape ?? {}).map(([name, child]) => toOption(name, child));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Converts the fields of a Zod object schema into `ComplexOption` properties:
|
|
81
|
+
* strings, numbers and booleans become single-value options (`SECRET` when
|
|
82
|
+
* marked `.meta({ secret: true })`), enums become strings with allowable
|
|
83
|
+
* values, nested objects become nested `ComplexOption`s and arrays of objects
|
|
84
|
+
* become array `ComplexOption`s. Optional and defaulted fields are optional.
|
|
85
|
+
* A field's `.describe()` text is its description, defaulting to its name.
|
|
86
|
+
*/
|
|
87
|
+
export function zodToComplexOption(schema: ZodType): ReadonlyArray<Option | ComplexOption> {
|
|
88
|
+
const { node } = unwrap(schema as unknown as ZodNode);
|
|
89
|
+
if (node.def.type !== "object") {
|
|
90
|
+
throw new Error(`Expected an object schema, got "${node.def.type}"`);
|
|
91
|
+
}
|
|
92
|
+
return toOptions(node);
|
|
93
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { Icon } from "@flowscripter/dynamic-cli-framework";
|
|
6
|
+
import type { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
7
|
+
import { fileLocation, installFilesystemPlugin } from "../fixtures/pluginStore.ts";
|
|
8
|
+
import { createStubContext } from "../fixtures/testContext.ts";
|
|
9
|
+
import { createCopyCommand } from "../../src/commands/copy.ts";
|
|
10
|
+
|
|
11
|
+
let registry: ProviderRegistry;
|
|
12
|
+
let cleanup: () => Promise<void>;
|
|
13
|
+
let root: string;
|
|
14
|
+
|
|
15
|
+
beforeAll(async () => {
|
|
16
|
+
({ registry, cleanup } = await installFilesystemPlugin());
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterAll(async () => {
|
|
20
|
+
await cleanup();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
beforeEach(async () => {
|
|
24
|
+
root = await mkdtemp(join(tmpdir(), "flowscripter-io-cli-test-"));
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterEach(async () => {
|
|
28
|
+
await rm(root, { recursive: true, force: true });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("copy", () => {
|
|
32
|
+
test("copies a file and prints the negotiated path", async () => {
|
|
33
|
+
await writeFile(join(root, "a.txt"), "hello");
|
|
34
|
+
const { context, icons, lines, longRunningModes } = createStubContext();
|
|
35
|
+
|
|
36
|
+
await createCopyCommand(registry).execute(context, {
|
|
37
|
+
source: fileLocation(root, { filename: "a.txt" }),
|
|
38
|
+
dest: fileLocation(root, { filename: "b.txt" }),
|
|
39
|
+
"payload-kind": "auto",
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect(await readFile(join(root, "b.txt"), "utf8")).toBe("hello");
|
|
43
|
+
expect(await readFile(join(root, "a.txt"), "utf8")).toBe("hello");
|
|
44
|
+
expect(lines).toEqual([
|
|
45
|
+
`Copied ${join(root, "a.txt")} to ${join(root, "b.txt")}\n`,
|
|
46
|
+
"path: file/js -> file/js, direct\n",
|
|
47
|
+
]);
|
|
48
|
+
expect(icons).toEqual([Icon.SUCCESS, undefined]);
|
|
49
|
+
expect(longRunningModes).toEqual([true, false]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("copies the files matching a pattern into a folder", async () => {
|
|
53
|
+
await writeFile(join(root, "a.txt"), "a");
|
|
54
|
+
await writeFile(join(root, "b.log"), "b");
|
|
55
|
+
await mkdir(join(root, "out"));
|
|
56
|
+
const { context, spinnerMessages } = createStubContext();
|
|
57
|
+
|
|
58
|
+
await createCopyCommand(registry).execute(context, {
|
|
59
|
+
source: fileLocation(root, { pattern: "*.txt" }),
|
|
60
|
+
dest: fileLocation(join(root, "out")),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
expect(await readFile(join(root, "out", "a.txt"), "utf8")).toBe("a");
|
|
64
|
+
await expect(stat(join(root, "out", "b.log"))).rejects.toThrow();
|
|
65
|
+
expect(spinnerMessages).toEqual([`Copying *.txt in ${root}...`]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("fails with the registry's error for an unavailable payload kind", async () => {
|
|
69
|
+
await writeFile(join(root, "a.txt"), "hello");
|
|
70
|
+
const { context } = createStubContext();
|
|
71
|
+
|
|
72
|
+
await expect(
|
|
73
|
+
createCopyCommand(registry).execute(context, {
|
|
74
|
+
source: fileLocation(root, { filename: "a.txt" }),
|
|
75
|
+
dest: fileLocation(root, { filename: "b.txt" }),
|
|
76
|
+
"payload-kind": "native",
|
|
77
|
+
}),
|
|
78
|
+
).rejects.toThrow('No provider for protocol "file" with payload kind "native" (available: js)');
|
|
79
|
+
});
|
|
80
|
+
});
|