Skip to content

Commit

Permalink
feature: Get application, version, and database automatically (#31)
Browse files Browse the repository at this point in the history
Gets application, and version from package.json if it exists in the current working directory. Gets database from SYMBOL_UPLOAD_DATABASE env variable.

Fixes #21
  • Loading branch information
bobbyg603 authored Nov 18, 2021
1 parent 1c0efbe commit 24bed05
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 104 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
dist
dist
.env
.DS_Store
26 changes: 8 additions & 18 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,17 @@
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"name": "Launch via NPM",
"request": "launch",
"name": "Launch Program",
"runtimeArgs": [
"run-script",
"start"
],
"runtimeExecutable": "npm",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/bin/index.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"args": [
"-database",
"fred",
"-application",
"my-ts-crasher",
"-version",
"1.0.0",
"-directory",
"./dist",
],
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
"type": "pwa-node"
},
]
}
55 changes: 30 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,35 @@ bobby@BugSplat % ~ % symbol-upload -h

Usage

-h, --help Print this usage guide.
-b, --database string Your BugSplat database name.
-a, --application string The name of your application. The value of application must match the
value used to post crash reports.
-v, --version string Your application's version. The value of version must match the value
used to post crash reports.
-u, --user string (optional) The email address used to log into your BugSplat account. If provided
--password must also be provided. This value can also be provided via the
SYMBOL_UPLOAD_USER environment variable.
-p, --password string (optional) The password for your BugSplat account. If provided --user must also be
provided. This value can also be provided via the SYMBOL_UPLOAD_PASSWORD
environment variable.
-i, --clientId string (optional) An OAuth2 Client Credentials Client ID for the specified database. If
provided --clientSecret must also be provided. This value can also be
provided via the SYMBOL_UPLOAD_CLIENT_ID environment variable.
-s, --clientSecret string (optional) An OAuth2 Client Credentials Client Secret for the specified database. If
provided --clientId must also be provided. This value can also be
provided via the SYMBOL_UPLOAD_CLIENT_SECRET environment variable.
-r, --remove Removes symbols for a specified database, application, and version. If
this option is provided no other actions are taken.
-f, --files string (optional) Glob pattern that specifies a set of files to upload. Defaults to
'*.js.map'
-d, --directory string (optional) Path of the base directory used to search for symbol files. This value
will be combined with the --files glob. Defaults to '.'
-h, --help Print this usage guide.
-b, --database string Your BugSplat database name. The value of database must match the value used
to post crash reports. This value can also be provided via the
BUGSPLAT_DATABASE environment variable.
-a, --application string The name of your application. The value of application must match the value
used to post crash reports. If not provided symbol-upload will attempt to use
the value of the name field in package.json if it exists in the current
working directory.
-v, --version string Your application's version. The value of version must match the value used to
post crash reports. If not provided symbol-upload will attempt to use the
value of the version field in package.json if it exists in the current
working directory.
-u, --user string (optional) The email address used to log into your BugSplat account. If provided
--password must also be provided. This value can also be provided via the
SYMBOL_UPLOAD_USER environment variable.
-p, --password string (optional) The password for your BugSplat account. If provided --user must also be
provided. This value can also be provided via the SYMBOL_UPLOAD_PASSWORD
environment variable.
-i, --clientId string (optional) An OAuth2 Client Credentials Client ID for the specified database. If
provided --clientSecret must also be provided. This value can also be
provided via the SYMBOL_UPLOAD_CLIENT_ID environment variable.
-s, --clientSecret string (optional) An OAuth2 Client Credentials Client Secret for the specified database. If
provided --clientId must also be provided. This value can also be provided
via the SYMBOL_UPLOAD_CLIENT_SECRET environment variable.
-r, --remove Removes symbols for a specified database, application, and version. If this
option is provided no other actions are taken.
-f, --files string (optional) Glob pattern that specifies a set of files to upload. Defaults to '*.js.map'
-d, --directory string (optional) Path of the base directory used to search for symbol files. This value will
be combined with the --files glob. Defaults to '.'
The -u and -p arguments are not required if you set the environment variables
SYMBOL_UPLOAD_USER and SYMBOL_UPLOAD_PASSWORD, or provide a clientId and
Expand All @@ -61,7 +66,7 @@ Links
💻 https://github.com/BugSplat-Git/symbol-upload
💌 [email protected]
💌 [email protected]
```
3. Run symbol-upload specifying a [glob](https://www.npmjs.com/package/glob#glob-primer) pattern for `-f` and a path with forward slashes for `-d`
Expand Down
6 changes: 3 additions & 3 deletions bin/command-line-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ export const argDefinitions: Array<CommandLineDefinition> = [
alias: 'b',
type: String,
typeLabel: '{underline string}',
description: 'Your BugSplat database name.'
description: 'Your BugSplat database name. The value of database must match the value used to post crash reports. This value can also be provided via the BUGSPLAT_DATABASE environment variable.'
},
{
name: 'application',
alias: 'a',
type: String,
typeLabel: '{underline string}',
description: 'The name of your application. The value of application must match the value used to post crash reports.',
description: 'The name of your application. The value of application must match the value used to post crash reports. If not provided symbol-upload will attempt to use the value of the name field in package.json if it exists in the current working directory.',
},
{
name: 'version',
alias: 'v',
type: String,
typeLabel: '{underline string}',
description: 'Your application\'s version. The value of version must match the value used to post crash reports.',
description: 'Your application\'s version. The value of version must match the value used to post crash reports. If not provided symbol-upload will attempt to use the value of the version field in package.json if it exists in the current working directory.',
},
{
name: 'user',
Expand Down
147 changes: 91 additions & 56 deletions bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
#! /usr/bin/env node
import { ApiClient, BugSplatApiClient, OAuthClientCredentialsClient, SymbolsApiClient } from '@bugsplat/js-api-client';
import commandLineArgs from 'command-line-args';
import commandLineArgs, { CommandLineOptions } from 'command-line-args';
import commandLineUsage from 'command-line-usage';
import fs from 'fs';
import { readFile, stat } from 'fs/promises';
import glob from 'glob-promise';
import { basename } from 'path';
import { argDefinitions, usageDefinitions } from './command-line-definitions';
import { argDefinitions, CommandLineDefinition, usageDefinitions } from './command-line-definitions';

let {
help,
database,
application,
version,
user,
password,
clientId,
clientSecret,
remove,
files,
directory
} = commandLineArgs(argDefinitions);

if (help) {
logHelpAndExit();
}

if (!database) {
logMissingArgAndExit('database');
}

if (!application) {
logMissingArgAndExit('application');
}

if (!version) {
logMissingArgAndExit('version');
}

user = user ?? process.env.SYMBOL_UPLOAD_USER;
password = password ?? process.env.SYMBOL_UPLOAD_PASSWORD;
clientId = clientId ?? process.env.SYMBOL_UPLOAD_CLIENT_ID;
clientSecret = clientSecret ?? process.env.SYMBOL_UPLOAD_CLIENT_SECRET;

if (
!validAuthenticationArguments({
(async () => {
let {
help,
database,
application,
version,
user,
password,
clientId,
clientSecret
})
) {
logMissingAuthAndExit();
}
clientSecret,
remove,
files,
directory
} = await getCommandLineOptions(argDefinitions);

if (help) {
logHelpAndExit();
}

(async () => {
database = database ?? process.env.BUGSPLAT_DATABASE;
user = user ?? process.env.SYMBOL_UPLOAD_USER;
password = password ?? process.env.SYMBOL_UPLOAD_PASSWORD;
clientId = clientId ?? process.env.SYMBOL_UPLOAD_CLIENT_ID;
clientSecret = clientSecret ?? process.env.SYMBOL_UPLOAD_CLIENT_SECRET;

if (!database) {
logMissingArgAndExit('database');
}

if (!application) {
logMissingArgAndExit('application');
}

if (!version) {
logMissingArgAndExit('version');
}

if (
!validAuthenticationArguments({
user,
password,
clientId,
clientSecret
})
) {
logMissingAuthAndExit();
}

console.log('About to authenticate...')

Expand Down Expand Up @@ -99,17 +100,18 @@ if (
console.log(`Found files:\n ${paths}`);
console.log(`About to upload symbols for ${database}-${application}-${version}...`);

const files = paths.map(path => {
const stat = fs.statSync(path);
const size = stat.size;
const name = basename(path);
const file = fs.createReadStream(path);
return {
name,
size,
file
};
});
const files = await Promise.all(
paths.map(async (path) => {
const size = (await stat(path)).size;
const name = basename(path);
const file = fs.createReadStream(path);
return {
name,
size,
file
};
})
);

await symbolsApiClient.postSymbols(
database,
Expand Down Expand Up @@ -142,6 +144,39 @@ async function createBugSplatClient({
return client;
}

async function fileExists(path: string): Promise<boolean> {
try {
return !!(await stat(path));
} catch {
return false;
}
}

async function getCommandLineOptions(argDefinitions: Array<CommandLineDefinition>): Promise<CommandLineOptions> {
const options = commandLineArgs(argDefinitions);
let { application, version } = options;
let packageJson;

if (!application || !version) {
const packageJsonPath = './package.json';
packageJson = await fileExists(packageJsonPath) ? JSON.parse((await readFile(packageJsonPath)).toString()) : null;
}

if (!application && packageJson) {
application = packageJson.name;
}

if (!version && packageJson) {
version = packageJson.version;
}

return {
...options,
application,
version
}
}

function logHelpAndExit() {
const help = commandLineUsage(usageDefinitions);
console.log(help);
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dist"
],
"scripts": {
"start": "ts-node ./bin/index.ts -b fred -a test -v 1.0 -d ./dist",
"start": "ts-node -r dotenv/config ./bin/index.ts -d ./dist",
"help": "ts-node ./bin/index.ts -h",
"build": "rm -rf ./dist && tsc",
"release": "npm run build && npm publish --access public"
Expand Down Expand Up @@ -38,6 +38,7 @@
"@types/command-line-usage": "^5.0.2",
"@types/glob": "^7.1.3",
"@types/node": "^14.14.37",
"dotenv": "^10.0.0",
"ts-node": "^10.2.1",
"typescript": "^4.3.2"
},
Expand Down

0 comments on commit 24bed05

Please sign in to comment.