@metaplay/metaplay-auth 1.4.1 → 1.5.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/src/utils.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  import { spawn } from 'child_process'
2
-
3
- export function stackApiBaseFromOptions (arg: string, options: Record<string, any>): string {
4
- return ''
5
- }
2
+ import path from 'path'
6
3
 
7
4
  /**
8
5
  * Checks if the given string is a fully qualified domain name (FQDN).
@@ -49,15 +46,27 @@ export function getGameserverAdminUrl (uri: string): string {
49
46
  return `${hostname}-admin.${domain}`
50
47
  }
51
48
 
52
- interface CommandResult {
53
- code: number | null
49
+ export interface ExecuteCommandResult {
50
+ exitCode: number | null
54
51
  stdout: any[]
55
52
  stderr: any[]
56
53
  }
57
54
 
58
- export async function executeCommand (command: string, args: string[]): Promise<CommandResult> {
55
+ /**
56
+ * Run a child process and return an awaitable Promise.
57
+ *
58
+ * @param command Name of the command/binary to execute.
59
+ * @param args List of arugments to pass to the child process.
60
+ * @param inheritStdio Should the stdin/stdout/stderr be inherited from the parent process? If false, the outputs are buffered for reading later.
61
+ * @param env Environment variables to pass in. Does not inherit parent process env. Use current process env if not specified.
62
+ * @returns Awaitable promise with the result of the execution.
63
+ */
64
+ export async function executeCommand (command: string, args: string[], inheritStdio: boolean, env?: NodeJS.ProcessEnv): Promise<ExecuteCommandResult> {
59
65
  return await new Promise((resolve, reject) => {
60
- const childProcess = spawn(command, args) //, { stdio: 'inherit' }) -- this pipes stdout & stderr to our output
66
+ const childProcess = spawn(command, args, {
67
+ stdio: inheritStdio ? 'inherit' : undefined,
68
+ env,
69
+ })
61
70
  let stdout: any[] = []
62
71
  let stderr: any[] = []
63
72
 
@@ -71,7 +80,7 @@ export async function executeCommand (command: string, args: string[]): Promise<
71
80
 
72
81
  // If program runs to completion, resolve promise with success
73
82
  childProcess.on('close', (code) => {
74
- resolve({ code, stdout, stderr })
83
+ resolve({ exitCode: code, stdout, stderr })
75
84
  })
76
85
 
77
86
  childProcess.on('error', (err) => {
@@ -83,3 +92,34 @@ export async function executeCommand (command: string, args: string[]): Promise<
83
92
  })
84
93
  })
85
94
  }
95
+
96
+ export function isRunningUnderNpx () {
97
+ // console.log('process.argv:', process.argv)
98
+ const matchBinary = (binaryPath: string, binaryName: string): boolean => {
99
+ const binary = path.basename(binaryPath, path.extname(binaryPath)) // drop path and extension
100
+ return binary === binaryName
101
+ }
102
+
103
+ // Check if 'npx' is the invoked binary (this check is not sufficient as it can also be 'node')
104
+ if (matchBinary(process.argv[0], 'npx')) {
105
+ return true
106
+ }
107
+
108
+ // Check if the script path contains a known npx temporary directory pattern
109
+ const scriptPath = process.argv[1]
110
+ if (scriptPath?.includes(`${path.sep}_npx${path.sep}`)) {
111
+ return true
112
+ }
113
+
114
+ // Check if the environment variable 'npm_execpath' is exactly 'npx'
115
+ if (process.env.npm_execpath && matchBinary(process.env.npm_execpath, 'npx')) {
116
+ return true
117
+ }
118
+
119
+ // Check if the environment variable '_' ends with 'npx' (specific to npx usage)
120
+ if (process.env._ && matchBinary(process.env._, 'npx')) {
121
+ return true
122
+ }
123
+
124
+ return false
125
+ }
package/src/version.ts ADDED
@@ -0,0 +1 @@
1
+ export const PACKAGE_VERSION = '1.5.0'