Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
feat: add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLeedham committed Nov 26, 2019
1 parent d466c4c commit 28d2b20
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 5 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"bin": {
"baa": "./build/cli/index.js"
},
"name": "browserslist-adobe-analytics",
"version": "0.0.0",
"description": "Use Adobe Analytics data to target browsers.",
Expand All @@ -13,7 +16,8 @@
"author": "Andrew Leedham <Andrew.Leedham@xerox.com>",
"license": "MIT",
"scripts": {
"build": "yarn run build:clean && tsc --project ./tsconfig.build.json",
"build": "yarn run build:clean && tsc --project ./tsconfig.build.json && chmod +x ./build/cli/index.js",
"build:windows": "yarn run build:clean && tsc --project ./tsconfig.build.json",
"build:clean": "rm -rf ./build",
"test": "jest",
"lint": "eslint ./source/ --ext .ts",
Expand Down Expand Up @@ -85,6 +89,8 @@
"@adobe/jwt-auth": "^0.3.0",
"moment": "^2.24.0",
"node-fetch": "^2.6.0",
"semver": "^6.3.0"
"ora": "^4.0.3",
"semver": "^6.3.0",
"yargs": "^15.0.2"
}
}
70 changes: 70 additions & 0 deletions source/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env node

import yargs, { Arguments } from 'yargs';
import { baseOptions, writeOptions } from './options';
import { getBrowserslistStats, writeBrowserslistStats } from '../index';
import { BaseOptions, WriteOptions } from '../types';
import ora from 'ora';

yargs
// Middleware to default all the arguments to environment variables.
.middleware((argv) => {
const options = argv._.includes('write') ? writeOptions : baseOptions;
for (const key of Object.keys(options)) {
if (typeof key === 'string') {
const parts = key.split(/(?=[A-Z])(?<![A-Z])/);
const envar = ['BAA', ...parts].join('_').toUpperCase();
if (envar && process.env[envar]) argv[envar] = process.env[envar];
}
}
})
// Default command to generate and print stats data.
.command(
'$0',
'Generate browserslist stats from Adobe Analytics data.',
(yargs) => yargs.options(baseOptions),
async (args: Arguments<BaseOptions>) => {
const spinner = ora(
'Generating browserslist stats from Adobe Analytics data.'
).start();
try {
const stats = await getBrowserslistStats(args);
spinner.succeed();
console.log(JSON.stringify(stats, null, 2));
} catch (e) {
spinner.fail();
console.error(e);
}
}
)
// Write command to generate and write stats data to file.
.command(
'write',
'Write browserslist stats to file from Adobe Analytics data.',
(yargs) => yargs.options(writeOptions),
async (args: Arguments<WriteOptions>) => {
const spinner = ora(
'Writing browserslist stats to file from Adobe Analytics data.'
).start();
try {
await writeBrowserslistStats(args);
spinner.succeed();
} catch (e) {
spinner.fail();
console.error(e);
}
}
)
// Allow config file and package.json option.
.config()
.pkgConf('baa')
.pkgConf('browserslist-adobe-analytics')
// Personal preference, default is too narrow.
.wrap(Math.min(100, yargs.terminalWidth()))
// Link to moment docs for duration/date formats.
.epilogue(
'For possible duration formats see https://momentjs.com/docs/#/durations/\nFor possible date formats see https://momentjs.com/docs/#/parsing/string/'
)
// Do not allow from and until because they are contradictory.
.conflicts('from', 'until')
.help().argv;
128 changes: 128 additions & 0 deletions source/cli/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { Options } from 'yargs';
import {
WriteOptions,
BaseOptionsWithPrivateKey,
BaseOptionsWithPrivateKeyPath,
} from '../types';

