@typespec/spector 0.1.0-alpha.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.
- package/CHANGELOG.md +1 -0
- package/LICENSE +21 -0
- package/cmd/cli.mjs +3 -0
- package/dist/generated-defs/TypeSpec.Spector.d.ts +22 -0
- package/dist/generated-defs/TypeSpec.Spector.d.ts.map +1 -0
- package/dist/generated-defs/TypeSpec.Spector.js +2 -0
- package/dist/generated-defs/TypeSpec.Spector.js.map +1 -0
- package/dist/generated-defs/TypeSpec.Spector.ts-test.d.ts +2 -0
- package/dist/generated-defs/TypeSpec.Spector.ts-test.d.ts.map +1 -0
- package/dist/generated-defs/TypeSpec.Spector.ts-test.js +5 -0
- package/dist/generated-defs/TypeSpec.Spector.ts-test.js.map +1 -0
- package/dist/src/actions/check-coverage.d.ts +11 -0
- package/dist/src/actions/check-coverage.d.ts.map +1 -0
- package/dist/src/actions/check-coverage.js +77 -0
- package/dist/src/actions/check-coverage.js.map +1 -0
- package/dist/src/actions/generate-scenario-summary.d.ts +9 -0
- package/dist/src/actions/generate-scenario-summary.d.ts.map +1 -0
- package/dist/src/actions/generate-scenario-summary.js +49 -0
- package/dist/src/actions/generate-scenario-summary.js.map +1 -0
- package/dist/src/actions/helper.d.ts +21 -0
- package/dist/src/actions/helper.d.ts.map +1 -0
- package/dist/src/actions/helper.js +81 -0
- package/dist/src/actions/helper.js.map +1 -0
- package/dist/src/actions/index.d.ts +2 -0
- package/dist/src/actions/index.d.ts.map +1 -0
- package/dist/src/actions/index.js +2 -0
- package/dist/src/actions/index.js.map +1 -0
- package/dist/src/actions/serve.d.ts +12 -0
- package/dist/src/actions/serve.d.ts.map +1 -0
- package/dist/src/actions/serve.js +73 -0
- package/dist/src/actions/serve.js.map +1 -0
- package/dist/src/actions/server-test.d.ts +7 -0
- package/dist/src/actions/server-test.d.ts.map +1 -0
- package/dist/src/actions/server-test.js +165 -0
- package/dist/src/actions/server-test.js.map +1 -0
- package/dist/src/actions/upload-coverage-report.d.ts +10 -0
- package/dist/src/actions/upload-coverage-report.d.ts.map +1 -0
- package/dist/src/actions/upload-coverage-report.js +19 -0
- package/dist/src/actions/upload-coverage-report.js.map +1 -0
- package/dist/src/actions/upload-scenario-manifest.d.ts +6 -0
- package/dist/src/actions/upload-scenario-manifest.d.ts.map +1 -0
- package/dist/src/actions/upload-scenario-manifest.js +18 -0
- package/dist/src/actions/upload-scenario-manifest.js.map +1 -0
- package/dist/src/actions/validate-mock-apis.d.ts +7 -0
- package/dist/src/actions/validate-mock-apis.d.ts.map +1 -0
- package/dist/src/actions/validate-mock-apis.js +71 -0
- package/dist/src/actions/validate-mock-apis.js.map +1 -0
- package/dist/src/actions/validate-scenarios.d.ts +7 -0
- package/dist/src/actions/validate-scenarios.d.ts.map +1 -0
- package/dist/src/actions/validate-scenarios.js +25 -0
- package/dist/src/actions/validate-scenarios.js.map +1 -0
- package/dist/src/app/app.d.ts +17 -0
- package/dist/src/app/app.d.ts.map +1 -0
- package/dist/src/app/app.js +134 -0
- package/dist/src/app/app.js.map +1 -0
- package/dist/src/app/config.d.ts +15 -0
- package/dist/src/app/config.d.ts.map +1 -0
- package/dist/src/app/config.js +2 -0
- package/dist/src/app/config.js.map +1 -0
- package/dist/src/app/index.d.ts +3 -0
- package/dist/src/app/index.d.ts.map +1 -0
- package/dist/src/app/index.js +3 -0
- package/dist/src/app/index.js.map +1 -0
- package/dist/src/app/request-processor.d.ts +5 -0
- package/dist/src/app/request-processor.d.ts.map +1 -0
- package/dist/src/app/request-processor.js +44 -0
- package/dist/src/app/request-processor.js.map +1 -0
- package/dist/src/cli/cli.d.ts +3 -0
- package/dist/src/cli/cli.d.ts.map +1 -0
- package/dist/src/cli/cli.js +316 -0
- package/dist/src/cli/cli.js.map +1 -0
- package/dist/src/config/config-schema.d.ts +4 -0
- package/dist/src/config/config-schema.d.ts.map +1 -0
- package/dist/src/config/config-schema.js +14 -0
- package/dist/src/config/config-schema.js.map +1 -0
- package/dist/src/config/config.d.ts +4 -0
- package/dist/src/config/config.d.ts.map +1 -0
- package/dist/src/config/config.js +12 -0
- package/dist/src/config/config.js.map +1 -0
- package/dist/src/config/schema-validator.d.ts +18 -0
- package/dist/src/config/schema-validator.d.ts.map +1 -0
- package/dist/src/config/schema-validator.js +43 -0
- package/dist/src/config/schema-validator.js.map +1 -0
- package/dist/src/config/types.d.ts +4 -0
- package/dist/src/config/types.d.ts.map +1 -0
- package/dist/src/config/types.js +2 -0
- package/dist/src/config/types.js.map +1 -0
- package/dist/src/constants.d.ts +4 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +4 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/coverage/common.d.ts +3 -0
- package/dist/src/coverage/common.d.ts.map +1 -0
- package/dist/src/coverage/common.js +9 -0
- package/dist/src/coverage/common.js.map +1 -0
- package/dist/src/coverage/coverage-report.d.ts +3 -0
- package/dist/src/coverage/coverage-report.d.ts.map +1 -0
- package/dist/src/coverage/coverage-report.js +9 -0
- package/dist/src/coverage/coverage-report.js.map +1 -0
- package/dist/src/coverage/coverage-tracker.d.ts +17 -0
- package/dist/src/coverage/coverage-tracker.d.ts.map +1 -0
- package/dist/src/coverage/coverage-tracker.js +125 -0
- package/dist/src/coverage/coverage-tracker.js.map +1 -0
- package/dist/src/coverage/index.d.ts +2 -0
- package/dist/src/coverage/index.d.ts.map +1 -0
- package/dist/src/coverage/index.js +2 -0
- package/dist/src/coverage/index.js.map +1 -0
- package/dist/src/coverage/scenario-manifest.d.ts +6 -0
- package/dist/src/coverage/scenario-manifest.d.ts.map +1 -0
- package/dist/src/coverage/scenario-manifest.js +32 -0
- package/dist/src/coverage/scenario-manifest.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lib/decorators.d.ts +24 -0
- package/dist/src/lib/decorators.d.ts.map +1 -0
- package/dist/src/lib/decorators.js +158 -0
- package/dist/src/lib/decorators.js.map +1 -0
- package/dist/src/lib/index.d.ts +4 -0
- package/dist/src/lib/index.d.ts.map +1 -0
- package/dist/src/lib/index.js +6 -0
- package/dist/src/lib/index.js.map +1 -0
- package/dist/src/lib/lib.d.ts +33 -0
- package/dist/src/lib/lib.d.ts.map +1 -0
- package/dist/src/lib/lib.js +31 -0
- package/dist/src/lib/lib.js.map +1 -0
- package/dist/src/lib/tsp-index.d.ts +2 -0
- package/dist/src/lib/tsp-index.d.ts.map +1 -0
- package/dist/src/lib/tsp-index.js +11 -0
- package/dist/src/lib/tsp-index.js.map +1 -0
- package/dist/src/lib/validate.d.ts +3 -0
- package/dist/src/lib/validate.d.ts.map +1 -0
- package/dist/src/lib/validate.js +41 -0
- package/dist/src/lib/validate.js.map +1 -0
- package/dist/src/logger.d.ts +3 -0
- package/dist/src/logger.d.ts.map +1 -0
- package/dist/src/logger.js +10 -0
- package/dist/src/logger.js.map +1 -0
- package/dist/src/routes/admin.d.ts +3 -0
- package/dist/src/routes/admin.d.ts.map +1 -0
- package/dist/src/routes/admin.js +13 -0
- package/dist/src/routes/admin.js.map +1 -0
- package/dist/src/routes/index.d.ts +3 -0
- package/dist/src/routes/index.d.ts.map +1 -0
- package/dist/src/routes/index.js +6 -0
- package/dist/src/routes/index.js.map +1 -0
- package/dist/src/scenarios-resolver.d.ts +17 -0
- package/dist/src/scenarios-resolver.d.ts.map +1 -0
- package/dist/src/scenarios-resolver.js +163 -0
- package/dist/src/scenarios-resolver.js.map +1 -0
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/index.d.ts.map +1 -0
- package/dist/src/server/index.js +2 -0
- package/dist/src/server/index.js.map +1 -0
- package/dist/src/server/server.d.ts +14 -0
- package/dist/src/server/server.d.ts.map +1 -0
- package/dist/src/server/server.js +68 -0
- package/dist/src/server/server.js.map +1 -0
- package/dist/src/spec-utils/import-spec.d.ts +6 -0
- package/dist/src/spec-utils/import-spec.d.ts.map +1 -0
- package/dist/src/spec-utils/import-spec.js +39 -0
- package/dist/src/spec-utils/import-spec.js.map +1 -0
- package/dist/src/spec-utils/index.d.ts +2 -0
- package/dist/src/spec-utils/index.d.ts.map +1 -0
- package/dist/src/spec-utils/index.js +2 -0
- package/dist/src/spec-utils/index.js.map +1 -0
- package/dist/src/utils/body-utils.d.ts +8 -0
- package/dist/src/utils/body-utils.d.ts.map +1 -0
- package/dist/src/utils/body-utils.js +8 -0
- package/dist/src/utils/body-utils.js.map +1 -0
- package/dist/src/utils/diagnostic-reporter.d.ts +13 -0
- package/dist/src/utils/diagnostic-reporter.d.ts.map +1 -0
- package/dist/src/utils/diagnostic-reporter.js +35 -0
- package/dist/src/utils/diagnostic-reporter.js.map +1 -0
- package/dist/src/utils/exec.d.ts +9 -0
- package/dist/src/utils/exec.d.ts.map +1 -0
- package/dist/src/utils/exec.js +30 -0
- package/dist/src/utils/exec.js.map +1 -0
- package/dist/src/utils/file-utils.d.ts +7 -0
- package/dist/src/utils/file-utils.d.ts.map +1 -0
- package/dist/src/utils/file-utils.js +13 -0
- package/dist/src/utils/file-utils.js.map +1 -0
- package/dist/src/utils/index.d.ts +6 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/index.js +6 -0
- package/dist/src/utils/index.js.map +1 -0
- package/dist/src/utils/misc-utils.d.ts +8 -0
- package/dist/src/utils/misc-utils.d.ts.map +1 -0
- package/dist/src/utils/misc-utils.js +47 -0
- package/dist/src/utils/misc-utils.js.map +1 -0
- package/dist/src/utils/path-utils.d.ts +2 -0
- package/dist/src/utils/path-utils.d.ts.map +1 -0
- package/dist/src/utils/path-utils.js +4 -0
- package/dist/src/utils/path-utils.js.map +1 -0
- package/dist/src/utils/request-utils.d.ts +3 -0
- package/dist/src/utils/request-utils.d.ts.map +1 -0
- package/dist/src/utils/request-utils.js +2 -0
- package/dist/src/utils/request-utils.js.map +1 -0
- package/generated-defs/TypeSpec.Spector.ts +46 -0
- package/generated-defs/TypeSpec.Spector.ts-test.ts +5 -0
- package/lib/main.tsp +37 -0
- package/package.json +79 -0
- package/src/actions/check-coverage.ts +98 -0
- package/src/actions/generate-scenario-summary.ts +63 -0
- package/src/actions/helper.ts +116 -0
- package/src/actions/index.ts +1 -0
- package/src/actions/serve.ts +88 -0
- package/src/actions/server-test.ts +198 -0
- package/src/actions/upload-coverage-report.ts +41 -0
- package/src/actions/upload-scenario-manifest.ts +30 -0
- package/src/actions/validate-mock-apis.ts +91 -0
- package/src/actions/validate-scenarios.ts +32 -0
- package/src/app/app.ts +166 -0
- package/src/app/config.ts +16 -0
- package/src/app/index.ts +2 -0
- package/src/app/request-processor.ts +71 -0
- package/src/cli/cli.ts +368 -0
- package/src/config/config-schema.ts +16 -0
- package/src/config/config.ts +15 -0
- package/src/config/schema-validator.ts +57 -0
- package/src/config/types.ts +3 -0
- package/src/constants.ts +3 -0
- package/src/coverage/common.ts +10 -0
- package/src/coverage/coverage-report.ts +13 -0
- package/src/coverage/coverage-tracker.ts +141 -0
- package/src/coverage/index.ts +1 -0
- package/src/coverage/scenario-manifest.ts +43 -0
- package/src/index.ts +1 -0
- package/src/lib/decorators.ts +225 -0
- package/src/lib/index.ts +18 -0
- package/src/lib/lib.ts +33 -0
- package/src/lib/tsp-index.ts +12 -0
- package/src/lib/validate.ts +55 -0
- package/src/logger.ts +10 -0
- package/src/routes/admin.ts +15 -0
- package/src/routes/index.ts +7 -0
- package/src/scenarios-resolver.ts +209 -0
- package/src/server/index.ts +1 -0
- package/src/server/server.ts +99 -0
- package/src/spec-utils/import-spec.ts +49 -0
- package/src/spec-utils/index.ts +1 -0
- package/src/utils/body-utils.test.ts +18 -0
- package/src/utils/body-utils.ts +8 -0
- package/src/utils/diagnostic-reporter.ts +49 -0
- package/src/utils/exec.ts +36 -0
- package/src/utils/file-utils.ts +14 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/misc-utils.ts +54 -0
- package/src/utils/path-utils.ts +3 -0
- package/src/utils/request-utils.ts +4 -0
- package/temp/.tsbuildinfo +1 -0
- package/tsconfig.build.json +13 -0
- package/tsconfig.json +4 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type ResolveModuleHost, resolveModule } from "@typespec/compiler/module-resolver";
|
|
2
|
+
import { readFile, realpath, stat } from "fs/promises";
|
|
3
|
+
import { pathToFileURL } from "url";
|
|
4
|
+
|
|
5
|
+
export async function importTypeSpec(
|
|
6
|
+
baseDir: string,
|
|
7
|
+
): Promise<typeof import("@typespec/compiler")> {
|
|
8
|
+
return importTypeSpecLibrary("@typespec/compiler", baseDir);
|
|
9
|
+
}
|
|
10
|
+
export async function importSpecExpect(
|
|
11
|
+
baseDir: string,
|
|
12
|
+
): Promise<typeof import("@typespec/spector")> {
|
|
13
|
+
return importTypeSpecLibrary("@typespec/spector", baseDir);
|
|
14
|
+
}
|
|
15
|
+
export async function importTypeSpecRest(
|
|
16
|
+
baseDir: string,
|
|
17
|
+
): Promise<typeof import("@typespec/rest")> {
|
|
18
|
+
return importTypeSpecLibrary("@typespec/rest", baseDir);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function importTypeSpecHttp(
|
|
22
|
+
baseDir: string,
|
|
23
|
+
): Promise<typeof import("@typespec/http")> {
|
|
24
|
+
return importTypeSpecLibrary("@typespec/http", baseDir);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function importTypeSpecLibrary(name: string, baseDir: string): Promise<any> {
|
|
28
|
+
try {
|
|
29
|
+
const host: ResolveModuleHost = {
|
|
30
|
+
realpath,
|
|
31
|
+
readFile: async (path: string) => await readFile(path, "utf-8"),
|
|
32
|
+
stat,
|
|
33
|
+
};
|
|
34
|
+
const resolved = await resolveModule(host, name, {
|
|
35
|
+
baseDir,
|
|
36
|
+
conditions: ["import"],
|
|
37
|
+
});
|
|
38
|
+
return import(
|
|
39
|
+
pathToFileURL(resolved.type === "module" ? resolved.mainFile : resolved.path).toString()
|
|
40
|
+
);
|
|
41
|
+
} catch (err: any) {
|
|
42
|
+
if (err.code === "MODULE_NOT_FOUND") {
|
|
43
|
+
// Resolution from cwd failed: use current package.
|
|
44
|
+
return import(name);
|
|
45
|
+
} else {
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./import-spec.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { cleanupBody } from "./body-utils.js";
|
|
3
|
+
|
|
4
|
+
describe("BodyUtils", () => {
|
|
5
|
+
describe("cleanupBody()", () => {
|
|
6
|
+
it("remove trailing whitespaces", () => {
|
|
7
|
+
expect(cleanupBody(" foo ")).toEqual("foo");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("remove trailing new lines", () => {
|
|
11
|
+
expect(cleanupBody("\nfoo\nbar\n")).toEqual("foo\nbar");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("replace windows line endings", () => {
|
|
15
|
+
expect(cleanupBody("foo\r\nbar")).toEqual("foo\nbar");
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getSourceLocation, SourceLocation, Type } from "@typespec/compiler";
|
|
2
|
+
import pc from "picocolors";
|
|
3
|
+
import { logger } from "../logger.js";
|
|
4
|
+
|
|
5
|
+
export interface Diagnostic {
|
|
6
|
+
message: string;
|
|
7
|
+
target?: Type | string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface DiagnosticReporter {
|
|
11
|
+
readonly diagnostics: Diagnostic[];
|
|
12
|
+
reportDiagnostic(diagnostic: Diagnostic): void;
|
|
13
|
+
reportDiagnostics(diagnostic: readonly Diagnostic[]): void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function createDiagnosticReporter(): DiagnosticReporter {
|
|
17
|
+
const diagnostics: Diagnostic[] = [];
|
|
18
|
+
function reportDiagnostic(diagnostic: Diagnostic) {
|
|
19
|
+
const target = diagnostic.target ? `\n ${resolveSourceLocation(diagnostic.target)}` : "";
|
|
20
|
+
logger.error(`${pc.red("✘")} ${diagnostic.message}${target}`);
|
|
21
|
+
diagnostics.push(diagnostic);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
diagnostics,
|
|
25
|
+
reportDiagnostic,
|
|
26
|
+
reportDiagnostics(diagnostics: readonly Diagnostic[]) {
|
|
27
|
+
for (const diagnostic of diagnostics) {
|
|
28
|
+
reportDiagnostic(diagnostic);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function resolveSourceLocation(target: Type | string) {
|
|
35
|
+
if (typeof target === "string") {
|
|
36
|
+
return pc.cyan(target);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const location = getSourceLocation(target);
|
|
40
|
+
return getSourceLocationStr(location);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getSourceLocationStr(location: SourceLocation) {
|
|
44
|
+
const position = location.file.getLineAndCharacterOfPosition(location.pos);
|
|
45
|
+
const path = pc.cyan(location.file.path);
|
|
46
|
+
const line = pc.yellow(position.line + 1);
|
|
47
|
+
const column = pc.yellow(position.character + 1);
|
|
48
|
+
return `${path}:${line}:${column}`;
|
|
49
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ChildProcess, spawn, SpawnOptions } from "child_process";
|
|
2
|
+
|
|
3
|
+
export async function execAsync(
|
|
4
|
+
command: string,
|
|
5
|
+
args: string[],
|
|
6
|
+
options: SpawnOptions = {},
|
|
7
|
+
): Promise<{ exitCode: number; stdout: string; stderr: string; out: string; proc: ChildProcess }> {
|
|
8
|
+
const child = spawn(command, args, options);
|
|
9
|
+
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
child.on("error", (error) => {
|
|
12
|
+
reject(error);
|
|
13
|
+
});
|
|
14
|
+
const stdout: Buffer[] = [];
|
|
15
|
+
const stderr: Buffer[] = [];
|
|
16
|
+
const out: Buffer[] = [];
|
|
17
|
+
child.stdout?.on("data", (data) => {
|
|
18
|
+
stdout.push(data);
|
|
19
|
+
out.push(data);
|
|
20
|
+
});
|
|
21
|
+
child.stderr?.on("data", (data) => {
|
|
22
|
+
stderr.push(data);
|
|
23
|
+
out.push(data);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
child.on("exit", (exitCode) => {
|
|
27
|
+
resolve({
|
|
28
|
+
exitCode: exitCode ?? -1,
|
|
29
|
+
stdout: Buffer.concat(stdout).toString(),
|
|
30
|
+
stderr: Buffer.concat(stderr).toString(),
|
|
31
|
+
out: Buffer.concat(out).toString(),
|
|
32
|
+
proc: child,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { mkdir } from "fs/promises";
|
|
2
|
+
import { globby } from "globby";
|
|
3
|
+
|
|
4
|
+
export async function findFilesFromPattern(pattern: string | string[]): Promise<string[]> {
|
|
5
|
+
return await globby(pattern);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Ensure the given dir exists.
|
|
10
|
+
* @param path Path to the dir.
|
|
11
|
+
*/
|
|
12
|
+
export async function ensureDir(path: string): Promise<void> {
|
|
13
|
+
await mkdir(path, { recursive: true });
|
|
14
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { execSync } from "child_process";
|
|
2
|
+
import { readFile, stat } from "fs/promises";
|
|
3
|
+
import { dirname, join } from "path";
|
|
4
|
+
import { logger } from "../logger.js";
|
|
5
|
+
|
|
6
|
+
export async function ensureScenariosPathExists(scenariosPath: string) {
|
|
7
|
+
try {
|
|
8
|
+
const stats = await stat(scenariosPath);
|
|
9
|
+
if (!stats.isDirectory()) {
|
|
10
|
+
throw new Error(`Scenarios path ${scenariosPath} is not a directory.`);
|
|
11
|
+
}
|
|
12
|
+
} catch (e) {
|
|
13
|
+
throw new Error(`Scenarios path ${scenariosPath} doesn't exists.`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getCommit(path: string): string {
|
|
18
|
+
return execSync("git rev-parse HEAD", { cwd: path }).toString().trim();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PackageJson {
|
|
22
|
+
name: string;
|
|
23
|
+
version: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function getPackageJson(path: string): Promise<PackageJson | undefined> {
|
|
27
|
+
logger.debug(`Loading package json for path "${path}"`);
|
|
28
|
+
const projectRoot = await findProjectRoot(path);
|
|
29
|
+
if (projectRoot === undefined) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
logger.debug(`Found project root "${projectRoot}"`);
|
|
33
|
+
const packageJsonPath = join(projectRoot, "package.json");
|
|
34
|
+
return JSON.parse((await readFile(packageJsonPath, "utf-8")).toString());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function findProjectRoot(path: string): Promise<string | undefined> {
|
|
38
|
+
let current = path;
|
|
39
|
+
while (true) {
|
|
40
|
+
const pkgPath = join(current, "package.json");
|
|
41
|
+
try {
|
|
42
|
+
const stats = await stat(pkgPath);
|
|
43
|
+
if (stats?.isFile()) {
|
|
44
|
+
return current;
|
|
45
|
+
}
|
|
46
|
+
} catch {}
|
|
47
|
+
|
|
48
|
+
const parent = dirname(current);
|
|
49
|
+
if (parent === current) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
current = parent;
|
|
53
|
+
}
|
|
54
|
+
}
|