Skip to content

Commit bad3343

Browse files
committed
Wrap main() so it can be tested in an easier way
The idea is to test exporting code using Dart test runner, it will make it easier to write cross platform integration tests for the various command line options.
1 parent 4891c69 commit bad3343

File tree

2 files changed

+185
-180
lines changed

2 files changed

+185
-180
lines changed

bin/flutterflow_cli.dart

Lines changed: 2 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,5 @@
1-
import 'dart:io';
2-
3-
import 'package:args/args.dart';
4-
import 'package:flutterflow_cli/src/flutterflow_api_client.dart';
5-
6-
const kDefaultEndpoint = 'https://api.flutterflow.io/v1';
1+
import 'package:flutterflow_cli/src/flutterflow_main.dart';
72

83
void main(List<String> args) async {
9-
final parsedArguments = _parseArgs(args);
10-
11-
final token =
12-
parsedArguments['token'] ?? Platform.environment['FLUTTERFLOW_API_TOKEN'];
13-
14-
final project = parsedArguments.command!['project'] ??
15-
Platform.environment['FLUTTERFLOW_PROJECT'];
16-
17-
if (project == null || project.isEmpty) {
18-
stderr.write(
19-
'Either --project option or FLUTTERFLOW_PROJECT environment variable must be set.\n');
20-
exit(1);
21-
}
22-
23-
if (parsedArguments['endpoint'] != null &&
24-
parsedArguments['environment'] != null) {
25-
stderr.write(
26-
'Only one of --endpoint and --environment options can be set.\n');
27-
exit(1);
28-
}
29-
30-
if (token?.isEmpty ?? true) {
31-
stderr.write(
32-
'Either --token option or FLUTTERFLOW_API_TOKEN environment variable must be set.\n');
33-
exit(1);
34-
}
35-
36-
late String endpoint;
37-
if (parsedArguments['endpoint'] != null) {
38-
endpoint = parsedArguments['endpoint'];
39-
} else if (parsedArguments['environment'] != null) {
40-
endpoint =
41-
"https://api-${parsedArguments['environment']}.flutterflow.io/v1";
42-
} else {
43-
endpoint = Platform.environment['FLUTTERFLOW_ENDPOINT'] ?? kDefaultEndpoint;
44-
}
45-
46-
try {
47-
switch (parsedArguments.command?.name) {
48-
case 'export-code':
49-
await exportCode(
50-
token: token,
51-
endpoint: endpoint,
52-
projectId: project,
53-
destinationPath: parsedArguments.command!['dest'],
54-
includeAssets: parsedArguments.command!['include-assets'],
55-
branchName: parsedArguments.command!['branch-name'],
56-
commitHash: parsedArguments.command!['commit-hash'],
57-
unzipToParentFolder: parsedArguments.command!['parent-folder'],
58-
fix: parsedArguments.command!['fix'],
59-
exportAsModule: parsedArguments.command!['as-module'],
60-
exportAsDebug: parsedArguments.command!['as-debug'],
61-
environmentName: parsedArguments.command!['project-environment'],
62-
);
63-
break;
64-
case 'deploy-firebase':
65-
await firebaseDeploy(
66-
token: token,
67-
projectId: project,
68-
appendRules: parsedArguments.command!['append-rules'],
69-
endpoint: endpoint,
70-
);
71-
break;
72-
default:
73-
}
74-
} catch (_) {
75-
exit(1);
76-
}
77-
}
78-
79-
ArgResults _parseArgs(List<String> args) {
80-
final exportCodeCommandParser = ArgParser()
81-
..addOption('project', abbr: 'p', help: 'Project id')
82-
..addOption(
83-
'dest',
84-
abbr: 'd',
85-
help: 'Destination directory',
86-
defaultsTo: '.',
87-
)
88-
..addOption(
89-
'branch-name',
90-
abbr: 'b',
91-
help: '(Optional) Specify a branch name',
92-
)
93-
..addOption(
94-
'commit-hash',
95-
abbr: 'c',
96-
help: '(Optional) Specify a commit hash',
97-
)
98-
..addFlag(
99-
'include-assets',
100-
negatable: true,
101-
help: 'Include assets. By default, assets are not included.\n'
102-
'We recommend setting this flag only when calling this command '
103-
'for the first time or after updating assets.\n'
104-
'Downloading code without assets is typically much faster.',
105-
defaultsTo: false,
106-
)
107-
..addFlag(
108-
'fix',
109-
negatable: true,
110-
help: 'Run "dart fix" on the downloaded code.',
111-
defaultsTo: false,
112-
)
113-
..addFlag(
114-
'parent-folder',
115-
negatable: true,
116-
help: 'Download into a sub-folder. By default, project is downloaded \n'
117-
'into a folder named <project>.\nSetting this flag to false will '
118-
'download all project code directly into the specified directory, '
119-
'or the current directory if --dest is not set.',
120-
defaultsTo: true,
121-
)
122-
..addFlag(
123-
'as-module',
124-
negatable: true,
125-
help: 'Generate the project as a Flutter module.',
126-
defaultsTo: false,
127-
)
128-
..addFlag(
129-
'as-debug',
130-
negatable: true,
131-
help: 'Generate the project with debug logging to be able to use '
132-
'FlutterFlow Debug Panel inside the DevTools.',
133-
defaultsTo: false,
134-
)
135-
..addOption(
136-
'project-environment',
137-
help: '(Optional) Specify a project environment name.',
138-
);
139-
140-
final firebaseDeployCommandParser = ArgParser()
141-
..addOption('project', abbr: 'p', help: 'Project id')
142-
..addFlag(
143-
'append-rules',
144-
abbr: 'a',
145-
help: 'Append to rules, instead of overwriting them.',
146-
defaultsTo: false,
147-
);
148-
149-
final parser = ArgParser()
150-
..addOption('endpoint', abbr: 'e', help: 'Endpoint', hide: true)
151-
..addOption('environment', help: 'Environment', hide: true)
152-
..addOption('token', abbr: 't', help: 'API Token')
153-
..addFlag('help', negatable: false, abbr: 'h', help: 'Help')
154-
..addCommand('export-code', exportCodeCommandParser)
155-
..addCommand('deploy-firebase', firebaseDeployCommandParser);
156-
157-
late ArgResults parsed;
158-
try {
159-
parsed = parser.parse(args);
160-
} catch (e) {
161-
stderr.write('$e\n');
162-
stderr.write(parser.usage);
163-
exit(1);
164-
}
165-
166-
if (parsed['help'] ?? false) {
167-
print(parser.usage);
168-
if (parsed.command != null) {
169-
print(parser.commands[parsed.command!.name]!.usage);
170-
} else {
171-
print('Available commands: ${parser.commands.keys.join(', ')}.');
172-
}
173-
exit(0);
174-
}
175-
176-
if (parsed.command == null) {
177-
print(parser.usage);
178-
print('Available commands: ${parser.commands.keys.join(', ')}.');
179-
exit(1);
180-
}
181-
182-
return parsed;
4+
await appMain(args);
1835
}

lib/src/flutterflow_main.dart

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import 'dart:io';
2+
3+
import 'package:args/args.dart';
4+
import 'package:flutterflow_cli/src/flutterflow_api_client.dart';
5+
6+
const kDefaultEndpoint = 'https://api.flutterflow.io/v1';
7+
8+
Future<void> appMain(List<String> args) async {
9+
final parsedArguments = _parseArgs(args);
10+
11+
final token =
12+
parsedArguments['token'] ?? Platform.environment['FLUTTERFLOW_API_TOKEN'];
13+
14+
final project = parsedArguments.command!['project'] ??
15+
Platform.environment['FLUTTERFLOW_PROJECT'];
16+
17+
if (project == null || project.isEmpty) {
18+
stderr.write(
19+
'Either --project option or FLUTTERFLOW_PROJECT environment variable must be set.\n');
20+
exit(1);
21+
}
22+
23+
if (parsedArguments['endpoint'] != null &&
24+
parsedArguments['environment'] != null) {
25+
stderr.write(
26+
'Only one of --endpoint and --environment options can be set.\n');
27+
exit(1);
28+
}
29+
30+
if (token?.isEmpty ?? true) {
31+
stderr.write(
32+
'Either --token option or FLUTTERFLOW_API_TOKEN environment variable must be set.\n');
33+
exit(1);
34+
}
35+
36+
late String endpoint;
37+
if (parsedArguments['endpoint'] != null) {
38+
endpoint = parsedArguments['endpoint'];
39+
} else if (parsedArguments['environment'] != null) {
40+
endpoint =
41+
"https://api-${parsedArguments['environment']}.flutterflow.io/v1";
42+
} else {
43+
endpoint = Platform.environment['FLUTTERFLOW_ENDPOINT'] ?? kDefaultEndpoint;
44+
}
45+
46+
try {
47+
switch (parsedArguments.command?.name) {
48+
case 'export-code':
49+
await exportCode(
50+
token: token,
51+
endpoint: endpoint,
52+
projectId: project,
53+
destinationPath: parsedArguments.command!['dest'],
54+
includeAssets: parsedArguments.command!['include-assets'],
55+
branchName: parsedArguments.command!['branch-name'],
56+
commitHash: parsedArguments.command!['commit-hash'],
57+
unzipToParentFolder: parsedArguments.command!['parent-folder'],
58+
fix: parsedArguments.command!['fix'],
59+
exportAsModule: parsedArguments.command!['as-module'],
60+
exportAsDebug: parsedArguments.command!['as-debug'],
61+
environmentName: parsedArguments.command!['project-environment'],
62+
);
63+
break;
64+
case 'deploy-firebase':
65+
await firebaseDeploy(
66+
token: token,
67+
projectId: project,
68+
appendRules: parsedArguments.command!['append-rules'],
69+
endpoint: endpoint,
70+
);
71+
break;
72+
default:
73+
}
74+
} catch (_) {
75+
exit(1);
76+
}
77+
}
78+
79+
ArgResults _parseArgs(List<String> args) {
80+
final exportCodeCommandParser = ArgParser()
81+
..addOption('project', abbr: 'p', help: 'Project id')
82+
..addOption(
83+
'dest',
84+
abbr: 'd',
85+
help: 'Destination directory',
86+
defaultsTo: '.',
87+
)
88+
..addOption(
89+
'branch-name',
90+
abbr: 'b',
91+
help: '(Optional) Specify a branch name',
92+
)
93+
..addOption(
94+
'commit-hash',
95+
abbr: 'c',
96+
help: '(Optional) Specify a commit hash',
97+
)
98+
..addFlag(
99+
'include-assets',
100+
negatable: true,
101+
help: 'Include assets. By default, assets are not included.\n'
102+
'We recommend setting this flag only when calling this command '
103+
'for the first time or after updating assets.\n'
104+
'Downloading code without assets is typically much faster.',
105+
defaultsTo: false,
106+
)
107+
..addFlag(
108+
'fix',
109+
negatable: true,
110+
help: 'Run "dart fix" on the downloaded code.',
111+
defaultsTo: false,
112+
)
113+
..addFlag(
114+
'parent-folder',
115+
negatable: true,
116+
help: 'Download into a sub-folder. By default, project is downloaded \n'
117+
'into a folder named <project>.\nSetting this flag to false will '
118+
'download all project code directly into the specified directory, '
119+
'or the current directory if --dest is not set.',
120+
defaultsTo: true,
121+
)
122+
..addFlag(
123+
'as-module',
124+
negatable: true,
125+
help: 'Generate the project as a Flutter module.',
126+
defaultsTo: false,
127+
)
128+
..addFlag(
129+
'as-debug',
130+
negatable: true,
131+
help: 'Generate the project with debug logging to be able to use '
132+
'FlutterFlow Debug Panel inside the DevTools.',
133+
defaultsTo: false,
134+
)
135+
..addOption(
136+
'project-environment',
137+
help: '(Optional) Specify a project environment name.',
138+
);
139+
140+
final firebaseDeployCommandParser = ArgParser()
141+
..addOption('project', abbr: 'p', help: 'Project id')
142+
..addFlag(
143+
'append-rules',
144+
abbr: 'a',
145+
help: 'Append to rules, instead of overwriting them.',
146+
defaultsTo: false,
147+
);
148+
149+
final parser = ArgParser()
150+
..addOption('endpoint', abbr: 'e', help: 'Endpoint', hide: true)
151+
..addOption('environment', help: 'Environment', hide: true)
152+
..addOption('token', abbr: 't', help: 'API Token')
153+
..addFlag('help', negatable: false, abbr: 'h', help: 'Help')
154+
..addCommand('export-code', exportCodeCommandParser)
155+
..addCommand('deploy-firebase', firebaseDeployCommandParser);
156+
157+
late ArgResults parsed;
158+
try {
159+
parsed = parser.parse(args);
160+
} catch (e) {
161+
stderr.write('$e\n');
162+
stderr.write(parser.usage);
163+
exit(1);
164+
}
165+
166+
if (parsed['help'] ?? false) {
167+
print(parser.usage);
168+
if (parsed.command != null) {
169+
print(parser.commands[parsed.command!.name]!.usage);
170+
} else {
171+
print('Available commands: ${parser.commands.keys.join(', ')}.');
172+
}
173+
exit(0);
174+
}
175+
176+
if (parsed.command == null) {
177+
print(parser.usage);
178+
print('Available commands: ${parser.commands.keys.join(', ')}.');
179+
exit(1);
180+
}
181+
182+
return parsed;
183+
}

0 commit comments

Comments
 (0)