Skip to content
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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
134 changes: 75 additions & 59 deletions packages/api/exec.mts
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';

Expand Down Expand Up @@ -25,6 +26,7 @@ export type NPMInstallRequestType = BaseExecRequestType & {

type NpxRequestType = BaseExecRequestType & {
args: Array<string>;
env?: NodeJS.ProcessEnv;
};

type SpawnCallRequestType = {
Expand All @@ -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;
}

Expand All @@ -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}`)}
Expand All @@ -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,
Copy link
Contributor

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 is vite vite. Surprising

Copy link
Contributor Author

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

Copy link
Contributor Author

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

env: {
...env,
FORCE_COLOR: '1',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
});
}
1 change: 0 additions & 1 deletion packages/api/server/channels/app.mts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ async function previewStart(
},
onExit: (code) => {
deleteAppProcess(app.externalId, 'vite:server');

wss.broadcast(`app:${app.externalId}`, 'preview:status', {
url: null,
status: 'stopped',
Expand Down
6 changes: 5 additions & 1 deletion packages/api/tsserver/tsservers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ export class TsServers {
// created, the dependencies are not installed and thus this will
// shut down immediately. Make sure that we handle this case after
// package.json has finished installing its deps.
const child = spawn('npx', ['tsserver'], {

const npxCmd = process.platform === 'win32' ? 'npx.cmd' : 'npx';

const child = spawn(npxCmd, ['tsserver'], {
cwd: options.cwd,
shell: process.platform === 'win32',
});

const server = new TsServer(child);
Expand Down
4 changes: 2 additions & 2 deletions packages/web/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type {import('tailwindcss').Config} */
const plugin = require('tailwindcss/plugin');
const path = require('path');
import plugin from 'tailwindcss/plugin';
import path from 'path';

module.exports = {
darkMode: ['class'],
Expand Down