{"name":"@putout/operator-filesystem","version":"10.2.0","keywords":["putout","putout-operator","operator","filesystem"],"author":{"url":"https://github.com/coderaiser","name":"coderaiser","email":"mnemonic.enemy@gmail.com"},"license":"MIT","_id":"@putout/operator-filesystem@10.2.0","maintainers":[{"name":"coderaiser","email":"mnemonic.enemy@gmail.com"}],"homepage":"https://github.com/coderaiser/putout/tree/master/packages/operator-filesystem#readme","bugs":{"url":"https://github.com/coderaiser/putout/issues"},"tag":false,"dist":{"shasum":"238e267c4505c177828258e9e908b68034f4ffd1","tarball":"http://registry-npm.ywwl.com/@putout/operator-filesystem/download/@putout/operator-filesystem-10.2.0.tgz","fileCount":6,"integrity":"sha512-BlC2Au23l0DKlDltGMOrH0BFESnUjFTrPXja+eYCcdz5AneNua8Ummi0kC6vH+aB2cDyEDl82JLCVse2fG7B6Q==","signatures":[{"sig":"MEUCIEZYiZbsSerPVHGQzu3XNEUF/5RsDZb5ydGnUyLqRjZ0AiEAtcu8J/E8x2A+L+MLCWKjPvRxBAXa8oCwd/TXLgUj4Hk=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":23058,"size":5785,"noattachment":false,"key":"/@putout/operator-filesystem/-/@putout/operator-filesystem-10.2.0.tgz"},"main":"lib/filesystem.js","type":"module","engines":{"node":">=22"},"gitHead":"12c52545338d695a90f8d49d0e9e528dec2147c5","release":false,"scripts":{"lint":"madrun lint","test":"madrun test","report":"madrun report","coverage":"madrun coverage","fix:lint":"madrun fix:lint","fresh:lint":"madrun fresh:lint","lint:fresh":"madrun lint:fresh","watch:test":"madrun watch:test"},"_npmUser":{"name":"coderaiser","email":"mnemonic.enemy@gmail.com"},"changelog":false,"repository":{"url":"git+https://github.com/coderaiser/putout.git","type":"git"},"_npmVersion":"11.4.2","description":"🐊Putout operator adds ability to filesystem referenced variables that was not defined","directories":{},"_nodeVersion":"25.4.0","dependencies":{"fullstore":"^4.0.0","try-catch":"^4.0.0","@putout/babel":"^5.0.0","@putout/operate":"^15.0.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.0.0","eslint":"^10.0.0-alpha.0","madrun":"^12.0.0","montag":"^1.2.1","nodemon":"^3.0.1","supertape":"^12.0.0","@putout/test":"^15.0.0","eslint-plugin-n":"^17.0.0","@putout/eslint-flat":"^4.0.0","eslint-plugin-putout":"^30.0.0","@putout/engine-parser":"^15.0.1","@putout/operator-json":"^3.0.0"},"peerDependencies":{"putout":">=41"},"_npmOperationalInternal":{"tmp":"tmp/operator-filesystem_10.2.0_1769187373908_0.1261307587295688","host":"s3://npm-registry-packages-npm-production"},"readme":"# @putout/operator-filesystem [![NPM version][NPMIMGURL]][NPMURL]\n\n[NPMIMGURL]: https://img.shields.io/npm/v/@putout/operator-filesystem.svg?style=flat&longCache=true\n[NPMURL]: https://npmjs.org/package/@putout/operator-filesystem \"npm\"\n\n🐊[**Putout**](https://github.com/coderaiser/putout) operator adds ability to lint filesystem.\n\n## Install\n\n```\nnpm i putout @putout/operator-filesystem\n```\n\n## API\n\n### `createFile(directoryPath: DirectoryPath, name: string, content?: string): FilePath`\n\n```js\nconst {operator} = require('putout');\nconst {findFile, createFile} = operator;\n\nconst [dirPath] = findFile(ast, 'hello');\nconst filePath = createFile(dirPath, 'world.txt', 'hello world');\n```\n\n### `createDirectory(directoryPath: DirectoryPath, name: string): DirectoryPath`\n\n```js\nconst {operator} = require('putout');\nconst {\n    createDirectory,\n    findFile,\n} = operator;\n\nconst [dirPath] = findFile(ast, 'hello');\n\nconst newDirectoryPath = createDirectory(dirPath, 'world');\n```\n\n### `createNestedDirectory(path: FilePath|DirectoryPath, name: string): DirectoryPath`\n\n```js\nconst {operator} = require('putout');\nconst {\n    createDirectory,\n    findFile,\n    createNestedDirectory,\n} = operator;\n\nconst newDirectoryPath = createNestedDirectory(ast, '/hello/world');\n```\n\n### `removeEmptyDirectory(DirectoryPath)`\n\n```js\nconst {operator} = require('putout');\nconst {\n    removeEmptyDirectory,\n    createNestedDirectory,\n} = operator;\n\nconst newDirectoryPath = createNestedDirectory(ast, '/hello/world');\n\nremoveEmptyDirectory(newDirectoryPath);\n```\n\n### `readDirectory(directoryPath: DirectoryPath): (FilePath | DirectoryPath)[]`\n\n```js\nconst {operator} = require('putout');\nconst {\n    finedFiles,\n    findFile,\n    readDirectory,\n} = operator;\n\nconst [dirPath] = findFile(ast, '/bin');\n\nreadDirectory(dirPath);\n// returns list of files\n[];\n```\n\n### `findFile(directoryPath: DirectoryPath, name: string | string[] | Set<string>, exclude?: string[]): (FilePath | DirectoryPath)[]`\n\nTraverse filesystem to search one or more files:\n\n```js\nconst {operator} = require('putout');\nconst {findFile} = operator;\n\nconst [filePath] = findFile(ast, 'hello');\n```\n\nYou can also pass array of file masks:\n\n```js\nimport {operator} from 'putout';\n\nconst {findFile} = operator;\nconst coupleFiles = findFile(ast, ['*.js', '*.ts']);\n```\n\nOr `exclude` some files:\n\n```js\nimport {operator} from 'putout';\n\nconst {findFile, getFilename} = operator;\n\nconst coupleFilesWithExcludeArray = findFile(ast, '*.ts', ['*.d.ts']);\n\nconst coupleFilesWithExcludeFn = findFile(ast, '*.ts', (filePath) => {\n    const name = getFilename(filePath);\n    return !name.endsWith('*.d.ts');\n});\n```\n\nAnd even search for a directory:\n\n```js\nimport {operator} from 'putout';\n\nconst {findFile} = operator;\nconst coupleFiles = findFile(root, [\n    '/home/coderaiser',\n    '/home/coderaiser/putout',\n]);\n```\n\nEach 🐊**Putout** plugin should use `findFile` independently since AST can change: files renamed, removed etc.\nAnyways inside one plugin while you applying changes related to one plugin you can speed things drastically crawling file system once. It works good if plugin do not mutates the file tree, only file content.\n\n☝️ *`findFile` expensive, when you need to call it often use `crawlDirectory()`*\n\n### `crawlDirectory(directoryPath: directoryPath): Crawled`\n\n```js\nimport {operator} from 'putout';\n\nconst {findFile} = operator;\nconst crawled = crawlDirectory(rootPath);\n\nconst [file] = findFile(root, 'hello', {\n    crawled,\n});\n```\n\n### `getFile(directoryPath: DirectoryPath, name: string | string[], options?: Options): FilePath[]`\n\n```ts\ntype Options = {\n    type?: 'file' | 'directory';\n};\n```\n\nGet file named `name` from `directoryPath`\n\n```js\nconst {operator} = require('putout');\nconst {getFile} = operator;\n\nconst [filePath] = getFile(root, 'package.json');\n```\n\nWhen you need a couple files use:\n\n```js\nimport {operator} from 'putout';\n\nconst {getFile} = operator;\nconst [index, indexSpec] = getFile(root, ['index.js', 'index.spec.js']);\n```\n\n### `getParentDirectory(path: FilePath | DirectoryPath): FilePath | null`\n\n```js\nconst {operator} = require('putout');\nconst {\n    createDirectory,\n    findFile,\n    getParentDirectory,\n} = operator;\n\nconst [dirPath] = findFile(ast, 'hello');\n\nconst newDirectoryPath = createDirectory(dirPath, 'world');\n\ndirPath === getParentDirectory(newDirectoryPath);\n// returns\ntrue;\n```\n\n### `getRootDirectory(path: FilePath | DirectoryPath | File): DrectoryPath`\n\n```js\nconst {operator} = require('putout');\nconst {\n    findFile,\n    getRootDirectory,\n} = operator;\n\nconst [filePath] = findFile(ast, 'hello');\n\ngetRootDirectory(dirPath);\n```\n\n### `getFilename(path: FilePath | DirectoryPath): string`\n\n```js\nconst {operator} = require('putout');\nconst {getFilename} = operator;\n\nconst name = getFilename(filePath);\n```\n\n### `getFileType(path: FilePath): string`\n\n```js\nconst {operator} = require('putout');\nconst {getFilename, getFileType} = operator;\n\ngetFileType(filePath);\n// returns\n'file';\n```\n\n### `removeFile(filePath: Path)`\n\n```js\nconst {operator} = require('putout');\nconst {removeFile} = operator;\n\nremoveFile(filePath);\n```\n\n### `copyFile(path: FilePath | DirectoryPath, directoryPath: DirectoryPath)`\n\n```js\nconst {operator} = require('putout');\nconst {moveFile, copyFile} = operator;\n\ncopyFile(filePath, dirPath);\n```\n\n### `moveFile(path: FilePath | DirectoryPath, directoryPath: DirectoryPath)`\n\n```js\nconst {operator} = require('putout');\nconst {moveFile} = operator;\n\nmoveFile(filePath, dirPath);\n```\n\n### `readFileContent(filePath: FilePath): string`\n\n```js\nconst {operator} = require('putout');\nconst {readFileContent} = operator;\n\nreadFileContent(filePath);\n// returns\n'hello';\n```\n\n### `writeFileContent(filePath: FilePath, content: string)`\n\n```js\nconst {operator} = require('putout');\nconst {\n    writeFileContent,\n    readFileContent,\n} = operator;\n\nwriteFileContent(filePath, 'hello');\nreadFileContent(filePath);\n// returns\n'hello';\n```\n\n### `renameFile(path: FilePath | DirectoryPath, name: string)`\n\n```js\nconst {operator} = require('putout');\nconst {renameFile, findFile} = operator;\n\nconst [filePath] = findFile(path, 'README.md');\n\nrenameFile(filePath, 'readme.md');\n```\n\nthis is the same as:\n\n```\nrenameFile(filePath, '/any/path/here/readme.md');\n```\n\nSince `basename` is used.\n\n> The `path.basename()` method returns the last portion of a path, similar to the Unix basename command. Trailing directory separators are ignored.\n>\n> (c) [nodejs.org](https://nodejs.org/api/path.html#pathbasenamepath-suffix)\n\nTo move file use [`moveFile()`](#movefilefilepath-filepath-dirpath-filepath).\n\n### `inject`\n\nInject filesystem API with methods:\n\n- ✅ `renameFile`;\n- ✅ `copyFile`;\n- ✅ `removeFile;\n- ✅ `createDirectory;\n- ✅ `readFileContent;\n- ✅ `writeFileContent;\n\nTo have ability to interact with any kind of filesystem representation.\n\n```js\nimport {inject} from '@putout/operator-filesystem/maybe';\nimport * as filesystemCLI from '@putout/cli-filesystem';\n\ninject(filesystemCLI);\n```\n\n### `eject`\n\n```js\nimport {eject} from '@putout/operator-filesystem/maybe';\n\neject();\n```\n\n### `pause`\n\nPause currently injected filesystem API\n\n```js\nimport {pause} from '@putout/operator-filesystem/maybe';\n\npause();\n```\n\n### `start`\n\nStart currently paused injected filesystem API.\n\n```js\nimport {start} from '@putout/operator-filesystem/maybe';\n\nstart();\n```\n\n## Example\n\n```js\nconst {montag} = require('montag');\nconst {\n    parse,\n    print,\n    operator,\n} = require('putout');\n\nconst {renameFile, findFile} = operator;\n\nconst ast = parse(montag`\n    putout_processor_filesystem({\n        \"type\": \"directory\",\n        \"filename\": \"/hello\",\n        \"files\": []\n    });\n`);\n\nconst [filePath] = findFile(ast, 'hello');\n\nrenameFile(filePath, 'world');\n\nprint(ast);\n// returns\n`\nputout_processor_filesystem({\n    \"type\": \"directory\",\n    \"filename\": \"/hello\",\n    \"files\": []\n});\n`;\n```\n\n## License\n\nMIT\n","publish_time":1769187374056,"_cnpm_publish_time":1769187374056,"dist-tags":{"latest":"11.4.0"}}