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 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 82 additions & 64 deletions packages/api/exec.mts
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;
Expand All @@ -25,6 +26,7 @@ export type NPMInstallRequestType = BaseExecRequestType & {

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

type SpawnCallRequestType = {
Expand All @@ -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 {
Copy link
Contributor

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 and spawnCallWindows 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.

Copy link
Contributor Author

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!

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;
}

Expand All @@ -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}`)}
Expand All @@ -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,
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
8 changes: 7 additions & 1 deletion packages/api/tsserver/tsservers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ 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';

console.log(`Spawning tsserver with ${npxCmd} in ${options.cwd}`);
aakash-a-dev marked this conversation as resolved.
Show resolved Hide resolved

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
Loading