@graphcommerce/cli 9.0.0-canary.107 → 9.0.0-canary.108

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 CHANGED
@@ -1,5 +1,7 @@
1
1
  # @graphcommerce/cli
2
2
 
3
+ ## 9.0.0-canary.108
4
+
3
5
  ## 9.0.0-canary.107
4
6
 
5
7
  ## 9.0.0-canary.106
@@ -1,26 +1,43 @@
1
1
  #!/usr/bin/env node
2
- import { isMonorepo } from '@graphcommerce/next-config';
3
- import { detect } from 'detect-package-manager';
4
2
  import { spawn } from 'node:child_process';
3
+ import 'node:fs';
4
+ import path from 'node:path';
5
+ import { findParentPath } from '@graphcommerce/next-config';
6
+ import { detect } from 'detect-package-manager';
5
7
 
8
+ const debug = process.env.DEBUG === "1";
9
+ const log = (message) => debug && console.log(`is-monorepo: ${message}`);
10
+ const logError = (message) => console.error(`is-monorepo: ${message}`);
6
11
  async function main() {
7
- const isMono = isMonorepo();
8
- const command = isMono ? process.argv.slice(2)[0] : process.argv.slice(2)[1];
12
+ const parentPath = findParentPath(process.cwd());
13
+ const command = parentPath ? process.argv.slice(2)[0] : process.argv.slice(2)[1];
14
+ if (!command) {
15
+ logError("No command provided");
16
+ process.exit(1);
17
+ }
9
18
  let packageManager = "yarn";
10
19
  try {
11
- packageManager = await detect({ cwd: isMono ? "../.." : "." });
20
+ packageManager = await detect({ cwd: "." });
12
21
  } catch {
13
- console.error("Could not detect package manager, defaulting to yarn");
22
+ log("Could not detect package manager, defaulting to yarn");
14
23
  }
15
- const commandArray = command.split(" ").map((arg) => arg.replace("[pkgrun]", `${packageManager} run`));
16
- if (isMono) commandArray.unshift("cd", "../..", "&&");
17
- const [cmd, ...args] = commandArray;
18
- const childProcess = spawn(cmd, args, { shell: true, stdio: "inherit" });
24
+ const relativePath = parentPath ? `cd ${path.relative(process.cwd(), parentPath)}/` : "cd .";
25
+ const commandArray = command.split(" ").map(
26
+ (arg) => arg.replace("[pkgrun]", `${packageManager}${packageManager === "npm" ? " run" : ""}`)
27
+ );
28
+ log(`Command: ${commandArray.join(" ")}`);
29
+ const finalCommand = `${relativePath} && ${commandArray.join(" ")}`;
30
+ log(`Executing: ${finalCommand}`);
31
+ const childProcess = spawn(finalCommand, [], { shell: true, stdio: "inherit" });
19
32
  childProcess.on("exit", (code) => {
20
33
  process.exit(code ?? 0);
21
34
  });
22
35
  }
