run method

  1. @override
Future<int> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<int> run() async {
  await super.run();

  final Completer<int> completer = Completer();

  final Lib lib = Lib(sharedOptions);
  await lib.init();

  AssetList? list = await AssetList.readAssetDir(lib, sharedOptions);
  await list?.writeListFile();

  AssetList? newList;

  Watcher(context.current).events.listen((WatchEvent e) {
    queue.add(() async {
      if (!context.isWithin(context.current, e.path)) return;
      final String changePath = context.prettyUri(e.path);

      if (sharedOptions.isListPath(changePath)) {
        newList = await AssetList.readListFile(lib, sharedOptions);
        if (newList.toString() == list.toString()) return;

        await newList?.checkAsset(
          oldList: list,
          nowWrite: false,
        );

        if (newList.toString() == list.toString()) return;

        list = newList;
        newList = null;
        await list?.writeListFile();
      } else if (sharedOptions.isAssetPath(changePath)) {
        if (e.type == ChangeType.REMOVE) {
          if (list?.list[changePath] == null) return;

          await list?.remove(changePath);
        } else if (e.type == ChangeType.ADD || e.type == ChangeType.MODIFY) {
          if (list?.list[changePath] != null) return;

          await list?.add(changePath);
        }
      } else if (sharedOptions.isLibPath(changePath)) {
        if (e.type == ChangeType.REMOVE) {
          await lib.remove(changePath);
          await list?.writeListFile();
        } else if (e.type == ChangeType.ADD || e.type == ChangeType.MODIFY) {
          await lib.add(changePath);
          await list?.writeListFile();
        }
      }
    }).catchError((error, stackTrace) {
      completer.completeError(error, stackTrace);
    });
  }).onError((error, stackTrace) {
    completer.completeError(error, stackTrace ?? StackTrace.current);
  });

  return await completer.future;
}