@@ -2,6 +2,11 @@ import path from 'path';
22import { stat , move , copy , remove , readFile } from 'fs-extra' ;
33import logger from 'jet-logger' ;
44import filesize from 'filesize' ;
5+ import { exec as syncExec } from 'child_process' ;
6+ import util from "util" ;
7+
8+ const exec = util . promisify ( syncExec ) ;
9+
510
611/**
712 * Function to create a working copy of the project at basePath substituting express
@@ -32,20 +37,6 @@ export const replaceExpress = async (basePath: string): Promise<boolean> => {
3237 logger . err ( e ) ;
3338 }
3439
35- // Ensure that expresso-api is installed locally
36- try {
37- const pkg = JSON . parse ( await readFile ( path . resolve ( basePath , 'package.json' ) , 'utf-8' ) ) ;
38- if ( ! Object . keys ( pkg . dependencies ) . includes ( 'expresso-api' ) ) {
39- logger . err (
40- "'expresso-api' is not present in the package.json, please install the package locally with:\n'npm install expresso-api'" ,
41- ) ;
42- return false ;
43- }
44- } catch ( e ) {
45- logger . err ( `Could not find package.json file within '${ basePath } '` ) ;
46- return false ;
47- }
48-
4940 try {
5041 const { size } = await stat ( basePath ) ;
5142 logger . info ( `Creating work copy for ${ basePath } (${ filesize ( size ) } )` ) ;
@@ -59,22 +50,32 @@ export const replaceExpress = async (basePath: string): Promise<boolean> => {
5950 // Move it within the original folder
6051 await move ( path . resolve ( basePath , '../.expresso-runtime' ) , path . resolve ( basePath , '.expresso-runtime' ) ) ;
6152 logger . info ( `Created folder '.expresso-runtime' work copy` ) ;
62- // Install the 'expresso-api' as 'express' within the work copy
53+
54+ const { stdout } = await exec ( "npm list -g | head -1" )
55+ const npmLibFolder = stdout . trim ( )
56+
57+ // Install the global 'expresso-api' as local 'express' within the work copy
6358 await copy (
64- path . resolve ( basePath , 'node_modules/expresso-api' ) ,
59+ path . resolve ( npmLibFolder , 'node_modules/expresso-api' ) ,
6560 path . resolve ( basePath , '.expresso-runtime/node_modules/express' ) ,
6661 {
67- recursive : true ,
68- } ,
69- ) ;
70- // Install the 'express' types within as types for 'expresso-api'
71- await copy (
72- path . resolve ( basePath , 'node_modules/@types/express/index.d.ts' ) ,
73- path . resolve ( basePath , '.expresso-runtime/node_modules/express/dist/lib/index.d.ts' ) ,
74- {
75- overwrite : true ,
76- } ,
77- ) ;
62+ recursive : true
63+ }
64+ )
65+
66+ try {
67+ // Install the 'express' types within as types for 'expresso-api'
68+ await copy (
69+ path . resolve ( basePath , 'node_modules/@types/express/index.d.ts' ) ,
70+ path . resolve ( basePath , '.expresso-runtime/node_modules/express/dist/lib/index.d.ts' ) ,
71+ {
72+ overwrite : true ,
73+ } ,
74+ ) ;
75+ } catch ( e ) {
76+ logger . warn ( "Could not find any 'express' types installed" )
77+ }
78+
7879 // Install the real 'express' within the work copy with a different name to avoid conflicts
7980 await copy (
8081 path . resolve ( basePath , 'node_modules/express' ) ,
@@ -84,6 +85,7 @@ export const replaceExpress = async (basePath: string): Promise<boolean> => {
8485 } ,
8586 ) ;
8687 logger . info ( `Created 'express' proxy within work copy` ) ;
88+
8789 } catch ( e ) {
8890 logger . err ( e ) ;
8991 logger . err ( `Failed to replace 'express' in folder '${ path . resolve ( basePath , '.expresso-runtime' ) } '` ) ;
0 commit comments