Opt-in/Opt-out CLI Task Executor
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.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module context, or `import` syntax in a CommonJS context.fixEnsure 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. -
TypeError: Cannot read properties of undefined (reading 'testOptIn')
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.fixVerify `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. -
Error: ENOENT: no such file or directory, open '.opt-in'
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.fixEnsure 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`.
Warnings
- breaking 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`.
- gotcha 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.
- breaking 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.
- security 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.
Install
-
npm install opt-cli -
yarn add opt-cli -
pnpm add opt-cli
Imports
- opt
import opt from 'opt-cli'; import { opt } from 'opt-cli';const opt = require('opt-cli'); - testOptIn
import { testOptIn } from 'opt-cli';opt.testOptIn('rule-name'); - getExplicitOpts
import { getExplicitOpts } from 'opt-cli';opt.getExplicitOpts();
Quickstart
const fs = require('fs');
const path = require('path');
const opt = require('opt-cli');
const optInFilePath = path.join(process.cwd(), '.opt-in');
const optOutFilePath = path.join(process.cwd(), '.opt-out');
async function runExample() {
try {
// Create a dummy .opt-in file for demonstration
fs.writeFileSync(optInFilePath, 'pre-commit\nlint-check', 'utf8');
console.log(`Created ${optInFilePath} with rules: pre-commit, lint-check`);
// Test opt-in for 'pre-commit'
const shouldRunPreCommit = opt.testOptIn('pre-commit');
console.log(`Should 'pre-commit' task run (opt-in)? ${shouldRunPreCommit}`); // Expected: true
// Test opt-in for 'test-task' (not in .opt-in)
const shouldRunTestTask = opt.testOptIn('test-task');
console.log(`Should 'test-task' run (opt-in)? ${shouldRunTestTask}`); // Expected: false
// Create a dummy .opt-out file for demonstration
fs.writeFileSync(optOutFilePath, 'pre-push', 'utf8');
console.log(`Created ${optOutFilePath} with rule: pre-push`);
// Test opt-out for 'pre-push' (meaning it is opted out, so it won't run)
const shouldRunPrePush = opt.testOptOut('pre-push');
console.log(`Should 'pre-push' task run (opt-out)? ${shouldRunPrePush}`); // Expected: false
// Test opt-out for 'build' (meaning it is not opted out, so it will run)
const shouldRunBuild = opt.testOptOut('build');
console.log(`Should 'build' task run (opt-out)? ${shouldRunBuild}`); // Expected: true
// Note: getExplicitOpts primarily reflects 'config.opt' in package.json, not file content directly
console.log('\nDemonstrating CLI usage (run these in your terminal after installing opt-cli):');
console.log(`$ npx opt --in pre-commit --exec 'echo "Pre-commit task ran if opted in!"'`);
console.log(`$ npx opt --out pre-push --exec 'echo "Pre-push task ran if NOT opted out!"'`);
} catch (error) {
console.error('An error occurred:', error);
} finally {
// Clean up dummy files
if (fs.existsSync(optInFilePath)) {
fs.unlinkSync(optInFilePath);
console.log(`Removed ${optInFilePath}`);
}
if (fs.existsSync(optOutFilePath)) {
fs.unlinkSync(optOutFilePath);
console.log(`Removed ${optOutFilePath}`);
}
}
}
runExample();