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.
Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
This package is a CommonJS module and does not support native ESM import syntax.
const opt = require('opt-cli');
Methods are accessed as properties of the default `opt` export, not as named exports.
opt.testOptIn('rule-name');
Methods are accessed as properties of the default `opt` export, not as named exports.
opt.getExplicitOpts();
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.
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();
opt --version
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.
Agent activity
0 hits · last 30 days
No traffic data recorded yet.