@flowscripter/flowscripter-io-cli 2.0.11 → 3.0.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.
Files changed (49) hide show
  1. package/README.md +71 -34
  2. package/bun.lock +15 -14
  3. package/functional_tests/features/commands.feature +76 -7
  4. package/functional_tests/features/steps/executable.py +6 -0
  5. package/functional_tests/features/steps/io_commands.py +5 -0
  6. package/package.json +9 -8
  7. package/src/cli.ts +19 -9
  8. package/src/commands/copy.ts +6 -62
  9. package/src/commands/delete.ts +28 -29
  10. package/src/commands/get-properties.ts +30 -34
  11. package/src/commands/hash.ts +102 -68
  12. package/src/commands/list.ts +76 -57
  13. package/src/commands/move.ts +6 -62
  14. package/src/commands/set-properties.ts +92 -37
  15. package/src/commands/transferCommand.ts +121 -0
  16. package/src/util/interruptSignals.ts +70 -0
  17. package/src/util/location/createLocationOption.ts +59 -0
  18. package/src/util/location/describeTarget.ts +6 -0
  19. package/src/util/location/targetKey.ts +9 -0
  20. package/src/util/location/toStructuredLocation.ts +8 -0
  21. package/src/util/payloadKindOption.ts +20 -0
  22. package/src/{pluginsDir.ts → util/pluginsDir.ts} +1 -1
  23. package/src/util/providerRegistry.ts +23 -0
  24. package/src/util/requireOperation.ts +19 -0
  25. package/src/util/zodToComplexOption.ts +93 -0
  26. package/tests/commands/copy.test.ts +80 -0
  27. package/tests/commands/delete.test.ts +44 -0
  28. package/tests/commands/get-properties.test.ts +52 -0
  29. package/tests/commands/hash.test.ts +76 -0
  30. package/tests/commands/list.test.ts +78 -0
  31. package/tests/commands/move.test.ts +45 -0
  32. package/tests/commands/set-properties.test.ts +68 -0
  33. package/tests/commands/transferCommand.test.ts +63 -0
  34. package/tests/fixtures/pluginStore.ts +39 -0
  35. package/tests/{testContext.ts → fixtures/testContext.ts} +17 -1
  36. package/tests/{byteScale_test.ts → util/byteScale.test.ts} +1 -1
  37. package/tests/util/interruptSignals.test.ts +80 -0
  38. package/tests/util/location/createLocationOption.test.ts +73 -0
  39. package/tests/util/location/describeTarget.test.ts +12 -0
  40. package/tests/util/location/targetKey.test.ts +15 -0
  41. package/tests/util/location/toStructuredLocation.test.ts +14 -0
  42. package/tests/util/payloadKindOption.test.ts +19 -0
  43. package/tests/util/providerRegistry.test.ts +39 -0
  44. package/tests/util/requireOperation.test.ts +25 -0
  45. package/tests/util/zodToComplexOption.test.ts +116 -0
  46. package/src/filesystemProvider.ts +0 -40
  47. package/tests/commands_test.ts +0 -186
  48. /package/src/{byteScale.ts → util/byteScale.ts} +0 -0
  49. /package/tests/{cli_test.ts → cli.test.ts} +0 -0
