Skip to content

Commit

Permalink
feat: 🎸 Kill already running app on the same port before launch (#213)
Browse files Browse the repository at this point in the history
* feat: 🎸 Kill already running app on the same port before launch

* chore: 🤖 Fixed package-lock.json
  • Loading branch information
jsimck authored Jun 20, 2022
1 parent 69df0a3 commit 3790164
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"express": "^4.18.1",
"express-static-gzip": "^2.1.7",
"globby": "11.1.0",
"kill-port": "^1.6.1",
"less": "4.1.2",
"less-loader": "^11.0.0",
"messageformat": "2.3.0",
Expand Down
19 changes: 13 additions & 6 deletions packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';

import open from 'better-opn';
import chalk from 'chalk';
import kill from 'kill-port';
import nodemon from 'nodemon';
import webpack from 'webpack';
import { CommandBuilder } from 'yargs';
Expand All @@ -14,7 +15,7 @@ import {
} from '../lib/cli';
import { watchCompiler, handleError } from '../lib/compiler';
import { logger } from '../lib/logger';
import { ImaCliArgs, HandlerFn } from '../types';
import { ImaCliArgs, ImaEnvironment, HandlerFn } from '../types';
import {
cleanup,
createDevServerConfig,
Expand All @@ -29,7 +30,7 @@ import {
* (all changes in server/ folder), to automatically restart the application
* server in case any change is detected.
*/
function startNodemon(args: ImaCliArgs) {
function startNodemon(args: ImaCliArgs, environment: ImaEnvironment) {
let serverHasStarted = false;

nodemon({
Expand All @@ -50,8 +51,7 @@ function startNodemon(args: ImaCliArgs) {
);

if (args.open && !serverHasStarted) {
const imaEnvironment = resolveEnvironment(args.rootDir);
const port = imaEnvironment?.$Server?.port ?? 3001;
const port = environment.$Server.port;
serverHasStarted = true;

try {
Expand Down Expand Up @@ -87,8 +87,9 @@ const dev: HandlerFn = async args => {
// Do cleanup
await cleanup(args);

// Load ima config
// Load ima config & env
const imaConfig = await resolveImaConfig(args);
const environment = resolveEnvironment(args.rootDir);

/**
* Set public env variable which is used to load assets in the SSR error view.
Expand All @@ -97,6 +98,12 @@ const dev: HandlerFn = async args => {
const devServerConfig = createDevServerConfig({ imaConfig, args });
process.env.IMA_CLI_DEV_SERVER_PUBLIC_URL = devServerConfig.publicUrl;

// Kill processes running on the same port
await Promise.all([
kill(environment.$Server.port),
kill(devServerConfig.port),
]).then(data => console.log(data));

// Run preProcess hook on IMA CLI Plugins
await runImaPluginsHook(args, imaConfig, 'preProcess');

Expand Down Expand Up @@ -130,7 +137,7 @@ const dev: HandlerFn = async args => {
]);

// Start nodemon and application server
startNodemon(args);
startNodemon(args, environment);
} catch (error) {
if (args.verbose) {
console.error(error);
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/typings/kill-port.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module 'kill-port' {
export default function (
port: number,
method: 'tcp' | 'udp' = 'tcp'
): Promise<void>;
}

0 comments on commit 3790164

Please sign in to comment.