run method

Future<void> run(
  1. List<String> arguments
)

Use this declaration as a custom Dart script: prepare, inspect, publish.

Implementation

Future<void> run(List<String> arguments) async {
  final parser = ArgParser()
    ..addFlag('help', abbr: 'h', negatable: false)
    ..addOption('registry', help: 'Registry for publish.')
    ..addOption('tag', help: 'npm distribution tag for new versions.')
    ..addOption('access', allowed: ['public', 'restricted']);
  try {
    final options = parser.parse(arguments);
    if (options.flag('help')) {
      stdout.writeln(
        'Usage: dart run ${path.relative(Platform.script.toFilePath())} [prepare|publish] [options]\n\n${parser.usage}',
      );
      return;
    }
    if (options.rest.length > 1) {
      throw const FormatException('Choose either prepare or publish.');
    }
    final command = options.rest.isEmpty ? 'prepare' : options.rest.single;
    if (command == 'prepare') {
      if (['registry', 'tag', 'access'].any(options.wasParsed)) {
        throw const FormatException(
          'Registry, tag and access options require publish.',
        );
      }
      stdout.writeln('Prepared ${(await prepare()).path}');
    } else if (command == 'publish') {
      await publish(
        registry: options.option('registry') == null
            ? null
            : Uri.parse(options.option('registry')!),
        tag: options.option('tag'),
        access: options.option('access') == null
            ? null
            : NpmAccess.values.byName(options.option('access')!),
      );
    } else {
      throw FormatException('Unknown command: $command');
    }
  } on FormatException catch (error) {
    stderr.writeln('napi: ${error.message}');
    exitCode = 64;
  } on ArgumentError catch (error) {
    stderr.writeln('napi: ${error.message}');
    exitCode = 64;
  } on StateError catch (error) {
    stderr.writeln('napi: ${error.message}');
    exitCode = 1;
  } on IOException catch (error) {
    stderr.writeln('napi: $error');
    exitCode = 1;
  }
}