diff --git a/README.md b/README.md index dd36a5f..b2cca69 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Analyzes tracking code in a project and generates data schemas ## Usage ```sh -npx @flisk/analyze-tracking /path/to/project +npx @flisk/analyze-tracking /path/to/project [options] ``` Optional arguments: diff --git a/bin/analyze-tracking.js b/bin/cli.js similarity index 50% rename from bin/analyze-tracking.js rename to bin/cli.js index 97a358f..02e1591 100755 --- a/bin/analyze-tracking.js +++ b/bin/cli.js @@ -2,21 +2,37 @@ const path = require('path'); const { execSync } = require('child_process'); +const commandLineArgs = require('command-line-args') const { run } = require('../src/index'); // Parse command-line arguments -const targetDir = process.argv[2]; -const repositoryArgIndex = process.argv.indexOf('--repository'); -const repositoryUrl = repositoryArgIndex !== -1 ? process.argv[repositoryArgIndex + 1] : null; -const outputArgIndex = process.argv.indexOf('--output'); -const outputPath = outputArgIndex !== -1 ? process.argv[outputArgIndex + 1] : 'tracking-schema.yaml'; +const optionDefinitions = [ + { + name: 'targetDir', + type: String, + defaultOption: true, + }, + { + name: 'repository', + alias: 'r', + type: String, + }, + { + name: 'output', + alias: 'o', + type: String, + defaultValue: 'tracking-schema.yaml', + }, +] +const options = commandLineArgs(optionDefinitions); +const { targetDir, output, repository } = options; if (!targetDir) { console.error('Please provide the path to the repository.'); process.exit(1); } -// Function to get the repository URL using Git +// Get the repository URL using Git function getRepositoryUrl() { try { const repoUrl = execSync('git config --get remote.origin.url', { cwd: targetDir, encoding: 'utf8' }); @@ -27,7 +43,6 @@ function getRepositoryUrl() { } } -// Determine the repository URL -const repository = repositoryUrl || getRepositoryUrl(); +const repositoryUrl = repository || getRepositoryUrl(); -run(path.resolve(targetDir), repository, outputPath); +run(path.resolve(targetDir), repositoryUrl, output); diff --git a/package-lock.json b/package-lock.json index 0d2badd..21abaf1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,22 +1,23 @@ { "name": "@flisk/analyze-tracking", - "version": "0.1.0", + "version": "0.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@flisk/analyze-tracking", - "version": "0.1.0", + "version": "0.1.2", "license": "MIT", "dependencies": { "@typescript-eslint/parser": "^8.1.0", "acorn": "^8.12.1", "acorn-walk": "^8.3.3", + "command-line-args": "^6.0.0", "js-yaml": "^4.1.0", "typescript": "^5.5.4" }, "bin": { - "analyze-tracking": "bin/analyze-tracking.js" + "analyze-tracking": "bin/cli.js" }, "devDependencies": { "jest": "^29.7.0" @@ -1675,6 +1676,15 @@ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", "license": "Python-2.0" }, + "node_modules/array-back": { + "version": "6.2.2", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-6.2.2.tgz", + "integrity": "sha512-gUAZ7HPyb4SJczXAMUXMGAvI976JoK3qEx9v1FTmeYuJj0IBiaKttG1ydtGKdkfqWkIkouke7nG8ufGy77+Cvw==", + "license": "MIT", + "engines": { + "node": ">=12.17" + } + }, "node_modules/array-union": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz", @@ -2027,6 +2037,21 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "license": "MIT" }, + "node_modules/command-line-args": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/command-line-args/-/command-line-args-6.0.0.tgz", + "integrity": "sha512-zDdHxHzlCp/gA1gy0VtPK3YL0Aob3ijJdwZ7H3HSl55hh8EziLtRlyj/od8EGRJfX8IjussC/mQkScl2Ms5Suw==", + "license": "MIT", + "dependencies": { + "array-back": "^6.2.2", + "find-replace": "^5.0.1", + "lodash.camelcase": "^4.3.0", + "typical": "^7.1.1" + }, + "engines": { + "node": ">=12.20" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -2576,6 +2601,18 @@ "node": ">=8" } }, + "node_modules/find-replace": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/find-replace/-/find-replace-5.0.1.tgz", + "integrity": "sha512-o5/Y8HrCNRuFF5rdNTkX8Vhv6kTFTV0t1zIoigwlCdbkA9qaapRzxvWPND2VvlFa9LBI05Q1i8ml/saMqkOJUQ==", + "license": "MIT", + "dependencies": { + "array-back": "^6.2.2" + }, + "engines": { + "node": ">=14" + } + }, "node_modules/find-up": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", @@ -3797,6 +3834,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/lodash.camelcase": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", + "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==", + "license": "MIT" + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", @@ -4753,6 +4796,15 @@ "node": ">=14.17" } }, + "node_modules/typical": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/typical/-/typical-7.1.1.tgz", + "integrity": "sha512-T+tKVNs6Wu7IWiAce5BgMd7OZfNYUndHwc5MknN+UHOudi7sGZzuHdCadllRuqJ3fPtgFtIH9+lt9qRv6lmpfA==", + "license": "MIT", + "engines": { + "node": ">=12.17" + } + }, "node_modules/undici-types": { "version": "6.19.8", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", diff --git a/package.json b/package.json index 487c8c1..009107b 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,12 @@ { "name": "@flisk/analyze-tracking", - "version": "0.1.1", + "version": "0.1.2", "description": "Analyzes tracking code in a project and generates data schemas", "main": "src/index.js", "bin": { - "analyze-tracking": "bin/analyze-tracking.js" + "analyze-tracking": "bin/cli.js" }, "scripts": { - "start": "node bin/analyze-tracking.js", "test": "jest" }, "repository": { @@ -24,6 +23,7 @@ "@typescript-eslint/parser": "^8.1.0", "acorn": "^8.12.1", "acorn-walk": "^8.3.3", + "command-line-args": "^6.0.0", "js-yaml": "^4.1.0", "typescript": "^5.5.4" }, diff --git a/src/cli.js b/src/cli.js deleted file mode 100644 index 7e08d95..0000000 --- a/src/cli.js +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env node - -const { run } = require('./index'); -const targetDir = process.argv[2]; - -if (!targetDir) { - console.error('Please provide the path to the repository.'); - process.exit(1); -} - -run(path.resolve(targetDir));