readFile static method

Future<AssetItem?> readFile(
  1. String path,
  2. Lib lib,
  3. SharedOptions options
)

Implementation

static Future<AssetItem?> readFile(String path, Lib lib, SharedOptions options) async {
  final File file = File(path);
  if (await file.exists()) {
    final List<String> names = context.split(path.replaceFirst(context.rootPrefix(path), '').replaceAll(RegExp('[\\.\\_\\-\\s]+'), '/'));
    String name = names.removeAt(0) + names.map((String name) => name
      .toLowerCase()
      .replaceRange(0, 1, name
        .substring(0, 1)
        .toUpperCase()
      )
    ).join();

    for (final String key in options.nameReplaces.keys) {
      final RegExp re = RegExp('^$key');
      if (!re.hasMatch(name)) continue;
      name = name.replaceFirst(re, options.nameReplaces[key] ?? '');
      name = name.replaceRange(0, 1, name
        .substring(0, 1)
        .toLowerCase()
      );
      break;
    }

    final Digest digest = md5.convert(path.codeUnits);
    final asset = AssetItem(
      path: path,
      name: name,
      hash: digest.toString().substring(0, 16),
      lib: lib,
      options: options,
    );

    await asset.updateConttent();

    return asset;
  } else {
    return null;
  }
}