-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working for Windows #466
base: main
Are you sure you want to change the base?
Working for Windows #466
Changes from all commits
27d00bd
72ad7a3
1709789
4d870d5
aa864e7
8eb7897
a44dfaf
ad43030
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/* eslint-disable turbo/no-undeclared-env-vars */ | ||
import Path from 'node:path'; | ||
import { spawn } from 'node:child_process'; | ||
import { spawn, execSync } from 'node:child_process'; | ||
|
||
interface NodeError extends Error { | ||
code?: string; | ||
|
@@ -25,6 +26,7 @@ export type NPMInstallRequestType = BaseExecRequestType & { | |
|
||
type NpxRequestType = BaseExecRequestType & { | ||
args: Array<string>; | ||
env?: NodeJS.ProcessEnv; | ||
}; | ||
|
||
type SpawnCallRequestType = { | ||
|
@@ -38,25 +40,56 @@ type SpawnCallRequestType = { | |
onError?: (err: NodeError) => void; | ||
}; | ||
|
||
export function spawnCall(options: SpawnCallRequestType) { | ||
const { cwd, env, command, args, stdout, stderr, onExit, onError } = options; | ||
const child = spawn(command, args, { cwd: cwd, env: env }); | ||
class ExecutableResolver { | ||
private static instance: ExecutableResolver; | ||
private cachedPaths: Map<string, string> = new Map(); | ||
|
||
child.stdout.on('data', stdout); | ||
child.stderr.on('data', stderr); | ||
static getInstance(): ExecutableResolver { | ||
if (!this.instance) { | ||
this.instance = new ExecutableResolver(); | ||
} | ||
return this.instance; | ||
} | ||
|
||
private findExecutablePath(command: string): string { | ||
try { | ||
if (process.platform === 'win32') { | ||
const paths = execSync(`where ${command}`) | ||
.toString() | ||
.trim() | ||
.split('\n') | ||
.map((p) => p.trim()); | ||
return paths.find((p) => p.includes('Program Files')) ?? paths[0] ?? command; | ||
} | ||
return execSync(`which ${command}`).toString().trim(); | ||
} catch { | ||
return command; | ||
} | ||
} | ||
|
||
child.on('error', (err) => { | ||
if (onError) { | ||
onError(err); | ||
} else { | ||
console.error(err); | ||
getPath(command: string): string { | ||
if (!this.cachedPaths.has(command)) { | ||
this.cachedPaths.set(command, this.findExecutablePath(command)); | ||
} | ||
}); | ||
return this.cachedPaths.get(command)!; | ||
} | ||
} | ||
|
||
export function spawnCall(options: SpawnCallRequestType) { | ||
const { cwd, env, command, args, stdout, stderr, onExit, onError } = options; | ||
|
||
child.on('exit', (code, signal) => { | ||
onExit(code, signal); | ||
const child = spawn(command, args, { | ||
cwd, | ||
env, | ||
windowsVerbatimArguments: process.platform === 'win32', | ||
shell: process.platform === 'win32', | ||
}); | ||
|
||
child.stdout.on('data', stdout); | ||
child.stderr.on('data', stderr); | ||
child.on('error', onError || console.error); | ||
child.on('exit', onExit); | ||
|
||
return child; | ||
} | ||
|
||
|
@@ -66,67 +99,56 @@ export function spawnCall(options: SpawnCallRequestType) { | |
* Example: | ||
* | ||
* node({ | ||
* cwd: '/Users/ben/.srcbook/30v2av4eee17m59dg2c29758to', | ||
* env: {FOO_ENV_VAR: 'foooooooo'}, | ||
* entry: '/Users/ben/.srcbook/30v2av4eee17m59dg2c29758to/src/foo.js', | ||
* cwd: '/path/to/project', | ||
* env: {FOO_ENV_VAR: 'value'}, | ||
* entry: '/path/to/file.js', | ||
* stdout(data) {console.log(data.toString('utf8'))}, | ||
* stderr(data) {console.error(data.toString('utf8'))}, | ||
* onExit(code) {console.log(`Exit code: ${code}`)} | ||
* }); | ||
* | ||
*/ | ||
export function node(options: NodeRequestType) { | ||
const { cwd, env, entry, stdout, stderr, onExit } = options; | ||
|
||
return spawnCall({ | ||
command: 'node', | ||
export const node = ({ cwd, env, entry, ...rest }: NodeRequestType) => | ||
spawnCall({ | ||
command: ExecutableResolver.getInstance().getPath('node'), | ||
cwd, | ||
args: [entry], | ||
stdout, | ||
stderr, | ||
onExit, | ||
env: { ...process.env, ...env }, | ||
...rest, | ||
}); | ||
} | ||
|
||
/** | ||
* Execute a TypeScript file using tsx. | ||
* | ||
* Example: | ||
* | ||
* tsx({ | ||
* cwd: '/Users/ben/.srcbook/30v2av4eee17m59dg2c29758to', | ||
* env: {FOO_ENV_VAR: 'foooooooo'}, | ||
* entry: '/Users/ben/.srcbook/30v2av4eee17m59dg2c29758to/src/foo.ts', | ||
* cwd: '/path/to/project', | ||
* env: {FOO_ENV_VAR: 'value'}, | ||
* entry: '/path/to/file.ts', | ||
* stdout(data) {console.log(data.toString('utf8'))}, | ||
* stderr(data) {console.error(data.toString('utf8'))}, | ||
* onExit(code) {console.log(`Exit code: ${code}`)} | ||
* }); | ||
* | ||
*/ | ||
export function tsx(options: NodeRequestType) { | ||
const { cwd, env, entry, stdout, stderr, onExit } = options; | ||
|
||
// We are making an assumption about `tsx` being the tool of choice | ||
// for running TypeScript, as well as where it's located on the file system. | ||
return spawnCall({ | ||
command: Path.join(cwd, 'node_modules', '.bin', 'tsx'), | ||
export const tsx = ({ cwd, env, entry, ...rest }: NodeRequestType) => | ||
spawnCall({ | ||
command: | ||
process.platform === 'win32' | ||
? Path.join(cwd, 'node_modules', '.bin', 'tsx.cmd') | ||
: Path.join(cwd, 'node_modules', '.bin', 'tsx'), | ||
cwd, | ||
args: [entry], | ||
stdout, | ||
stderr, | ||
onExit, | ||
env: { ...process.env, ...env }, | ||
...rest, | ||
}); | ||
} | ||
|
||
/** | ||
* Run npm install. | ||
* | ||
* Install all packages: | ||
* | ||
* npmInstall({ | ||
* cwd: '/Users/ben/.srcbook/foo', | ||
* cwd: '/path/to/project', | ||
* stdout(data) {console.log(data.toString('utf8'))}, | ||
* stderr(data) {console.error(data.toString('utf8'))}, | ||
* onExit(code) {console.log(`Exit code: ${code}`)} | ||
|
@@ -135,38 +157,34 @@ export function tsx(options: NodeRequestType) { | |
* Install a specific package: | ||
* | ||
* npmInstall({ | ||
* cwd: '/Users/ben/.srcbook/foo', | ||
* package: 'marked', | ||
* cwd: '/path/to/project', | ||
* packages: ['lodash'], | ||
* stdout(data) {console.log(data.toString('utf8'))}, | ||
* stderr(data) {console.error(data.toString('utf8'))}, | ||
* onExit(code) {console.log(`Exit code: ${code}`)} | ||
* }); | ||
* | ||
*/ | ||
export function npmInstall(options: NPMInstallRequestType) { | ||
const { cwd, stdout, stderr, onExit } = options; | ||
const args = options.packages | ||
? ['install', '--include=dev', ...(options.args || []), ...options.packages] | ||
: ['install', '--include=dev', ...(options.args || [])]; | ||
|
||
return spawnCall({ | ||
export const npmInstall = ({ cwd, packages = [], args = [], ...rest }: NPMInstallRequestType) => | ||
spawnCall({ | ||
command: 'npm', | ||
cwd, | ||
args, | ||
stdout, | ||
stderr, | ||
onExit, | ||
args: ['install', '--include=dev', ...args, ...packages], | ||
env: process.env, | ||
...rest, | ||
}); | ||
} | ||
|
||
/** | ||
* Run vite. | ||
*/ | ||
export function vite(options: NpxRequestType) { | ||
return spawnCall({ | ||
...options, | ||
command: Path.join(options.cwd, 'node_modules', '.bin', 'vite'), | ||
env: process.env, | ||
export const vite = ({ cwd, args = [], env = process.env, ...rest }: NpxRequestType) => | ||
spawnCall({ | ||
command: | ||
process.platform === 'win32' ? 'npx.cmd' : Path.join(cwd, 'node_modules', '.bin', 'vite'), | ||
cwd, | ||
args: process.platform === 'win32' ? ['vite', ...args] : args, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So on windows, we need to call the command and then add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have to hard code it, only then it's running otherwise vite is not getting started, atleast for me it havn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alien, Yes! While Vite code which was already there didn't started vite and thrown error, require to hard code this in the following way |
||
env: { | ||
...env, | ||
FORCE_COLOR: '1', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ..env is for copying all the object, for creating in the following object although I added FORCE_COLOR: '1' Because it was getting difficult to find the response so for making it shine I added, I will remove it if you want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For just making sure I can track that this log can easily noticeable while I was debugging, if you want I will remove FORCE_COLOR it was for me to make sure that it is logging |
||
}, | ||
...rest, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit scared of this change. It's a pretty big refactor of an important part of the app.
I'm wondering if we can remove the new class implementation, and instead have some simple function based routing.
So first run a function
spawnCall
which will call:spawnCallUnix
andspawnCallWindows
based on the process.platform switch you do line 56.That way the mac / unix code can stay the exact same which I think is safeer, and windows can have it's own code path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me change it then!