Skip to content

Commit 98117dd

Browse files
small refactors to address comments
1 parent c0b11eb commit 98117dd

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ API access is available only to users with active subscriptions. Visit https://a
3434

3535
## Deploy Firebase
3636

37+
### Prerequisites
38+
39+
`npm` and `firebase-tools` must be installed in order to deploy to Firebase. You can follow the instructions at https://firebase.google.com/docs/cli#install_the_firebase_cli.
40+
3741
### Usage
3842

3943
`flutterflow deploy-firebase --project <project id> --dest <output folder> --token <token>`
@@ -47,7 +51,6 @@ API access is available only to users with active subscriptions. Visit https://a
4751
| ----------- | ----------- | ----------- |
4852
| `--project` | `-p` | [Required or environment variable] Project ID. |
4953
| `--token` | `-t` | [Required or environment variable] API Token. |
50-
| `--dest` | `-d` | [Optional] Output folder. Defaults to the current directory if none is specified. |
5154

5255
## Issues
5356

bin/flutterflow_cli.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ void main(List<String> args) async {
5858
exportAsModule: parsedArguments.command!['as-module'],
5959
);
6060
break;
61-
case 'firebase-deploy':
61+
case 'deploy-firebase':
6262
await firebaseDeploy(
6363
token: token,
6464
projectId: project,
65-
destinationPath: parsedArguments.command!['dest'],
6665
endpoint: endpoint,
6766
);
6867
break;
@@ -112,17 +111,15 @@ ArgResults _parseArgs(List<String> args) {
112111
);
113112

114113
final firebaseDeployCommandParser = ArgParser()
115-
..addOption('project', abbr: 'p', help: 'Project id')
116-
..addOption('dest',
117-
abbr: 'd', help: 'Destination directory', defaultsTo: '.');
114+
..addOption('project', abbr: 'p', help: 'Project id');
118115

119116
final parser = ArgParser()
120117
..addOption('endpoint', abbr: 'e', help: 'Endpoint', hide: true)
121118
..addOption('environment', help: 'Environment', hide: true)
122119
..addOption('token', abbr: 't', help: 'API Token')
123120
..addFlag('help', negatable: false, abbr: 'h', help: 'Help')
124121
..addCommand('export-code', exportCodeCommandParser)
125-
..addCommand('firebase-deploy', firebaseDeployCommandParser);
122+
..addCommand('deploy-firebase', firebaseDeployCommandParser);
126123

127124
late ArgResults parsed;
128125
try {

lib/src/flutterflow_api_client.dart

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,13 @@ Future _runFix({
283283
Future firebaseDeploy({
284284
required String token,
285285
required String projectId,
286-
required String destinationPath,
287286
String endpoint = kDefaultEndpoint,
288287
}) async {
289288
final endpointUrl = Uri.parse(endpoint);
290289
final body = jsonEncode({
291290
'project': {
292291
'path': 'projects/$projectId',
293292
},
294-
'token': token,
295293
});
296294
final result = await _callEndpoint(
297295
client: http.Client(),
@@ -304,47 +302,51 @@ Future firebaseDeploy({
304302
// Download actual code
305303
final projectZipBytes = base64Decode(result['firebase_zip']);
306304
final firebaseProjectId = result['firebase_project_id'];
305+
final tmpFolder =
306+
Directory.systemTemp.createTempSync('${projectId}_$firebaseProjectId');
307307
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';
328310

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.
331313
await Process.run(
332314
'npm',
333315
['install'],
334-
workingDirectory: '$firebaseDir/custom_cloud_functions',
316+
workingDirectory: '$firebaseDir/functions',
335317
runInShell: true,
336318
stdoutEncoding: utf8,
337319
stderrEncoding: utf8,
338320
);
339-
}
321+
final directoriesResult = tmpFolder.listSync(recursive: true);
340322

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+
}
350352
}

0 commit comments

Comments
 (0)