Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to use the CLI programmatically #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion bin/logrocket
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
#!/usr/bin/env node

require('../dist/index.js');
import yargs from 'yargs';

process.on('unhandledRejection', (err) => {
console.error(err.message);
process.exit(1);
});

yargs // eslint-disable-line no-unused-expressions
.usage('\nUsage: logrocket [-k <apikey>] <command> [<args>]')
.env('LOGROCKET')
.alias('h', 'help')
.option('k', {
alias: 'apikey',
type: 'string',
describe: 'Your LogRocket API key',
demand: 'You must provide a LogRocket API key.',
global: true,
requiresArg: true,
})
.option('apihost', { // testing param to override api url
type: 'string',
describe: false,
})
.option('v', { // for debugging
alias: 'verbose',
boolean: true,
describe: false,
})
.commandDir('../src/commands')
.help()
.demand(1, 'Missing command, expected `release` or `upload`')
.recommendCommands()
.argv;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./src');
module.exports = require('./dist');
1 change: 0 additions & 1 deletion src/apiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ApiClient {

async _makeRequest({ url, data }) {
const [orgSlug, appSlug] = this.apikey.split(':');

return fetch(`${this.apihost}/v1/orgs/${orgSlug}/apps/${appSlug}/${url}/`, {
method: 'POST',
headers: this.headers,
Expand Down
3 changes: 3 additions & 0 deletions src/commands/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export const builder = (args) => {
};

export const handler = async ({ version, strict, apikey, apihost, verbose }) => {
if (!version) throw new Error('Missing release version');
if (!apikey) throw new Error('Missing api key');

console.info(`Creating release: ${version} ...`);

const client = apiClient({ apikey, apihost });
Expand Down
9 changes: 8 additions & 1 deletion src/commands/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ async function gatherFiles(paths) {
}

export const handler = async (args) => {
const { paths, release, apikey, apihost, verbose, urlPrefix } = args;
const { release, apikey, apihost, verbose, urlPrefix = '' } = args;
let { paths } = args;
if (!release) throw new Error('Missing release version');
if (!paths) throw new Error('Missing paths');
if (!apikey) throw new Error('Missing api key');
if (typeof paths === 'string') {
paths = [paths];
}

console.info(`Preparing to upload sourcemaps for release ${release} ...`);
console.info('Gathering file list...');
Expand Down
35 changes: 3 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
import yargs from 'yargs';
const { handler: releaseHandler } = require('./commands/release');
const { handler: uploadHandler } = require('./commands/upload');

process.on('unhandledRejection', (err) => {
console.error(err.message);
process.exit(1);
});

yargs // eslint-disable-line no-unused-expressions
.usage('\nUsage: logrocket [-k <apikey>] <command> [<args>]')
.env('LOGROCKET')
.alias('h', 'help')
.option('k', {
alias: 'apikey',
type: 'string',
describe: 'Your LogRocket API key',
demand: 'You must provide a LogRocket API key.',
global: true,
requiresArg: true,
})
.option('apihost', { // testing param to override api url
type: 'string',
describe: false,
})
.option('v', { // for debugging
alias: 'verbose',
boolean: true,
describe: false,
})
.commandDir('commands')
.help()
.demand(1, 'Missing command, expected `release` or `upload`')
.recommendCommands()
.argv;
module.exports = { release: releaseHandler, upload: uploadHandler };