// BaseOptions -> yargs mapping.
export const baseOptions: {
[key in keyof Required<
BaseOptionsWithPrivateKey & BaseOptionsWithPrivateKeyPath
>]: Options;
} = {
clientId: {
alias: 'cid',
type: 'string',
group: 'Adobe Analytics',
describe: 'Integration client ID.',
demandOption: true,
},
technicalAccountId: {
alias: 't',
type: 'string',
group: 'Adobe Analytics',
describe: 'Integration technical account ID.',
demandOption: true,
},
orgId: {
alias: 'o',
type: 'string',
group: 'Adobe Analytics',
describe: 'Organization ID.',
demandOption: true,
},
clientSecret: {
alias: 'cs',
type: 'string',
group: 'Adobe Analytics',
describe: 'Integration client secret.',
demandOption: true,
},
privateKey: {
alias: 'pk',
type: 'string',
group: 'Adobe Analytics',
describe: 'Private key.',
},
privateKeyPath: {
alias: 'pkp',
type: 'string',
group: 'Adobe Analytics',
describe: 'Private key path.',
},
passphrase: {
alias: 'p',
type: 'string',
group: 'Adobe Analytics',
describe: 'Passphrase for private key.',
},
ims: {
alias: 'i',
type: 'string',
group: 'Adobe Analytics',
describe: 'Identity management system ID.',
},
rsid: {
alias: 'r',
type: 'string',
group: 'Adobe Analytics',
describe: 'Resource ID.',
demandOption: true,
},
globalId: {
alias: 'g',
type: 'string',
group: 'Adobe Analytics',
describe: 'Global ID.',
demandOption: true,
},
duration: {
alias: 'd',
type: 'string',
group: 'Time',
describe: 'Period of time to request data for.',
defaultDescription: 'P3M',
},
from: {
alias: 'f',
type: 'string',
group: 'Time',
describe: 'Date to request data from.',
},
until: {
alias: 'u',
type: 'string',
group: 'Time',
describe: 'Date to request data until.',
defaultDescription: "Today's date",
},
limit: {
alias: 'l',
type: 'number',
group: 'Adobe Analytics',
describe: 'The maximum number of browser entries to request.',
defaultDescription: '50',
},
};

// WriteOptions -> yargs mapping.
export const writeOptions: {
[key in keyof Required<WriteOptions>]: Options;
} = {
...baseOptions,
cwd: {
alias: 'c',
type: 'string',
group: 'File Writing',
describe: 'The current working directory to write the file in.',
defaultDescription: 'process.cwd()',
},
filename: {
alias: 'fn',
type: 'string',
group: 'File Writing',
describe: 'The name of the file to write.',
defaultDescription: 'browserslist-stats.json',
},
};
89 changes: 86 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ ansi-styles@^3.2.0, ansi-styles@^3.2.1:
dependencies:
color-convert "^1.9.0"

ansi-styles@^4.0.0:
ansi-styles@^4.0.0, ansi-styles@^4.1.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.2.0.tgz#5681f0dcf7ae5880a7841d8831c4724ed9cc0172"
integrity sha512-7kFQgnEaMdRtwf6uSfUnVr9gSGC7faurn+J/Mv90/W+iTtN0405/nLdopfMWwchyxhbGYl6TC4Sccn9TUkGAgg==
Expand Down Expand Up @@ -1587,6 +1587,14 @@ chalk@2.4.2, chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.2, chalk@^2.4.
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chardet@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
Expand Down Expand Up @@ -1649,6 +1657,18 @@ cli-cursor@^2.1.0:
dependencies:
restore-cursor "^2.0.0"

cli-cursor@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-3.1.0.tgz#264305a7ae490d1d03bf0c9ba7c925d1753af307"
integrity sha512-I/zHAwsKf9FqGoXM4WWRACob9+SNukZTd94DWF57E4toouRulbCxcUh6RKUEOQlYTHJnzkPMySvPNaaSLNfLZw==
dependencies:
restore-cursor "^3.1.0"

cli-spinners@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/cli-spinners/-/cli-spinners-2.2.0.tgz#e8b988d9206c692302d8ee834e7a85c0144d8f77"
integrity sha512-tgU3fKwzYjiLEQgPMD9Jt+JjHVL9kW93FiIMX/l7rivvOD4/LL0Mf7gda3+4U2KJBloybwgj5KEoQgGRioMiKQ==

cli-table3@^0.5.0, cli-table3@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/cli-table3/-/cli-table3-0.5.1.tgz#0252372d94dfc40dbd8df06005f48f31f656f202"
Expand Down Expand Up @@ -3187,6 +3207,11 @@ has-flag@^3.0.0:
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0=

has-flag@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==

has-symbols@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/has-symbols/-/has-symbols-1.0.0.tgz#ba1a8f1af2a0fc39650f5c850367704122063b44"
Expand Down Expand Up @@ -3635,6 +3660,11 @@ is-installed-globally@^0.1.0:
global-dirs "^0.1.0"
is-path-inside "^1.0.0"

is-interactive@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-interactive/-/is-interactive-1.0.0.tgz#cea6e6ae5c870a7b0a0004070b7b587e0252912e"
integrity sha512-2HvIEKRoqS62guEC+qBjpvRubdX910WCMuJTZ+I9yvqKU2/12eSL549HMwtabb4oupdj2sMP50k+XJfB/8JE6w==

