Skip to content

Commit

Permalink
feat(cli): allow inline arguments for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
smellai committed Sep 20, 2022
1 parent d2064e5 commit 7ebeeec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Archived repositories are filtered out.

> :warning: GitHub API are rate limited, and search API in particular has the additional [secondary rate limit](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#secondary-rate-limits). _package-adoption_ implements the [best practices guidelines](https://docs.github.com/en/rest/guides/best-practices-for-integrators#dealing-with-secondary-rate-limits) to deal with it, but you should know that limitations could happen in any case.
> :warning: GitHub search is not 100% reliable and sometimes returns deleted / outdated files or multiple versions of the same file. The library version in the output could be inaccurate for this reason.
> :warning: GitHub search API is not 100% reliable and sometimes returns deleted / outdated files or multiple versions of the same file. The library version in the output could be inaccurate for this reason.
## Install

Expand Down Expand Up @@ -63,18 +63,24 @@ npm install -g package-adoption
package-adoption --config /path/to/config.js --output /path/to/output.json
```

If output option omitted, `package-adoption` ouputs to stdout.
If output file path omitted, `package-adoption` ouputs to stdout.
When config option omitted, default for config file will be local `config.js`. The file must export an object like this:

```ts
module.exports = {
ORG: 'myOrg',
DAYS_UNTIL_STALE: 90,
DAYS_UNTIL_STALE: 90, // If omitted, 360 will be used as default
GH_AUTHTOKEN: 'my-GH-auth-token',
PKG_NAME: 'myPkg',
};
```

### With inline arguments

```bash
package-adoption --org=myOrg --token=my-GH-auth-token --pkg=myPkg --output /path/to/output.json
```

[build-img]: https://github.com/jimdo/package-adoption/actions/workflows/release.yml/badge.svg
[build-url]: https://github.com/jimdo/package-adoption/actions/workflows/release.yml
[downloads-img]: https://img.shields.io/npm/dt/package-adoption
Expand Down
43 changes: 23 additions & 20 deletions bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,55 @@ import { cwd } from 'process';
import fs from 'fs';

import { getFilteredReposWithPackageForOrg } from '../src/getFilteredReposWithPackageForOrg';
import { Config } from '../src/types';

const argv = yargs(process.argv.slice(2)).options({
config: { type: 'string', default: 'config.js' },
output: { type: 'string' },
'days-until-stale': { type: 'number', default: 360 },
pkg: { type: 'string' },
token: { type: 'string' },
org: { type: 'string' },
}).argv;

interface Config {
ORG: string;
PKG_NAME: string;
GH_AUTHTOKEN: string;
DAYS_UNTIL_STALE: number;
}

const validateConfig = (config: Config) => {
const errors: string[] = [];

if (config.ORG === undefined) {
errors.push('ORG is missing in the config file');
if (config.ORG === undefined || config.ORG === '') {
errors.push('ORG argument is missing');
} else if (typeof config.ORG !== 'string') {
errors.push('ORG must be a string');
}

if (config.PKG_NAME === undefined) {
errors.push('PKG_NAME is missing in the config file');
if (config.PKG_NAME === undefined || config.PKG_NAME === '') {
errors.push('PKG NAME argument is missing');
} else if (typeof config.PKG_NAME !== 'string') {
errors.push('PKG_NAME must be a string');
}

if (config.GH_AUTHTOKEN === undefined) {
errors.push('GH_AUTHTOKEN is missing in the config file');
if (config.GH_AUTHTOKEN === undefined || config.GH_AUTHTOKEN === '') {
errors.push('GH_AUTHTOKEN argument is missing');
} else if (typeof config.GH_AUTHTOKEN !== 'string') {
errors.push('GH_AUTHTOKEN must be a string');
}

if (config.DAYS_UNTIL_STALE === undefined) {
errors.push('DAYS_UNTIL_STALE is missing in the config file');
} else if (typeof config.DAYS_UNTIL_STALE !== 'number') {
errors.push('DAYS_UNTIL_STALE must be a string');
}

return errors;
};

(async () => {
const configPath = path.resolve(cwd(), argv.config);
const config: Config = require(configPath);
let config: Config;

try {
config = require(configPath);
} catch {
config = {
ORG: argv.org || '',
PKG_NAME: argv.pkg || '',
GH_AUTHTOKEN: argv.token || '',
DAYS_UNTIL_STALE: argv['days-until-stale'],
};
}

const { ORG, PKG_NAME, GH_AUTHTOKEN, DAYS_UNTIL_STALE } = config;

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { getFilteredReposWithPackageForOrg } from './getFilteredReposWithPackageForOrg';
export { RelevantRepo } from './types';
export { RelevantRepo, Config } from './types';
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface InputParameters {
org: string;
/**
* To filter out stale repositories
* @default 365
*/
daysUntilStale: number;
/**
Expand All @@ -40,3 +41,10 @@ export interface InputParameters {
*/
pkgName: string;
}

export interface Config {
ORG: string;
PKG_NAME: string;
GH_AUTHTOKEN: string;
DAYS_UNTIL_STALE: number;
}

0 comments on commit 7ebeeec

Please sign in to comment.