@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
package/src/commands/copy.ts
CHANGED
|
@@ -1,63 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type Context,
|
|
5
|
-
type PrinterService,
|
|
6
|
-
type SubCommand,
|
|
7
|
-
type Values,
|
|
8
|
-
ValueTypeName,
|
|
9
|
-
} from "@flowscripter/dynamic-cli-framework";
|
|
10
|
-
import { copy as copyProvider } from "@flowscripter/pluggable-io-framework";
|
|
11
|
-
import { getFilesystemProvider } from "../filesystemProvider.ts";
|
|
12
|
-
import { createByteScale } from "../byteScale.ts";
|
|
1
|
+
import type { SubCommand } from "@flowscripter/dynamic-cli-framework";
|
|
2
|
+
import type { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
3
|
+
import { createTransferCommand } from "./transferCommand.ts";
|
|
13
4
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
positionals: [
|
|
18
|
-
{
|
|
19
|
-
name: "source",
|
|
20
|
-
description: "Source path",
|
|
21
|
-
type: ValueTypeName.STRING,
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
name: "destination",
|
|
25
|
-
description: "Destination path",
|
|
26
|
-
type: ValueTypeName.STRING,
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
options: [],
|
|
30
|
-
async execute(context: Context, argumentValues: Values): Promise<void> {
|
|
31
|
-
const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
|
|
32
|
-
const source = argumentValues.source as string;
|
|
33
|
-
const destination = argumentValues.destination as string;
|
|
34
|
-
|
|
35
|
-
const provider = await getFilesystemProvider("");
|
|
36
|
-
try {
|
|
37
|
-
const { size } = await provider.getProperties(source);
|
|
38
|
-
const byteScale = createByteScale(size ?? 100);
|
|
39
|
-
const handle = await printerService.showProgressBar({
|
|
40
|
-
message: `Copying ${source}`,
|
|
41
|
-
total: byteScale.total,
|
|
42
|
-
format: byteScale.format,
|
|
43
|
-
formatRate: byteScale.formatRate,
|
|
44
|
-
});
|
|
45
|
-
try {
|
|
46
|
-
await copyProvider(provider, source, provider, destination, {
|
|
47
|
-
telemetry: {
|
|
48
|
-
onProgress: (event) => {
|
|
49
|
-
printerService.updateProgressBar(handle, byteScale.scale(event.bytesProcessed));
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
} finally {
|
|
54
|
-
await printerService.hideProgressBar(handle);
|
|
55
|
-
}
|
|
56
|
-
await printerService.print(`Copied ${source} to ${destination}\n`, Icon.SUCCESS);
|
|
57
|
-
} finally {
|
|
58
|
-
await provider[Symbol.asyncDispose]();
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export default copy;
|
|
5
|
+
export function createCopyCommand(registry: ProviderRegistry): SubCommand {
|
|
6
|
+
return createTransferCommand(registry, "copy");
|
|
7
|
+
}
|
package/src/commands/delete.ts
CHANGED
|
@@ -5,38 +5,37 @@ import {
|
|
|
5
5
|
type PrinterService,
|
|
6
6
|
type SubCommand,
|
|
7
7
|
type Values,
|
|
8
|
-
ValueTypeName,
|
|
9
8
|
} from "@flowscripter/dynamic-cli-framework";
|
|
10
|
-
import {
|
|
9
|
+
import type { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
10
|
+
import { createLocationOption } from "../util/location/createLocationOption.ts";
|
|
11
|
+
import { targetKey } from "../util/location/targetKey.ts";
|
|
12
|
+
import { toStructuredLocation } from "../util/location/toStructuredLocation.ts";
|
|
13
|
+
import { requireOperation } from "../util/requireOperation.ts";
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
],
|
|
22
|
-
options: [],
|
|
23
|
-
async execute(context: Context, argumentValues: Values): Promise<void> {
|
|
24
|
-
const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
|
|
25
|
-
const path = argumentValues.path as string;
|
|
15
|
+
export function createDeleteCommand(registry: ProviderRegistry): SubCommand {
|
|
16
|
+
return {
|
|
17
|
+
name: "delete",
|
|
18
|
+
description: "Delete a file/folder",
|
|
19
|
+
positionals: [],
|
|
20
|
+
options: [createLocationOption(registry, "location", "Location to delete")],
|
|
21
|
+
async execute(context: Context, argumentValues: Values): Promise<void> {
|
|
22
|
+
const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
|
|
23
|
+
const location = toStructuredLocation(argumentValues.location as Values);
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
await printerService.showSpinner(`Deleting ${path}...`);
|
|
25
|
+
const { provider, target } = await registry.createProviderForLocation(location);
|
|
30
26
|
try {
|
|
31
|
-
|
|
27
|
+
const deleteEntry = requireOperation(provider, "delete", location.protocol);
|
|
28
|
+
const key = targetKey(target, "delete");
|
|
29
|
+
await printerService.showSpinner(`Deleting ${key}...`);
|
|
30
|
+
try {
|
|
31
|
+
await deleteEntry(key);
|
|
32
|
+
} finally {
|
|
33
|
+
await printerService.hideSpinner();
|
|
34
|
+
}
|
|
35
|
+
await printerService.print(`Deleted ${key}\n`, Icon.SUCCESS);
|
|
32
36
|
} finally {
|
|
33
|
-
await
|
|
37
|
+
await provider[Symbol.asyncDispose]();
|
|
34
38
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export default deleteCommand;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -8,40 +8,36 @@ import {
|
|
|
8
8
|
type SubCommand,
|
|
9
9
|
type SyntaxHighlighterService,
|
|
10
10
|
type Values,
|
|
11
|
-
ValueTypeName,
|
|
12
11
|
} from "@flowscripter/dynamic-cli-framework";
|
|
13
|
-
import {
|
|
12
|
+
import type { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
13
|
+
import { createLocationOption } from "../util/location/createLocationOption.ts";
|
|
14
|
+
import { targetKey } from "../util/location/targetKey.ts";
|
|
15
|
+
import { toStructuredLocation } from "../util/location/toStructuredLocation.ts";
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
) as PrettyPrinterService;
|
|
31
|
-
const syntaxHighlighterService = context.getServiceById(
|
|
32
|
-
SYNTAX_HIGHLIGHTER_SERVICE_ID,
|
|
33
|
-
) as SyntaxHighlighterService;
|
|
34
|
-
const path = argumentValues.path as string;
|
|
35
|
-
|
|
36
|
-
const provider = await getFilesystemProvider("");
|
|
37
|
-
try {
|
|
38
|
-
const properties = await provider.getProperties(path);
|
|
39
|
-
const pretty = await prettyPrinterService.prettify(JSON.stringify(properties), "json");
|
|
40
|
-
await printerService.print(`${syntaxHighlighterService.highlight(pretty, "json")}\n`);
|
|
41
|
-
} finally {
|
|
42
|
-
await provider[Symbol.asyncDispose]();
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
};
|
|
17
|
+
export function createGetPropertiesCommand(registry: ProviderRegistry): SubCommand {
|
|
18
|
+
return {
|
|
19
|
+
name: "get-properties",
|
|
20
|
+
description: "Get properties of a file/folder",
|
|
21
|
+
positionals: [],
|
|
22
|
+
options: [createLocationOption(registry, "location", "Location to inspect")],
|
|
23
|
+
async execute(context: Context, argumentValues: Values): Promise<void> {
|
|
24
|
+
const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
|
|
25
|
+
const prettyPrinterService = context.getServiceById(
|
|
26
|
+
PRETTY_PRINTER_SERVICE_ID,
|
|
27
|
+
) as PrettyPrinterService;
|
|
28
|
+
const syntaxHighlighterService = context.getServiceById(
|
|
29
|
+
SYNTAX_HIGHLIGHTER_SERVICE_ID,
|
|
30
|
+
) as SyntaxHighlighterService;
|
|
31
|
+
const location = toStructuredLocation(argumentValues.location as Values);
|
|
46
32
|
|
|
47
|
-
|
|
33
|
+
const { provider, target } = await registry.createProviderForLocation(location);
|
|
34
|
+
try {
|
|
35
|
+
const properties = await provider.getProperties(targetKey(target, "get-properties"));
|
|
36
|
+
const pretty = await prettyPrinterService.prettify(JSON.stringify(properties), "json");
|
|
37
|
+
await printerService.print(`${syntaxHighlighterService.highlight(pretty, "json")}\n`);
|
|
38
|
+
} finally {
|
|
39
|
+
await provider[Symbol.asyncDispose]();
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
package/src/commands/hash.ts
CHANGED
|
@@ -1,73 +1,106 @@
|
|
|
1
1
|
import {
|
|
2
|
+
Icon,
|
|
2
3
|
PRINTER_SERVICE_ID,
|
|
4
|
+
SHUTDOWN_SERVICE_ID,
|
|
3
5
|
type Context,
|
|
4
6
|
type PrinterService,
|
|
7
|
+
type ShutdownService,
|
|
5
8
|
type SubCommand,
|
|
6
9
|
type Values,
|
|
7
10
|
ValueTypeName,
|
|
8
11
|
} from "@flowscripter/dynamic-cli-framework";
|
|
9
|
-
import {
|
|
12
|
+
import type { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
13
|
+
import { type Item, PayloadKind } from "@flowscripter/pluggable-io-framework-api";
|
|
10
14
|
import { Sha256Hasher } from "@flowscripter/flowscripter-io-cli-hash-native";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
15
|
+
import { createByteScale } from "../util/byteScale.ts";
|
|
16
|
+
import { createLocationOption } from "../util/location/createLocationOption.ts";
|
|
17
|
+
import { toStructuredLocation } from "../util/location/toStructuredLocation.ts";
|
|
18
|
+
import { payloadKindOption, toPayloadKind } from "../util/payloadKindOption.ts";
|
|
19
|
+
import { createInterruptSignals } from "../util/interruptSignals.ts";
|
|
13
20
|
|
|
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
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Builds the `hash` command, which consumes a provider's readable stream
|
|
23
|
+
* directly. The first Ctrl-C stops reading and prints the digest of the
|
|
24
|
+
* bytes read so far, marked as stopped; the second cancels.
|
|
25
|
+
*/
|
|
26
|
+
export function createHashCommand(registry: ProviderRegistry): SubCommand {
|
|
27
|
+
return {
|
|
28
|
+
name: "hash",
|
|
29
|
+
description: "Hash a file by piping its readable stream through a hasher",
|
|
30
|
+
positionals: [],
|
|
31
|
+
options: [
|
|
32
|
+
createLocationOption(registry, "location", "Location of the file to hash"),
|
|
33
|
+
{
|
|
34
|
+
name: "algorithm",
|
|
35
|
+
description: "Hash algorithm",
|
|
36
|
+
type: ValueTypeName.STRING,
|
|
37
|
+
shortAlias: "a",
|
|
38
|
+
isOptional: true,
|
|
39
|
+
defaultValue: "sha256",
|
|
40
|
+
allowableValues: ["sha1", "sha256", "sha384", "sha512", "md5"],
|
|
41
|
+
},
|
|
42
|
+
payloadKindOption,
|
|
43
|
+
],
|
|
44
|
+
async execute(context: Context, argumentValues: Values): Promise<void> {
|
|
45
|
+
const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
|
|
46
|
+
const shutdownService = context.getServiceById(SHUTDOWN_SERVICE_ID) as ShutdownService;
|
|
47
|
+
const location = toStructuredLocation(argumentValues.location as Values);
|
|
48
|
+
const algorithm = argumentValues.algorithm as string;
|
|
39
49
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const { size } = await provider.getProperties(path);
|
|
43
|
-
const byteScale = createByteScale(size ?? 100);
|
|
44
|
-
const progressHandle = await printerService.showProgressBar({
|
|
45
|
-
message: `Hashing ${path}`,
|
|
46
|
-
total: byteScale.total,
|
|
47
|
-
format: byteScale.format,
|
|
48
|
-
formatRate: byteScale.formatRate,
|
|
50
|
+
const { provider, target } = await registry.createProviderForLocation(location, {
|
|
51
|
+
kind: toPayloadKind(argumentValues["payload-kind"]),
|
|
49
52
|
});
|
|
50
|
-
let bytesProcessed = 0;
|
|
51
|
-
const onChunk = (chunkLength: number): void => {
|
|
52
|
-
bytesProcessed += chunkLength;
|
|
53
|
-
printerService.updateProgressBar(progressHandle, byteScale.scale(bytesProcessed));
|
|
54
|
-
};
|
|
55
53
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
if (target.kind !== "entry") {
|
|
55
|
+
throw new Error("hash needs a single entry location");
|
|
56
|
+
}
|
|
57
|
+
const path = target.key;
|
|
58
|
+
const { size } = await provider.getProperties(path);
|
|
59
|
+
const byteScale = createByteScale(size ?? 100);
|
|
60
|
+
const progressHandle = await printerService.showProgressBar({
|
|
61
|
+
message: `Hashing ${path}`,
|
|
62
|
+
total: byteScale.total,
|
|
63
|
+
format: byteScale.format,
|
|
64
|
+
formatRate: byteScale.formatRate,
|
|
65
|
+
});
|
|
66
|
+
let bytesProcessed = 0;
|
|
67
|
+
const onChunk = (chunkLength: number): void => {
|
|
68
|
+
bytesProcessed += chunkLength;
|
|
69
|
+
printerService.updateProgressBar(progressHandle, byteScale.scale(bytesProcessed));
|
|
70
|
+
};
|
|
71
|
+
let digestHex: string;
|
|
72
|
+
let stopped: boolean;
|
|
73
|
+
try {
|
|
74
|
+
using signals = createInterruptSignals(shutdownService);
|
|
75
|
+
const handle = await provider.getReadableStream(path);
|
|
76
|
+
const reader = (handle.stream as ReadableStream<Item>).getReader();
|
|
77
|
+
const cancel = () => {
|
|
78
|
+
reader.cancel().catch(() => {});
|
|
79
|
+
};
|
|
80
|
+
signals.stop.addEventListener("abort", cancel);
|
|
81
|
+
signals.signal.addEventListener("abort", cancel);
|
|
82
|
+
digestHex =
|
|
83
|
+
algorithm === "sha256"
|
|
84
|
+
? await hashWithNativeSha256(reader, onChunk)
|
|
85
|
+
: await hashWithBunCryptoHasher(reader, algorithm, onChunk);
|
|
86
|
+
signals.signal.throwIfAborted();
|
|
87
|
+
stopped = signals.stop.aborted;
|
|
88
|
+
} finally {
|
|
89
|
+
await printerService.hideProgressBar(progressHandle);
|
|
90
|
+
}
|
|
91
|
+
if (stopped) {
|
|
92
|
+
await printerService.print(
|
|
93
|
+
`Hashing stopped after ${bytesProcessed} bytes, digest covers only those bytes\n`,
|
|
94
|
+
Icon.ALERT,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
62
97
|
await printerService.print(`${digestHex} ${path}\n`);
|
|
63
98
|
} finally {
|
|
64
|
-
await
|
|
99
|
+
await provider[Symbol.asyncDispose]();
|
|
65
100
|
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
},
|
|
70
|
-
};
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
71
104
|
|
|
72
105
|
function toHex(bytes: Uint8Array): string {
|
|
73
106
|
return Array.from(bytes)
|
|
@@ -76,27 +109,28 @@ function toHex(bytes: Uint8Array): string {
|
|
|
76
109
|
}
|
|
77
110
|
|
|
78
111
|
async function hashWithNativeSha256(
|
|
79
|
-
reader: ReadableStreamDefaultReader<
|
|
112
|
+
reader: ReadableStreamDefaultReader<Item>,
|
|
80
113
|
onChunk: (chunkLength: number) => void,
|
|
81
114
|
): Promise<string> {
|
|
82
115
|
const hasher = new Sha256Hasher();
|
|
83
116
|
for (;;) {
|
|
84
117
|
const { done, value } = await reader.read();
|
|
85
118
|
if (done) break;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
119
|
+
const { payload } = value;
|
|
120
|
+
if (payload.kind === PayloadKind.Js) {
|
|
121
|
+
hasher.update(payload.data);
|
|
122
|
+
onChunk(payload.data.byteLength);
|
|
89
123
|
} else {
|
|
90
|
-
hasher.updatePointer(
|
|
91
|
-
onChunk(
|
|
92
|
-
|
|
124
|
+
hasher.updatePointer(payload.ptr, payload.length);
|
|
125
|
+
onChunk(payload.length);
|
|
126
|
+
payload.release();
|
|
93
127
|
}
|
|
94
128
|
}
|
|
95
129
|
return toHex(hasher.final());
|
|
96
130
|
}
|
|
97
131
|
|
|
98
132
|
async function hashWithBunCryptoHasher(
|
|
99
|
-
reader: ReadableStreamDefaultReader<
|
|
133
|
+
reader: ReadableStreamDefaultReader<Item>,
|
|
100
134
|
algorithm: string,
|
|
101
135
|
onChunk: (chunkLength: number) => void,
|
|
102
136
|
): Promise<string> {
|
|
@@ -106,14 +140,14 @@ async function hashWithBunCryptoHasher(
|
|
|
106
140
|
for (;;) {
|
|
107
141
|
const { done, value } = await reader.read();
|
|
108
142
|
if (done) break;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
143
|
+
const { payload } = value;
|
|
144
|
+
if (payload.kind === PayloadKind.Js) {
|
|
145
|
+
hasher.update(payload.data);
|
|
146
|
+
onChunk(payload.data.byteLength);
|
|
112
147
|
} else {
|
|
113
|
-
|
|
148
|
+
payload.release();
|
|
149
|
+
throw new Error(`hash command only supports js-kind payloads for algorithm "${algorithm}"`);
|
|
114
150
|
}
|
|
115
151
|
}
|
|
116
152
|
return hasher.digest("hex");
|
|
117
153
|
}
|
|
118
|
-
|
|
119
|
-
export default hash;
|
package/src/commands/list.ts
CHANGED
|
@@ -1,72 +1,91 @@
|
|
|
1
1
|
import {
|
|
2
|
+
Icon,
|
|
2
3
|
PRETTY_PRINTER_SERVICE_ID,
|
|
3
4
|
PRINTER_SERVICE_ID,
|
|
5
|
+
SHUTDOWN_SERVICE_ID,
|
|
4
6
|
SYNTAX_HIGHLIGHTER_SERVICE_ID,
|
|
5
7
|
type Context,
|
|
6
8
|
type PrettyPrinterService,
|
|
7
9
|
type PrinterService,
|
|
10
|
+
type ShutdownService,
|
|
8
11
|
type SubCommand,
|
|
9
12
|
type SyntaxHighlighterService,
|
|
10
13
|
type Values,
|
|
11
14
|
ValueTypeName,
|
|
12
15
|
} from "@flowscripter/dynamic-cli-framework";
|
|
13
|
-
import {
|
|
16
|
+
import { globToRegex, type ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
17
|
+
import { createLocationOption } from "../util/location/createLocationOption.ts";
|
|
18
|
+
import { toStructuredLocation } from "../util/location/toStructuredLocation.ts";
|
|
19
|
+
import { createInterruptSignals, iterateUntilInterrupted } from "../util/interruptSignals.ts";
|
|
20
|
+
import { requireOperation } from "../util/requireOperation.ts";
|
|
14
21
|
|
|
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
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Builds the `list` command. The first Ctrl-C stops listing gracefully,
|
|
24
|
+
* the second cancels.
|
|
25
|
+
*/
|
|
26
|
+
export function createListCommand(registry: ProviderRegistry): SubCommand {
|
|
27
|
+
return {
|
|
28
|
+
name: "list",
|
|
29
|
+
description: "List files/folders, optionally recursive and filtered by regex or pattern",
|
|
30
|
+
positionals: [],
|
|
31
|
+
options: [
|
|
32
|
+
createLocationOption(registry, "location", "Folder or pattern location to list"),
|
|
33
|
+
{
|
|
34
|
+
name: "recursive",
|
|
35
|
+
description: "List recursively",
|
|
36
|
+
type: ValueTypeName.BOOLEAN,
|
|
37
|
+
shortAlias: "r",
|
|
38
|
+
isOptional: true,
|
|
39
|
+
defaultValue: false,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: "regex",
|
|
43
|
+
description: "Only list items whose path matches this regex",
|
|
44
|
+
type: ValueTypeName.STRING,
|
|
45
|
+
shortAlias: "e",
|
|
46
|
+
isOptional: true,
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
async execute(context: Context, argumentValues: Values): Promise<void> {
|
|
50
|
+
const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
|
|
51
|
+
const prettyPrinterService = context.getServiceById(
|
|
52
|
+
PRETTY_PRINTER_SERVICE_ID,
|
|
53
|
+
) as PrettyPrinterService;
|
|
54
|
+
const syntaxHighlighterService = context.getServiceById(
|
|
55
|
+
SYNTAX_HIGHLIGHTER_SERVICE_ID,
|
|
56
|
+
) as SyntaxHighlighterService;
|
|
57
|
+
const shutdownService = context.getServiceById(SHUTDOWN_SERVICE_ID) as ShutdownService;
|
|
58
|
+
const location = toStructuredLocation(argumentValues.location as Values);
|
|
59
|
+
const recursive = argumentValues.recursive as boolean | undefined;
|
|
60
|
+
const regex = argumentValues.regex as string | undefined;
|
|
53
61
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
const { provider, target } = await registry.createProviderForLocation(location);
|
|
63
|
+
try {
|
|
64
|
+
const list = requireOperation(provider, "list", location.protocol);
|
|
65
|
+
if (target.kind === "entry") {
|
|
66
|
+
throw new Error("list needs a folder or pattern location, not a single entry");
|
|
67
|
+
}
|
|
68
|
+
if (target.kind === "pattern" && regex !== undefined) {
|
|
69
|
+
throw new Error("list accepts either a pattern location or --regex, not both");
|
|
70
|
+
}
|
|
71
|
+
const items =
|
|
72
|
+
target.kind === "pattern"
|
|
73
|
+
? list(target.containerKey, { recursive, regex: globToRegex(target.pattern) })
|
|
74
|
+
: list(target.key, { recursive, regex: regex ? new RegExp(regex) : undefined });
|
|
75
|
+
using signals = createInterruptSignals(shutdownService);
|
|
76
|
+
for await (const item of iterateUntilInterrupted(items, signals)) {
|
|
77
|
+
const pretty = await prettyPrinterService.prettify(
|
|
78
|
+
JSON.stringify({ path: item.path, ...item.properties }),
|
|
79
|
+
"json",
|
|
80
|
+
);
|
|
81
|
+
await printerService.print(`${syntaxHighlighterService.highlight(pretty, "json")}\n`);
|
|
82
|
+
}
|
|
83
|
+
if (signals.stop.aborted) {
|
|
84
|
+
await printerService.print("Listing stopped before all items were listed\n", Icon.ALERT);
|
|
85
|
+
}
|
|
86
|
+
} finally {
|
|
87
|
+
await provider[Symbol.asyncDispose]();
|
|
65
88
|
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export default list;
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
package/src/commands/move.ts
CHANGED
|
@@ -1,63 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type Context,
|
|
5
|
-
type PrinterService,
|
|
6
|
-
type SubCommand,
|
|
7
|
-
type Values,
|
|
8
|
-
ValueTypeName,
|
|
9
|
-
} from "@flowscripter/dynamic-cli-framework";
|
|
10
|
-
import { move as moveProvider } from "@flowscripter/pluggable-io-framework";
|
|
11
|
-
import { getFilesystemProvider } from "../filesystemProvider.ts";
|
|
12
|
-
import { createByteScale } from "../byteScale.ts";
|
|
1
|
+
import type { SubCommand } from "@flowscripter/dynamic-cli-framework";
|
|
2
|
+
import type { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
3
|
+
import { createTransferCommand } from "./transferCommand.ts";
|
|
13
4
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
positionals: [
|
|
18
|
-
{
|
|
19
|
-
name: "source",
|
|
20
|
-
description: "Source path",
|
|
21
|
-
type: ValueTypeName.STRING,
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
name: "destination",
|
|
25
|
-
description: "Destination path",
|
|
26
|
-
type: ValueTypeName.STRING,
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
options: [],
|
|
30
|
-
async execute(context: Context, argumentValues: Values): Promise<void> {
|
|
31
|
-
const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
|
|
32
|
-
const source = argumentValues.source as string;
|
|
33
|
-
const destination = argumentValues.destination as string;
|
|
34
|
-
|
|
35
|
-
const provider = await getFilesystemProvider("");
|
|
36
|
-
try {
|
|
37
|
-
const { size } = await provider.getProperties(source);
|
|
38
|
-
const byteScale = createByteScale(size ?? 100);
|
|
39
|
-
const handle = await printerService.showProgressBar({
|
|
40
|
-
message: `Moving ${source}`,
|
|
41
|
-
total: byteScale.total,
|
|
42
|
-
format: byteScale.format,
|
|
43
|
-
formatRate: byteScale.formatRate,
|
|
44
|
-
});
|
|
45
|
-
try {
|
|
46
|
-
await moveProvider(provider, source, provider, destination, {
|
|
47
|
-
telemetry: {
|
|
48
|
-
onProgress: (event) => {
|
|
49
|
-
printerService.updateProgressBar(handle, byteScale.scale(event.bytesProcessed));
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
} finally {
|
|
54
|
-
await printerService.hideProgressBar(handle);
|
|
55
|
-
}
|
|
56
|
-
await printerService.print(`Moved ${source} to ${destination}\n`, Icon.SUCCESS);
|
|
57
|
-
} finally {
|
|
58
|
-
await provider[Symbol.asyncDispose]();
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export default move;
|
|
5
|
+
export function createMoveCommand(registry: ProviderRegistry): SubCommand {
|
|
6
|
+
return createTransferCommand(registry, "move");
|
|
7
|
+
}
|