22/* Based on webpack/bin/webpack.js */
33/* eslint-disable no-console */
44
5- "use strict" ;
5+ import cp from "node:child_process" ;
6+ import nodeModule from "node:module" ;
7+ import path from "node:path" ;
8+ import readLine from "node:readline" ;
9+ import { fileURLToPath , pathToFileURL } from "node:url" ;
10+ import fs from "graceful-fs" ;
611
712/**
813 * @param {string } command process to run
914 * @param {string[] } args command line arguments
1015 * @returns {Promise<void> } promise
1116 */
12- const runCommand = ( command , args ) => {
13- const cp = require ( "node:child_process" ) ;
14-
15- return new Promise ( ( resolve , reject ) => {
17+ const runCommand = ( command , args ) =>
18+ new Promise ( ( resolve , reject ) => {
1619 const executedCommand = cp . spawn ( command , args , {
1720 stdio : "inherit" ,
1821 shell : true ,
@@ -30,7 +33,6 @@ const runCommand = (command, args) => {
3033 }
3134 } ) ;
3235 } ) ;
33- } ;
3436
3537/**
3638 * @param {string } packageName name of the package
@@ -41,10 +43,7 @@ const isInstalled = (packageName) => {
4143 return true ;
4244 }
4345
44- const path = require ( "node:path" ) ;
45- const fs = require ( "graceful-fs" ) ;
46-
47- let dir = __dirname ;
46+ let dir = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
4847
4948 do {
5049 try {
@@ -58,9 +57,9 @@ const isInstalled = (packageName) => {
5857 }
5958 } while ( dir !== ( dir = path . dirname ( dir ) ) ) ;
6059
61- // https://github.com/nodejs/node/blob/v18.9.1 /lib/internal/modules/cjs/loader.js#L1274
60+ // https://github.com/nodejs/node/blob/v22.12.0 /lib/internal/modules/cjs/loader.js#L1818
6261 // @ts -expect-error
63- for ( const internalPath of require ( "node:module" ) . globalPaths ) {
62+ for ( const internalPath of nodeModule . globalPaths ) {
6463 try {
6564 if ( fs . statSync ( path . join ( internalPath , packageName ) ) . isDirectory ( ) ) {
6665 return true ;
@@ -75,29 +74,20 @@ const isInstalled = (packageName) => {
7574
7675/**
7776 * @param {CliOption } cli options
78- * @returns {void }
77+ * @returns {Promise< void> }
7978 */
80- const runCli = ( cli ) => {
79+ const runCli = async ( cli ) => {
8180 if ( cli . preprocess ) {
8281 cli . preprocess ( ) ;
8382 }
8483
85- const path = require ( "node:path" ) ;
84+ const pkgUrl = import . meta. resolve ( `${ cli . package } /package.json` ) ;
85+ const pkgPath = fileURLToPath ( pkgUrl ) ;
86+ const pkg = ( await import ( pkgUrl , { with : { type : "json" } } ) ) . default ;
8687
87- const pkgPath = require . resolve ( ` ${ cli . package } /package.json` ) ;
88+ const binPath = path . resolve ( path . dirname ( pkgPath ) , pkg . bin [ cli . binName ] ) ;
8889
89- const pkg = require ( pkgPath ) ;
90-
91- if ( pkg . type === "module" || / \. m j s / i. test ( pkg . bin [ cli . binName ] ) ) {
92- import ( path . resolve ( path . dirname ( pkgPath ) , pkg . bin [ cli . binName ] ) ) . catch (
93- ( error ) => {
94- console . error ( error ) ;
95- process . exitCode = 1 ;
96- } ,
97- ) ;
98- } else {
99- require ( path . resolve ( path . dirname ( pkgPath ) , pkg . bin [ cli . binName ] ) ) ;
100- }
90+ await import ( pathToFileURL ( binPath ) . href ) ;
10191} ;
10292
10393/**
@@ -123,10 +113,6 @@ const cli = {
123113} ;
124114
125115if ( ! cli . installed ) {
126- const path = require ( "node:path" ) ;
127- const fs = require ( "graceful-fs" ) ;
128- const readLine = require ( "node:readline" ) ;
129-
130116 const notify = `CLI for webpack must be installed.\n ${ cli . name } (${ cli . url } )\n` ;
131117
132118 console . error ( notify ) ;
@@ -187,14 +173,17 @@ if (!cli.installed) {
187173 ) ;
188174
189175 runCommand ( packageManager , [ ...installOptions , cli . package ] )
190- . then ( ( ) => {
191- runCli ( cli ) ;
192- } )
176+ . then ( ( ) => runCli ( cli ) )
193177 . catch ( ( error ) => {
194178 console . error ( error ) ;
195179 process . exitCode = 1 ;
196180 } ) ;
197181 } ) ;
198182} else {
199- runCli ( cli ) ;
183+ try {
184+ await runCli ( cli ) ;
185+ } catch ( error ) {
186+ console . error ( error ) ;
187+ process . exitCode = 1 ;
188+ }
200189}
0 commit comments