Skip to content

Commit eeca37d

Browse files
authored
fix: windows and python compatibility (#786)
1 parent d46ea67 commit eeca37d

File tree

7 files changed

+43
-12
lines changed

7 files changed

+43
-12
lines changed

src/lib/exec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type SpawnOptions, type SpawnOptionsWithoutStdio, spawn } from 'node:ch
22

33
import { normalizeExecutablePath } from './hooks/runtimes/utils.js';
44
import { run } from './outputs.js';
5+
import { cliDebugPrint } from './utils/cliDebugPrint.js';
56

67
const windowsOptions: SpawnOptions = {
78
shell: true,
@@ -14,6 +15,8 @@ const windowsOptions: SpawnOptions = {
1415
const spawnPromised = async (cmd: string, args: string[], opts: SpawnOptionsWithoutStdio) => {
1516
const escapedCommand = normalizeExecutablePath(cmd);
1617

18+
cliDebugPrint('SpawnPromised', { escapedCommand, args, opts });
19+
1720
// NOTE: Pipes stderr, stdout to main process
1821
const childProcess = spawn(escapedCommand, args, {
1922
...opts,

src/lib/hooks/runtimes/javascript.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import which from 'which';
66

77
import type { Runtime } from '../useCwdProject.js';
88
import { normalizeExecutablePath } from './utils.js';
9+
import { cliDebugPrint } from '../../utils/cliDebugPrint.js';
910

1011
const cwdCache = new Map<string, Option<Runtime>>();
1112

@@ -51,6 +52,7 @@ export async function useJavaScriptRuntime(cwd = process.cwd()): Promise<Option<
5152
const cached = cwdCache.get(cwd);
5253

5354
if (cached) {
55+
cliDebugPrint('useJavaScriptRuntime', { cacheHit: true, cwd, runtime: cached.unwrapOr(null) });
5456
return cached;
5557
}
5658

@@ -86,6 +88,8 @@ export async function useJavaScriptRuntime(cwd = process.cwd()): Promise<Option<
8688

8789
cwdCache.set(cwd, some(res));
8890

91+
cliDebugPrint('useJavaScriptRuntime', { cacheHit: false, cwd, runtime: cwdCache.get(cwd)?.unwrap() });
92+
8993
return some(res);
9094
}
9195
} catch {
@@ -95,5 +99,7 @@ export async function useJavaScriptRuntime(cwd = process.cwd()): Promise<Option<
9599

96100
cwdCache.set(cwd, none);
97101

102+
cliDebugPrint('useJavaScriptRuntime', { cacheHit: false, cwd, runtime: null });
103+
98104
return none;
99105
}

src/lib/hooks/runtimes/python.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { access } from 'node:fs/promises';
21
import { platform } from 'node:os';
32
import { join } from 'node:path';
43
import process from 'node:process';
@@ -9,6 +8,7 @@ import which from 'which';
98

109
import type { Runtime } from '../useCwdProject.js';
1110
import { normalizeExecutablePath } from './utils.js';
11+
import { cliDebugPrint } from '../../utils/cliDebugPrint.js';
1212

1313
const cwdCache = new Map<string, Option<Runtime>>();
1414

@@ -44,6 +44,7 @@ export async function usePythonRuntime({
4444
const cached = cwdCache.get(cwd);
4545

4646
if (cached && !force) {
47+
cliDebugPrint('usePythonRuntime', { cacheHit: true, cwd, runtime: cached.unwrapOr(null) });
4748
return cached;
4849
}
4950

@@ -62,8 +63,6 @@ export async function usePythonRuntime({
6263
fullPythonVenvPath = normalizeExecutablePath(fullPythonVenvPath);
6364

6465
try {
65-
await access(fullPythonVenvPath);
66-
6766
const version = await getPythonVersion(fullPythonVenvPath);
6867

6968
if (version) {
@@ -75,10 +74,9 @@ export async function usePythonRuntime({
7574
}),
7675
);
7776

78-
return some({
79-
executablePath: fullPythonVenvPath,
80-
version,
81-
});
77+
cliDebugPrint('usePythonRuntime', { cacheHit: false, cwd, runtime: cwdCache.get(cwd)?.unwrap() });
78+
79+
return cwdCache.get(cwd)!;
8280
}
8381
} catch {
8482
// Ignore errors
@@ -101,10 +99,9 @@ export async function usePythonRuntime({
10199
}),
102100
);
103101

104-
return some({
105-
executablePath: fullPath,
106-
version,
107-
});
102+
cliDebugPrint('usePythonRuntime', { cacheHit: false, cwd, runtime: cwdCache.get(cwd)?.unwrap() });
103+
104+
return cwdCache.get(cwd)!;
108105
}
109106
} catch {
110107
// Continue
@@ -113,5 +110,7 @@ export async function usePythonRuntime({
113110

114111
cwdCache.set(cwd, none);
115112

113+
cliDebugPrint('usePythonRuntime', { cacheHit: false, cwd, runtime: null });
114+
116115
return none;
117116
}

src/lib/hooks/useActorConfig.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { err, ok, type Result } from '@sapphire/result';
77

88
import { ACTOR_SPECIFICATION_VERSION, DEPRECATED_LOCAL_CONFIG_NAME } from '../consts.js';
99
import { error, info, warning } from '../outputs.js';
10+
import { cliDebugPrint } from '../utils/cliDebugPrint.js';
1011
import { confirmAction } from '../utils/confirm.js';
1112
import { getJsonFileContent, getLocalConfigPath } from '../utils.js';
1213

@@ -43,6 +44,7 @@ export async function useActorConfig(
4344
const cached = cwdCache.get(cwd);
4445

4546
if (cached) {
47+
cliDebugPrint('useActorConfig', { cacheHit: true, config: cached });
4648
return ok(cached);
4749
}
4850

@@ -100,6 +102,8 @@ export async function useActorConfig(
100102

101103
cwdCache.set(cwd, { exists: true, migrated, config: config || deprecatedConfig || {} });
102104

105+
cliDebugPrint('useActorConfig', { cacheHit: false, config: cwdCache.get(cwd) });
106+
103107
return ok({ exists: true, migrated, config: config || deprecatedConfig || {} });
104108
}
105109

src/lib/hooks/useCwdProject.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ok, type Result } from '@sapphire/result';
77
import { useJavaScriptRuntime } from './runtimes/javascript.js';
88
import { usePythonRuntime } from './runtimes/python.js';
99
import { ScrapyProjectAnalyzer } from '../projects/scrapy/ScrapyProjectAnalyzer.js';
10+
import { cliDebugPrint } from '../utils/cliDebugPrint.js';
1011

1112
export enum ProjectLanguage {
1213
JavaScript = 0,
@@ -51,6 +52,7 @@ export async function useCwdProject({
5152
const cached = cwdCache.get(cwd);
5253

5354
if (cached) {
55+
cliDebugPrint('useCwdProject', { cacheHit: true, project: cached });
5456
return ok(cached);
5557
}
5658

@@ -124,9 +126,11 @@ export async function useCwdProject({
124126
const maybeErr = await check();
125127

126128
if (maybeErr?.isErr()) {
129+
cliDebugPrint('useCwdProject', { cacheHit: false, error: maybeErr });
127130
return maybeErr;
128131
}
129132

133+
cliDebugPrint('useCwdProject', { cacheHit: false, project });
130134
cwdCache.set(cwd, project);
131135

132136
return ok(project);

src/lib/hooks/useModuleVersion.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { none, some, type Option } from '@sapphire/result';
22
import { execa } from 'execa';
33

44
import { ProjectLanguage, type CwdProject } from './useCwdProject.js';
5+
import { cliDebugPrint } from '../utils/cliDebugPrint.js';
56

67
export interface UseModuleVersionInput {
78
moduleName: string;
@@ -77,6 +78,7 @@ const moduleVersionScripts: Record<string, (mod: string) => string[]> = {
7778

7879
export async function useModuleVersion({ moduleName, project }: UseModuleVersionInput): Promise<Option<string>> {
7980
if (!project.runtime) {
81+
cliDebugPrint('useModuleVersion', { status: 'no_runtime_found', project, moduleName });
8082
return none;
8183
}
8284

@@ -87,12 +89,14 @@ export async function useModuleVersion({ moduleName, project }: UseModuleVersion
8789
} else if (project.type === ProjectLanguage.Python || project.type === ProjectLanguage.Scrapy) {
8890
moduleVersionScriptKey = 'python';
8991
} else {
92+
cliDebugPrint('useModuleVersion', { status: 'unsupported_project_type', project, moduleName });
9093
return none;
9194
}
9295

9396
const args = moduleVersionScripts[moduleVersionScriptKey]?.(moduleName);
9497

9598
if (!args) {
99+
cliDebugPrint('useModuleVersion', { status: 'no_version_script_found', project, moduleName });
96100
return none;
97101
}
98102

@@ -103,11 +107,15 @@ export async function useModuleVersion({ moduleName, project }: UseModuleVersion
103107
});
104108

105109
if (result.stdout.trim() === 'n/a') {
110+
cliDebugPrint('useModuleVersion', { status: 'no_version_found', project, moduleName });
106111
return none;
107112
}
108113

114+
cliDebugPrint('useModuleVersion', { status: 'success', project, moduleName, version: result.stdout.trim() });
115+
109116
return some(result.stdout.trim());
110-
} catch {
117+
} catch (ex) {
118+
cliDebugPrint('useModuleVersion', { status: 'failed_to_run_version_script', project, moduleName, error: ex });
111119
return none;
112120
}
113121
}

src/lib/utils/cliDebugPrint.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import chalk from 'chalk';
2+
3+
export function cliDebugPrint(tag: string, ...args: unknown[]) {
4+
if (process.env.APIFY_CLI_DEBUG) {
5+
console.error(chalk.gray(`[${tag}]`), ...args);
6+
}
7+
}

0 commit comments

Comments
 (0)