Skip to content

Commit 2bc9e5e

Browse files
authored
Merge pull request #7 from FlutterFlow/runfix
Add "--fix" option
2 parents 495b35e + 33c8424 commit 2bc9e5e

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ API access is available only to users with active subscriptions. Visit https://a
1212

1313
## Usage
1414

15-
`flutterflow export-code --project <project id> --dest <output folder> --[no-]include-assets --token <token>`
15+
`flutterflow export-code --project <project id> --dest <output folder> --[no-]include-assets --token <token> --[no-]fix`
1616

1717
Alternatively, instead of passing `--token` you can set `FLUTTERFLOW_API_TOKEN` environment variable.
1818

@@ -25,6 +25,7 @@ Alternatively, instead of passing `--token` you can set `FLUTTERFLOW_API_TOKEN`
2525
| `--dest` | `-d` | [Optional] Output folder |
2626
| `--[no-]include-assets` | None | [Optional] Whether to include media assets. Defaults to `false` |
2727
| `--branch-name` | `-b` | [Optional] Which branch to download. Defaults to `main` |
28+
| `--[no-]fix` | None | [Optional] Whether to run `dart fix` on the downloaded code. Defaults to `false`. |
2829

2930
## Issues
3031

bin/flutterflow_cli.dart

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void main(List<String> args) async {
2828
destinationPath: parsedArguments.command!['dest'],
2929
includeAssets: parsedArguments.command!['include-assets'],
3030
branchName: parsedArguments.command!['branch-name'],
31+
fix: parsedArguments.command!['fix'],
3132
);
3233
}
3334

@@ -46,6 +47,12 @@ ArgResults _parseArgs(List<String> args) {
4647
'for the first time or after updating assets.\n'
4748
'Downloading code without assets is typically much faster.',
4849
defaultsTo: false,
50+
)
51+
..addFlag(
52+
'fix',
53+
negatable: true,
54+
help: 'Run "dart fix" on the downloaded code.',
55+
defaultsTo: false,
4956
);
5057

5158
final parser = ArgParser()
@@ -94,6 +101,7 @@ Future _exportCode({
94101
required String projectId,
95102
required String destinationPath,
96103
required bool includeAssets,
104+
required bool fix,
97105
String? branchName,
98106
}) async {
99107
final endpointUrl = Uri.parse(endpoint);
@@ -112,13 +120,22 @@ Future _exportCode({
112120
final projectFolder = ZipDecoder().decodeBytes(projectZipBytes);
113121
extractArchiveToDisk(projectFolder, destinationPath);
114122

115-
// Download assets
116-
if (includeAssets) {
117-
await _downloadAssets(
118-
client: client,
119-
destinationPath: destinationPath,
120-
assetDescriptions: result['assets'],
121-
);
123+
final postCodeGenerationFutures = <Future>[
124+
if (fix)
125+
_runFix(
126+
destinationPath: destinationPath,
127+
projectFolder: projectFolder,
128+
),
129+
if (includeAssets)
130+
_downloadAssets(
131+
client: client,
132+
destinationPath: destinationPath,
133+
assetDescriptions: result['assets'],
134+
),
135+
];
136+
137+
if (postCodeGenerationFutures.isNotEmpty) {
138+
await Future.wait(postCodeGenerationFutures);
122139
}
123140
} finally {
124141
client.close();
@@ -194,3 +211,45 @@ Future _downloadAssets({
194211
});
195212
await Future.wait(futures);
196213
}
214+
215+
Future _runFix({
216+
required String destinationPath,
217+
required Archive projectFolder,
218+
}) async {
219+
try {
220+
if (projectFolder.isEmpty) {
221+
return;
222+
}
223+
final firstFilePath = projectFolder.files.first.name;
224+
final directory = path_util.split(firstFilePath).first;
225+
226+
final pubGetResult = await Process.run(
227+
'flutter',
228+
['pub', 'get'],
229+
workingDirectory: path_util.join(destinationPath, directory),
230+
runInShell: true,
231+
stdoutEncoding: utf8,
232+
stderrEncoding: utf8,
233+
);
234+
if (pubGetResult.exitCode != 0) {
235+
stderr.write(
236+
'"flutter pub get" failed with code ${pubGetResult.exitCode}, stderr:\n${pubGetResult.stderr}\n');
237+
return;
238+
}
239+
240+
final dartFixResult = await Process.run(
241+
'dart',
242+
['fix', '--apply', directory],
243+
workingDirectory: destinationPath,
244+
runInShell: true,
245+
stdoutEncoding: utf8,
246+
stderrEncoding: utf8,
247+
);
248+
if (dartFixResult.exitCode != 0) {
249+
stderr.write(
250+
'"dart fix" failed with code ${dartFixResult.exitCode}, stderr:\n${dartFixResult.stderr}\n');
251+
}
252+
} catch (e) {
253+
stderr.write('Error running "dart fix": $e\n');
254+
}
255+
}

0 commit comments

Comments
 (0)