Skip to content

Commit

Permalink
fix(bit-server-cli), skip bit-server for init command and when port f…
Browse files Browse the repository at this point in the history
…ile is not there (#9077)
  • Loading branch information
davidfirst authored Jul 31, 2024
1 parent 01be0d1 commit fe2db45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 4 additions & 2 deletions scopes/harmony/bit/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ if (process.argv.includes('--get-yargs-completions')) {
}

if (shouldUseBitServer()) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
new ServerCommander().execute();
new ServerCommander().execute().catch(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
initApp();
});
} else {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
initApp();
Expand Down
20 changes: 16 additions & 4 deletions scopes/harmony/bit/server-commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import { findScopePath } from '@teambit/scope.modules.find-scope-path';
import chalk from 'chalk';
import loader from '@teambit/legacy/dist/cli/loader';

export class ServerPortFileNotFound extends Error {
constructor(filePath: string) {
super(`server port file not found at ${filePath}`);
}
}
export class ServerNotFound extends Error {
constructor(port: number) {
super(`bit server is not running on port ${port}`);
}
}

export class ServerCommander {
private isJson = false;
async execute() {
Expand All @@ -28,6 +39,7 @@ export class ServerCommander {

process.exit(0);
} catch (err: any) {
if (err instanceof ServerPortFileNotFound || err instanceof ServerNotFound) throw err;
// eslint-disable-next-line no-console
console.error(chalk.red(err.message));
process.exit(1);
Expand Down Expand Up @@ -64,7 +76,7 @@ export class ServerCommander {
});
} catch (err: any) {
if (err.code === 'ECONNREFUSED') {
throw new Error(`bit server is not running on port ${port}`);
throw new ServerNotFound(port);
}
throw new Error(`failed to run command "${cmd}" on the server. ${err.message}`);
}
Expand Down Expand Up @@ -118,7 +130,7 @@ export class ServerCommander {
return parseInt(fileContent, 10);
} catch (err: any) {
if (err.code === 'ENOENT') {
throw new Error(`server port file not found at ${filePath}`);
throw new ServerPortFileNotFound(filePath);
}
throw err;
}
Expand All @@ -134,12 +146,12 @@ export class ServerCommander {
}

export function shouldUseBitServer() {
const longRunningCommands = ['start', 'run', 'watch'];
const commandsToSkip = ['start', 'run', 'watch', 'init'];
return (
process.env.BIT_CLI_SERVER &&
!process.argv.includes('--help') &&
!process.argv.includes('-h') &&
process.argv.length > 2 && // if it has no args, it shows the help
!longRunningCommands.includes(process.argv[2])
!commandsToSkip.includes(process.argv[2])
);
}

0 comments on commit fe2db45

Please sign in to comment.