Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 1296972

Browse files
Misc bug fixes and clean up
1 parent b3a26be commit 1296972

19 files changed

+251
-176
lines changed

README.md

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,14 @@ topcoder-cli
66

77
# Installation
88

9-
- Install the package via npm-cli:
9+
- Install the package via npm cli
1010

1111
``` node
1212
npm i -g @topcoder/topcoder-cli
1313
```
1414

1515
# Usage
1616

17-
First, install the package, and then run `tc-submission-cli` command on the root directory of your project with `.topcoderrc` file.
18-
It'll then automatically zip all files under the root directory recursively(except the .topcoderrc file itself) and finally upload the zip file to the TC challenge as a submission.
17+
* To understand the commands supported by Topcoder CLI, after installing the package run `topcoder --help` which would list out all supported commands.
1918

20-
An example `.topcoderrc` file should conform to at least the following structure.
21-
22-
``` jsonr
23-
{
24-
"challengeIds": [
25-
"30095545" // at least one item here
26-
],
27-
"username": "TonyJ",
28-
"password": "******"
29-
}
30-
```
19+
* If you need to understand the options available with each command, run `topcoder [ command ] --help`. E.g. Executing the command `topcoder submit --help` will display the help content related to making topcoder submission using CLI

bin/tc-submission-cli.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

