{"name":"@putout/engine-runner","version":"24.1.1","keywords":["putout","putout-engine"],"author":{"url":"https://github.com/coderaiser","name":"coderaiser","email":"mnemonic.enemy@gmail.com"},"license":"MIT","_id":"@putout/engine-runner@24.1.1","maintainers":[{"name":"coderaiser","email":"mnemonic.enemy@gmail.com"}],"homepage":"https://github.com/coderaiser/putout/tree/master/packages/engine-runner#readme","bugs":{"url":"https://github.com/coderaiser/putout/issues"},"tag":false,"dist":{"shasum":"ac1dddb75143636204bf5f0996dc74f12f4431da","tarball":"http://registry-npm.ywwl.com/@putout/engine-runner/download/@putout/engine-runner-24.1.1.tgz","fileCount":21,"integrity":"sha512-Y5tUPr0Dr1NJuvTFrQzCKOVmffUrxXEV+ZAZRS5lIMtqeV9QF1QXFhqRcQrDVwPaE55ZbrM97EHM55/lzRymig==","signatures":[{"sig":"MEUCIQC1DwhvXzj72i6968HDbsLUuTb10ZpaQKGmuuv65tylzgIgQPAjRcFZHfLdW81dgHQMSM5K/QgdnEsqvxu75icOfcg=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":44681,"size":11654,"noattachment":false,"key":"/@putout/engine-runner/-/@putout/engine-runner-24.1.1.tgz"},"main":"./lib/index.js","type":"commonjs","engines":{"node":">=20"},"exports":{".":"./lib/index.js","./progress":"./lib/progress.js"},"gitHead":"edbab501088832f1ea04c7e33c949e3c07b59c32","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.1.0","description":"Run 🐊Putout plugins","directories":{},"_nodeVersion":"20.19.0","dependencies":{"once":"^1.4.0","debug":"^4.1.1","jessy":"^4.0.0","nessy":"^5.2.0","wraptile":"^3.0.0","fullstore":"^3.0.0","try-catch":"^3.0.0","@putout/babel":"^3.0.0","@putout/compare":"^17.0.0","@putout/operate":"^13.0.0","@putout/engine-parser":"^13.0.0","@putout/operator-json":"^2.0.0","@putout/operator-declare":"^12.0.0","@putout/plugin-filesystem":"^9.0.0","@putout/operator-filesystem":"^7.0.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.0.0","eslint":"^9.0.0","madrun":"^11.0.0","montag":"^1.0.0","putout":"*","nodemon":"^3.0.1","supertape":"^11.0.3","eslint-plugin-n":"^17.0.0","just-camel-case":"^6.2.0","@putout/eslint-flat":"^3.0.0","eslint-plugin-putout":"^26.0.0","@putout/engine-loader":"*","@putout/plugin-minify":"*"},"peerDependencies":{"putout":"*"},"_npmOperationalInternal":{"tmp":"tmp/engine-runner_24.1.1_1743281477727_0.6214196003092276","host":"s3://npm-registry-packages-npm-production"},"readme":"# @putout/engine-runner [![NPM version][NPMIMGURL]][NPMURL]\n\n[NPMIMGURL]: https://img.shields.io/npm/v/@putout/engine-runner.svg?style=flat&longCache=true\n[NPMURL]: https://npmjs.org/package/@putout/engine-runner \"npm\"\n\nRun 🐊[**Putout**](https://github.com/coderaiser/putout) plugins.\n\n## Install\n\n```\nnpm i @putout/engine-runner\n```\n\n## Supported Plugin Types\n\nThere is a couple plugin types available in 🐊**Putout**:\n\n- ✅ [**Declarator**](#Declarator)\n- ✅ [**Replacer**](#replacer)\n- ✅ [**Includer**](#includer)\n- ✅ [**Traverser**](#traverser)\n- ✅ [**Scanner**](#scanner)\n- ✅ [**Finder**](#finder)\n\nAll of them supports subset of **JavaScript** 🦎[**PutoutScript**](https://github.com/coderaiser/putout/blob/master/docs/putout-script.md#-putoutscript) described in [`@putout/compare`](https://github.com/coderaiser/putout/tree/master/packages/compare#readme).\n\nThey goes from simplest to hardest. Let's start from **Declarator**.\n\n### Declarator\n\nThe simplest possible type of plugin, even simpler then [`Replacer`](#replacer):\n\n```js\nexport const declare = () => ({\n    isString: `const isString = (a) => typeof a === 'string'`,\n});\n```\n\nBased on [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/master/packages/operator-declare#readme), helps to add declaration to any referenced `Identifier`.\n\n### Replacer\n\n**Replacer** converts code in declarative way. Simplest possible form of 🐊**Putout Plugin**, no need to use `fix`. Just `from` and `to` parts\naccording to [`template variables syntax`](https://github.com/coderaiser/putout/tree/master/packages/compare#supported-template-variables).\n\nSimplest replace example:\n\n```js\nexport const report = () => 'any message here';\n\nexport const replace = () => ({\n    'const a = 1': 'const b = 1',\n});\n\n// optional\nexport const filter = (path, {options}) => {\n    return true;\n};\n\n// optional\nexport const match = () => ({\n    'const __a = 1': ({__a}) => {\n        return true;\n    },\n});\n\n// optional\nexport const exclude = () => [\n    `const hello = 'world'`,\n    'ArrowFunctionExpression',\n];\n```\n\nSimplest remove example:\n\n```js\nexport const report = () => 'debugger should not be used';\n\nexport const replace = () => ({\n    debugger: '',\n}); // debugger; alert(); -> alert();\n```\n\nTemplates:\n\n```js\nexport const report = () => 'any message here';\n\nexport const replace = () => ({\n    'var __a = 1': 'const __a = 1',\n}); // var x = 1; -> const x = 1;\n```\n\nA couple variables example:\n\n```js\nexport const report = () => 'any message here';\n\nexport const replace = () => ({\n    'const __a = __b': 'const __b = __a',\n}); // const hello = world; -> const world = hello;\n```\n\n#### Processing of node using functions\n\nYou can pass a function as object value for more soficticated processing.\n\nRemove node:\n\n```js\nexport const report = () => 'any message here';\n\nexport const replace = () => ({\n    'for (const __a of __b) __c': ({__a, __b, __c}, path) => {\n        // remove node\n        return '';\n    },\n}); // for (a of b) {}; alert(); -> alert();\n```\n\nUpdate node:\n\n```js\nexport const report = () => 'any message here';\n\nexport const replace = () => ({\n    'for (const __a of __array) __c': ({__a, __array, __c}, path) => {\n        // update __array elements\n        path.node.right.elements = [];\n        return path;\n    },\n}); // for (const a of [1, 2, 3]) {}; -> for (const a of []) {};\n```\n\nUpdate node using template variables:\n\n```js\nexport const report = () => 'any message here';\n\nexport const replace = () => ({\n    'for (const __a of __array) __c': ({__a, __array, __c}, path) => {\n        // update the whole node using template string\n        return 'for (const x of y) z';\n    },\n}); // for (const item of array) {}; -> for (const x of y) z;\n```\n\n### Includer\n\n**Includer** is the most preferable format of a plugin, simplest to use (after [`Replacer`](#replacer)):\n\n```js\nexport const report = () => 'debugger statement should not be used';\n\nexport const fix = (path) => {\n    path.remove();\n};\n\nexport const include = () => [\n    'debugger',\n];\n\n// optional\nexport const exclude = () => {};\n\n// optional\nexport const filter = (path, {options}) => {\n    return true;\n};\n```\n\n`include` and `exclude` returns an array of [@babel/types](https://babeljs.io/docs/en/babel-types), or code blocks:\n\n```js\nconst __ = 'hello';\n```\n\nWhere `__` can be any node. All this possible with help of [@putout/compare](https://github.com/coderaiser/putout/tree/master/packages/compare#readme). Templates format supported in `traverse` and `find` plugins as well.\n\n### Traverser\n\n**Traverser** gives you more power to `filter` and `fix` nodes you need.\n\n```js\nexport const report = () => 'debugger statement should not be used';\n\nexport const fix = (path, {options}) => {\n    path.remove();\n};\n\nexport const traverse = ({push}) => ({\n    'debugger'(path) {\n        push(path);\n    },\n});\n```\n\n#### Stores\n\nStores is preferred way of keeping 🐊**Putout** data, `traverse` init function called only once, and any other way\nof handling variables will most likely will lead to bugs. There is a couple store types:\n\n- ✅ [`listStore`](#liststore);\n- ✅ [`pathStore`](#pathstore);\n- ✅ [`store`](#store);\n- ✅ [`upstore`](#upstore);\n- ✅ [`uplist`](#uplist);\n\nLet's talk about each of them.\n\n##### `listStore`\n\nTo save things as a list without duplicates use `listStore`.\nLet's suppose you have such code:\n\n```js\ndebugger;\nconst hello = '';\ndebugger;\nconst world = '';\n```\n\nLet's process it!\n\n```js\nexport const traverse = ({listStore}) => ({\n    'debugger'(path) {\n        listStore(path);\n    },\n    \n    Program: {\n        exit() {\n            console.log(listStore());\n            // returns\n            [{\n                type: 'DebuggerStatement',\n            }, {\n                type: 'DebuggerStatement',\n            }];\n            \n            // for code\n            'debugger; debugger';\n        },\n    },\n});\n```\n\n##### `pathStore`\n\nWhen you want additional check that `path` not removed.\n\n```js\ndebugger;\nconst hello = '';\n```\n\nLet's process it!\n\n```js\nexport const traverse = ({pathStore}) => ({\n    'debugger'(path) {\n        pathStore(path);\n        path.remove();\n    },\n    \n    Program: {\n        exit() {\n            console.log(pathStore());\n            // returns\n            [];\n        },\n    },\n});\n```\n\n##### `store`\n\nWhen you need `key-value` use `store`:\n\n```js\nexport const traverse = ({push, store}) => ({\n    'debugger'(path) {\n        store('hello', 'world');\n        push(path);\n    },\n    \n    Program: {\n        exit() {\n            store();\n            // returns\n            ['world'];\n            \n            store.entries();\n            // returns\n            [\n                ['hello', 'world'],\n            ];\n            \n            store('hello');\n            // returns\n            'world';\n        },\n    },\n});\n```\n\n##### `upstore`\n\nWhen you need to update already saved values, use `upstore`:\n\n```js\nexport const traverse = ({push, upstore}) => ({\n    TSTypeAliasDeclaration(path) {\n        if (path.parentPath.isExportNamedDeclaration())\n            return;\n        \n        upstore(path.node.id.name, {\n            path,\n        });\n    },\n    \n    ObjectProperty(path) {\n        const {value} = path.node;\n        const {name} = value;\n        \n        store(name, {\n            used: true,\n        });\n    },\n    \n    Program: {\n        exit() {\n            for (const {path, used} of upstore()) {\n                if (used)\n                    continue;\n                \n                push(path);\n            }\n        },\n    },\n});\n```\n\n##### `uplist`\n\nWhen you need to update named arrays:\n\n```js\nexport const traverse = ({uplist, push}) => ({\n    'const __object = __a.__b': (fullPath) => {\n        const {__a, __b} = getTemplateValues(fullPath, 'const __object = __a.__b');\n        const initPath = fullPath.get('declarations.0.init');\n        const {uid} = initPath.scope;\n        \n        if (isIdentifier(__a) || isCallExpression(__a)) {\n            const {code} = generate(__a);\n            const id = `${uid}-${code}`;\n            \n            return uplist(id, initPath);\n        }\n    },\n    'Program': {\n        exit: () => {\n            for (const items of uplist()) {\n                if (items.length < 2)\n                    continue;\n                \n                const index = items.length - 1;\n                const path = items[index];\n                \n                push({\n                    path,\n                    items,\n                });\n            }\n        },\n    },\n});\n```\n\n### Scanner\n\n**Scanner** gives you all ability to work with files.\n\n```js\nexport const report = () => 'Create file hello.txt';\n\nexport const fix = (rootPath) => {\n    createFile(rootPath, 'hello.txt', 'hello world');\n};\n\nexport const scan = (rootPath, {push}) => {\n    const [filePath] = findFile(rootPath, 'hello.txt');\n    \n    if (filePath)\n        return null;\n    \n    push(rootPath);\n};\n```\n\nYou can also subscribe to `progress` events:\n\n- `start`;\n- `end`;\n- `push`;\n\nAnd one more: `file` if your plugin uses `progress` function:\n\n```js\nexport const report = () => 'Create file hello.txt';\n\nexport const fix = (rootPath) => {\n    createFile(rootPath, 'hello.txt', 'hello world');\n};\n\nexport const scan = (rootPath, {progress}) => {\n    const [filePath] = findFile(rootPath, 'hello.txt');\n    \n    if (filePath)\n        return null;\n    \n    progress({\n        i: 0,\n        n: 1,\n    });\n    \n    push(filePath);\n};\n```\n\n#### `trackFile`\n\nOr better `trackFile`, which does the same, but also count `progress`:\n\n```js\nexport const report = () => 'Add file';\nexport const fix = (file) => {\n    writeFileContent(file, 'hello');\n};\n\nexport const scan = (rootPath, {push, trackFile}) => {\n    for (const file of trackFile(rootPath, 'hello.txt')) {\n        push(file);\n    }\n};\n```\n\n☝️ *When you use `trackFile` use [`progress()`](https://github.com/coderaiser/putout/tree/master/packages/test#progressfilename-expected) for testing*\n\n#### `crawlFile`\n\nWhen you need to find files related to other files like in [`esm/apply-name-to-imported-file`](https://github.com/coderaiser/putout/blob/master/packages/plugin-esm/README.md#apply-name-to-imported-file) you can end up with:\n\n```js\nexport const report = () => 'Add file';\nexport const fix = (file) => {\n    writeFileContent(file, 'hello');\n};\n\nexport const scan = (rootPath, {push, trackFile}) => {\n    findFile(rootPath, 'again and again');\n};\n```\n\nIt will be very slow! And you can use [`crawlDirectory()`](https://github.com/coderaiser/putout/blob/master/packages/operator-filesystem/README.md#crawldirectorydirectorypath-directorypath-crawled) instead:\n\n```js\nexport const report = () => 'Add file';\nexport const fix = (file) => {\n    writeFileContent(file, 'hello');\n};\n\nexport const scan = (rootPath, {push, trackFile}) => {\n    const crawled = crawlDirectory(rootPath);\n    \n    findFile(rootPath, 'again and again', {\n        crawled,\n    });\n};\n```\n\nBut it is easy to forget, or remove during refactoring, since everything will work as before, only twice slower on big amount of files.\n\nSo you better use **crawlFile**:\n\n```js\nexport const report = () => 'Add file';\nexport const fix = (file) => {\n    writeFileContent(file, 'hello');\n};\n\nexport const scan = (rootPath, {push, trackFile, crawlFile}) => {\n    const files = crawlFile(rootPath, 'no matter how many times');\n};\n```\n\nIt works much faster, use it when you do not mutate filesystem tree: neither remove nor rename files.\n\n### Finder\n\n**Finder** gives you all the control over traversing, but it's the slowest format.\nBecause `traversers` not merged in contrast with other plugin formats.\n\n```js\nexport const report = () => 'debugger statement should not be used';\n\nexport const fix = (path) => {\n    path.remove();\n};\n\nexport const find = (ast, {push, traverse}) => {\n    traverse(ast, {\n        'debugger'(path) {\n            push(path);\n        },\n    });\n};\n```\n\n## Example\n\n```js\nimport {runPlugins} from '@putout/engine-runner';\nimport {parse} from '@putout/engine-parser';\n\nconst plugins = [{\n    rule: 'remove-debugger',\n    msg: '', // optional\n    options: {}, // optional\n    plugin: {\n        include: () => [\n            'debugger',\n        ],\n        fix: (path) => path.remove(),\n        report: () => `debugger should not be used`,\n    },\n}];\n\nconst ast = parse('const m = \"hi\"; debugger');\n\nconst places = runPlugins({\n    ast,\n    shebang: false, // default\n    fix: false, // default\n    fixCount: 2, // default\n    plugins,\n    parser: 'babel', // default\n});\n```\n\n## Logs\n\nTo see logs, use:\n\n- ✅ `DEBUG=putout:runner:*`\n- ✅ `DEBUG=putout:runner:fix`\n- ✅ `DEBUG=putout:runner:find`\n- ✅ `DEBUG=putout:runner:template`\n- ✅ `DEBUG=putout:runner:replace`\n\n## License\n\nMIT\n","publish_time":1743281477921,"_cnpm_publish_time":1743281477921,"dist-tags":{"latest":"28.7.0"}}