@@ -0,0 +1,39 @@
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { mkdtemp, rm } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { PayloadKind } from "@flowscripter/pluggable-io-framework-api";
6
+ import { createProviderRegistry } from "../../src/util/providerRegistry.ts";
7
+ import { installFilesystemPlugin } from "../fixtures/pluginStore.ts";
8
+
9
+ describe("createProviderRegistry", () => {
10
+ let cleanup: () => Promise<void>;
11
+
12
+ beforeAll(async () => {
13
+ ({ cleanup } = await installFilesystemPlugin());
14
+ });
15
+
16
+ afterAll(async () => {
17
+ await cleanup();
18
+ });
19
+
20
+ test("discovers the provider plugins in the local plugin store", async () => {
21
+ const registry = await createProviderRegistry();
22
+ expect(registry.getProtocols()).toEqual(["file"]);
23
+ expect(registry.getKinds("file")).toEqual([PayloadKind.Js]);
24
+ });
25
+ });
26
+
27
+ describe("createProviderRegistry with an empty plugin store", () => {
28
+ test("discovers no protocols", async () => {
29
+ const pluginsPath = await mkdtemp(join(tmpdir(), "flowscripter-io-cli-empty-plugins-"));
30
+ process.env.FLOWSCRIPTER_IO_CLI_PLUGINS_PATH = pluginsPath;
31
+ try {
32
+ const registry = await createProviderRegistry();
33
+ expect(registry.getProtocols()).toEqual([]);
34
+ } finally {
35
+ delete process.env.FLOWSCRIPTER_IO_CLI_PLUGINS_PATH;
36
+ await rm(pluginsPath, { recursive: true, force: true });
37
+ }
38
+ });
39
+ });
@@ -0,0 +1,25 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { IOProvider } from "@flowscripter/pluggable-io-framework-api";
3
+ import { requireOperation } from "../../src/util/requireOperation.ts";
4
+
5
+ describe("requireOperation", () => {
6
+ test("returns the operation bound to the provider", async () => {
7
+ const deleted: string[] = [];
8
+ const provider = {
9
+ prefix: "x:",
10
+ async delete(this: { prefix: string }, path: string) {
11
+ deleted.push(this.prefix + path);
12
+ },
13
+ } as unknown as IOProvider;
14
+
15
+ await requireOperation(provider, "delete", "file")("a");
16
+
17
+ expect(deleted).toEqual(["x:a"]);
18
+ });
19
+
20
+ test("names the protocol when the operation is not implemented", () => {
21
+ expect(() => requireOperation({} as IOProvider, "list", "https")).toThrow(
22
+ 'list is not supported by protocol "https"',
23
+ );
24
+ });
25
+ });
@@ -0,0 +1,116 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { ComplexValueTypeName, ValueTypeName } from "@flowscripter/dynamic-cli-framework";
3
+ import { z } from "zod";
4
+ import { zodToComplexOption } from "../../src/util/zodToComplexOption.ts";
5
+
6
+ describe("zodToComplexOption", () => {
7
+ test("converts scalar fields to typed options", () => {
8
+ const properties = zodToComplexOption(
9
+ z.object({ path: z.string(), count: z.number(), force: z.boolean() }),
10
+ );
11
+
12
+ expect(properties).toEqual([
13
+ { name: "path", description: "path", type: ValueTypeName.STRING },
14
+ { name: "count", description: "count", type: ValueTypeName.NUMBER },
15
+ { name: "force", description: "force", type: ValueTypeName.BOOLEAN },
16
+ ]);
17
+ });
18
+
19
+ test("marks optional and defaulted fields as optional", () => {
20
+ const properties = zodToComplexOption(
21
+ z.object({ path: z.string().default("/"), filename: z.string().optional() }),
22
+ );
23
+
24
+ expect(properties).toEqual([
25
+ { name: "path", description: "path", type: ValueTypeName.STRING, isOptional: true },
26
+ { name: "filename", description: "filename", type: ValueTypeName.STRING, isOptional: true },
27
+ ]);
28
+ });
29
+
30
+ test("uses a field's description when it has one", () => {
31
+ const properties = zodToComplexOption(
32
+ z.object({ path: z.string().describe("Folder path").optional() }),
33
+ );
34
+
35
+ expect(properties).toEqual([
36
+ { name: "path", description: "Folder path", type: ValueTypeName.STRING, isOptional: true },
37
+ ]);
38
+ });
39
+
40
+ test("converts fields marked secret to secret options", () => {
41
+ const properties = zodToComplexOption(
42
+ z.object({
43
+ password: z.string().optional().meta({ secret: true }),
44
+ token: z.string().meta({ secret: true }).optional(),
45
+ }),
46
+ );
47
+
48
+ expect(properties).toEqual([
49
+ { name: "password", description: "password", type: ValueTypeName.SECRET, isOptional: true },
50
+ { name: "token", description: "token", type: ValueTypeName.SECRET, isOptional: true },
51
+ ]);
52
+ });
53
+
54
+ test("converts enum fields to strings with allowable values", () => {
55
+ const properties = zodToComplexOption(z.object({ method: z.enum(["PUT", "POST"]) }));
56
+
57
+ expect(properties).toEqual([
58
+ {
59
+ name: "method",
60
+ description: "method",
61
+ type: ValueTypeName.STRING,
62
+ allowableValues: ["PUT", "POST"],
63
+ },
64
+ ]);
65
+ });
66
+
67
+ test("converts nested objects and arrays of objects to complex options", () => {
68
+ const properties = zodToComplexOption(
69
+ z.object({
70
+ auth: z.object({ username: z.string() }),
71
+ headers: z.array(z.object({ name: z.string(), value: z.string() })).optional(),
72
+ }),
73
+ );
74
+
75
+ expect(properties).toEqual([
76
+ {
77
+ name: "auth",
78
+ description: "auth",
79
+ type: ComplexValueTypeName.COMPLEX,
80
+ properties: [{ name: "username", description: "username", type: ValueTypeName.STRING }],
81
+ },
82
+ {
83
+ name: "headers",
84
+ description: "headers",
85
+ type: ComplexValueTypeName.COMPLEX,
86
+ isOptional: true,
87
+ isArray: true,
88
+ properties: [
89
+ { name: "name", description: "name", type: ValueTypeName.STRING },
90
+ { name: "value", description: "value", type: ValueTypeName.STRING },
91
+ ],
92
+ },
93
+ ]);
94
+ });
95
+
96
+ test("accepts a refined object schema", () => {
97
+ const properties = zodToComplexOption(
98
+ z.object({ path: z.string() }).refine((value) => value.path !== ""),
99
+ );
100
+
101
+ expect(properties).toEqual([{ name: "path", description: "path", type: ValueTypeName.STRING }]);
102
+ });
103
+
104
+ test("rejects unsupported field types", () => {
105
+ expect(() => zodToComplexOption(z.object({ when: z.date() }))).toThrow(
106
+ 'Unsupported schema type "date" for "when"',
107
+ );
108
+ expect(() => zodToComplexOption(z.object({ tags: z.array(z.string()) }))).toThrow(
109
+ 'Unsupported array element type "string" for "tags"',
110
+ );
111
+ });
112
+
113
+ test("rejects a non-object schema", () => {
114
+ expect(() => zodToComplexOption(z.string())).toThrow('Expected an object schema, got "string"');
115
+ });
116
+ });
@@ -1,40 +0,0 @@
1
- import { DefaultPluginManager, NpmPluginRepository } from "@flowscripter/dynamic-plugin-framework";
2
- import {
3
- PLUGGABLE_IO_FRAMEWORK_PROVIDER_FACTORY_EXTENSION_POINT,
4
- type IOProvider,
5
- type IOProviderFactory,
6
- } from "@flowscripter/pluggable-io-framework-api";
7
- import {
8
- PLUGGABLE_IO_FRAMEWORK_PACKAGE_JSON_NAMESPACE,
9
- getPluginsNodeModulesPath,
10
- } from "./pluginsDir.ts";
11
-
12
- /**
13
- * Discovers and instantiates the filesystem provider (`io-plugin-filesystem`
14
- * or any other installed provider-factory plugin) purely via
15
- * `dynamic-plugin-framework`'s `NpmPluginRepository`, scanning the same
16
- * local plugin store the CLI's `plugin:add` command installs into (see
17
- * cli.ts) - this package has no direct npm dependency on any provider
18
- * plugin; it must be installed by the user first, e.g.
19
- * `flowscripter-io-cli plugin:add @flowscripter/io-plugin-filesystem`.
20
- */
21
- export async function getFilesystemProvider(rootPath: string): Promise<IOProvider> {
22
- const repository = new NpmPluginRepository({
23
- nodeModulesPath: getPluginsNodeModulesPath(),
24
- packageJsonNamespace: PLUGGABLE_IO_FRAMEWORK_PACKAGE_JSON_NAMESPACE,
25
- });
26
-
27
- const pluginManager = new DefaultPluginManager([repository]);
28
- await pluginManager.registerExtensions(PLUGGABLE_IO_FRAMEWORK_PROVIDER_FACTORY_EXTENSION_POINT);
29
- const [extension] = await pluginManager.getRegisteredExtensions(
30
- PLUGGABLE_IO_FRAMEWORK_PROVIDER_FACTORY_EXTENSION_POINT,
31
- );
32
- if (!extension) {
33
- throw new Error(
34
- "No pluggable-io-framework provider plugin installed - run: " +
35
- "flowscripter-io-cli plugin:add @flowscripter/io-plugin-filesystem",
36
- );
37
- }
38
- const factory = (await pluginManager.instantiate(extension.extensionHandle)) as IOProviderFactory;
39
- return factory.createProvider(factory.configSchema.parse({ rootPath }));
40
- }
@@ -1,186 +0,0 @@
1
- import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test";
2
- import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { join, resolve } from "node:path";
5
- import { Icon } from "@flowscripter/dynamic-cli-framework";
6
- import copy from "../src/commands/copy.ts";
7
- import deleteCommand from "../src/commands/delete.ts";
8
- import getProperties from "../src/commands/get-properties.ts";
9
- import hash from "../src/commands/hash.ts";
10
- import list from "../src/commands/list.ts";
11
- import move from "../src/commands/move.ts";
12
- import setProperties from "../src/commands/set-properties.ts";
13
- import { createStubContext } from "./testContext.ts";
14
-
15
- let root: string;
16
- let pluginsPath: string;
17
-
18
- beforeAll(async () => {
19
- // Simulate `flowscripter-io-cli plugin:add @flowscripter/io-plugin-filesystem`
20
- // by symlinking the real published package (a devDependency here purely
21
- // for test setup - never a runtime dependency of this CLI) into a temp
22
- // plugin store, discovered purely via NpmPluginRepository.
23
- pluginsPath = await mkdtemp(join(tmpdir(), "flowscripter-io-cli-plugins-test-"));
24
- await mkdir(join(pluginsPath, "@flowscripter"), { recursive: true });
25
- await symlink(
26
- resolve(import.meta.dir, "..", "node_modules", "@flowscripter", "io-plugin-filesystem"),
27
- join(pluginsPath, "@flowscripter", "io-plugin-filesystem"),
28
- "junction",
29
- );
30
- process.env.FLOWSCRIPTER_IO_CLI_PLUGINS_PATH = pluginsPath;
31
- });
32
-
33
- afterAll(async () => {
34
- delete process.env.FLOWSCRIPTER_IO_CLI_PLUGINS_PATH;
35
- await rm(pluginsPath, { recursive: true, force: true });
36
- });
37
-
38
- beforeEach(async () => {
39
- root = await mkdtemp(join(tmpdir(), "flowscripter-io-cli-test-"));
40
- });
41
-
42
- afterEach(async () => {
43
- await rm(root, { recursive: true, force: true });
44
- });
45
-
46
- describe("list", () => {
47
- test("prints one JSON line per item", async () => {
48
- await writeFile(join(root, "a.txt"), "hello");
49
- const { lines, context } = createStubContext();
50
-
51
- await list.execute(context, { path: root });
52
-
53
- expect(lines.length).toBe(1);
54
- expect(JSON.parse(lines[0]!).path).toBe("a.txt");
55
- });
56
- });
57
-
58
- describe("get-properties", () => {
59
- test("prints properties as pretty-printed JSON", async () => {
60
- await writeFile(join(root, "a.txt"), "hello");
61
- const { lines, context } = createStubContext();
62
-
63
- await getProperties.execute(context, { path: join(root, "a.txt") });
64
-
65
- expect(JSON.parse(lines[0]!).size).toBe(5);
66
- expect(lines[0]).toContain("\n");
67
- });
68
- });
69
-
70
- describe("set-properties", () => {
71
- test("applies mode", async () => {
72
- await writeFile(join(root, "a.txt"), "hello");
73
- const { context, icons } = createStubContext();
74
-
75
- await setProperties.execute(context, { path: join(root, "a.txt"), mode: 0o600 });
76
-
77
- const { lines, context: getPropertiesContext } = createStubContext();
78
- await getProperties.execute(getPropertiesContext, { path: join(root, "a.txt") });
79
- const properties = JSON.parse(lines[0]!);
80
- if (process.platform !== "win32") {
81
- expect(properties.properties.mode & 0o777).toBe(0o600);
82
- }
83
- expect(icons).toEqual([Icon.SUCCESS]);
84
- });
85
-
86
- test("shows a spinner while applying properties", async () => {
87
- await writeFile(join(root, "a.txt"), "hello");
88
- const { context, spinnerMessages } = createStubContext();
89
-
90
- await setProperties.execute(context, { path: join(root, "a.txt"), mode: 0o600 });
91
-
92
- expect(spinnerMessages).toEqual([`Setting properties for ${join(root, "a.txt")}...`]);
93
- });
94
- });
95
-
96
- describe("delete", () => {
97
- test("removes a file", async () => {
98
- await writeFile(join(root, "a.txt"), "hello");
99
- const { context, icons } = createStubContext();
100
-
101
- await deleteCommand.execute(context, { path: join(root, "a.txt") });
102
-
103
- const { lines, context: listContext } = createStubContext();
104
- await list.execute(listContext, { path: root });
105
- expect(lines.length).toBe(0);
106
- expect(icons).toEqual([Icon.SUCCESS]);
107
- });
108
-
109
- test("shows a spinner while deleting", async () => {
110
- await writeFile(join(root, "a.txt"), "hello");
111
- const { context, spinnerMessages } = createStubContext();
112
-
113
- await deleteCommand.execute(context, { path: join(root, "a.txt") });
114
-
115
- expect(spinnerMessages).toEqual([`Deleting ${join(root, "a.txt")}...`]);
116
- });
117
- });
118
-
119
- describe("copy", () => {
120
- test("copies a file", async () => {
121
- await writeFile(join(root, "a.txt"), "hello");
122
- const { context, icons } = createStubContext();
123
-
124
- await copy.execute(context, { source: join(root, "a.txt"), destination: join(root, "b.txt") });
125
-
126
- const { lines } = createStubContext();
127
- const { context: getPropsContext, lines: getPropsLines } = createStubContext();
128
- await getProperties.execute(getPropsContext, { path: join(root, "b.txt") });
129
- expect(JSON.parse(getPropsLines[0]!).size).toBe(5);
130
- expect(lines).toEqual([]);
131
- expect(icons).toEqual([Icon.SUCCESS]);
132
- });
133
- });
134
-
135
- describe("move", () => {
136
- test("moves a file", async () => {
137
- await writeFile(join(root, "a.txt"), "hello");
138
- const { context, icons } = createStubContext();
139
-
140
- await move.execute(context, { source: join(root, "a.txt"), destination: join(root, "b.txt") });
141
-
142
- const { lines, context: listContext } = createStubContext();
143
- await list.execute(listContext, { path: root });
144
- expect(lines.map((line) => JSON.parse(line).path)).toEqual(["b.txt"]);
145
- expect(icons).toEqual([Icon.SUCCESS]);
146
- });
147
- });
148
-
149
- describe("hash", () => {
150
- test("algorithm option has a curated set of allowable values", () => {
151
- const algorithmOption = hash.options.find((option) => option.name === "algorithm") as
152
- | { allowableValues?: readonly unknown[] }
153
- | undefined;
154
- expect(algorithmOption?.allowableValues).toEqual(["sha1", "sha256", "sha384", "sha512", "md5"]);
155
- });
156
-
157
- test("hashes a file with the default algorithm using the native hasher", async () => {
158
- await writeFile(join(root, "a.txt"), "hello");
159
- const { lines, context } = createStubContext();
160
-
161
- await hash.execute(context, { path: join(root, "a.txt"), algorithm: "sha256" });
162
-
163
- const expected = new Bun.CryptoHasher("sha256").update("hello").digest("hex");
164
- expect(lines[0]).toBe(`${expected} ${join(root, "a.txt")}\n`);
165
- });
166
-
167
- test("hashes a file with a non-sha256 algorithm via the Bun.CryptoHasher fallback", async () => {
168
- await writeFile(join(root, "a.txt"), "hello");
169
- const { lines, context } = createStubContext();
170
-
171
- await hash.execute(context, { path: join(root, "a.txt"), algorithm: "sha1" });
172
-
173
- const expected = new Bun.CryptoHasher("sha1").update("hello").digest("hex");
174
- expect(lines[0]).toBe(`${expected} ${join(root, "a.txt")}\n`);
175
- });
176
-
177
- test("reports progress as the file is read", async () => {
178
- await writeFile(join(root, "a.txt"), "hello");
179
- const { context, progressUpdates } = createStubContext();
180
-
181
- await hash.execute(context, { path: join(root, "a.txt"), algorithm: "sha256" });
182
-
183
- expect(progressUpdates.length).toBeGreaterThan(0);
184
- expect(progressUpdates.at(-1)).toBe(5);
185
- });
186
- });
File without changes
File without changes