@@ -283,15 +283,13 @@ Future _runFix({
283
283
Future firebaseDeploy ({
284
284
required String token,
285
285
required String projectId,
286
- required String destinationPath,
287
286
String endpoint = kDefaultEndpoint,
288
287
}) async {
289
288
final endpointUrl = Uri .parse (endpoint);
290
289
final body = jsonEncode ({
291
290
'project' : {
292
291
'path' : 'projects/$projectId ' ,
293
292
},
294
- 'token' : token,
295
293
});
296
294
final result = await _callEndpoint (
297
295
client: http.Client (),
@@ -304,47 +302,51 @@ Future firebaseDeploy({
304
302
// Download actual code
305
303
final projectZipBytes = base64Decode (result['firebase_zip' ]);
306
304
final firebaseProjectId = result['firebase_project_id' ];
305
+ final tmpFolder =
306
+ Directory .systemTemp.createTempSync ('${projectId }_$firebaseProjectId ' );
307
307
final projectFolder = ZipDecoder ().decodeBytes (projectZipBytes);
308
- extractArchiveToCurrentDirectory (projectFolder, destinationPath);
309
- final firebaseDir = '$destinationPath /firebase' ;
310
-
311
- // Install required modules for deployment.
312
- await Process .run (
313
- 'npm' ,
314
- ['install' ],
315
- workingDirectory: '$firebaseDir /functions' ,
316
- runInShell: true ,
317
- stdoutEncoding: utf8,
318
- stderrEncoding: utf8,
319
- );
320
- final directoriesResult = await Process .run (
321
- 'ls' ,
322
- [],
323
- workingDirectory: firebaseDir,
324
- runInShell: true ,
325
- stdoutEncoding: utf8,
326
- stderrEncoding: utf8,
327
- );
308
+ extractArchiveToCurrentDirectory (projectFolder, tmpFolder.path);
309
+ final firebaseDir = '${tmpFolder .path }/firebase' ;
328
310
329
- // This directory only exists if there were custom cloud functions.
330
- if (directoriesResult.stdout. contains ( 'custom_cloud_functions' )) {
311
+ try {
312
+ // Install required modules for deployment.
331
313
await Process .run (
332
314
'npm' ,
333
315
['install' ],
334
- workingDirectory: '$firebaseDir /custom_cloud_functions ' ,
316
+ workingDirectory: '$firebaseDir /functions ' ,
335
317
runInShell: true ,
336
318
stdoutEncoding: utf8,
337
319
stderrEncoding: utf8,
338
320
);
339
- }
321
+ final directoriesResult = tmpFolder. listSync (recursive : true );
340
322
341
- final deployProcess = await Process .start (
342
- 'firebase' ,
343
- ['deploy' , '--project' , firebaseProjectId],
344
- workingDirectory: firebaseDir,
345
- runInShell: true ,
346
- );
347
- // There may be a need for the user to interactively provide inputs.
348
- deployProcess.stdout.transform (utf8.decoder).forEach (print);
349
- deployProcess.stdin.addStream (stdin);
323
+ // This directory only exists if there were custom cloud functions.
324
+ if (directoriesResult.map ((f) => f.path).any (
325
+ (path) => path.startsWith ('$firebaseDir /custom_cloud_functions' ))) {
326
+ await Process .run (
327
+ 'npm' ,
328
+ ['install' ],
329
+ workingDirectory: '$firebaseDir /custom_cloud_functions' ,
330
+ runInShell: true ,
331
+ stdoutEncoding: utf8,
332
+ stderrEncoding: utf8,
333
+ );
334
+ }
335
+
336
+ final deployProcess = await Process .start (
337
+ 'firebase' ,
338
+ ['deploy' , '--project' , firebaseProjectId],
339
+ workingDirectory: firebaseDir,
340
+ runInShell: true ,
341
+ );
342
+ // There may be a need for the user to interactively provide inputs.
343
+ deployProcess.stdout.transform (utf8.decoder).forEach (print);
344
+ deployProcess.stdin.addStream (stdin);
345
+ final exitCode = await deployProcess.exitCode;
346
+ if (exitCode != 0 ) {
347
+ stderr.write ('Failed to deploy to Firebase.\n ' );
348
+ }
349
+ } finally {
350
+ tmpFolder.deleteSync (recursive: true );
351
+ }
350
352
}
0 commit comments