aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/turbo-ignore/src/getWorkspace.ts
diff options
context:
space:
mode:
author简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
committer简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
commitdd84b9d64fb98746a230cd24233ff50a562c39c9 (patch)
treeb583261ef00b3afe72ec4d6dacb31e57779a6faf /packages/turbo-ignore/src/getWorkspace.ts
parent0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff)
downloadHydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz
HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip
Diffstat (limited to 'packages/turbo-ignore/src/getWorkspace.ts')
-rw-r--r--packages/turbo-ignore/src/getWorkspace.ts37
1 files changed, 37 insertions, 0 deletions
diff --git a/packages/turbo-ignore/src/getWorkspace.ts b/packages/turbo-ignore/src/getWorkspace.ts
new file mode 100644
index 0000000..e0b3167
--- /dev/null
+++ b/packages/turbo-ignore/src/getWorkspace.ts
@@ -0,0 +1,37 @@
+import fs from "fs";
+import path from "path";
+import { error, info } from "./logger";
+import { TurboIgnoreArgs } from "./types";
+
+export function getWorkspace(args: TurboIgnoreArgs): string | null {
+ const { directory = process.cwd(), workspace } = args;
+
+ // if the workspace is provided via args, use that
+ if (workspace) {
+ info(`Using "${workspace}" as workspace from arguments`);
+ return workspace;
+ }
+
+ // otherwise, try and infer it from a package.json in the current directory
+ const packageJsonPath = path.join(directory, "package.json");
+ try {
+ const raw = fs.readFileSync(packageJsonPath, "utf8");
+ const packageJsonContent: Record<string, string> & { name: string } =
+ JSON.parse(raw);
+
+ if (!packageJsonContent.name) {
+ error(`"${packageJsonPath}" is missing the "name" field (required).`);
+ return null;
+ }
+
+ info(
+ `Inferred "${packageJsonContent.name}" as workspace from "package.json"`
+ );
+ return packageJsonContent.name;
+ } catch (e) {
+ error(
+ `"${packageJsonPath}" could not be found. turbo-ignore inferencing failed`
+ );
+ return null;
+ }
+}