Skip to content

Instantly share code, notes, and snippets.

@maraisr
Last active August 9, 2021 00:57
Show Gist options
  • Save maraisr/e36f63d22673da3bc45f08f8cba9e083 to your computer and use it in GitHub Desktop.
Save maraisr/e36f63d22673da3bc45f08f8cba9e083 to your computer and use it in GitHub Desktop.
swrt — Safe Write file
type FileType = string | Buffer | URL;
type DataType = string | Buffer;
declare function write(path: FileType, body: DataType, content_type?: string): Promise<unknown>;
declare function writeSync(path: FileType, body: DataType, content_type?: string): unknown;
export { write, writeSync };
import { mkdir, writeFile } from "fs/promises";
import { mkdirSync, writeFileSync } from "fs";
import { dirname } from "path";
export const write = async (path, body, content_type) => {
content_type = content_type || "utf8";
await mkdir(dirname(path), { recursive: true });
await writeFile(path, body, content_type);
};
export const writeSync = async (path, body, content_type) => {
content_type = content_type || "utf8";
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, body, content_type);
};
{
"name": "swrt",
"version": "0.0.1",
"repository": "",
"license": "MIT",
"author": {
"name": "Marais Rossouw",
"email": "[email protected]",
"url": "https://marais.io"
},
"sideEffects": false,
"type": "module",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./package.json": "./package.json"
},
"main": "dist/index.js",
"unpkg": "dist/index.min.js",
"module": "dist/index.mjs",
"types": "index.d.ts",
"files": [
"*.d.ts",
"dist"
],
"scripts": {
"build": "bundt index.js"
},
"devDependencies": {
"@types/node": "^16.4.9",
"bundt": "^1.1.5"
},
"engines": {
"node": ">16"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment