-
Notifications
You must be signed in to change notification settings - Fork 269
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
9277dc8
efb3372
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,3 +1,4 @@ | ||
/* eslint-disable turbo/no-undeclared-env-vars */ | ||
import Path from 'node:path'; | ||
import { spawn } from 'node:child_process'; | ||
|
||
|
@@ -25,6 +26,7 @@ export type NPMInstallRequestType = BaseExecRequestType & { | |
|
||
type NpxRequestType = BaseExecRequestType & { | ||
args: Array<string>; | ||
env?: NodeJS.ProcessEnv; | ||
}; | ||
|
||
type SpawnCallRequestType = { | ||
|
@@ -38,25 +40,54 @@ type SpawnCallRequestType = { | |
onError?: (err: NodeError) => void; | ||
}; | ||
|
||
/** | ||
* Main spawnCall function that routes to platform-specific implementations. | ||
*/ | ||
export function spawnCall(options: SpawnCallRequestType) { | ||
if (process.platform === 'win32') { | ||
return spawnCallWindows(options); | ||
} else { | ||
return spawnCallUnix(options); | ||
} | ||
} | ||
|
||
/** | ||
* Unix-specific implementation of spawnCall. | ||
*/ | ||
function spawnCallUnix(options: SpawnCallRequestType) { | ||
const { cwd, env, command, args, stdout, stderr, onExit, onError } = options; | ||
const child = spawn(command, args, { cwd: cwd, env: env }); | ||
|
||
const child = spawn(command, args, { | ||
cwd, | ||
env, | ||
}); | ||
|
||
child.stdout.on('data', stdout); | ||
child.stderr.on('data', stderr); | ||
child.on('error', onError || console.error); | ||
child.on('exit', onExit); | ||
|
||
child.on('error', (err) => { | ||
if (onError) { | ||
onError(err); | ||
} else { | ||
console.error(err); | ||
} | ||
}); | ||
return child; | ||
} | ||
|
||
/** | ||
* Windows-specific implementation of spawnCall. | ||
*/ | ||
function spawnCallWindows(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: true, | ||
shell: true, | ||
}); | ||
|
||
child.stdout.on('data', stdout); | ||
child.stderr.on('data', stderr); | ||
child.on('error', onError || console.error); | ||
child.on('exit', onExit); | ||
|
||
return child; | ||
} | ||
|
||
|
@@ -66,67 +97,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({ | ||
export const node = ({ cwd, env, entry, ...rest }: NodeRequestType) => | ||
spawnCall({ | ||
command: '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 +155,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, | ||
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.
So on windows, we need to call the command and then add
vite
as an arg? Feels like this isvite vite
. SurprisingThere 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.
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 comment
The 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