{"id":20541278,"url":"https://github.com/sobesednik/wrote","last_synced_at":"2025-07-20T01:35:12.865Z","repository":{"id":57152878,"uuid":"91851311","full_name":"Sobesednik/wrote","owner":"Sobesednik","description":"Promise-based write for Node.js","archived":false,"fork":false,"pushed_at":"2018-01-02T04:17:45.000Z","size":157,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-16T12:53:25.955Z","etag":null,"topics":["fs","io","nodejs","promise","read","stream","write"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sobesednik.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-19T22:59:05.000Z","updated_at":"2021-01-24T18:48:26.000Z","dependencies_parsed_at":"2022-09-06T07:00:58.255Z","dependency_job_id":null,"html_url":"https://github.com/Sobesednik/wrote","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/Sobesednik/wrote","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sobesednik%2Fwrote","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sobesednik%2Fwrote/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sobesednik%2Fwrote/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sobesednik%2Fwrote/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sobesednik","download_url":"https://codeload.github.com/Sobesednik/wrote/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sobesednik%2Fwrote/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266053878,"owners_count":23869499,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fs","io","nodejs","promise","read","stream","write"],"created_at":"2024-11-16T01:21:00.040Z","updated_at":"2025-07-20T01:35:12.833Z","avatar_url":"https://github.com/Sobesednik.png","language":"JavaScript","readme":"# wrote\n\n[![npm version](https://badge.fury.io/js/wrote.svg)](https://badge.fury.io/js/wrote)\n\nPromise-based write and read operations for Node.js\n\n## ES5\n\nThe package uses some newer language features. For your convenience, it's been\ntranspiled to be compatible with Node 4. You can use the following snippet.\n\n```js\nconst wrote = require('wrote/es5/src/')\n```\n\n## `wrote.createWritable(path=: string): Promise.\u003cWritable\u003e`\n\nCreate an open Writable stream to the file.\n\n```js\nconst { Writable } = require('stream')\nconst path = require('path')\nconst HOME_DIR = require('os').homedir()\nconst { createWritable } = require('wrote')\n\nconst file = path.join(HOME_DIR, `wrote-${Math.floor(Math.random() * 1e5)}.data`);\n\n(async () =\u003e {\n    const ws = await createWritable(file)\n    console.log(ws instanceof Writable) // true\n    console.log(ws.path) // ~/wrote-35558.data\n})()\n```\n\nIf you don't have a file, a new one in the temp directory will be created for you.\n\n```js\n(async () =\u003e {\n    const ws = await createWritable()\n    console.log(ws instanceof Writable) // true\n    console.log(ws.path) // /var/folders/s0/1h2g/T/wrote-48315.data\n})()\n```\n\n## `wrote.exists(path: string): Promise.\u003cboolean\u003e`\n\nCheck if the path on the filesystem exists. Throws if path is not accessible\ndue to permissions.\n\n```js\nconst { exists } = require('wrote');\n\n(async () =\u003e {\n    await exists('unknown-path') // false\n    await exists(__filename) // true\n    await exists(__dirname) // true\n})()\n```\n\n## `wrote.assertExists(path: string): Promise`\n\nCheck if the path on the filesystem exists, and throw if it does not, or cannot\nbe accessed.\n\n```js\nconst { assertExists } = require('wrote');\n\n(async () =\u003e {\n    try {\n        await assertExists('unknown-path')\n    } catch (err) {\n        console.log(err) // Path unknown-path does not exist.\n    }\n    await assertExists(__filename) // ok\n})()\n```\n\n## `wrote.assertDoesNotExist(path: string): Promise`\n\nCheck if the path on the filesystem does not exists, and throw if it does, or\ncannot be accessed.\n\n```js\nconst { assertDoesNotExist } = require('wrote');\n\n(async () =\u003e {\n    try {\n        await assertDoesNotExist(__filename)\n    } catch (err) {\n        console.log(err) // Path /wrote/examples/assert-does-not-exist.js exists.\n    }\n    await assertDoesNotExist('unknown-file') // ok\n})()\n```\n\n## `wrote.clone({ from: string, to: string, regexes: [] }): Promise`\n\nClone a directory by copying contents of files and creating symlinks. Regular\nexpressions can be used to transform data being copied.\n\n```js\nconst { clone } = require('wrote');\n\n(async () =\u003e {\n    const from = './directory'\n    const to = './clone'\n\n    await clone({\n        from,\n        to,\n        regexes: [\n            {\n                re: /{{ name }}/g, // use /g flag for global replacements\n                replacement: 'Garry',\n            },\n            {\n                re: /{{ age }}/,\n                replacement: '30',\n            },\n        ],\n    })\n    // or,\n    // await clone({ to, from })\n})()\n```\n\n## `wrote.erase(ws: Writable): Promise.\u003cWritable\u003e`\n\nErase file and close stream.\n\n```js\nconst { createWritable, erase } = require('wrote')\nconst { Writable } = require('stream')\nconst path = require('path')\nconst HOME_DIR = require('os').homedir()\n\nconst file = path.join(HOME_DIR, `wrote-${Math.floor(Math.random() * 1e5)}.data`);\n\n(async () =\u003e {\n    const ws = await createWritable(file)\n    console.log(ws instanceof Writable) // true\n    console.log(ws.writable) // true\n    console.log(ws.path) // ~/wrote-35558.data\n    await erase(ws)\n    console.log(ws.closed) // true\n})()\n\n```\n\n## `wrote.write(ws: Writable, data?: string|Readable): Promise.\u003cWritable\u003e`\n\nPipe a `Readable` to the `Writable` stream and wait until it is finished, or end\n `Writable` with given data (pass `null` to end stream without any more data).\n\n```js\nconst { write } = require('wrote')\nconst assert = require('assert')\nconst { Writable } = require('stream')\n\nconst testString = 'hello world'\nconst buffer = Buffer.from(testString)\nconst allRawData = []\nconst ws = new Writable({\n    write(chunk, encoding, next) {\n        allRawData.push(chunk)\n        next()\n    },\n});\n\n(async () =\u003e {\n    await write(ws, buffer)\n    console.log(allRawData.map(d =\u003e String(d))) // [ 'hello world' ]\n    assert.deepEqual(allRawData, [\n        buffer,\n    ])\n})()\n```\n\n## `wrote.ensurePath(filePath: string): Promise\u003cstring\u003e`\n\nCreate all required directories for the filepath to exist. If a directory on the way is\nnon-executable, the Promise will be rejected. Resolves with the filepath.\n\n```js\nconst { ensurePath } = require('wrote')\nconst { resolve } = require('path');\n\n(async () =\u003e {\n    const path = 'path/to/temp/file.data'\n    const res = await ensurePath(path)\n    console.log(res) // path/to/temp/file.data, path/to/temp is created in your cwd\n\n    const absolutePath = resolve(process.cwd(), 'path/to/temp/file.data')\n    const res2 = await ensurePath(absolutePath)\n    console.log(res2) // $(pwd)/path/to/temp/file.data\n})()\n```\n\n## `wrote.read(path: string, { binary?: boolean }): Promise.\u003cstring\u003e`\n\nRead a file fully. Returns a Promise resolved with the file contents, and\nrejects if path is not a string or file not found (`ENOENT`).\n\n```js\nconst { read } = require('wrote');\n\n(async () =\u003e {\n    const res = await read(__filename)\n    console.log(res)\n})()\n```\n\n`examples/read.js`: _this program will print the contents of itself_\n\nPass `{ binary: true }` options to read as a `Buffer`:\n\n```js\nconst { read } = require('wrote');\n\n(async () =\u003e {\n    const buffer = await read(__filename, { binary: true })\n    console.log(buffer) // // \u003cBuffer 63 6f 6e 73 74 20 7b ... \u003e\n})()\n```\n\n## `wrote.readJSON(path: string): Promise.\u003cobject\u003e`\n\nRead a json file and parse its contents.\n\n```js\nconst { readJSON } = require('wrote');\n\n(async () =\u003e {\n    const res = await read('path/to/file.json')\n    console.log(res)\n})()\n```\n\n## `wrote.writeJSON(path: string, object: object, { replacer?: function, space?: string|number }): Promise`\n\nSerialise an object with `JSON.stringify` and write it to a file. Pass `space`\nand `replacer` in the options object.\n\n```js\nconst { writeJSON } = require('wrote');\n\n(async () =\u003e {\n    const object = {\n        test: 'data',\n        date: new Date(),\n    }\n    await writeJSON('path/to/file.json', object, {\n        space: 2,\n        // replacer: () =\u003e {}\n    })\n})()\n```\n\n## `wrote.readDir(dirPath: string, recursive=: boolean): Promise\u003cobject\u003e`\n\nRead a directory, and return contents of contained files.\n\nFor example, the following directory structure:\n\n```fs\ndirectory\n - subdirectory\n    - subdirFileA.txt\n    ` subdirFileB.txt\n - fileA.txt\n - fileB.txt\n ` fileC.txt\n```\n\ncan be read either shallowly (by default):\n\n```js\nconst { readDir } = require('wrote')\nconst path = require('path')\n\nconst dirPath = path.join(__dirname, 'directory');\n\n(async () =\u003e {\n    const res = await readDir(dirPath)\n    console.log(res)\n    // { 'fileA.txt': 'fileA\\n',\n    //   'fileB.txt': 'fileB\\n',\n    //   'fileC.txt': 'fileC\\n' }\n})()\n```\n\nor recursively:\n\n```js\n(async () =\u003e {\n    const res = await readDir(dirPath, true)\n    console.log(res)\n    // { 'fileA.txt': 'fileA\\n',\n    //   'fileB.txt': 'fileB\\n',\n    //   'fileC.txt': 'fileC\\n',\n    //   subdirectory:\n    //    { 'subdirFileA.txt': 'subdirFileA\\n',\n    //      'subdirFileB.txt': 'subdirFileB\\n' } }\n})()\n```\n\n## `wrote.readDirStructure(dirPath: string): Promise\u003cDirectoryStructure\u003e`\n\nGet the full structure of the directory recursively. An array of either\nfile names as strings, or an object representing all directories of the\ncurrent one, with keys being their names, and values being arrays similar\nto the root one.\n\n```js\nconst path = require('path')\nconst { readDirStructure } = require('..')\n\nconst DIR_PATH = path.join(__dirname, '../test/fixtures/directory');\n\n/**\n * Read directory's structure.\n */\n\n(async () =\u003e {\n    const res = await readDirStructure(DIR_PATH)\n    console.log(JSON.stringify(res, null, 2))\n})()\n```\n\n```fs\n{\n  \"type\": \"Directory\",\n  \"content\": {\n    \"subdirectory-ln\": {\n      \"type\": \"SymbolicLink\"\n    },\n    \"test-ln.data\": {\n      \"type\": \"SymbolicLink\"\n    },\n    \"test.data\": {\n      \"type\": \"File\"\n    },\n    \"subdirectory\": {\n      \"type\": \"Directory\",\n      \"content\": {\n        \"file.data\": {\n          \"type\": \"File\"\n        },\n        \"file2.data\": {\n          \"type\": \"File\"\n        }\n      }\n    },\n    \"subdirectory2\": {\n      \"type\": \"Directory\",\n      \"content\": {\n        \"file3.data\": {\n          \"type\": \"File\"\n        },\n        \"subsubdir\": {\n          \"type\": \"Directory\",\n          \"content\": {\n            \"file4.py\": {\n              \"type\": \"File\"\n            }\n          }\n        },\n        \"subsubdir2\": {\n          \"type\": \"Directory\",\n          \"content\": {}\n        }\n      }\n    }\n  }\n}\n```\n\n## todo\n\n- `eraseDir` to rm -rf\n- `cloneFile` to clone a single file\n- `write` with string as path\n- `erase` with string as path\n- `clone` with permissions\n- pass options to `fs.createWriteStream` in `wrote.createWritable`\n\n---\n\nLicence: MIT\n\n*(c) [Sobesednik-Media](https://sobesednik.media) 2017*\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsobesednik%2Fwrote","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsobesednik%2Fwrote","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsobesednik%2Fwrote/lists"}