@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
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { ComplexValueTypeName, ValueTypeName } from "@flowscripter/dynamic-cli-framework";
|
|
3
|
+
import type { ProviderRegistry } from "@flowscripter/pluggable-io-framework";
|
|
4
|
+
import type { IOProviderFactory } from "@flowscripter/pluggable-io-framework-api";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { createLocationOption } from "../../../src/util/location/createLocationOption.ts";
|
|
7
|
+
|
|
8
|
+
const factories: Record<string, Partial<IOProviderFactory>> = {
|
|
9
|
+
file: { locationSchema: z.object({ path: z.string().default("/") }) },
|
|
10
|
+
s3: {
|
|
11
|
+
locationSchema: z.object({
|
|
12
|
+
bucket: z.string(),
|
|
13
|
+
secretAccessKey: z.string().optional().meta({ secret: true }),
|
|
14
|
+
}),
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const registry = {
|
|
19
|
+
getProtocols: () => Object.keys(factories),
|
|
20
|
+
getFactory: (protocol: string) => factories[protocol],
|
|
21
|
+
} as unknown as ProviderRegistry;
|
|
22
|
+
|
|
23
|
+
describe("createLocationOption", () => {
|
|
24
|
+
test("has a protocol property and one optional group per protocol", () => {
|
|
25
|
+
const option = createLocationOption(registry, "source", "Source location");
|
|
26
|
+
|
|
27
|
+
expect(option.name).toBe("source");
|
|
28
|
+
expect(option.type).toBe(ComplexValueTypeName.COMPLEX);
|
|
29
|
+
expect(option.properties).toEqual([
|
|
30
|
+
{
|
|
31
|
+
name: "protocol",
|
|
32
|
+
description: "Protocol of the location",
|
|
33
|
+
type: ValueTypeName.STRING,
|
|
34
|
+
allowableValues: ["file", "s3"],
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "file",
|
|
38
|
+
description: "Location fields for the file protocol",
|
|
39
|
+
type: ComplexValueTypeName.COMPLEX,
|
|
40
|
+
isOptional: true,
|
|
41
|
+
properties: [
|
|
42
|
+
{ name: "path", description: "path", type: ValueTypeName.STRING, isOptional: true },
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: "s3",
|
|
47
|
+
description: "Location fields for the s3 protocol",
|
|
48
|
+
type: ComplexValueTypeName.COMPLEX,
|
|
49
|
+
isOptional: true,
|
|
50
|
+
properties: [
|
|
51
|
+
{ name: "bucket", description: "bucket", type: ValueTypeName.STRING },
|
|
52
|
+
{
|
|
53
|
+
name: "secretAccessKey",
|
|
54
|
+
description: "secretAccessKey",
|
|
55
|
+
type: ValueTypeName.SECRET,
|
|
56
|
+
isOptional: true,
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("validates the selected protocol's group against its location schema", () => {
|
|
64
|
+
const { validate } = createLocationOption(registry, "source", "Source location");
|
|
65
|
+
|
|
66
|
+
expect(validate?.({ protocol: "file" })).toBeUndefined();
|
|
67
|
+
expect(validate?.({ protocol: "s3", s3: { bucket: "b" } })).toBeUndefined();
|
|
68
|
+
expect(validate?.({ protocol: "s3", file: { path: "/" } })).toStartWith(
|
|
69
|
+
"Invalid s3 location: bucket:",
|
|
70
|
+
);
|
|
71
|
+
expect(validate?.({ protocol: "ftp" })).toBe('No provider installed for protocol "ftp"');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { describeTarget } from "../../../src/util/location/describeTarget.ts";
|
|
3
|
+
|
|
4
|
+
describe("describeTarget", () => {
|
|
5
|
+
test("describes entry, container and pattern targets", () => {
|
|
6
|
+
expect(describeTarget({ kind: "entry", key: "/a/b.txt" })).toBe("/a/b.txt");
|
|
7
|
+
expect(describeTarget({ kind: "container", key: "/a" })).toBe("/a");
|
|
8
|
+
expect(describeTarget({ kind: "pattern", containerKey: "/a", pattern: "*.txt" })).toBe(
|
|
9
|
+
"*.txt in /a",
|
|
10
|
+
);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { targetKey } from "../../../src/util/location/targetKey.ts";
|
|
3
|
+
|
|
4
|
+
describe("targetKey", () => {
|
|
5
|
+
test("returns the key of an entry or container target", () => {
|
|
6
|
+
expect(targetKey({ kind: "entry", key: "/a/b.txt" }, "delete")).toBe("/a/b.txt");
|
|
7
|
+
expect(targetKey({ kind: "container", key: "/a" }, "delete")).toBe("/a");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("rejects a pattern target, naming the command", () => {
|
|
11
|
+
expect(() =>
|
|
12
|
+
targetKey({ kind: "pattern", containerKey: "/a", pattern: "*" }, "delete"),
|
|
13
|
+
).toThrow("delete does not accept a pattern location");
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { toStructuredLocation } from "../../../src/util/location/toStructuredLocation.ts";
|
|
3
|
+
|
|
4
|
+
describe("toStructuredLocation", () => {
|
|
5
|
+
test("takes the selected protocol's group as the location", () => {
|
|
6
|
+
expect(
|
|
7
|
+
toStructuredLocation({ protocol: "file", file: { path: "/a" }, s3: { bucket: "b" } }),
|
|
8
|
+
).toEqual({ protocol: "file", location: { path: "/a" } });
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("uses an empty location when the protocol's group is absent", () => {
|
|
12
|
+
expect(toStructuredLocation({ protocol: "file" })).toEqual({ protocol: "file", location: {} });
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { PayloadKind } from "@flowscripter/pluggable-io-framework-api";
|
|
3
|
+
import { payloadKindOption, toPayloadKind } from "../../src/util/payloadKindOption.ts";
|
|
4
|
+
|
|
5
|
+
describe("payloadKindOption", () => {
|
|
6
|
+
test("defaults to auto and allows auto, js and native", () => {
|
|
7
|
+
expect(payloadKindOption.defaultValue).toBe("auto");
|
|
8
|
+
expect(payloadKindOption.allowableValues).toEqual(["auto", "js", "native"]);
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe("toPayloadKind", () => {
|
|
13
|
+
test("leaves auto to the registry and passes an explicit kind", () => {
|
|
14
|
+
expect(toPayloadKind("auto")).toBeUndefined();
|
|
15
|
+
expect(toPayloadKind(undefined)).toBeUndefined();
|
|
16
|
+
expect(toPayloadKind("js")).toBe(PayloadKind.Js);
|
|
17
|
+
expect(toPayloadKind("native")).toBe(PayloadKind.Native);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -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
|
-
}
|
package/tests/commands_test.ts
DELETED
|
@@ -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
|