1- "use strict" ;
1+ import chalk from "chalk" ;
2+ import { SpawnSyncReturns } from "child_process" ;
3+ import * as spawn from "cross-spawn" ;
4+ import * as inquirer from "inquirer" ;
5+ import * as path from "path" ;
26
3- const inquirer = require ( "inquirer" ) ;
4- const path = require ( "path" ) ;
5- const chalk = require ( "chalk" ) ;
6- const spawn = require ( "cross-spawn" ) ;
7- const List = require ( "@webpack-cli/webpack-scaffold" ) . List ;
8- const processPromise = require ( "@webpack-cli/utils/resolve-packages" )
9- . processPromise ;
7+ import { processPromise } from "@webpack-cli/utils/resolve-packages" ;
8+ import { List } from "@webpack-cli/webpack-scaffold" ;
109
1110/**
1211 *
@@ -16,9 +15,9 @@ const processPromise = require("@webpack-cli/utils/resolve-packages")
1615 * @returns {Void }
1716 */
1817
19- const spawnNPMWithArg = cmd =>
18+ const spawnNPMWithArg = ( cmd : string ) : SpawnSyncReturns < Buffer > =>
2019 spawn . sync ( "npm" , [ "install" , "webpack-dev-server" , cmd ] , {
21- stdio : "inherit"
20+ stdio : "inherit" ,
2221 } ) ;
2322
2423/**
@@ -29,9 +28,9 @@ const spawnNPMWithArg = cmd =>
2928 * @returns {Void }
3029 */
3130
32- const spawnYarnWithArg = cmd =>
31+ const spawnYarnWithArg = ( cmd : string ) : SpawnSyncReturns < Buffer > =>
3332 spawn . sync ( "yarn" , [ "add" , "webpack-dev-server" , cmd ] , {
34- stdio : "inherit"
33+ stdio : "inherit" ,
3534 } ) ;
3635
3736/**
@@ -42,7 +41,7 @@ const spawnYarnWithArg = cmd =>
4241 * @returns {String } string with given path
4342 */
4443
45- const getRootPathModule = dep => path . resolve ( process . cwd ( ) , dep ) ;
44+ const getRootPathModule = ( dep : string ) : string => path . resolve ( process . cwd ( ) , dep ) ;
4645
4746/**
4847 *
@@ -53,39 +52,39 @@ const getRootPathModule = dep => path.resolve(process.cwd(), dep);
5352 */
5453
5554function serve ( ) {
56- let packageJSONPath = getRootPathModule ( "package.json" ) ;
55+ const packageJSONPath : string = getRootPathModule ( "package.json" ) ;
5756 if ( ! packageJSONPath ) {
5857 console . log (
5958 "\n" ,
6059 chalk . red ( "✖ Could not find your package.json file" ) ,
61- "\n"
60+ "\n" ,
6261 ) ;
6362 process . exit ( 1 ) ;
6463 }
65- let packageJSON = require ( packageJSONPath ) ;
64+ const packageJSON : object = require ( packageJSONPath ) ;
6665 /*
6766 * We gotta do this, cause some configs might not have devdep,
6867 * dep or optional dep, so we'd need sanity checks for each
6968 */
70- let hasDevServerDep = packageJSON
71- ? Object . keys ( packageJSON ) . filter ( p => packageJSON [ p ] [ "webpack-dev-server" ] )
69+ const hasDevServerDep : string [ ] = packageJSON
70+ ? Object . keys ( packageJSON ) . filter ( ( p : string ) => packageJSON [ p ] [ "webpack-dev-server" ] )
7271 : [ ] ;
7372
7473 if ( hasDevServerDep . length ) {
75- let WDSPath = getRootPathModule (
76- "node_modules/webpack-dev-server/bin/webpack-dev-server.js"
74+ const WDSPath : string = getRootPathModule (
75+ "node_modules/webpack-dev-server/bin/webpack-dev-server.js" ,
7776 ) ;
7877 if ( ! WDSPath ) {
7978 console . log (
8079 "\n" ,
8180 chalk . red (
82- "✖ Could not find the webpack-dev-server dependency in node_modules root path"
83- )
81+ "✖ Could not find the webpack-dev-server dependency in node_modules root path" ,
82+ ) ,
8483 ) ;
8584 console . log (
8685 chalk . bold . green ( " ✔︎" ) ,
8786 "Try this command:" ,
88- chalk . bold . green ( "rm -rf node_modules && npm install" )
87+ chalk . bold . green ( "rm -rf node_modules && npm install" ) ,
8988 ) ;
9089 process . exit ( 1 ) ;
9190 }
@@ -94,81 +93,86 @@ function serve() {
9493 process . stdout . write (
9594 "\n" +
9695 chalk . bold (
97- "✖ We didn't find any webpack-dev-server dependency in your project,"
96+ "✖ We didn't find any webpack-dev-server dependency in your project," ,
9897 ) +
9998 "\n" +
10099 chalk . bold . green ( " 'webpack serve'" ) +
101100 " " +
102101 chalk . bold ( "requires you to have it installed " ) +
103- "\n\n"
102+ "\n\n" ,
104103 ) ;
105104 return inquirer
106105 . prompt ( [
107106 {
108- type : "confirm" ,
109- name : "confirmDevserver" ,
107+ default : "Y" ,
110108 message : "Do you want to install it? (default: Y)" ,
111- default : "Y"
112- }
109+ name : "confirmDevserver" ,
110+ type : "confirm" ,
111+ } ,
113112 ] )
114- . then ( answer => {
115- if ( answer [ "confirmDevserver" ] ) {
113+ . then ( ( answer : {
114+ confirmDevserver : boolean ,
115+ } ) => {
116+ if ( answer . confirmDevserver ) {
116117 return inquirer
117118 . prompt (
118119 List (
119120 "confirmDepType" ,
120121 "What kind of dependency do you want it to be under? (default: devDependency)" ,
121- [ "devDependency" , "optionalDependency" , "dependency" ]
122- )
122+ [ "devDependency" , "optionalDependency" , "dependency" ] ,
123+ ) ,
123124 )
124- . then ( depTypeAns => {
125- const packager = getRootPathModule ( "package-lock.json" )
125+ . then ( ( depTypeAns : {
126+ confirmDepType : string ;
127+ } ) => {
128+ const packager : string = getRootPathModule ( "package-lock.json" )
126129 ? "npm"
127130 : "yarn" ;
128- let spawnAction ;
129- if ( depTypeAns [ " confirmDepType" ] === "devDependency" ) {
131+ let spawnAction : ( _ ?: void ) => SpawnSyncReturns < Buffer > ;
132+ if ( depTypeAns . confirmDepType === "devDependency" ) {
130133 if ( packager === "yarn" ) {
131- spawnAction = _ => spawnYarnWithArg ( "--dev" ) ;
134+ spawnAction = ( _ ?: void ) => spawnYarnWithArg ( "--dev" ) ;
132135 } else {
133- spawnAction = _ => spawnNPMWithArg ( "--save-dev" ) ;
136+ spawnAction = ( _ ?: void ) => spawnNPMWithArg ( "--save-dev" ) ;
134137 }
135138 }
136- if ( depTypeAns [ " confirmDepType" ] === "dependency" ) {
139+ if ( depTypeAns . confirmDepType === "dependency" ) {
137140 if ( packager === "yarn" ) {
138- spawnAction = _ => spawnYarnWithArg ( " " ) ;
141+ spawnAction = ( _ ?: void ) => spawnYarnWithArg ( " " ) ;
139142 } else {
140- spawnAction = _ => spawnNPMWithArg ( "--save" ) ;
143+ spawnAction = ( _ ?: void ) => spawnNPMWithArg ( "--save" ) ;
141144 }
142145 }
143- if ( depTypeAns [ " confirmDepType" ] === "optionalDependency" ) {
146+ if ( depTypeAns . confirmDepType === "optionalDependency" ) {
144147 if ( packager === "yarn" ) {
145- spawnAction = _ => spawnYarnWithArg ( "--optional" ) ;
148+ spawnAction = ( _ ?: void ) => spawnYarnWithArg ( "--optional" ) ;
146149 } else {
147- spawnAction = _ => spawnNPMWithArg ( "--save-optional" ) ;
150+ spawnAction = ( _ ?: void ) => spawnNPMWithArg ( "--save-optional" ) ;
148151 }
149152 }
150- return processPromise ( spawnAction ( ) ) . then ( _ => {
151- // Recursion doesn't work well with require call being cached
152- delete require . cache [ require . resolve ( packageJSONPath ) ] ;
153- return serve ( ) ;
153+ return processPromise ( spawnAction ( ) )
154+ . then ( ( _ : void ) => {
155+ // Recursion doesn't work well with require call being cached
156+ delete require . cache [ require . resolve ( packageJSONPath ) ] ;
157+ return serve ( ) ;
154158 } ) ;
155159 } ) ;
156160 } else {
157161 console . log ( chalk . bold . red ( "✖ Serve aborted due cancelling" ) ) ;
158162 process . exitCode = 1 ;
159163 }
160164 } )
161- . catch ( err => {
165+ . catch ( ( err : object ) => {
162166 console . log ( chalk . red ( "✖ Serve aborted due to some errors" ) ) ;
163167 console . error ( err ) ;
164168 process . exitCode = 1 ;
165169 } ) ;
166170 }
167171}
168172
169- module . exports = {
170- serve,
173+ export = {
171174 getRootPathModule,
175+ serve,
172176 spawnNPMWithArg,
173- spawnYarnWithArg
177+ spawnYarnWithArg,
174178} ;
0 commit comments