Skip to content

Commit 5e8f2f4

Browse files
authored
Merge pull request #421 from pharindoko/feat/add-validation-cli-option
Feat/add validation cli option
2 parents e42450a + 2f56f50 commit 5e8f2f4

23 files changed

+366
-132
lines changed

.travis.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ notifications:
1212
node_js:
1313
- '10'
1414
before_install:
15-
- pip install awscli
1615
- git log $(git describe --tags --abbrev=0)..HEAD --oneline
1716
- echo $COMMITCOUNT
1817
install:
@@ -38,16 +37,6 @@ jobs:
3837
- npm run test
3938
before_script: cd packages/server
4039
name: test-server
41-
- script:
42-
- npm run build
43-
- bin/run --version
44-
- npm run deploy
45-
before_script:
46-
- make fake-credentials
47-
- cd packages/cli
48-
after_script: npm run remove
49-
name: deploy-cli
50-
5140
- stage: release
5241
if: (type = push AND branch = master)
5342
before_script:

.vscode/launch.json

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

packages/cli/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $ npm install -g json-serverless
1919
$ jsonsls COMMAND
2020
running command...
2121
$ jsonsls (-v|--version|version)
22-
json-serverless/1.5.41 linux-x64 node-v10.21.0
22+
json-serverless/1.5.41 darwin-x64 node-v14.2.0
2323
$ jsonsls --help [COMMAND]
2424
USAGE
2525
$ jsonsls COMMAND
@@ -32,6 +32,7 @@ USAGE
3232
* [`jsonsls help [COMMAND]`](#jsonsls-help-command)
3333
* [`jsonsls run FILE`](#jsonsls-run-file)
3434
* [`jsonsls update-stack`](#jsonsls-update-stack)
35+
* [`jsonsls validate`](#jsonsls-validate)
3536

3637
## `jsonsls create-stack FILE [STAGE]`
3738

@@ -106,4 +107,20 @@ OPTIONS
106107
-r, --readonly set api to readonly (true) or writeable (false)
107108
-s, --[no-]swagger enable or disable swagger interface support
108109
```
110+
111+
## `jsonsls validate`
112+
113+
Describe the command here
114+
115+
```
116+
USAGE
117+
$ jsonsls validate
118+
119+
OPTIONS
120+
-n, --name=name name to print
121+
122+
DESCRIPTION
123+
...
124+
Extra documentation goes here
125+
```
109126
<!-- commandsstop -->

packages/cli/src/actions/helpers.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import figlet from 'figlet';
66
import chalk from 'chalk';
77
import cli from 'cli-ux';
88
export class Helpers {
9+
static readFileSync(directoryPath: string): string {
10+
const normalizedPath = path.normalize(directoryPath);
11+
if (!fs.existsSync(normalizedPath)) {
12+
throw new Error('file' + normalizedPath + ' does not exist');
13+
} else {
14+
const file = fs.readFileSync(normalizedPath, 'UTF-8');
15+
return file;
16+
}
17+
}
18+
919
static validateFile(filePath: string): string {
1020
if (!path.isAbsolute(filePath)) {
1121
filePath = path.normalize(process.cwd() + '/' + filePath);
@@ -217,7 +227,6 @@ export class Helpers {
217227
apiAuth: boolean,
218228
enableSwagger: boolean
219229
) {
220-
221230
const rows = JSON.stringify(slsinfo).split('\\n') as any[];
222231
const createKeyValues = rows.map((x, i, rows) => {
223232
if (x.startsWith(' ANY -')) {

packages/cli/src/commands/create-stack.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import * as inquirer from 'inquirer';
22
import fs from 'fs-extra';
33
import { Command, flags } from '@oclif/command';
44
import Listr = require('listr');
5-
import { AppConfig } from 'json-serverless-lib';
5+
import { AppConfig, LogLevel } from 'json-serverless-lib';
66
import * as path from 'path';
77
import cli from 'cli-ux';
88
import { Helpers } from '../actions/helpers';
99
import { AWSActions } from '../actions/aws-actions';
1010
import { ServerlessConfig } from '../classes/serverlessconfig';
1111
import chalk from 'chalk';
1212
export class CreateStackCommand extends Command {
13-
static description = 'create the stackfolder and deploy the stack in the cloud';
13+
static description =
14+
'create the stackfolder and deploy the stack in the cloud';
1415

1516
static flags = {
1617
help: flags.help({ char: 'h' }),
@@ -65,6 +66,14 @@ export class CreateStackCommand extends Command {
6566
default: false, // default value if flag not passed (can be a function that returns a string or undefined)
6667
required: false, // make flag required (this is not common and you should probably use an argument instead)
6768
}),
69+
loglevel: flags.string({
70+
char: 'l', // shorter flag version
71+
description: 'loglevel of outputs', // help description for flag
72+
hidden: false, // hide from help
73+
default: 'info',
74+
options: ['info', 'debug'], // default value if flag not passed (can be a function that returns a string or undefined)
75+
required: false, // make flag required (this is not common and you should probably use an argument instead)
76+
}),
6877
};
6978

7079
static args = [
@@ -186,6 +195,7 @@ export class CreateStackCommand extends Command {
186195
appconfig.readOnly = flags.readonly;
187196
appconfig.enableSwagger = flags.swagger;
188197
appconfig.stackName = stackName!;
198+
appconfig.logLevel = flags.loglevel as LogLevel;;
189199
Helpers.createDir(stackFolder + '/config');
190200
fs.writeFileSync(
191201
path.normalize(stackFolder + '/config/appconfig.json'),

packages/cli/src/commands/run.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Command, flags } from '@oclif/command';
2-
import { startServer, AppConfig } from 'json-serverless-lib';
2+
import { startServer, AppConfig, LogLevel } from 'json-serverless-lib';
33
import express from 'express';
44
import { Helpers } from '../actions/helpers';
55
import cli from 'cli-ux';
@@ -33,6 +33,14 @@ export class Run extends Command {
3333
default: false, // default value if flag not passed (can be a function that returns a string or undefined)
3434
required: false, // default value if flag not passed (can be a function that returns a string or undefined)
3535
}),
36+
loglevel: flags.string({
37+
char: 'l', // shorter flag version
38+
description: 'loglevel of outputs', // help description for flag
39+
hidden: false, // hide from help
40+
default: 'info',
41+
options: ['info', 'debug'], // default value if flag not passed (can be a function that returns a string or undefined)
42+
required: false, // make flag required (this is not common and you should probably use an argument instead)
43+
}),
3644
};
3745

3846
static args = [
@@ -53,6 +61,7 @@ export class Run extends Command {
5361
const defaultConfig = new AppConfig();
5462
defaultConfig.readOnly = flags.readonly;
5563
defaultConfig.enableSwagger = flags.swagger;
64+
defaultConfig.logLevel = flags.loglevel as LogLevel;
5665
defaultConfig.jsonFile = args.file;
5766
if (args.file && flags.env) {
5867
const promise = startServer(

packages/cli/src/commands/update-stack.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import { Helpers } from '../actions/helpers';
66
import { AWSActions } from '../actions/aws-actions';
77
import chalk from 'chalk';
88
import cli from 'cli-ux';
9-
import { AppConfig } from 'json-serverless-lib';
9+
import { AppConfig, LogLevel } from 'json-serverless-lib';
1010

1111
export class UpdateStackCommand extends Command {
12-
static description = 'update the stackfolder and update the stack in the cloud';
12+
static description =
13+
'update the stackfolder and update the stack in the cloud';
1314

1415
static flags = {
1516
help: flags.help({ char: 'h' }),
@@ -43,6 +44,14 @@ export class UpdateStackCommand extends Command {
4344
default: '', // default value if flag not passed (can be a function that returns a string or undefined)
4445
required: false, // make flag required (this is not common and you should probably use an argument instead)
4546
}),
47+
loglevel: flags.string({
48+
char: 'l', // shorter flag version
49+
description: 'loglevel of outputs', // help description for flag
50+
hidden: false, // hide from help
51+
default: 'info',
52+
options: ['info', 'debug'], // default value if flag not passed (can be a function that returns a string or undefined)
53+
required: false, // make flag required (this is not common and you should probably use an argument instead)
54+
}),
4655
};
4756

4857
async run() {
@@ -116,12 +125,13 @@ export class UpdateStackCommand extends Command {
116125
{
117126
title: 'Update Appconfig',
118127
task: (ctx, task) => {
119-
const appConfig = JSON.parse(
128+
const appConfig: AppConfig = JSON.parse(
120129
fs.readFileSync(stackFolder + '/config/appconfig.json', 'UTF-8')
121130
);
122131
appConfig.enableApiKeyAuth = flags.apikeyauth;
123132
appConfig.readOnly = flags.readonly;
124133
appConfig.enableSwagger = flags.swagger;
134+
appConfig.logLevel = flags.loglevel as LogLevel;
125135
fs.writeFileSync(
126136
path.normalize(stackFolder + '/config/appconfig.json'),
127137
JSON.stringify(appConfig, null, 2),
@@ -190,7 +200,6 @@ export class UpdateStackCommand extends Command {
190200
appConfig.enableApiKeyAuth,
191201
appConfig.enableSwagger
192202
);
193-
194203
} catch (error) {
195204
this.log(`${chalk.red(error.message)}`);
196205
this.log(slsinfo);

packages/cli/src/commands/validate.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Command, flags } from '@oclif/command';
2+
import { JSONValidator, Output } from 'json-serverless-lib';
3+
import { Helpers } from '../actions/helpers';
4+
import chalk from 'chalk';
5+
export class Validate extends Command {
6+
static description = 'describe the command here';
7+
8+
static flags = {
9+
help: flags.help({ char: 'h' }),
10+
swagger: flags.boolean({
11+
char: 's', // shorter flag version
12+
description: 'enable or disable swagger interface support', // help description for flag
13+
hidden: false, // hide from help
14+
default: true, // default value if flag not passed (can be a function that returns a string or undefined)
15+
required: false, // make flag required (this is not common and you should probably use an argument instead)
16+
allowNo: true,
17+
}),
18+
};
19+
20+
static args = [
21+
{
22+
name: 'file', // name of arg to show in help and reference with args[name]
23+
required: true, // make the arg required with `required: true`
24+
description: 'path of JSON file', // help description
25+
hidden: false, // hide this arg from help
26+
},
27+
];
28+
29+
async run() {
30+
const logo = await Helpers.generateLogo('json-serverless');
31+
this.log(`${chalk.blueBright(logo)}`);
32+
this.log();
33+
const { args, flags } = this.parse(Validate);
34+
const filePath = Helpers.validateFile(args.file);
35+
const jsonFileContent = JSON.parse(Helpers.readFileSync(filePath));
36+
const validationResult = JSONValidator.validate(
37+
jsonFileContent,
38+
flags.swagger
39+
);
40+
if (validationResult.isValid) {
41+
this.log(
42+
`${chalk.green('Validation was successful - No errors found.')}`
43+
);
44+
} else {
45+
this.log(
46+
`${chalk.red('Validation was not successful - see details below.')}`
47+
);
48+
}
49+
this.log();
50+
Output.printValidationReport(validationResult);
51+
}
52+
}

packages/server/package-lock.json

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/server/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"scripts": {
1212
"test": "npx jest",
13+
"debug": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec node --inspect-brk -r ts-node/register example/index.ts",
1314
"start": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' example/index.ts",
1415
"type-check": "tsc --noEmit",
1516
"type-check:watch": "npm run type-check -- --watch",
@@ -29,6 +30,7 @@
2930
},
3031
"homepage": "https://github.com/pharindoko/json-serverless.git#readme",
3132
"dependencies": {
33+
"ajv": "^6.12.2",
3234
"cors": "^2.8.5",
3335
"dotenv": "^8.2.0",
3436
"express": "^4.17.1",
@@ -47,7 +49,8 @@
4749
"source-map-support": "^0.5.16",
4850
"supertest": "^4.0.2",
4951
"swagger-to-graphql": "^4.0.2",
50-
"swagger-ui-express": "^4.1.4"
52+
"swagger-ui-express": "^4.1.4",
53+
"table": "^5.4.6"
5154
},
5255
"devDependencies": {
5356
"@types/aws-lambda": "8.10.52",
@@ -65,6 +68,7 @@
6568
"@types/supertest": "2.0.9",
6669
"@types/swagger-schema-official": "2.0.21",
6770
"@types/swagger-ui-express": "4.1.2",
71+
"@types/table": "^5.0.0",
6872
"cz-conventional-changelog": "3.2.0",
6973
"eslint": "7.1.0",
7074
"eslint-config-airbnb-base": "14.1.0",
@@ -75,7 +79,7 @@
7579
"nodemon": "2.0.4",
7680
"ts-jest": "26.1.0",
7781
"ts-loader": "7.0.5",
78-
"typescript": "3.9.3"
82+
"typescript": "^3.9.3"
7983
},
8084
"config": {
8185
"commitizen": {

0 commit comments

Comments
 (0)