getDartDataHomeNode function
Get the Dart data home directory, using Node.js APIs.
nodeProcess should be the Node.js process object.
The packageName must be a valid Dart package name.
The directory location depends on the platform property of nodeProcess:
- on Windows ('win32'):
%LOCALAPPDATA%\Dart\<packageName>
- on Mac OS ('darwin'):
$HOME/Library/Application Support/Dart/<packageName>
- on Linux (and others):
$XDG_STATE_HOME/Dart/<packageName>if$XDG_STATE_HOMEis defined, and$HOME/.local/state/Dart/<packageName>otherwise.
The Dart data home can be overridden with the DART_DATA_HOME environment
variable in nodeProcess's env.
Implementation
String getDartDataHomeNode(String packageName, JSObject nodeProcess) {
// Extract env and platform from nodeProcess
final env = nodeProcess.getProperty<JSObject>('env'.toJS);
final platformJS = nodeProcess.getProperty<JSString>('platform'.toJS).toDart;
final os = switch (platformJS) {
'win32' => 'windows',
'darwin' => 'macos',
_ => 'linux',
};
String? getEnv(String key) => env.hasProperty(key.toJS).toDart
? env.getProperty<JSString?>(key.toJS)?.toDart
: null;
// Use the appropriate path context based on the target OS.
final context = os == 'windows' ? p.windows : p.posix;
return context.join(
getDartDataHomeInternal(os: os, getEnv: getEnv, pathContext: context),
packageName,
);
}