23
- main().catch((error) => {
24
- console.error(error);
36
+ main().catch((err) => {
37
+ if (err instanceof Error) {
38
+ logError(err.message);
39
+ } else {
40
+ logError("An unknown error occurred");
41
+ }
25
42
  process.exit(1);
26
43
  });
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/cli",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "9.0.0-canary.107",
5
+ "version": "9.0.0-canary.108",
6
6
  "scripts": {
7
7
  "dev": "pkgroll --watch",
8
8
  "build": "pkgroll",
@@ -31,8 +31,8 @@
31
31
  "cosmiconfig": "^8.3.6",
32
32
  "detect-package-manager": "^3.0.2",
33
33
  "dotenv": "16.4.5",
34
- "graphql": "^16.7.1",
35
- "graphql-codegen-typescript-validation-schema": "^0.16.0",
34
+ "graphql": "^16.9.0",
35
+ "graphql-codegen-typescript-validation-schema": "^0.17.0",
36
36
  "graphql-tag": "^2.12.6",
37
37
  "rimraf": "^5.0.10",
38
38
  "tslib": "^2.7.0",
@@ -40,12 +40,12 @@
40
40
  "yaml": "2.5.1"
41
41
  },
42
42
  "peerDependencies": {
43
- "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.107",
44
- "@graphcommerce/graphql-mesh": "^9.0.0-canary.107",
45
- "@graphcommerce/hygraph-cli": "^9.0.0-canary.107",
46
- "@graphcommerce/next-config": "^9.0.0-canary.107",
47
- "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.107",
48
- "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.107",
43
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.108",
44
+ "@graphcommerce/graphql-mesh": "^9.0.0-canary.108",
45
+ "@graphcommerce/hygraph-cli": "^9.0.0-canary.108",
46
+ "@graphcommerce/next-config": "^9.0.0-canary.108",
47
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.108",
48
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.108",
49
49
  "react": "^18.2.0"
50
50
  },
51
51
  "sideEffects": false,
@@ -1,44 +1,88 @@
1
1
  #!/usr/bin/env node
2
- import { isMonorepo } from '@graphcommerce/next-config'
3
- import { detect } from 'detect-package-manager'
4
2
  import type { ChildProcess } from 'node:child_process'
5
3
  import { spawn } from 'node:child_process'
4
+ import fs from 'node:fs'
5
+ import path from 'node:path'
6
+ import { findParentPath, isMonorepo } from '@graphcommerce/next-config'
7
+ import { detect } from 'detect-package-manager'
8
+
9
+ const debug = process.env.DEBUG === '1'
10
+ // eslint-disable-next-line no-console
11
+ const log = (message: string) => debug && console.log(`is-monorepo: ${message}`)
12
+ const logError = (message: string) => console.error(`is-monorepo: ${message}`)
13
+
14
+ /** Find the nearest parent directory containing a @graphcommerce/* package */
15
+ function findRootDir(startDir: string): string | null {
16
+ // Start from the parent directory to find a parent @graphcommerce package
17
+ let currentDir = path.dirname(startDir)
18
+ log(`Looking for parent packages starting from: ${currentDir}`)
19
+
20
+ while (currentDir !== path.parse(currentDir).root) {
21
+ try {
22
+ const packageJson = JSON.parse(fs.readFileSync(path.join(currentDir, 'package.json'), 'utf8'))
23
+ if (packageJson.name.startsWith('@graphcommerce/')) {
24
+ log(`Found root directory at: ${currentDir}`)
25
+ return currentDir
26
+ }
27
+ } catch {
28
+ // Continue if package.json doesn't exist or can't be parsed
29
+ }
30
+ currentDir = path.dirname(currentDir)
31
+ }
32
+
33
+ return null
34
+ }
6
35
 
7
36
  /**
8
- * Executes a command dependening if we're running in a monorepo or not Usage:
37
+ * Executes a command depending if we're running in a monorepo or not. Usage: is-monorepo '[pkgrun]
38
+ * run my-script' '[pkgrun] run my-other-script'
9
39
  *
10
- * is-monorepo '[pkgrun] run my-script' '[pkgrun] run my-other-script'
40
+ * The [pkgrun] placeholder will be replaced with the detected package manager:
11
41
  *
12
- * We're using the `[pkgrun]` placeholder to replace it with the package manager we're using. For
13
- * example, if we're using `yarn` it will replace `[pkgrun]` with `yarn`. If we're using `npm` it
14
- * will replace `[pkgrun]` with `npm run`.
42
+ * - Yarn -> 'yarn'
43
+ * - Npm -> 'npm run'
15
44
  */
16
45
  async function main() {
17
- const isMono = isMonorepo()
18
- const command = isMono ? process.argv.slice(2)[0] : process.argv.slice(2)[1]
46
+ // const isMono = isMonorepo()
47
+ const parentPath = findParentPath(process.cwd())
48
+
49
+ const command = parentPath ? process.argv.slice(2)[0] : process.argv.slice(2)[1]
50
+
51
+ if (!command) {
52
+ logError('No command provided')
53
+ process.exit(1)
54
+ }
19
55
 
20
56
  let packageManager = 'yarn'
21
57
  try {
22
- packageManager = await detect({ cwd: isMono ? '../..' : '.' })
58
+ packageManager = await detect({ cwd: '.' })
23
59
  } catch {
24
- console.error('Could not detect package manager, defaulting to yarn')
60
+ log('Could not detect package manager, defaulting to yarn')
25
61
  }
26
62
 
63
+ const relativePath = parentPath ? `cd ${path.relative(process.cwd(), parentPath)}/` : 'cd .'
27
64
  const commandArray = command
28
65
  .split(' ')
29
- .map((arg) => arg.replace('[pkgrun]', `${packageManager} run`))
66
+ .map((arg) =>
67
+ arg.replace('[pkgrun]', `${packageManager}${packageManager === 'npm' ? ' run' : ''}`),
68
+ )
69
+ log(`Command: ${commandArray.join(' ')}`)
30
70
 
31
- if (isMono) commandArray.unshift('cd', '../..', '&&')
71
+ const finalCommand = `${relativePath} && ${commandArray.join(' ')}`
72
+ log(`Executing: ${finalCommand}`)
32
73
 
33
- const [cmd, ...args] = commandArray
34
- const childProcess: ChildProcess = spawn(cmd, args, { shell: true, stdio: 'inherit' })
74
+ const childProcess: ChildProcess = spawn(finalCommand, [], { shell: true, stdio: 'inherit' })
35
75
 
36
76
  childProcess.on('exit', (code) => {
37
77
  process.exit(code ?? 0)
38
78
  })
39
79
  }
40
80
 
41
- main().catch((error) => {
42
- console.error(error)
81
+ main().catch((err: unknown) => {
82
+ if (err instanceof Error) {
83
+ logError(err.message)
84
+ } else {
85
+ logError('An unknown error occurred')
86
+ }
43
87
  process.exit(1)
44
88
  })