prepend method

Future<void> prepend(
  1. String path,
  2. String text, {
  3. String separator = '\n',
})

Adds text to the start of the file at path, before separator when the file already has content. Creates the file if needed.

Implementation

Future<void> prepend(String path, String text, {String separator = '\n'}) =>
    _run(path, () async {
      _guardWrite('prepend');
      final String target = _resolve(path);
      final String existing = await driver.fileExists(target)
          ? utf8.decode(await driver.read(target))
          : '';
      await driver.write(
        target,
        utf8.encode(existing.isEmpty ? text : '$text$separator$existing'),
      );
      _written(target);
    });