This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d466c4c
commit 28d2b20
Showing
4 changed files
with
292 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters