@@ -6,9 +6,9 @@ import 'dart:io';
6
6
import 'package:yaml/yaml.dart' ;
7
7
8
8
// .json
9
- final jsonRegex = RegExp (r'.*.json$' );
9
+ final jsonRegex = RegExp (r'^ .*.json$' );
10
10
// .yml & .yaml
11
- final yamlRegex = RegExp (r'.*(.ya?ml)$' );
11
+ final yamlRegex = RegExp (r'^ .*(.ya?ml)$' );
12
12
13
13
final _supportedRegexes = [jsonRegex, yamlRegex];
14
14
@@ -21,12 +21,14 @@ final _supportedRegexes = [jsonRegex, yamlRegex];
21
21
/// - yml
22
22
///
23
23
/// It also throws an error when the specification doesn't exist on disk.
24
+ ///
25
+ /// WARNING: THIS DOESN'T VALIDATE THE SPECIFICATION CONTENT
24
26
FutureOr <Map <String , dynamic >> loadSpec (
25
27
{required String specPath, bool isCached = false }) async {
26
28
// If the spec file doesn't match any of the currently supported spec formats
27
29
// reject the request.
28
30
if (! _supportedRegexes.any ((fileEnding) => fileEnding.hasMatch (specPath))) {
29
- return Future .error ('Invalid spec format' );
31
+ return Future .error ('Invalid spec file format' );
30
32
}
31
33
32
34
final file = File (specPath);
@@ -210,9 +212,29 @@ List<dynamic> convertYamlListToDartList({required YamlList yamlList}) {
210
212
211
213
/// Caches the updated [spec] to disk for use in future comparisons.
212
214
///
213
- /// Caches the [spec] to the given [outputDirectory ] . By default this will be likely
214
- /// be the .dart_tool or build directory.
215
+ /// Caches the [spec] to the given [outputLocation ] . By default this will be likely
216
+ /// be the .dart_tool/openapi-generator-cache.json
215
217
Future <void > cacheSpec ({
216
- required String outputDirectory ,
218
+ required String outputLocation ,
217
219
required Map <String , dynamic > spec,
218
- }) async {}
220
+ }) async {
221
+ final outputPath = outputLocation;
222
+ final outputFile = File (outputPath);
223
+ if (outputFile.existsSync ()) {
224
+ log ('Found cached asset updating' );
225
+ } else {
226
+ log ('No previous openapi-generated cache found. Creating cache' );
227
+ }
228
+
229
+ return await outputFile.writeAsString (jsonEncode (spec)).then (
230
+ (_) => log ('Successfully wrote cache.' ),
231
+ onError: (e, st) {
232
+ log (
233
+ 'Failed to write cache' ,
234
+ error: e,
235
+ stackTrace: st,
236
+ );
237
+ return Future .error ('Failed to write cache' );
238
+ },
239
+ );
240
+ }
0 commit comments