D
Deno

help

Read-only FS detection

DDanielduel11/12/2023
Hi, I have few deps that by default create cache/log dirs... I can disable it, but the detection itself is unclear. Is there a better way than
const canWriteToFs = (() => {
try {
Deno.mkdirSync("./log", { recursive: true });
return true;
}
catch {
return false;
}
})();
const canWriteToFs = (() => {
try {
Deno.mkdirSync("./log", { recursive: true });
return true;
}
catch {
return false;
}
})();
DAHDeno AI Helper11/12/2023
The documentation suggests that your approach is generally correct. However, it's important to note that you should be careful with the error handling. In your current setup, you're catching all errors and assuming they mean the file system is read-only. However, other errors could occur, such as Deno.errors.PermissionDenied if you don't have the --allow-write flag, or Deno.errors.AlreadyExists if the directory already exists. Here's a more precise way to check if the file system is read-only:
const canWriteToFs = (() => {
try {
Deno.mkdirSync("./log", { recursive: true });
return true;
} catch (err) {
if (err instanceof Deno.errors.PermissionDenied) {
return false;
}
throw err; // Rethrow other errors
}
})();
const canWriteToFs = (() => {
try {
Deno.mkdirSync("./log", { recursive: true });
return true;
} catch (err) {
if (err instanceof Deno.errors.PermissionDenied) {
return false;
}
throw err; // Rethrow other errors
}
})();
This code will return false if the PermissionDenied error is thrown, indicating that the file system is read-only. If any other error is thrown, it will be rethrown, allowing you to handle it elsewhere in your code. Remember to run your script with the --unstable and --allow-write flags, as the Deno.mkdirSync function requires write permissions. 🦕
DDanielduel11/12/2023
Actually that's smart

Looking for more? Join the community!