12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- import { basename , join } from 'path' ;
15
+ import { basename , join , relative } from 'path' ;
16
16
import { exit } from 'process' ;
17
- import { promises as fs } from 'fs' ;
17
+ import { writeFile , copyFile , stat } from 'fs/promises ' ;
18
18
19
19
import { build } from './frameworks' ;
20
- import { defaultFirebaseToolsOptions , DEFAULT_REGION , shortSiteName , spawn } from './utils' ;
20
+ import { defaultFirebaseToolsOptions , DEFAULT_REGION , exec , shortSiteName , spawn } from './utils' ;
21
21
import { getFirebaseTools , normalizedHostingConfigs , getInquirer , needProjectId } from './firebase' ;
22
-
23
- const { writeFile, copyFile } = fs ;
22
+ import { spawnSync } from 'child_process' ;
24
23
25
24
const NODE_VERSION = parseInt ( process . versions . node , 10 ) . toString ( ) ;
26
25
const FIREBASE_ADMIN_VERSION = '__FIREBASE_ADMIN_VERSION__' ;
@@ -123,13 +122,22 @@ export const prepare = async (targetNames: string[], context: any, options: any)
123
122
packageJson . main = 'server.js' ;
124
123
delete packageJson . devDependencies ;
125
124
packageJson . dependencies ||= { } ;
126
- if ( FIREBASE_FRAMEWORKS_VERSION . startsWith ( 'file:' ) ) {
127
- const file = FIREBASE_FRAMEWORKS_VERSION . split ( ':' ) [ 1 ] ;
128
- const filename = basename ( file ) ;
129
- await copyFile ( file , join ( functionsDist , filename ) ) ;
130
- packageJson . dependencies [ 'firebase-frameworks' ] = `file:${ filename } ` ;
131
- } else {
132
- packageJson . dependencies [ 'firebase-frameworks' ] = FIREBASE_FRAMEWORKS_VERSION ;
125
+ packageJson . dependencies [ 'firebase-frameworks' ] = FIREBASE_FRAMEWORKS_VERSION ;
126
+ for ( const [ name , version ] of Object . entries ( packageJson . dependencies as Record < string , string > ) ) {
127
+ if ( version . startsWith ( 'file:' ) ) {
128
+ const path = version . split ( ':' ) [ 1 ] ;
129
+ const stats = await stat ( path ) ;
130
+ if ( stats . isDirectory ( ) ) {
131
+ const result = spawnSync ( 'npm' , [ 'pack' , relative ( functionsDist , path ) ] , { cwd : functionsDist } ) ;
132
+ if ( ! result . stdout ) continue ;
133
+ const filename = result . stdout . toString ( ) . trim ( ) ;
134
+ packageJson . dependencies [ name ] = `file:${ filename } ` ;
135
+ } else {
136
+ const filename = basename ( path ) ;
137
+ await copyFile ( path , join ( functionsDist , filename ) ) ;
138
+ packageJson . dependencies [ name ] = `file:${ filename } ` ;
139
+ }
140
+ }
133
141
}
134
142
// TODO test these with semver, error if already set out of range
135
143
packageJson . dependencies [ 'firebase-admin' ] ||= FIREBASE_ADMIN_VERSION ;
@@ -149,11 +157,10 @@ export const prepare = async (targetNames: string[], context: any, options: any)
149
157
// Welp, that didn't work, since firebase-tools checks that they have a minimum firebase-frameworks SDK installed...
150
158
// TODO explore symlinks and ways to make this faster, better, stronger
151
159
// log to firebase-tools
152
- await spawn ( 'npm' , [ 'i' , '--only' , 'production' , '--no-audit' , '--no-fund' , '--silent' ] , { cwd : functionsDist } , stdoutChunk => {
153
- console . log ( stdoutChunk . toString ( ) ) ;
154
- } , errChunk => {
155
- console . error ( errChunk . toString ( ) ) ;
156
- } ) ;
160
+ const npmInstall = spawnSync ( 'npm' , [ 'i' , '--only' , 'production' , '--no-audit' , '--no-fund' , '--silent' ] , { cwd : functionsDist } ) ;
161
+ if ( npmInstall . status ) {
162
+ console . error ( npmInstall . output . toString ( ) ) ;
163
+ }
157
164
158
165
// TODO allow configuration of the Cloud Function
159
166
await writeFile ( join ( functionsDist , 'settings.js' ) , `exports.HTTPS_OPTIONS = {};
0 commit comments