{"id":17045,"library":"opt-cli","title":"Opt-in/Opt-out CLI Task Executor","description":"opt-cli is a Node.js utility designed to conditionally execute CLI statements based on opt-in or opt-out rules. Its primary use case, as highlighted in its documentation, is for managing `ghooks` or similar pre-commit/pre-push scripts, allowing developers to enable or disable specific tasks without modifying `package.json` scripts directly. Rules are defined in `.opt-in` or `.opt-out` files in the project root, or via the `config.opt` field in `package.json`. The package has been stable at version 1.6.0 since October 2017, with no further releases or active development observed, indicating an abandoned status. It explicitly supports Node.js versions `>=4` and primarily functions as a CommonJS module, without apparent ESM support.","status":"abandoned","version":"1.6.0","language":"javascript","source_language":"en","source_url":"https://github.com/ta2edchimp/opt-cli","tags":["javascript","executer","cli","opt-in","opt-out"],"install":[{"cmd":"npm install opt-cli","lang":"bash","label":"npm"},{"cmd":"yarn add opt-cli","lang":"bash","label":"yarn"},{"cmd":"pnpm add opt-cli","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a CommonJS module and does not support native ESM import syntax.","wrong":"import opt from 'opt-cli';\nimport { opt } from 'opt-cli';","symbol":"opt","correct":"const opt = require('opt-cli');"},{"note":"Methods are accessed as properties of the default `opt` export, not as named exports.","wrong":"import { testOptIn } from 'opt-cli';","symbol":"testOptIn","correct":"opt.testOptIn('rule-name');"},{"note":"Methods are accessed as properties of the default `opt` export, not as named exports.","wrong":"import { getExplicitOpts } from 'opt-cli';","symbol":"getExplicitOpts","correct":"opt.getExplicitOpts();"}],"quickstart":{"code":"const fs = require('fs');\nconst path = require('path');\nconst opt = require('opt-cli');\n\nconst optInFilePath = path.join(process.cwd(), '.opt-in');\nconst optOutFilePath = path.join(process.cwd(), '.opt-out');\n\nasync function runExample() {\n  try {\n    // Create a dummy .opt-in file for demonstration\n    fs.writeFileSync(optInFilePath, 'pre-commit\\nlint-check', 'utf8');\n    console.log(`Created ${optInFilePath} with rules: pre-commit, lint-check`);\n\n    // Test opt-in for 'pre-commit'\n    const shouldRunPreCommit = opt.testOptIn('pre-commit');\n    console.log(`Should 'pre-commit' task run (opt-in)? ${shouldRunPreCommit}`); // Expected: true\n\n    // Test opt-in for 'test-task' (not in .opt-in)\n    const shouldRunTestTask = opt.testOptIn('test-task');\n    console.log(`Should 'test-task' run (opt-in)? ${shouldRunTestTask}`); // Expected: false\n\n    // Create a dummy .opt-out file for demonstration\n    fs.writeFileSync(optOutFilePath, 'pre-push', 'utf8');\n    console.log(`Created ${optOutFilePath} with rule: pre-push`);\n\n    // Test opt-out for 'pre-push' (meaning it is opted out, so it won't run)\n    const shouldRunPrePush = opt.testOptOut('pre-push');\n    console.log(`Should 'pre-push' task run (opt-out)? ${shouldRunPrePush}`); // Expected: false\n\n    // Test opt-out for 'build' (meaning it is not opted out, so it will run)\n    const shouldRunBuild = opt.testOptOut('build');\n    console.log(`Should 'build' task run (opt-out)? ${shouldRunBuild}`); // Expected: true\n\n    // Note: getExplicitOpts primarily reflects 'config.opt' in package.json, not file content directly\n    console.log('\\nDemonstrating CLI usage (run these in your terminal after installing opt-cli):');\n    console.log(`$ npx opt --in pre-commit --exec 'echo \"Pre-commit task ran if opted in!\"'`);\n    console.log(`$ npx opt --out pre-push --exec 'echo \"Pre-push task ran if NOT opted out!\"'`);\n\n  } catch (error) {\n    console.error('An error occurred:', error);\n  } finally {\n    // Clean up dummy files\n    if (fs.existsSync(optInFilePath)) {\n      fs.unlinkSync(optInFilePath);\n      console.log(`Removed ${optInFilePath}`);\n    }\n    if (fs.existsSync(optOutFilePath)) {\n      fs.unlinkSync(optOutFilePath);\n      console.log(`Removed ${optOutFilePath}`);\n    }\n  }\n}\n\nrunExample();","lang":"javascript","description":"This example demonstrates how to use `opt-cli` programmatically to check opt-in and opt-out rules by simulating the creation of `.opt-in` and `.opt-out` files, and then using `testOptIn` and `testOptOut` methods. It also illustrates how to retrieve combined explicit configurations and suggests CLI usage."},"warnings":[{"fix":"Use CommonJS `const opt = require('opt-cli');` in CJS files, or consider a build step (e.g., Babel, Webpack) for ESM projects that need to consume CJS packages.","message":"This package is a CommonJS module and is not compatible with native ES Modules (ESM) import syntax. Attempting to `import opt from 'opt-cli'` in an ESM context will result in a `ReferenceError`.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Evaluate the necessity of this package. For new projects, consider modern alternatives or implement similar logic manually. For existing projects, be aware of the lack of maintenance and potential security implications.","message":"The `opt-cli` project appears to be abandoned, with its last release (v1.6.0) in October 2017. This means it no longer receives updates for new features, bug fixes, or security patches, posing potential risks for long-term projects or compatibility with newer Node.js versions.","severity":"gotcha","affected_versions":">=1.6.0"},{"fix":"Thoroughly test `opt-cli` against your target Node.js version. If issues arise, a manual fork and fix, or migration to an alternative, may be necessary.","message":"While the package states `engines: {'node': '>=4'}`, newer Node.js versions (e.g., Node.js 14, 16, 18+) may introduce breaking changes or deprecations in core modules (like `child_process`) that this unmaintained package might not handle correctly, leading to unexpected behavior or runtime errors.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Upgrade to `opt-cli` version 1.5.0 or later immediately. Given the project's abandoned status, further security issues will not be patched.","message":"Versions of `opt-cli` prior to 1.5.0 contained a vulnerability in its CLI module. This was addressed in version 1.5.0. Ensure you are using `opt-cli` version 1.5.0 or higher to mitigate this known issue.","severity":"security","affected_versions":"<1.5.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure your file is treated as CommonJS (e.g., `.js` file without `\"type\": \"module\"` in `package.json`, or explicitly `.cjs` extension) when using `require`. If your project is ESM, `opt-cli` cannot be directly imported; consider a wrapper or alternative.","cause":"Attempting to use `require()` in an ES Module context, or `import` syntax in a CommonJS context.","error":"ReferenceError: require is not defined"},{"fix":"Verify `opt-cli` is correctly installed (`npm list opt-cli`). Ensure the `require('opt-cli')` statement is at the top level of your CommonJS module. Check that the file where `require` is used is indeed treated as CommonJS.","cause":"This error typically means the `opt` variable is `undefined` or not correctly resolved, indicating a failure to import the module or that `require('opt-cli')` did not return the expected object.","error":"TypeError: Cannot read properties of undefined (reading 'testOptIn')"},{"fix":"Ensure the `.opt-in` or `.opt-out` files are present in the root of your project (or `process.cwd()`). Verify file permissions allow Node.js to read them. Alternatively, specify rules within the `config.opt` field of your `package.json`.","cause":"The `opt-cli` library expects `.opt-in` or `.opt-out` files to exist in the current working directory, or a `config.opt` entry in `package.json`, and one of these was not found or accessible.","error":"Error: ENOENT: no such file or directory, open '.opt-in'"}],"ecosystem":"npm","meta_description":null}