os_paths 1.0.0
os_paths: ^1.0.0 copied to clipboard
OS-specific standard directories (XDG on Linux/BSD, ~/Library on macOS, %APPDATA% on Windows) for Flutter and Dart apps.
import 'dart:io';
import 'package:os_paths/os_paths.dart';
/// Prints the standard directories of the host platform.
///
/// Nothing is created or written unless you pass `--write`, which runs the
/// `ensure*` demo against a throwaway directory rather than your real config.
Future<void> main(List<String> arguments) async {
final paths = OsPaths.instance;
stdout.writeln('platform: ${paths.family.name}');
stdout.writeln('home: ${paths.homeOrNull ?? "<unknown>"}');
stdout.writeln('config: ${paths.configHome}');
stdout.writeln('data: ${paths.dataHome}');
stdout.writeln('local data: ${paths.localDataHome}');
stdout.writeln('cache: ${paths.cacheHome}');
stdout.writeln('state: ${paths.stateHome}');
stdout.writeln('logs: ${paths.logHome}');
stdout.writeln('runtime: ${paths.runtimeDir ?? "<none, use temp>"}');
stdout.writeln('temp: ${paths.tempDir}');
stdout.writeln('documents: ${paths.documentsDir}');
stdout.writeln('downloads: ${paths.downloadsDir}');
stdout.writeln('data dirs: ${paths.dataDirs.join(", ")}');
final app = paths.app('Fern Notes', organization: 'Lexmata');
stdout.writeln('\nThis app would use:');
stdout.writeln(' config: ${app.config}');
stdout.writeln(' data: ${app.data}');
stdout.writeln(' cache: ${app.cache}');
stdout.writeln(' logs: ${app.logs}');
if (!arguments.contains('--write')) {
stdout.writeln('\n(no directories were created — pass --write for a demo)');
return;
}
// Demonstrate the ensure* helpers against a sandbox, so running the example
// never leaves anything behind in the real config tree.
final sandbox = await Directory.systemTemp.createTemp('os_paths_example');
try {
final demo = ExplicitPaths(
family: paths.family,
dataHome: '${sandbox.path}/data',
cacheHome: '${sandbox.path}/cache',
).app('Fern Notes', organization: 'Lexmata');
final configDir = await demo.ensureConfig();
final settings = File(paths.join(configDir.path, 'settings.json'));
await settings.writeAsString('{"theme":"dark"}');
stdout.writeln('\nwrote ${settings.path}');
} finally {
await sandbox.delete(recursive: true);
stdout.writeln('cleaned up ${sandbox.path}');
}
}