-
-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathexec-utils.ts
More file actions
72 lines (66 loc) · 2.18 KB
/
Copy pathexec-utils.ts
File metadata and controls
72 lines (66 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { execSync as _exec, execFileSync, type ExecSyncOptions } from 'child_process';
import { fileURLToPath } from 'url';
/**
* Utility for executing command synchronously and prints outputs on current console
*/
export function execSync(cmd: string, options?: Omit<ExecSyncOptions, 'env'> & { env?: Record<string, string> }): void {
const { env, ...restOptions } = options ?? {};
const mergedEnv = env ? { ...process.env, ...env } : undefined;
_exec(cmd, {
encoding: 'utf-8',
stdio: options?.stdio ?? 'inherit',
env: mergedEnv,
...restOptions,
});
}
/**
* Utility for running package commands through npx/bunx
*/
export function execPackage(
cmd: string,
options?: Omit<ExecSyncOptions, 'env'> & { env?: Record<string, string> },
): void {
const packageManager = process?.versions?.['bun'] ? 'bunx' : 'npx';
const [executable, ...args] = cmd.split(' ');
execFileSync(packageManager, [executable, ...args], {
encoding: 'utf-8',
stdio: options?.stdio ?? 'inherit',
env: options?.env ? { ...process.env, ...options.env } : undefined,
...options,
});
}
/**
* Utility for running prisma commands
*/
export function execPrisma(args: string, options?: Omit<ExecSyncOptions, 'env'> & { env?: Record<string, string> }) {
let prismaPath: string | undefined;
try {
if (typeof import.meta.resolve === 'function') {
// esm
prismaPath = fileURLToPath(import.meta.resolve('prisma/build/index.js'));
} else {
// cjs
prismaPath = require.resolve('prisma/build/index.js');
}
} catch {
// ignore and fallback
}
const _options = {
...options,
env: {
...options?.env,
PRISMA_HIDE_UPDATE_MESSAGE: '1',
},
};
if (!prismaPath) {
// fallback to npx/bunx execute
execPackage(`prisma ${args}`, _options);
return;
}
execFileSync('node', [prismaPath, ...args.split(' ')], {
encoding: 'utf-8',
stdio: _options?.stdio ?? 'inherit',
env: _options?.env ? { ...process.env, ..._options.env } : undefined,
..._options,
});
}