Skip to content

Commit

Permalink
fix cli arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
skarim committed Aug 21, 2024
1 parent 1a2d575 commit 0256973
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 24 additions & 9 deletions bin/analyze-tracking.js → bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand All @@ -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);
58 changes: 55 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
},
Expand Down
11 changes: 0 additions & 11 deletions src/cli.js

This file was deleted.

0 comments on commit 0256973

Please sign in to comment.