bin/topcoder-cli.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
3+
const program = require('commander')
4+
const submissionHandler = require('../src/commands/submit')
5+
const payHandler = require('../src/commands/pay')
6+
const configHandler = require('../src/commands/config')
7+
const logger = require('../src/common/logger')
8+
9+
// Overall help text which will be displayed after usage information
10+
program.on('--help', () => {
11+
console.log(`\nTopcoder CLI to interact with Topcoder systems\n`)
12+
})
13+
14+
program
15+
.command('submit')
16+
.description('Submit the contents of current working directory to Topcoder challenge(s)')
17+
.option('-u, --username <uname>', 'Topcoder username')
18+
.option('-p, --password <password>', 'Topcoder password')
19+
.option('-m, --memberId <memberid>', 'Admin submitting on behalf of other member will use the member id')
20+
.option('-c, --challengeIds <ids>', 'Comma separated challenge IDs to which submission need to be done')
21+
.option('--dev', 'Points to Topcoder development environment')
22+
.on('--help', () => {
23+
console.log(`\nEither use CLI parameters or Create a file .topcoderrc in JSON format with below details
24+
{
25+
"challengeIds": [
26+
"30095545" // at least one item here
27+
],
28+
"username": "<Topcoder username>",
29+
"password": "<Topcoder password>"
30+
}
31+
and execute command \`topcoder submit\` to submit the contents of current working directory except .topcoderrc file to the challenge`)
32+
}).action((args) => {
33+
try {
34+
if (args.dev) {
35+
process.env.NODE_ENV = 'dev'
36+
}
37+
submissionHandler(program.args[0])
38+
} catch (error) {
39+
logger.error(error)
40+
logger.error(error.message)
41+
}
42+
})
43+
44+
program
45+
.command('config')
46+
.description('Setup global configuration for the Topcoder CLI')
47+
.option('-l --list', 'Print the keys in the config file')
48+
.option('-a --add <key> <value>', 'Add / Replace a key in the config file.')
49+
.option('--unset <key>', 'Removes a key from the config file')
50+
.action((...args) => {
51+
try {
52+
configHandler.handleSubCommand(args)
53+
} catch (error) {
54+
logger.error(error.message)
55+
}
56+
})
57+
58+
program
59+
.command('pay')
60+
.description('Let copilot/managers process private task payments')
61+
.option('-o --copilot <payment>', 'copilot payment.')
62+
.option('--dev', 'Points to Topcoder development environment')
63+
.action((...args) => {
64+
if (args.dev) {
65+
process.env.NODE_ENV = 'dev'
66+
}
67+
payHandler.handleCommand(args)
68+
})
69+
70+
// error on unknown commands
71+
program.on('command:*', function () {
72+
console.error('Invalid command: %s\nEnter topcoder --help for the list of available commands.', program.args.join(' '))
73+
process.exit(1)
74+
})
75+
76+
program.parse(process.argv)
77+
78+
// If the CLI is invoked without any command, display help
79+
if (process.argv.length < 3) {
80+
program.help()
81+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Configuration here.
2+
* Development environment configuration for Topcoder CLI
33
*/
44

55
module.exports = {
@@ -9,6 +9,6 @@ module.exports = {
99
AUTH0_URL: process.env.AUTH0_URL || 'https://topcoder-dev.auth0.com/oauth/token',
1010
TC_AUTHN_URL: process.env.TC_AUTHN_URL || 'https://topcoder-dev.auth0.com/oauth/ro',
1111
TC_AUTHZ_URL: process.env.TC_AUTHZ_URL || 'https://api.topcoder-dev.com/v3/authorizations',
12-
TC_CLIENT_ID: process.env.TC_CLIENT_ID || 'JFDo7HMkf0q2CkVFHojy3zHWafziprhT', // for dev 'JFDo7HMkf0q2CkVFHojy3zHWafziprhT',
12+
TC_CLIENT_ID: process.env.TC_CLIENT_ID || 'JFDo7HMkf0q2CkVFHojy3zHWafziprhT',
1313
TC_CLIENT_V2CONNECTION: process.env.CLIENT_V2CONNECTION || 'TC-User-Database'
1414
}

config/prod.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Production or default configuration for the Topcoder CLI
3+
*/
4+
5+
module.exports = {
6+
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
7+
TC_MEMBERS_API: process.env.TC_MEMBERS_API || 'https://api.topcoder.com/v3/members',
8+
SUBMISSION_API_URL: process.env.TEST_SUBMISSION_API_URL || 'https://api.topcoder.com/v5/submissions',
9+
AUTH0_URL: process.env.AUTH0_URL || 'https://topcoder.auth0.com/oauth/token',
10+
TC_AUTHN_URL: process.env.TC_AUTHN_URL || 'https://topcoder.auth0.com/oauth/ro',
11+
TC_AUTHZ_URL: process.env.TC_AUTHZ_URL || 'https://api.topcoder.com/v3/authorizations',
12+
TC_CLIENT_ID: process.env.TC_CLIENT_ID || '6ZwZEUo2ZK4c50aLPpgupeg5v2Ffxp9P',
13+
TC_CLIENT_V2CONNECTION: process.env.CLIENT_V2CONNECTION || 'TC-User-Database'
14+
}

constants.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
2-
* Constants here.
2+
* Application constants will be stored here
33
*/
4+
45
module.exports = {
56
rc: {
67
name: '.topcoderrc'
@@ -22,5 +23,12 @@ module.exports = {
2223
scope: 'openid profile offline_access',
2324
responseType: 'token',
2425
grantType: 'password',
25-
device: 'Browser'
26+
device: 'Browser',
27+
errorMessages: {
28+
connectionError: 'Error while connecting to topcoder. Please check your connection and try again.',
29+
invalidCredentials: 'Invalid Authentication credentials. Please check your credentials and try again.',
30+
noGlobalConfig: 'Topcoder config file not found',
31+
invalidOptions: 'Only one option is required and allowed for this command. Execute topcoder config --help for more details',
32+
invalidArgs: 'Invalid number of values passed.'
33+
}
2634
}

docs/Development.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@ The following parameters can be set in config files or in env variables:
3030
After that, run `npm publish` again to republish the package.
3131
- If you want to remove the package from npm registry anyway, run `npm unpublish --force` under the root directory of the project.
3232

33-
# test
33+
## Installing globally
3434

35-
## Prepare
36-
- Install dependencies `npm install`
35+
To install the tool globally so that you can test it's working, run the below command from project root directory.
36+
37+
```
38+
npm install -g .
39+
```
40+
41+
Later on you will be able to run `topcoder` command from any directory
3742

3843
## Unit test
44+
45+
- Install dependencies `npm install`
46+
3947
To run unit tests alone
4048

4149
```bash

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@topcoder/topcoder-cli",
33
"version": "1.0.3",
4-
"description": "A CLI tool that will be used by Topcoder members to submit their solutions on challenges.",
4+
"description": "A CLI tool to interact with Topcoder systems.",
55
"main": "index.js",
66
"scripts": {
77
"lint": "standard",
@@ -13,7 +13,6 @@
1313
"@topcoder-platform/topcoder-submission-api-wrapper": "^1.1.0",
1414
"adm-zip": "^0.4.11",
1515
"commander": "^3.0.0",
16-
"config": "^3.1.0",
1716
"fast-glob": "^2.2.6",
1817
"get-parameter-names": "^0.3.0",
1918
"ini": "^1.3.5",
@@ -26,13 +25,13 @@
2625
"winston": "^3.2.1"
2726
},
2827
"bin": {
29-
"tc-submission-cli": "bin/tc-submission-cli.js"
28+
"topcoder": "bin/topcoder-cli.js"
3029
},
3130
"repository": {
3231
"type": "git",
3332
"url": "git+ssh://[email protected]/topcoder-platform/topcoder-cli.git"
3433
},
35-
"author": "lazybaer",
34+
"author": "Topcoder",
3635
"license": "ISC",
3736
"bugs": {
3837
"url": "https://github.com/topcoder-platform/topcoder-cli/issues"

src/commands/config.js

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
const homedir = require('os').homedir()
2-
const path = require('path')
3-
const constants = require('../../constants')
41
const configService = require('../services/configService')
52
const errors = require('../common/errors')
6-
7-
const configPath = path.join(homedir, constants.config.name)
3+
const constants = require('../../constants')
84

95
function handleSubCommand (args) {
106
const options = args[args.length - 1].opts()
117
// Count the number of options which are enabled.
128
const numOptions = Object.values(options).reduce((t, obj) => obj !== undefined ? t + 1 : t, 0)
139

14-
if (numOptions > 1) {
15-
throw errors.invalidNoOfOptionsUsedError()
10+
if (numOptions > 1 || numOptions === 0) {
11+
throw errors.customError(constants.errorMessages.invalidOptions)
1612
}
1713

1814
const allOptions = Object.keys(options)
@@ -28,34 +24,23 @@ function handleSubCommand (args) {
2824

2925
switch (selectedOption) {
3026
case 'list':
31-
showConfigFile()
27+
console.log(configService.showConfigFileService())
3228
break
3329
case 'add':
3430
// key is set to the value of option `add`
3531
// args[0] should contain the value we want to add
3632
// console.log(options.add, args[0])
3733
if (args.length > 2) {
38-
throw errors.invalidNoOfArgsPassedError()
34+
throw errors.customError(constants.errorMessages.invalidArgs)
3935
}
40-
addToConfigFile(options.add, args[0])
41-
break
36+
return configService.addToConfigFileService(options.add, args[0])
37+
case 'unset':
38+
return configService.deleteFromConfigFileService(options.unset)
4239
default :
43-
4440
break
4541
}
4642
}
4743

48-
function showConfigFile () {
49-
console.log(configService.showConfigFileService(configPath))
50-
}
51-
52-
function addToConfigFile (key, value) {
53-
return configService.addToConfigFileService(key, value, configPath)
54-
}
55-
5644
module.exports = {
57-
handleSubCommand,
58-
showConfigFile,
59-
addToConfigFile,
60-
configPath
45+
handleSubCommand
6146
}

src/commands/submit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const uploadSubmissionService = require('../services/uploadSubmissionService')
2-
const errors = require('../common/errors')
2+
const constants = require('../../constants')
33
const logger = require('../common/logger')
44
const _ = require('lodash')
55
const fs = require('fs')
@@ -35,7 +35,7 @@ const submit = (params) => {
3535
.catch(err => {
3636
switch (err.code) {
3737
case 'ENOTFOUND':
38-
logger.error(errors.connectionErrorMsg)
38+
logger.error(constants.errorMessages.connectionError)
3939
break
4040
default:
4141
logger.error(err.message)

0 commit comments

Comments
 (0)