1
0
Эх сурвалжийг харах

feat(create): Check server port is free before install

Michael Bromley 5 жил өмнө
parent
commit
202f68d090

+ 2 - 1
packages/create/package.json

@@ -42,6 +42,7 @@
     "listr": "^0.14.3",
     "prompts": "^2.0.1",
     "rxjs": "^6.4.0",
-    "semver": "^6.0.0"
+    "semver": "^6.0.0",
+    "tcp-port-used": "^1.0.1"
   }
 }

+ 1 - 0
packages/create/src/constants.ts

@@ -1,4 +1,5 @@
 export const REQUIRED_NODE_VERSION = '>=8.9.0';
+export const SERVER_PORT = 3000;
 /**
  * The TypeScript version needs to pinned because minor versions often
  * introduce breaking changes.

+ 6 - 1
packages/create/src/create-vendure-app.ts

@@ -8,7 +8,7 @@ import os from 'os';
 import path from 'path';
 import { Observable } from 'rxjs';
 
-import { REQUIRED_NODE_VERSION } from './constants';
+import { REQUIRED_NODE_VERSION, SERVER_PORT } from './constants';
 import { gatherCiUserResponses, gatherUserResponses } from './gather-user-responses';
 import {
     checkDbConnection,
@@ -17,6 +17,7 @@ import {
     getDependencies,
     installPackages,
     isSafeToCreateProjectIn,
+    isServerPortInUse,
     shouldUseYarn,
 } from './helpers';
 import { CliLogLevel } from './types';
@@ -61,6 +62,10 @@ async function createApp(
     if (!runPreChecks(name, useNpm)) {
         return;
     }
+    if (await isServerPortInUse()) {
+        console.log(chalk.red(`Port ${SERVER_PORT} is in use. Please make it available and then re-try.`));
+        process.exit(1);
+    }
 
     console.log(`Welcome to @vendure/create v${packageJson.version}!`);
     console.log();

+ 11 - 1
packages/create/src/helpers.ts

@@ -6,7 +6,7 @@ import fs from 'fs-extra';
 import path from 'path';
 import semver from 'semver';
 
-import { TYPESCRIPT_VERSION } from './constants';
+import { SERVER_PORT, TYPESCRIPT_VERSION } from './constants';
 import { CliLogLevel, DbType } from './types';
 
 /**
@@ -348,3 +348,13 @@ function throwConnectionError(err: any) {
 function throwDatabaseDoesNotExist(name: string) {
     throw new Error(`Database "${name}" does not exist. Please create the database and then try again.`);
 }
+
+export async function isServerPortInUse(): Promise<boolean> {
+    const tcpPortUsed = require('tcp-port-used');
+    try {
+        return tcpPortUsed.check(SERVER_PORT);
+    } catch (e) {
+        console.log(chalk.yellow(`Warning: could not determine whether port ${SERVER_PORT} is available`));
+        return false;
+    }
+}

+ 30 - 1
yarn.lock

@@ -6583,6 +6583,13 @@ debug@3.1.0, debug@=3.1.0, debug@~3.1.0:
   dependencies:
     ms "2.0.0"
 
+debug@4.1.0:
+  version "4.1.0"
+  resolved "https://registry.npmjs.org/debug/-/debug-4.1.0.tgz#373687bffa678b38b1cd91f861b63850035ddc87"
+  integrity sha512-heNPJUJIqC+xB6ayLAMHaIrmN9HKa7aQO8MGqKpvCA+uJYVcvR6l5kgdrhRuwPFHU7P5/A1w0BjByPHwpfTDKg==
+  dependencies:
+    ms "^2.1.1"
+
 debug@^3.0.0, debug@^3.1.0, debug@^3.1.1, debug@^3.2.5, debug@^3.2.6:
   version "3.2.6"
   resolved "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
@@ -6656,7 +6663,7 @@ deep-extend@^0.6.0:
   resolved "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz#c4fa7c95404a17a9c3e8ca7e1537312b736330ac"
   integrity sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==
 
-deep-is@~0.1.3:
+deep-is@^0.1.3, deep-is@~0.1.3:
   version "0.1.3"
   resolved "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"
   integrity sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=
@@ -10025,6 +10032,11 @@ is-upper-case@^1.1.0:
   dependencies:
     upper-case "^1.1.0"
 
+is-url@^1.2.2:
+  version "1.2.4"
+  resolved "https://registry.npmjs.org/is-url/-/is-url-1.2.4.tgz#04a4df46d28c4cff3d73d01ff06abeb318a1aa52"
+  integrity sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==
+
 is-utf8@^0.2.0, is-utf8@^0.2.1:
   version "0.2.1"
   resolved "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz#4b0da1442104d1b336340e80797e865cf39f7d72"
@@ -10062,6 +10074,15 @@ is-yarn-global@^0.3.0:
   resolved "https://registry.npmjs.org/is-yarn-global/-/is-yarn-global-0.3.0.tgz#d502d3382590ea3004893746754c89139973e232"
   integrity sha512-VjSeb/lHmkoyd8ryPVIKvOCn4D1koMqY+vqyjjUfc3xyKtP4dYOxM44sZrnqQSzSds3xyOrUTLTC9LVCVgLngw==
 
+is2@2.0.1:
+  version "2.0.1"
+  resolved "https://registry.npmjs.org/is2/-/is2-2.0.1.tgz#8ac355644840921ce435d94f05d3a94634d3481a"
+  integrity sha512-+WaJvnaA7aJySz2q/8sLjMb2Mw14KTplHmSwcSpZ/fWJPkUmqw3YTzSWbPJ7OAwRvdYTWF2Wg+yYJ1AdP5Z8CA==
+  dependencies:
+    deep-is "^0.1.3"
+    ip-regex "^2.1.0"
+    is-url "^1.2.2"
+
 isarray@0.0.1:
   version "0.0.1"
   resolved "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
@@ -16757,6 +16778,14 @@ tar@^5.0.5:
     mkdirp "^0.5.0"
     yallist "^4.0.0"
 
+tcp-port-used@^1.0.1:
+  version "1.0.1"
+  resolved "https://registry.npmjs.org/tcp-port-used/-/tcp-port-used-1.0.1.tgz#46061078e2d38c73979a2c2c12b5a674e6689d70"
+  integrity sha512-rwi5xJeU6utXoEIiMvVBMc9eJ2/ofzB+7nLOdnZuFTmNCLqRiQh2sMG9MqCxHU/69VC/Fwp5dV9306Qd54ll1Q==
+  dependencies:
+    debug "4.1.0"
+    is2 "2.0.1"
+
 temp-dir@^1.0.0:
   version "1.0.0"
   resolved "https://registry.npmjs.org/temp-dir/-/temp-dir-1.0.0.tgz#0a7c0ea26d3a39afa7e0ebea9c1fc0bc4daa011d"