Skip to content

Instantly share code, notes, and snippets.

@maraisr
Last active August 9, 2021 00:57

Revisions

  1. maraisr revised this gist Aug 9, 2021. 3 changed files with 10 additions and 10 deletions.
    6 changes: 3 additions & 3 deletions index.d.ts
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    type FileType = string | Buffer | URL;
    type DataType = string | Buffer;

    declare function write(path: FileType, body: DataType, content_type?: string): Promise<unknown>;
    declare function writeFile(path: FileType, body: DataType, content_type?: string): Promise<void>;

    declare function writeSync(path: FileType, body: DataType, content_type?: string): unknown;
    declare function writeFileSync(path: FileType, body: DataType, content_type?: string): void;

    export { write, writeSync };
    export { writeFile, writeFileSync };
    12 changes: 6 additions & 6 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    import { mkdir, writeFile } from "fs/promises";
    import { mkdirSync, writeFileSync } from "fs";
    import { mkdir, writeFile as _writeFile } from "fs/promises";
    import { mkdirSync, writeFileSync as _writeFileSync } from "fs";
    import { dirname } from "path";

    export const write = async (path, body, content_type) => {
    export const writeFile = async (path, body, content_type) => {
    content_type = content_type || "utf8";
    await mkdir(dirname(path), { recursive: true });
    await writeFile(path, body, content_type);
    await _writeFile(path, body, content_type);
    };

    export const writeSync = (path, body, content_type) => {
    export const writeFileSync = (path, body, content_type) => {
    content_type = content_type || "utf8";
    mkdirSync(dirname(path), { recursive: true });
    writeFileSync(path, body, content_type);
    _writeFileSync(path, body, content_type);
    };
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    {
    "name": "swrt",
    "version": "0.0.5",
    "version": "0.0.6",
    "repository": "https://gist.github.com/maraisr/e36f63d22673da3bc45f08f8cba9e083",
    "license": "MIT",
    "author": {
  2. maraisr revised this gist Aug 6, 2021. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    {
    "name": "swrt",
    "version": "0.0.4",
    "version": "0.0.5",
    "repository": "https://gist.github.com/maraisr/e36f63d22673da3bc45f08f8cba9e083",
    "license": "MIT",
    "author": {
    @@ -9,7 +9,6 @@
    "url": "https://marais.io"
    },
    "sideEffects": false,
    "type": "module",
    "exports": {
    ".": {
    "import": "./dist/index.mjs",
  3. maraisr revised this gist Aug 6, 2021. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    {
    "name": "swrt",
    "version": "0.0.3",
    "version": "0.0.4",
    "repository": "https://gist.github.com/maraisr/e36f63d22673da3bc45f08f8cba9e083",
    "license": "MIT",
    "author": {
    @@ -30,8 +30,5 @@
    "devDependencies": {
    "@types/node": "^16.4.9",
    "bundt": "^1.1.5"
    },
    "engines": {
    "node": ">16"
    }
    }
  4. maraisr revised this gist Aug 1, 2021. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    {
    "name": "swrt",
    "version": "0.0.2",
    "version": "0.0.3",
    "repository": "https://gist.github.com/maraisr/e36f63d22673da3bc45f08f8cba9e083",
    "license": "MIT",
    "author": {
    @@ -18,7 +18,6 @@
    "./package.json": "./package.json"
    },
    "main": "dist/index.js",
    "unpkg": "dist/index.min.js",
    "module": "dist/index.mjs",
    "types": "index.d.ts",
    "files": [
  5. maraisr revised this gist Aug 1, 2021. No changes.
  6. maraisr revised this gist Aug 1, 2021. 2 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ export const write = async (path, body, content_type) => {
    await writeFile(path, body, content_type);
    };

    export const writeSync = async (path, body, content_type) => {
    export const writeSync = (path, body, content_type) => {
    content_type = content_type || "utf8";
    mkdirSync(dirname(path), { recursive: true });
    writeFileSync(path, body, content_type);
    4 changes: 2 additions & 2 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    {
    "name": "swrt",
    "version": "0.0.1",
    "repository": "",
    "version": "0.0.2",
    "repository": "https://gist.github.com/maraisr/e36f63d22673da3bc45f08f8cba9e083",
    "license": "MIT",
    "author": {
    "name": "Marais Rossouw",
  7. maraisr revised this gist Aug 1, 2021. 4 changed files with 9 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    9 changes: 9 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # SWRT <sup>Safe Write</sup>

    A safe-write, that classic `write the file`, but make any folders firstly.

    ```js
    import { write } from 'swrt';

    await write('./dist/things.html', '<h1>hello world</h1>');
    ```
  8. maraisr created this gist Aug 1, 2021.
    8 changes: 8 additions & 0 deletions .\index.d.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    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 };
    15 changes: 15 additions & 0 deletions .\index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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);
    };
    38 changes: 38 additions & 0 deletions .\package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    {
    "name": "swrt",
    "version": "0.0.1",
    "repository": "",
    "license": "MIT",
    "author": {
    "name": "Marais Rossouw",
    "email": "me@marais.dev",
    "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"
    }
    }