// builtin import { unlink as _unlink } from 'node:fs' import { versions } from 'node:process' const nodeVersion = String(versions.node || '0') // external import accessible, { W_OK } from 'https://unpkg.com/@bevry/fs-accessible@^2.5.0/edition-deno/index.ts' import Errlop from 'https://unpkg.com/errlop@^8.4.0/edition-deno/index.ts' /** Remove a file. */ export default async function unlink( path: string | Array ): Promise { if (Array.isArray(path)) { return Promise.all(path.map((i) => unlink(i))).then(() => {}) } // check exists try { await accessible(path) } catch (err: any) { // if it doesn't exist, then we don't care return } // check writable try { await accessible(path, W_OK) } catch (err: any) { if (err.code === 'ENOENT') { // if it doesn't exist, then we don't care (this may not seem necessary due to the earlier accessible check, however it is necessary, testen would fail on @bevry/json otherwise) return } throw new Errlop(`unable to remove the non-writable file: ${path}`, err) } // remove the file return new Promise(function (resolve, reject) { _unlink(path, function (err) { if (err) { return reject( new Errlop( `failed to remove the accessible and writable file: ${path}`, err ) ) } return resolve() }) }) }