@@ -6,46 +6,49 @@ import * as path from "path";
6
6
7
7
import { processPromise } from "@webpack-cli/utils/resolve-packages" ;
8
8
9
+ interface Commands {
10
+ dependency : string [ ] ;
11
+ devDependency : string [ ] ;
12
+ optionalDependency : string [ ] ;
13
+ }
14
+
15
+ interface PackageManagerConfig {
16
+ [ key : string ] : Commands ;
17
+ }
18
+
19
+ const pmConfig : PackageManagerConfig = {
20
+ npm : {
21
+ dependency : [ "install" , "--save" ] ,
22
+ devDependency : [ "install" , "--save-dev" ] ,
23
+ optionalDependency : [ "install" , "--save-optional" ]
24
+ } ,
25
+ yarn : {
26
+ dependency : [ "add" ] ,
27
+ devDependency : [ "add" , "-D" ] ,
28
+ optionalDependency : [ "add" , "--optional" ]
29
+ }
30
+ } ;
31
+
9
32
/**
10
33
*
11
- * Installs WDS using NPM with --save --dev etc
34
+ * Installs WDS using the respective package manager with corresponding commands
12
35
*
13
- * @param {Object } cmd - arg to spawn with
14
- * @returns {Void }
15
- */
16
-
17
- /**
36
+ * @param {String } pm - package manager to be used
37
+ * @param {String } cmd - arg to spawn with
38
+ * @returns {Function } spawn - installs WDS
18
39
*
19
- * Installs WDS using Yarn with add etc
40
+ * The dependency installation commands for the
41
+ * respective package manager is available as
42
+ * nested objects within pmConfig
20
43
*
21
- * @param {Object } cmd - arg to spawn with
22
- * @returns {Void }
44
+ * We gonna extract the root installation command
45
+ * and rest of the flags from pmConfig object
46
+ * by means of array destructuring
23
47
*/
24
48
25
- interface ConfigType {
26
- installCmd : string ;
27
- dependency : string ;
28
- devDependency : string ;
29
- optionalDependency : string ;
30
- }
31
-
32
- const npmConfig : ConfigType = {
33
- installCmd : "install" ,
34
- dependency : "--save" ,
35
- devDependency : "--save-dev" ,
36
- optionalDependency : "--save-optional"
37
- } ;
38
-
39
- const yarnConfig : ConfigType = {
40
- installCmd : "add" ,
41
- dependency : " " ,
42
- devDependency : "--save" ,
43
- optionalDependency : "--optional"
44
- } ;
45
-
46
49
const spawnWithArg = ( pm : string , cmd : string ) : SpawnSyncReturns < Buffer > => {
47
- const pmConfig : ConfigType = pm === "npm" ? npmConfig : yarnConfig ;
48
- const options : string [ ] = [ pmConfig . installCmd , "webpack-dev-server" , pmConfig [ cmd ] ] ;
50
+ const [ installCmd , ... flags ] = pmConfig [ pm ] [ cmd ] ;
51
+ const options : string [ ] = [ installCmd , "webpack-dev-server" , ... flags ] ;
49
52
return spawn . sync ( pm , options , { stdio : "inherit" } ) ;
50
53
} ;
51
54
0 commit comments