is-npm@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-npm/-/is-npm-1.0.0.tgz#f2fb63a65e4905b406c86072765a1a4dc793b9f4"
Expand Down Expand Up @@ -4798,6 +4828,13 @@ lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.15, lodash@^4.17
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==

log-symbols@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-3.0.0.tgz#f3a08516a5dea893336a7dee14d18a1cfdab77c4"
integrity sha512-dSkNGuI7iG3mfvDzUuYZyvk5dD9ocYCYzNU6CYDE6+Xqd+gwme6Z00NS3dUh8mq/73HaEtT7m6W+yUPtU6BZnQ==
dependencies:
chalk "^2.4.2"

loose-envify@^1.0.0, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
Expand Down Expand Up @@ -5165,7 +5202,7 @@ mute-stream@0.0.7:
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
integrity sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=

mute-stream@~0.0.4:
mute-stream@0.0.8, mute-stream@~0.0.4:
version "0.0.8"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==
Expand Down Expand Up @@ -5733,6 +5770,20 @@ optionator@^0.8.1, optionator@^0.8.2:
type-check "~0.3.2"
wordwrap "~1.0.0"

ora@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/ora/-/ora-4.0.3.tgz#752a1b7b4be4825546a7a3d59256fa523b6b6d05"
integrity sha512-fnDebVFyz309A73cqCipVL1fBZewq4vwgSHfxh43vVy31mbyoQ8sCH3Oeaog/owYOs/lLlGVPCISQonTneg6Pg==
dependencies:
chalk "^3.0.0"
cli-cursor "^3.1.0"
cli-spinners "^2.2.0"
is-interactive "^1.0.0"
log-symbols "^3.0.0"
mute-stream "0.0.8"
strip-ansi "^6.0.0"
wcwidth "^1.0.1"

os-homedir@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"
Expand Down Expand Up @@ -6632,6 +6683,14 @@ restore-cursor@^2.0.0:
onetime "^2.0.0"
signal-exit "^3.0.2"

restore-cursor@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-3.1.0.tgz#39f67c54b3a7a58cea5236d95cf0034239631f7e"
integrity sha512-l+sSefzHpj5qimhFSE5a8nufZYAM3sBSVMAPtYkmC+4EH2anSGaEMXSD0izRQbu9nfyQ9y5JrVmp7E8oZrUjvA==
dependencies:
onetime "^5.1.0"
signal-exit "^3.0.2"

ret@~0.1.10:
version "0.1.15"
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
Expand Down Expand Up @@ -7283,6 +7342,13 @@ supports-color@^6.1.0:
dependencies:
has-flag "^3.0.0"

supports-color@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.1.0.tgz#68e32591df73e25ad1c4b49108a2ec507962bfd1"
integrity sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==
dependencies:
has-flag "^4.0.0"

supports-hyperlinks@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/supports-hyperlinks/-/supports-hyperlinks-1.0.1.tgz#71daedf36cc1060ac5100c351bb3da48c29c0ef7"
Expand Down Expand Up @@ -7732,7 +7798,7 @@ walker@^1.0.7, walker@~1.0.5:
dependencies:
makeerror "1.0.x"

wcwidth@^1.0.0:
wcwidth@^1.0.0, wcwidth@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/wcwidth/-/wcwidth-1.0.1.tgz#f0b0dcf915bc5ff1528afadb2c0e17b532da2fe8"
integrity sha1-8LDc+RW8X/FSivrbLA4XtTLaL+g=
Expand Down Expand Up @@ -8016,3 +8082,20 @@ yargs@^15.0.1:
which-module "^2.0.0"
y18n "^4.0.0"
yargs-parser "^16.1.0"

yargs@^15.0.2:
version "15.0.2"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.0.2.tgz#4248bf218ef050385c4f7e14ebdf425653d13bd3"
integrity sha512-GH/X/hYt+x5hOat4LMnCqMd8r5Cv78heOMIJn1hr7QPPBqfeC6p89Y78+WB9yGDvfpCvgasfmWLzNzEioOUD9Q==
dependencies:
cliui "^6.0.0"
decamelize "^1.2.0"
find-up "^4.1.0"
get-caller-file "^2.0.1"
require-directory "^2.1.1"
require-main-filename "^2.0.0"
set-blocking "^2.0.0"
string-width "^4.2.0"
which-module "^2.0.0"
y18n "^4.0.0"
yargs-parser "^16.1.0"

0 comments on commit 28d2b20

Please sign in to comment.