@decohere/core 0.0.1-dev.126 → 0.0.1-dev.5
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/package.json +40 -15
- package/src/caching/cache-manager.ts +102 -0
- package/src/caching/cache.decorator.ts +56 -0
- package/src/caching/cache.ts +11 -0
- package/{es6/src/caching/index.js → src/caching/index.ts} +1 -1
- package/src/caching/invalidate-cache.decorator.ts +81 -0
- package/src/caching/memoize.decorator.ts +34 -0
- package/src/caching/node-cache-memoize.adapter.ts +47 -0
- package/src/cli/action.ts +77 -0
- package/src/cli/command.ts +173 -0
- package/src/cli/index.ts +30 -0
- package/src/cli/module.type.ts +48 -0
- package/src/cli/options.ts +81 -0
- package/src/collections/find-duplicates.ts +16 -0
- package/src/collections/merge-arrays.ts +9 -0
- package/src/collections/sum.ts +71 -0
- package/src/collections/unique-objects.ts +23 -0
- package/src/composition/index.ts +48 -0
- package/src/configuration/configuration.types.ts +13 -0
- package/src/diagnostics/index.ts +1 -0
- package/src/diagnostics/trace.ts +97 -0
- package/{es6/src/extensibility/decorator-factory-base.js → src/extensibility/decorator-factory-base.ts} +17 -9
- package/src/extensibility/decorator-factory.ts +44 -0
- package/src/extensibility/extension.ts +34 -0
- package/src/extensibility/mixwith.ts +42 -0
- package/src/extensibility/pipeline.ts +60 -0
- package/{types/src/extensibility/step.d.ts → src/extensibility/step.ts} +6 -4
- package/src/interop/docker/docker.ts +57 -0
- package/src/interop/errors/interop.error.ts +1 -0
- package/src/interop/git/git.ts +98 -0
- package/src/interop/git/git.types.ts +17 -0
- package/src/interop/index.ts +22 -0
- package/src/interop/node/index.ts +1 -0
- package/src/interop/node/node.ts +38 -0
- package/src/interop/npm/npm.ts +168 -0
- package/src/interop/npm/npm.types.ts +28 -0
- package/src/interop/pnpm/pnpm.ts +22 -0
- package/src/interop/pnpm/pnpm.types.ts +3 -0
- package/src/interop/shell/shell.argv.ts +31 -0
- package/src/interop/shell/shell.ts +100 -0
- package/src/interop/shell/shell.types.ts +9 -0
- package/src/interop/types/detect-package-manager.ts +45 -0
- package/src/interop/types/package-manager.ts +7 -0
- package/src/interop/yarn/yarn.ts +28 -0
- package/src/interop/yarn/yarn.types.ts +3 -0
- package/src/io/copy-file.ts +13 -0
- package/src/io/delete-file.ts +4 -0
- package/src/io/directory-exists.ts +9 -0
- package/src/io/file-exists.ts +10 -0
- package/src/io/find-directories.ts +13 -0
- package/src/io/find-file.ts +37 -0
- package/{es6/src/io/io.js → src/io/io.ts} +3 -0
- package/src/io/read-file.ts +25 -0
- package/src/io/remove-dir.ts +11 -0
- package/src/io/types/directory.type.ts +4 -0
- package/src/io/types/file.ts +34 -0
- package/src/io/url-to-path.ts +17 -0
- package/src/io/watch.ts +290 -0
- package/src/io/write-file.ts +21 -0
- package/src/objects/deep-merge.ts +131 -0
- package/{es6/src/reflection/index.js → src/reflection/index.ts} +1 -0
- package/src/reflection/types/reflection.types.ts +28 -0
- package/src/reflection/utilities.ts +92 -0
- package/src/semver/index.ts +1 -0
- package/src/semver/semver.ts +96 -0
- package/src/system/camel-case.ts +3 -0
- package/src/system/debounce.ts +13 -0
- package/src/system/decorator.ts +41 -0
- package/src/system/environment.ts +6 -0
- package/{types/src/system/function.types.d.ts → src/system/function.types.ts} +10 -2
- package/src/system/retry.decorator.ts +43 -0
- package/src/system/sleep.ts +6 -0
- package/src/system/type.utils.ts +5 -0
- package/src/testing/flush-promises.ts +7 -0
- package/src/testing/glob-with-memfs.ts +25 -0
- package/src/testing/index.ts +5 -0
- package/src/testing/memfs.ts +28 -0
- package/src/testing/mock-logger.ts +42 -0
- package/src/testing/spy-on-constructor.ts +33 -0
- package/tests/caching/cache-manager.spec.ts +147 -0
- package/tests/caching/cache-test-class.ts +91 -0
- package/tests/caching/cache.decorator.spec.ts +259 -0
- package/tests/caching/invalidate-cache-test-class.ts +52 -0
- package/tests/caching/invalidate-cache.decorator.spec.ts +120 -0
- package/tests/caching/memoize-test-class.ts +99 -0
- package/tests/caching/memoize.decorator.spec.ts +203 -0
- package/tests/cli/command.model.spec.ts +91 -0
- package/tests/collections/merge-arrays.spec.ts +50 -0
- package/tests/collections/sum.spec.ts +86 -0
- package/tests/collections/unique-objects.spec.ts +233 -0
- package/tests/extensibility/extension.spec.ts +29 -0
- package/tests/extensibility/pipeline.spec.ts +379 -0
- package/tests/interop/detect-package-manager.spec.ts +70 -0
- package/tests/interop/npm.spec.ts +129 -0
- package/tests/interop/pnpm.spec.ts +30 -0
- package/tests/interop/shell.spec.ts +47 -0
- package/tests/interop/yarn.spec.ts +18 -0
- package/tests/io/find-file.spec.ts +188 -0
- package/tests/io/watch.real-fs.spec.ts +76 -0
- package/tests/io/watch.spec.ts +291 -0
- package/tests/mock-prototype.ts +12 -0
- package/tests/semver/semver.spec.ts +51 -0
- package/tests/system/decorator.spec.ts +250 -0
- package/tests/system/retry.decorator.spec.ts +57 -0
- package/LICENSE +0 -15
- package/commonjs/src/caching/cache-manager.js +0 -98
- package/commonjs/src/caching/cache.decorator.js +0 -69
- package/commonjs/src/caching/cache.js +0 -6
- package/commonjs/src/caching/index.js +0 -23
- package/commonjs/src/caching/invalidate-cache.decorator.js +0 -92
- package/commonjs/src/caching/map-cache.js +0 -39
- package/commonjs/src/caching/memoize.decorator.js +0 -36
- package/commonjs/src/collections/collections.js +0 -21
- package/commonjs/src/collections/find-duplicates.js +0 -35
- package/commonjs/src/collections/index.js +0 -52
- package/commonjs/src/collections/merge-arrays.js +0 -34
- package/commonjs/src/collections/sum.js +0 -63
- package/commonjs/src/collections/unique-objects.js +0 -39
- package/commonjs/src/composition/index.js +0 -4
- package/commonjs/src/configuration/configuration.types.js +0 -11
- package/commonjs/src/configuration/index.js +0 -18
- package/commonjs/src/extensibility/decorator-factory-base.js +0 -22
- package/commonjs/src/extensibility/decorator-factory.js +0 -31
- package/commonjs/src/extensibility/extension.js +0 -37
- package/commonjs/src/extensibility/index.js +0 -23
- package/commonjs/src/extensibility/mixwith.js +0 -36
- package/commonjs/src/extensibility/pipeline.js +0 -56
- package/commonjs/src/extensibility/step.js +0 -18
- package/commonjs/src/io/copy-file.js +0 -25
- package/commonjs/src/io/delete-file.js +0 -21
- package/commonjs/src/io/directory-exists.js +0 -24
- package/commonjs/src/io/file-exists.js +0 -19
- package/commonjs/src/io/index.js +0 -66
- package/commonjs/src/io/io.js +0 -25
- package/commonjs/src/io/read-file.js +0 -39
- package/commonjs/src/io/remove-dir.js +0 -22
- package/commonjs/src/io/types/directory.type.js +0 -4
- package/commonjs/src/io/types/file.js +0 -28
- package/commonjs/src/io/types/index.js +0 -19
- package/commonjs/src/io/url-to-path.js +0 -33
- package/commonjs/src/io/write-file.js +0 -30
- package/commonjs/src/objects/deep-merge.js +0 -67
- package/commonjs/src/objects/index.js +0 -11
- package/commonjs/src/reflection/index.js +0 -20
- package/commonjs/src/reflection/types/index.js +0 -18
- package/commonjs/src/reflection/types/reflection.types.js +0 -26
- package/commonjs/src/reflection/utilities.js +0 -118
- package/commonjs/src/system/camel-case.js +0 -13
- package/commonjs/src/system/debounce.js +0 -19
- package/commonjs/src/system/decorator.js +0 -46
- package/commonjs/src/system/environment.js +0 -23
- package/commonjs/src/system/function.types.js +0 -18
- package/commonjs/src/system/index.js +0 -24
- package/commonjs/src/system/retry.decorator.js +0 -40
- package/commonjs/src/system/sleep.js +0 -13
- package/commonjs/src/system/type.utils.js +0 -4
- package/es6/src/caching/cache-manager.js +0 -82
- package/es6/src/caching/cache.decorator.js +0 -51
- package/es6/src/caching/cache.js +0 -3
- package/es6/src/caching/invalidate-cache.decorator.js +0 -71
- package/es6/src/caching/map-cache.js +0 -35
- package/es6/src/caching/memoize.decorator.js +0 -27
- package/es6/src/collections/find-duplicates.js +0 -17
- package/es6/src/collections/merge-arrays.js +0 -16
- package/es6/src/collections/sum.js +0 -45
- package/es6/src/collections/unique-objects.js +0 -21
- package/es6/src/composition/index.js +0 -1
- package/es6/src/configuration/configuration.types.js +0 -1
- package/es6/src/extensibility/decorator-factory.js +0 -21
- package/es6/src/extensibility/extension.js +0 -27
- package/es6/src/extensibility/mixwith.js +0 -26
- package/es6/src/extensibility/pipeline.js +0 -46
- package/es6/src/extensibility/step.js +0 -16
- package/es6/src/io/copy-file.js +0 -10
- package/es6/src/io/delete-file.js +0 -6
- package/es6/src/io/directory-exists.js +0 -9
- package/es6/src/io/file-exists.js +0 -9
- package/es6/src/io/read-file.js +0 -13
- package/es6/src/io/remove-dir.js +0 -7
- package/es6/src/io/types/directory.type.js +0 -1
- package/es6/src/io/types/file.js +0 -18
- package/es6/src/io/url-to-path.js +0 -15
- package/es6/src/io/write-file.js +0 -15
- package/es6/src/objects/deep-merge.js +0 -57
- package/es6/src/reflection/types/reflection.types.js +0 -8
- package/es6/src/reflection/utilities.js +0 -74
- package/es6/src/system/camel-case.js +0 -3
- package/es6/src/system/debounce.js +0 -9
- package/es6/src/system/decorator.js +0 -30
- package/es6/src/system/environment.js +0 -5
- package/es6/src/system/function.types.js +0 -26
- package/es6/src/system/retry.decorator.js +0 -30
- package/es6/src/system/sleep.js +0 -3
- package/es6/src/system/type.utils.js +0 -1
- package/types/src/caching/cache-manager.d.ts +0 -59
- package/types/src/caching/cache.d.ts +0 -11
- package/types/src/caching/cache.decorator.d.ts +0 -18
- package/types/src/caching/index.d.ts +0 -6
- package/types/src/caching/invalidate-cache.decorator.d.ts +0 -17
- package/types/src/caching/map-cache.d.ts +0 -23
- package/types/src/caching/memoize.decorator.d.ts +0 -16
- package/types/src/collections/collections.d.ts +0 -4
- package/types/src/collections/find-duplicates.d.ts +0 -2
- package/types/src/collections/index.d.ts +0 -1
- package/types/src/collections/merge-arrays.d.ts +0 -2
- package/types/src/collections/sum.d.ts +0 -4
- package/types/src/collections/unique-objects.d.ts +0 -2
- package/types/src/composition/index.d.ts +0 -37
- package/types/src/configuration/configuration.types.d.ts +0 -10
- package/types/src/configuration/index.d.ts +0 -1
- package/types/src/extensibility/decorator-factory-base.d.ts +0 -39
- package/types/src/extensibility/decorator-factory.d.ts +0 -19
- package/types/src/extensibility/extension.d.ts +0 -9
- package/types/src/extensibility/index.d.ts +0 -6
- package/types/src/extensibility/mixwith.d.ts +0 -6
- package/types/src/extensibility/pipeline.d.ts +0 -12
- package/types/src/io/copy-file.d.ts +0 -4
- package/types/src/io/delete-file.d.ts +0 -1
- package/types/src/io/directory-exists.d.ts +0 -1
- package/types/src/io/file-exists.d.ts +0 -1
- package/types/src/io/index.d.ts +0 -2
- package/types/src/io/io.d.ts +0 -8
- package/types/src/io/read-file.d.ts +0 -4
- package/types/src/io/remove-dir.d.ts +0 -5
- package/types/src/io/types/directory.type.d.ts +0 -4
- package/types/src/io/types/file.d.ts +0 -20
- package/types/src/io/types/index.d.ts +0 -2
- package/types/src/io/url-to-path.d.ts +0 -2
- package/types/src/io/write-file.d.ts +0 -5
- package/types/src/objects/deep-merge.d.ts +0 -41
- package/types/src/objects/index.d.ts +0 -1
- package/types/src/reflection/index.d.ts +0 -3
- package/types/src/reflection/types/index.d.ts +0 -1
- package/types/src/reflection/types/reflection.types.d.ts +0 -21
- package/types/src/reflection/utilities.d.ts +0 -27
- package/types/src/system/camel-case.d.ts +0 -1
- package/types/src/system/debounce.d.ts +0 -1
- package/types/src/system/decorator.d.ts +0 -15
- package/types/src/system/environment.d.ts +0 -6
- package/types/src/system/index.d.ts +0 -7
- package/types/src/system/retry.decorator.d.ts +0 -6
- package/types/src/system/sleep.d.ts +0 -1
- package/types/src/system/type.utils.d.ts +0 -1
- /package/{es6/src/collections/collections.js → src/collections/collections.ts} +0 -0
- /package/{es6/src/collections/index.js → src/collections/index.ts} +0 -0
- /package/{es6/src/configuration/index.js → src/configuration/index.ts} +0 -0
- /package/{es6/src/extensibility/index.js → src/extensibility/index.ts} +0 -0
- /package/{es6/src/io/index.js → src/io/index.ts} +0 -0
- /package/{es6/src/io/types/index.js → src/io/types/index.ts} +0 -0
- /package/{es6/src/objects/index.js → src/objects/index.ts} +0 -0
- /package/{es6/src/reflection/types/index.js → src/reflection/types/index.ts} +0 -0
- /package/{es6/src/system/index.js → src/system/index.ts} +0 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Constructable } from "../reflection/index.js";
|
|
2
2
|
import type { PipelineStep } from "./pipeline.js";
|
|
3
|
+
|
|
3
4
|
/**
|
|
4
5
|
* Wraps a pipeline step with an optional `when` predicate.
|
|
5
6
|
* When `when` is provided, the step only runs if `when(ctx)` returns `true`.
|
|
@@ -9,8 +10,9 @@ import type { PipelineStep } from "./pipeline.js";
|
|
|
9
10
|
* Predicates are declared in the command constructor via `addDisableStepOption`,
|
|
10
11
|
* keeping skip conditions co-located with the option that controls them.
|
|
11
12
|
*/
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
export class Step<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
14
|
+
public constructor(
|
|
15
|
+
public readonly type: Constructable<PipelineStep<T>>,
|
|
16
|
+
public readonly when?: (ctx: T) => boolean,
|
|
17
|
+
) {}
|
|
16
18
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { InteropError } from "../errors/interop.error.js";
|
|
2
|
+
import { Shell } from "../shell/shell.js";
|
|
3
|
+
|
|
4
|
+
export type DockerBuildOptions = {
|
|
5
|
+
cwd?: string;
|
|
6
|
+
dockerfile?: string;
|
|
7
|
+
tags?: string[];
|
|
8
|
+
buildArgs?: Record<string, string>;
|
|
9
|
+
secrets?: Record<string, string>;
|
|
10
|
+
noCache?: boolean;
|
|
11
|
+
pull?: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type DockerPushOptions = {
|
|
15
|
+
cwd?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export class Docker extends Shell {
|
|
19
|
+
public readonly name = "docker" as const;
|
|
20
|
+
protected program = "docker";
|
|
21
|
+
|
|
22
|
+
public async build(contextDir: string, options: DockerBuildOptions = {}) {
|
|
23
|
+
if (contextDir == null || contextDir.trim() === "") {
|
|
24
|
+
throw new InteropError("Docker build context is required");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const args = ["build"];
|
|
28
|
+
if (options.dockerfile) {
|
|
29
|
+
args.push("-f", options.dockerfile);
|
|
30
|
+
}
|
|
31
|
+
for (const tag of options.tags ?? []) {
|
|
32
|
+
args.push("-t", tag);
|
|
33
|
+
}
|
|
34
|
+
for (const [key, value] of Object.entries(options.buildArgs ?? {})) {
|
|
35
|
+
args.push("--build-arg", `${key}=${value}`);
|
|
36
|
+
}
|
|
37
|
+
for (const [id, env] of Object.entries(options.secrets ?? {})) {
|
|
38
|
+
args.push("--secret", `id=${id},env=${env}`);
|
|
39
|
+
}
|
|
40
|
+
if (options.noCache) {
|
|
41
|
+
args.push("--no-cache");
|
|
42
|
+
}
|
|
43
|
+
if (options.pull) {
|
|
44
|
+
args.push("--pull");
|
|
45
|
+
}
|
|
46
|
+
args.push(contextDir);
|
|
47
|
+
return await this.run({ cwd: options.cwd }, ...args);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async push(tag: string, options: DockerPushOptions = {}) {
|
|
51
|
+
if (tag == null || tag.trim() === "") {
|
|
52
|
+
throw new InteropError("Docker tag is required");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return await this.run({ cwd: options.cwd }, "push", tag);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class InteropError extends Error {}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { InteropError } from "../errors/interop.error.js";
|
|
2
|
+
import { Shell } from "../shell/shell.js";
|
|
3
|
+
import type { ShellResponse } from "../shell/shell.types.js";
|
|
4
|
+
import type { CommitOptions, GitOptions, PushOptions, StageOptions } from "./git.types.js";
|
|
5
|
+
|
|
6
|
+
const Commands = {
|
|
7
|
+
stage: "stage",
|
|
8
|
+
tag: "tag",
|
|
9
|
+
add: "add",
|
|
10
|
+
commit: "commit",
|
|
11
|
+
push: "push",
|
|
12
|
+
listFiles: "ls-files",
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const defaultOptions: GitOptions = { cwd: "./" };
|
|
16
|
+
|
|
17
|
+
export class Git extends Shell {
|
|
18
|
+
protected program: string = "git";
|
|
19
|
+
public async getModifiedFiles(options: GitOptions = defaultOptions) {
|
|
20
|
+
const args = [Commands.listFiles, "--modified"];
|
|
21
|
+
const { cwd } = options;
|
|
22
|
+
const result = await this.run({ cwd }, ...args);
|
|
23
|
+
return result.data;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public async getBranchName(options: GitOptions = defaultOptions) {
|
|
27
|
+
const { cwd } = options;
|
|
28
|
+
const args = ["rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD"];
|
|
29
|
+
const result = await this.run({ cwd }, ...args);
|
|
30
|
+
return result.data[0];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async tag(tag: string, options: GitOptions = defaultOptions) {
|
|
34
|
+
const args = [Commands.tag, tag];
|
|
35
|
+
const { cwd } = options;
|
|
36
|
+
const result = await this.run({ cwd }, ...args);
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public async pushTag(
|
|
41
|
+
tag: string,
|
|
42
|
+
options: GitOptions & { remote?: string } = defaultOptions,
|
|
43
|
+
) {
|
|
44
|
+
const { cwd, remote = "origin" } = options;
|
|
45
|
+
const result = await this.run({ cwd }, Commands.push, remote, tag);
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public async commit(message: string, options: CommitOptions): Promise<ShellResponse> {
|
|
50
|
+
const optionsAndDefaults = { ...defaultOptions, ...options };
|
|
51
|
+
const { cwd, files = [], all } = optionsAndDefaults;
|
|
52
|
+
const args = [Commands.commit];
|
|
53
|
+
|
|
54
|
+
if (files.length > 0 && all) {
|
|
55
|
+
throw new InteropError("Cannot commit all and files at the same time");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (files.length > 0) {
|
|
59
|
+
await this.stage({ files });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (options.all) {
|
|
63
|
+
args.push("-a");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
args.push("-m", message);
|
|
67
|
+
const result = await this.run({ cwd }, ...args);
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public async push(branch: string, options?: PushOptions) {
|
|
72
|
+
const optionsAndDefaults = { ...defaultOptions, ...options };
|
|
73
|
+
const { cwd, setUpstream } = optionsAndDefaults;
|
|
74
|
+
const args = [Commands.push];
|
|
75
|
+
args.push("--set-upstream", setUpstream ?? "origin");
|
|
76
|
+
args.push(branch);
|
|
77
|
+
const result = await this.run({ cwd }, ...args);
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public async pull(branch: string, options: GitOptions = defaultOptions) {
|
|
82
|
+
const { cwd } = options;
|
|
83
|
+
return this.run({ cwd }, "pull", "--rebase", "origin", branch);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public async stage(options: StageOptions) {
|
|
87
|
+
const optionsAndDefaults = { ...defaultOptions, ...options };
|
|
88
|
+
const { cwd, files = [], all } = optionsAndDefaults;
|
|
89
|
+
if (all) {
|
|
90
|
+
await this.run({ cwd }, Commands.stage, "-a");
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (const file of files) {
|
|
95
|
+
await this.run({ cwd }, Commands.stage, file);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type GitOptions = {
|
|
2
|
+
cwd?: string;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export type CommitOptions = {
|
|
6
|
+
files?: string[];
|
|
7
|
+
all?: boolean;
|
|
8
|
+
} & GitOptions;
|
|
9
|
+
|
|
10
|
+
export type PushOptions = {
|
|
11
|
+
setUpstream: string;
|
|
12
|
+
} & GitOptions;
|
|
13
|
+
|
|
14
|
+
export type StageOptions = {
|
|
15
|
+
files?: string[];
|
|
16
|
+
all?: string;
|
|
17
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// << Git >>
|
|
2
|
+
|
|
3
|
+
// << Docker >>
|
|
4
|
+
export * from "./docker/docker.js";
|
|
5
|
+
export * from "./git/git.js";
|
|
6
|
+
export * as git from "./git/git.types.js";
|
|
7
|
+
// << Node >>
|
|
8
|
+
export * from "./node/node.js";
|
|
9
|
+
// << Npm >>
|
|
10
|
+
export * from "./npm/npm.js";
|
|
11
|
+
export * as npm from "./npm/npm.types.js";
|
|
12
|
+
// << Pnpm >>
|
|
13
|
+
export * from "./pnpm/pnpm.js";
|
|
14
|
+
export * as pnpm from "./pnpm/pnpm.types.js";
|
|
15
|
+
// << Shell >>
|
|
16
|
+
export * from "./shell/shell.js";
|
|
17
|
+
export * as shell from "./shell/shell.types.js";
|
|
18
|
+
export * from "./types/detect-package-manager.js";
|
|
19
|
+
export * from "./types/package-manager.js";
|
|
20
|
+
// << Yarn >>
|
|
21
|
+
export * from "./yarn/yarn.js";
|
|
22
|
+
export * as yarn from "./yarn/yarn.types.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./node.js";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type SpawnOptions, spawn } from "node:child_process";
|
|
2
|
+
import { Shell } from "../shell/shell.js";
|
|
3
|
+
|
|
4
|
+
export class Node extends Shell {
|
|
5
|
+
protected program: string = "node";
|
|
6
|
+
|
|
7
|
+
public async runScript(script: string, ...args: string[]) {
|
|
8
|
+
const spawnOpt: SpawnOptions = {
|
|
9
|
+
cwd: ".",
|
|
10
|
+
stdio: "inherit",
|
|
11
|
+
env: {
|
|
12
|
+
...process.env,
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const proc = spawn("node", [script, ...args], spawnOpt);
|
|
18
|
+
|
|
19
|
+
proc.on("exit", (code) => {
|
|
20
|
+
if (code !== 0) {
|
|
21
|
+
reject();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
resolve(null);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
proc.on("close", (code) => {
|
|
28
|
+
resolve(null);
|
|
29
|
+
if (code === 0) {
|
|
30
|
+
resolve({
|
|
31
|
+
message: `${script} completed`,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
reject(`Running '${script}' failed with code ${code}`);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { InteropError } from "../errors/interop.error.js";
|
|
2
|
+
import { Shell } from "../shell/shell.js";
|
|
3
|
+
import type { PackageManager } from "../types/package-manager.js";
|
|
4
|
+
import type { NpmOptions, PublishOptions, VersionOptions, VersionType } from "./npm.types.js";
|
|
5
|
+
|
|
6
|
+
const Commands = {
|
|
7
|
+
publish: "publish",
|
|
8
|
+
version: "version",
|
|
9
|
+
link: "link",
|
|
10
|
+
install: "install",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class Npm extends Shell implements PackageManager {
|
|
14
|
+
public readonly name = "npm" as const;
|
|
15
|
+
protected program: string = "npm";
|
|
16
|
+
|
|
17
|
+
public async publish(directory: string, options: PublishOptions = { channel: "beta" }) {
|
|
18
|
+
const { cwd, dryRun, skipIfPublished, channel, authToken, otp, provenance } = options;
|
|
19
|
+
if (directory == null) {
|
|
20
|
+
throw new InteropError("Directory is required");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (skipIfPublished && !dryRun) {
|
|
24
|
+
const alreadyPublished = await this.isPublished(directory, { cwd });
|
|
25
|
+
if (alreadyPublished) {
|
|
26
|
+
return { message: `Skipping publish — version already exists on registry`, data: [] };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const args = [Commands.publish, directory];
|
|
31
|
+
let tmpUserConfigPath: string | undefined;
|
|
32
|
+
|
|
33
|
+
args.push("--tag", channel ?? "beta");
|
|
34
|
+
|
|
35
|
+
if (provenance) {
|
|
36
|
+
args.push("--provenance");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Explicit --token (local bootstrap). OIDC publish must not inherit cwd .npmrc / NODE_AUTH_TOKEN.
|
|
40
|
+
if (authToken?.trim()) {
|
|
41
|
+
tmpUserConfigPath = await this.createTempUserConfig(authToken.trim());
|
|
42
|
+
args.push("--userconfig", tmpUserConfigPath);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (dryRun) {
|
|
46
|
+
args.push("--dry-run");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const env = { ...process.env };
|
|
50
|
+
if (!authToken?.trim()) {
|
|
51
|
+
delete env.NODE_AUTH_TOKEN;
|
|
52
|
+
delete env.NPM_TOKEN;
|
|
53
|
+
env.NPM_CONFIG_USERCONFIG = "/dev/null";
|
|
54
|
+
}
|
|
55
|
+
if (otp) {
|
|
56
|
+
env.NPM_CONFIG_OTP = otp;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
return await this.run({ cwd, env }, ...args);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (skipIfPublished && !dryRun && isAlreadyPublishedError(error)) {
|
|
63
|
+
return { message: `Skipping publish — version already exists on registry`, data: [] };
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
} finally {
|
|
67
|
+
if (tmpUserConfigPath) {
|
|
68
|
+
await this.removeTempUserConfig(tmpUserConfigPath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @description Runs in the directory of the package
|
|
75
|
+
* @param options
|
|
76
|
+
* @returns
|
|
77
|
+
*/
|
|
78
|
+
public async version(versionType: VersionType, options: VersionOptions) {
|
|
79
|
+
const { cwd, noTag, preid } = options;
|
|
80
|
+
const args = [Commands.version, versionType];
|
|
81
|
+
if (noTag) {
|
|
82
|
+
args.push("--no-git-tag-version");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (preid) {
|
|
86
|
+
args.push("--preid", preid);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return this.run({ cwd }, ...args);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public async link(directory: string, options: NpmOptions = { cwd: "./" }) {
|
|
93
|
+
const { cwd } = options;
|
|
94
|
+
const args = [Commands.link, directory];
|
|
95
|
+
return this.run({ cwd }, ...args);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public async install(options: NpmOptions = { cwd: "./" }) {
|
|
99
|
+
const { cwd } = options;
|
|
100
|
+
return this.run({ cwd }, Commands.install);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Install only `dependencies` as real folders: `--omit=dev` skips devDependencies and
|
|
105
|
+
* `--no-bin-links` skips the `.bin` symlinks, so the result holds no symlinks at all.
|
|
106
|
+
*/
|
|
107
|
+
public async installProduction(options: NpmOptions = { cwd: "./" }) {
|
|
108
|
+
const { cwd } = options;
|
|
109
|
+
return this.run(
|
|
110
|
+
{ cwd },
|
|
111
|
+
Commands.install,
|
|
112
|
+
"--omit=dev",
|
|
113
|
+
"--no-bin-links",
|
|
114
|
+
"--no-audit",
|
|
115
|
+
"--no-fund",
|
|
116
|
+
"--loglevel=error",
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public async syncLockFile(options: NpmOptions = { cwd: "./" }) {
|
|
121
|
+
const { cwd } = options;
|
|
122
|
+
return this.run({ cwd }, Commands.install, "--package-lock-only", "--ignore-scripts");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* True when package@version exists on the registry.
|
|
127
|
+
* Uses ambient npm config (cwd `.npmrc`); do not pass tokens on argv.
|
|
128
|
+
*/
|
|
129
|
+
private async isPublished(directory: string, options: NpmOptions): Promise<boolean> {
|
|
130
|
+
const { readFileSync } = await import("node:fs");
|
|
131
|
+
const { join } = await import("node:path");
|
|
132
|
+
try {
|
|
133
|
+
const pkgJson = JSON.parse(readFileSync(join(directory, "package.json"), "utf8"));
|
|
134
|
+
const spec = `${pkgJson.name}@${pkgJson.version}`;
|
|
135
|
+
await this.run(options, "view", spec, "version");
|
|
136
|
+
return true;
|
|
137
|
+
} catch {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private async createTempUserConfig(authToken: string): Promise<string> {
|
|
143
|
+
const { mkdtemp, writeFile } = await import("node:fs/promises");
|
|
144
|
+
const { tmpdir } = await import("node:os");
|
|
145
|
+
const { join } = await import("node:path");
|
|
146
|
+
const tempDir = await mkdtemp(join(tmpdir(), "decohere-npmrc-"));
|
|
147
|
+
const npmrcPath = join(tempDir, ".npmrc");
|
|
148
|
+
const npmrc = [
|
|
149
|
+
"registry=https://registry.npmjs.org/",
|
|
150
|
+
"always-auth=true",
|
|
151
|
+
`//registry.npmjs.org/:_authToken=${authToken}`,
|
|
152
|
+
].join("\n");
|
|
153
|
+
await writeFile(npmrcPath, `${npmrc}\n`, { mode: 0o600 });
|
|
154
|
+
return npmrcPath;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private async removeTempUserConfig(npmrcPath: string): Promise<void> {
|
|
158
|
+
const { rm } = await import("node:fs/promises");
|
|
159
|
+
const { dirname } = await import("node:path");
|
|
160
|
+
const dir = dirname(npmrcPath);
|
|
161
|
+
await rm(dir, { recursive: true, force: true });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function isAlreadyPublishedError(error: unknown): boolean {
|
|
166
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
167
|
+
return /cannot publish over the previously published versions/i.test(message);
|
|
168
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export type NpmOptions = {
|
|
2
|
+
cwd?: string;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export type PublishOptions = {
|
|
6
|
+
dryRun?: boolean;
|
|
7
|
+
skipIfPublished?: boolean;
|
|
8
|
+
authToken?: string;
|
|
9
|
+
otp?: string;
|
|
10
|
+
provenance?: boolean;
|
|
11
|
+
/** @deprecated use channel */
|
|
12
|
+
versionType?: VersionType;
|
|
13
|
+
channel?: string;
|
|
14
|
+
} & NpmOptions;
|
|
15
|
+
|
|
16
|
+
export type VersionType = "major" | "minor" | "patch" | "prerelease";
|
|
17
|
+
|
|
18
|
+
export const VersionTypes = {
|
|
19
|
+
major: "major",
|
|
20
|
+
minor: "minor",
|
|
21
|
+
patch: "patch",
|
|
22
|
+
prerelease: "prerelease",
|
|
23
|
+
} satisfies Record<VersionType, VersionType>;
|
|
24
|
+
|
|
25
|
+
export type VersionOptions = {
|
|
26
|
+
preid?: string;
|
|
27
|
+
noTag?: boolean;
|
|
28
|
+
} & NpmOptions;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Shell } from "../shell/shell.js";
|
|
2
|
+
import type { PackageManager } from "../types/package-manager.js";
|
|
3
|
+
import type { PnpmOptions } from "./pnpm.types.js";
|
|
4
|
+
|
|
5
|
+
const Commands = {
|
|
6
|
+
install: "install",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export class Pnpm extends Shell implements PackageManager {
|
|
10
|
+
public readonly name = "pnpm" as const;
|
|
11
|
+
protected program: string = "pnpm";
|
|
12
|
+
|
|
13
|
+
public async install(options: PnpmOptions = { cwd: "./" }) {
|
|
14
|
+
const { cwd } = options;
|
|
15
|
+
return this.run({ cwd }, Commands.install);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public async syncLockFile(options: PnpmOptions = { cwd: "./" }) {
|
|
19
|
+
const { cwd } = options;
|
|
20
|
+
return this.run({ cwd }, Commands.install, "--lockfile-only", "--ignore-scripts");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function fields(args: string[]): Record<string, string | boolean | string[]> {
|
|
2
|
+
const positional: string[] = [];
|
|
3
|
+
const flags: Record<string, string | boolean> = {};
|
|
4
|
+
for (let i = 0; i < args.length; i++) {
|
|
5
|
+
const arg = args[i];
|
|
6
|
+
if (arg === "--") {
|
|
7
|
+
positional.push(...args.slice(i + 1));
|
|
8
|
+
break;
|
|
9
|
+
}
|
|
10
|
+
if (arg.startsWith("--")) {
|
|
11
|
+
const body = arg.slice(2);
|
|
12
|
+
const eq = body.indexOf("=");
|
|
13
|
+
if (eq !== -1) {
|
|
14
|
+
flags[body.slice(0, eq)] = body.slice(eq + 1);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
const next = args[i + 1];
|
|
18
|
+
if (next != null && !next.startsWith("-")) {
|
|
19
|
+
flags[body] = next;
|
|
20
|
+
i += 1;
|
|
21
|
+
} else {
|
|
22
|
+
flags[body] = true;
|
|
23
|
+
}
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
positional.push(arg);
|
|
27
|
+
}
|
|
28
|
+
return { _: positional, ...flags };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const shellArgv = { fields };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { type SpawnOptions, spawn } from "node:child_process";
|
|
2
|
+
import { scribe } from "@decohere/scribe";
|
|
3
|
+
import { InteropError } from "../errors/interop.error.js";
|
|
4
|
+
import { shellArgv } from "./shell.argv.js";
|
|
5
|
+
import type { ShellOptions, ShellResponse } from "./shell.types.js";
|
|
6
|
+
|
|
7
|
+
export abstract class Shell {
|
|
8
|
+
protected abstract program: string;
|
|
9
|
+
protected async run(options: ShellOptions, ...args: string[]) {
|
|
10
|
+
const logger = scribe.logger(Shell.name);
|
|
11
|
+
const argv = args ?? [];
|
|
12
|
+
const argvFields = shellArgv.fields(argv);
|
|
13
|
+
const positionals = Array.isArray(argvFields._) ? argvFields._ : [];
|
|
14
|
+
const commandName = [this.program, ...positionals].join(" ").trim();
|
|
15
|
+
logger.info("Running", { program: this.program, args: argvFields });
|
|
16
|
+
return new Promise<ShellResponse>((resolve, reject) => {
|
|
17
|
+
let settled = false;
|
|
18
|
+
const resolveOnce = (response: ShellResponse) => {
|
|
19
|
+
if (settled) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
settled = true;
|
|
23
|
+
resolve(response);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const rejectOnce = (error: unknown) => {
|
|
27
|
+
if (settled) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
settled = true;
|
|
31
|
+
reject(error);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const { cwd, env } = options;
|
|
35
|
+
const spawnOpt: SpawnOptions = {
|
|
36
|
+
cwd: cwd ?? ".",
|
|
37
|
+
stdio: ["inherit", "pipe", "pipe"],
|
|
38
|
+
env: env ?? { ...process.env },
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const proc = spawn(this.program, argv, spawnOpt);
|
|
42
|
+
const responseData: string[] = [];
|
|
43
|
+
const errorData: string[] = [];
|
|
44
|
+
|
|
45
|
+
proc.on("close", (code, signal) => {
|
|
46
|
+
if (code === 0) {
|
|
47
|
+
resolveOnce({
|
|
48
|
+
message: `${commandName} completed`,
|
|
49
|
+
data: responseData,
|
|
50
|
+
});
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const stderr = errorData.join("\n").trim();
|
|
55
|
+
const stdout = responseData.join("\n").trim();
|
|
56
|
+
const exitCodeText = code == null ? "unknown" : String(code);
|
|
57
|
+
const signalText = signal ? ` signal ${signal}` : "";
|
|
58
|
+
const base = `Running '${commandName}' failed with code ${exitCodeText}${signalText}`;
|
|
59
|
+
const details = [
|
|
60
|
+
stderr ? `stderr:\n${stderr}` : "",
|
|
61
|
+
!stderr && stdout ? `stdout:\n${stdout}` : "",
|
|
62
|
+
]
|
|
63
|
+
.filter(Boolean)
|
|
64
|
+
.join("\n");
|
|
65
|
+
|
|
66
|
+
rejectOnce(new InteropError(details ? `${base}\n${details}` : base));
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
proc.on("error", (err) => {
|
|
70
|
+
rejectOnce(err);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
proc.stdout.on("error", (err) => {
|
|
74
|
+
rejectOnce(err);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
proc.stdout.on("data", (data) => {
|
|
78
|
+
const lines = this.parseStream(data);
|
|
79
|
+
console.log(data.toString());
|
|
80
|
+
responseData.push(...lines);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
proc.stderr?.on("error", (err) => {
|
|
84
|
+
rejectOnce(err);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
proc.stderr?.on("data", (data) => {
|
|
88
|
+
const lines = this.parseStream(data);
|
|
89
|
+
console.error(data.toString());
|
|
90
|
+
errorData.push(...lines);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private parseStream(data: Buffer | string) {
|
|
96
|
+
const dataStr: string = data.toString();
|
|
97
|
+
const lines = dataStr.split("\n").filter((l) => l !== "");
|
|
98
|
+
return lines;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { createInterface } from "node:readline/promises";
|
|
4
|
+
import { Npm } from "../npm/npm.js";
|
|
5
|
+
import { Pnpm } from "../pnpm/pnpm.js";
|
|
6
|
+
import { Yarn } from "../yarn/yarn.js";
|
|
7
|
+
import type { PackageManager } from "./package-manager.js";
|
|
8
|
+
|
|
9
|
+
const LOCKFILES: Array<[string, () => PackageManager]> = [
|
|
10
|
+
["pnpm-lock.yaml", () => new Pnpm()],
|
|
11
|
+
["yarn.lock", () => new Yarn()],
|
|
12
|
+
["package-lock.json", () => new Npm()],
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
function fromLockfile(cwd: string): PackageManager | undefined {
|
|
16
|
+
for (const [file, factory] of LOCKFILES) {
|
|
17
|
+
if (existsSync(join(cwd, file))) return factory();
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function fromUserAgent(): PackageManager | undefined {
|
|
23
|
+
const agent = process.env.npm_config_user_agent ?? "";
|
|
24
|
+
if (agent.startsWith("pnpm")) return new Pnpm();
|
|
25
|
+
if (agent.startsWith("yarn")) return new Yarn();
|
|
26
|
+
if (agent.startsWith("npm")) return new Npm();
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function detectPackageManager(cwd: string): PackageManager | undefined {
|
|
31
|
+
return fromLockfile(cwd) ?? fromUserAgent();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function packageManagerFromString(name: string): PackageManager {
|
|
35
|
+
if (name === "yarn") return new Yarn();
|
|
36
|
+
if (name === "npm") return new Npm();
|
|
37
|
+
return new Pnpm();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function promptForPackageManager(): Promise<PackageManager> {
|
|
41
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
42
|
+
const answer = await rl.question("Package manager? [pnpm/yarn/npm] (default: pnpm): ");
|
|
43
|
+
rl.close();
|
|
44
|
+
return packageManagerFromString(answer.trim().toLowerCase());
|
|
45
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ShellResponse } from "../shell/shell.types.js";
|
|
2
|
+
|
|
3
|
+
export interface PackageManager {
|
|
4
|
+
readonly name: "npm" | "pnpm" | "yarn";
|
|
5
|
+
install(options?: { cwd?: string }): Promise<ShellResponse>;
|
|
6
|
+
syncLockFile(options?: { cwd?: string }): Promise<ShellResponse>;
|
|
7
|
+
}
|