Skip to content

Commit a82fced

Browse files
authored
Merge pull request #1 from contentstack/develop
Merging Code from Develop Branch
2 parents 22d676c + a6ee872 commit a82fced

File tree

15 files changed

+159
-140
lines changed

15 files changed

+159
-140
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
/yarn.lock
88
node_modules
99
/coverage
10-
results.csv
10+
/results

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fileignoreconfig:
22
- filename: messages/index.json
3-
checksum: 625754a9f5ac2ddf6b55f4566b02a9b5ddfc8538eb92ecc6a03b5810f3aca198
3+
checksum: 3f5de31da9c8956bf1d313a027faa78c1695eff7d6c0104d48e9f6aa45d7a083
44
- filename: package-lock.json
55
checksum: 4b09083eb562350533e3055723cf09722e5e415ea44884b62991bb718206f21b
66
- filename: src/commands/cm/stacks/validate-regex.ts

README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,48 @@ Validate Fields with Regex Property of Content Type and Global Field in a Stack
1515
# Usage
1616
<!-- usage -->
1717
```sh-session
18-
$ npm install -g cli-cm-regex-validate
19-
$ oclif-example COMMAND
18+
$ npm install -g @contentstack/cli-cm-regex-validate
19+
$ csdx COMMAND
2020
running command...
21-
$ oclif-example (-v|--version|version)
22-
cli-cm-regex-validate/1.0.0 linux-x64 node-v16.2.0
23-
$ oclif-example --help [COMMAND]
21+
$ csdx (-v|--version|version)
22+
@contentstack/cli-cm-regex-validate/1.0.0 linux-x64 node-v16.2.0
23+
$ csdx --help [COMMAND]
2424
USAGE
25-
$ oclif-example COMMAND
25+
$ csdx COMMAND
2626
...
2727
```
2828
<!-- usagestop -->
2929
# Commands
3030
<!-- commands -->
31-
* [`oclif-example cm:stacks:validate-regex [FILE]`](#oclif-example-cmstacksvalidate-regex-file)
31+
* [`csdx cm:stacks:validate-regex`](#csdx-cmstacksvalidate-regex)
3232

33-
## `oclif-example cm:stacks:validate-regex [FILE]`
33+
## `csdx cm:stacks:validate-regex`
3434

35-
describe the command here
35+
This command is used to find all the invalid regexes present in the content types and global fields of your stack.
3636

3737
```
3838
USAGE
39-
$ oclif-example cm:stacks:validate-regex [FILE]
39+
$ csdx cm:stacks:validate-regex
4040
4141
OPTIONS
42-
-f, --force
43-
-h, --help show CLI help
44-
-n, --name=name name to print
42+
-a, --alias=alias Alias (name) assigned to the management token
43+
-c, --contentType To find invalid regexes within the content types
44+
45+
-f, --filePath=filePath [optional] The path or the location in your file system where the CSV output file should be
46+
stored.
47+
48+
-g, --globalField To find invalid regexes within the global fields
49+
50+
-h, --help To show details for the CLI command
51+
52+
EXAMPLES
53+
$ csdx cm:stacks:validate-regex
54+
$ csdx cm:stacks:validate-regex -a <management_token_alias>
55+
$ csdx cm:stacks:validate-regex -c
56+
$ csdx cm:stacks:validate-regex -g
57+
$ csdx cm:stacks:validate-regex -f <path/to/the/directory>
58+
$ csdx cm:stacks:validate-regex -a <management_token_alias> -c -g
59+
$ csdx cm:stacks:validate-regex -a <management_token_alias> -c -g -f <path/to/the/directory>
4560
```
4661

4762
_See code: [src/commands/cm/stacks/validate-regex.ts](https://github.com/contentstack/cli-cm-regex-validate/blob/v1.0.0/src/commands/cm/stacks/validate-regex.ts)_

messages/index.json

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
{
22
"validateRegex": {
3-
"requireToken": "Enter Management Token Alias:",
4-
"required": "Required",
5-
"tokenDetails": "Token Details: ",
6-
"selectSchema": "Select the module you need to check.",
7-
"selectOne": "Select atleast one option",
3+
"command":{
4+
"description": "This command is used to find all the invalid regexes present in the content types and global fields of your stack.",
5+
"alias": "Alias (name) assigned to the management token",
6+
"contentTypes": "To find invalid regexes within the content types",
7+
"globalFields": "To find invalid regexes within the global fields",
8+
"filePath": "[optional] The path or the location in your file system where the CSV output file should be stored.",
9+
"help": "To show the flags that can be used with this CLI command",
10+
"login": "https://www.contentstack.com/docs/developers/cli/authenticate-with-the-cli/#login",
11+
"addManagementToken": "https://www.contentstack.com/docs/developers/cli/authenticate-with-the-cli/#add-management-token"
12+
},
13+
"interactive": {
14+
"requireToken": "Enter management token alias:",
15+
"required": "Required.",
16+
"selectSchema": "Select the module you need to check.",
17+
"selectOne": "Select at least one option."
18+
},
819
"cliAction": {
9-
"connectStackStart": "Connecting Stack",
10-
"connectStackStop": "Stack Connection Established. Time taken: ",
11-
"processStackStart": "Processing Stack",
12-
"processStackStop": "Stack Processing Completed. Time taken: "
20+
"connectStackStart": "Connecting stack",
21+
"connectStackStop": "Stack connection established. Time taken: ",
22+
"processStackStart": "Processing stack",
23+
"processStackStop": "Stack processing completed. Time taken: "
1324
},
1425
"errors": {
15-
"login": "Authorization failed, you're not logged in. Login using csdx auth:login",
16-
"tokenNotFound": "Token not found, add token using csdx auth:tokens:add",
26+
"login": "Could not log in as authorization failed. Log in using csdx auth:login",
27+
"tokenNotFound": "Token not found. Add a token using csdx auth:tokens:add",
1728
"stack": {
18-
"fetch": "Error in connecting to the stack",
19-
"apiKey": "Invalid stack API Key"
29+
"fetch": "Error in connecting to the stack. Please try again.",
30+
"apiKey": "Invalid stack API Key provided.",
31+
"contentTypes": "Error in querying content types.",
32+
"globalFields": "Error in querying global fields."
2033
},
21-
"contentTypes": "Error in querying content types.",
22-
"globalFields": "Error in querying global fields.",
23-
"csvOutput": "Generate CSV Output failed"
34+
"csvOutput": "Failed to generate CSV output."
2435
},
25-
"tableOutput": "The following table shows the invalid regexes present in your stack.",
26-
"csvOutput": "Csv output stored successfully at:",
27-
"noInvalidRegex": "There are no invalid regexes in your stack.",
28-
"docsLink": "To know more, visit our documentation site: https://www.contentstack.com/docs/developers/create-content-types/validation-regex/#prevent-catastrophic-backtracking"
36+
"output":{
37+
"tableOutput": "The following table shows the invalid regexes present in your stack.",
38+
"csvOutput": "CSV output stored successfully at:",
39+
"noInvalidRegex": "There are no invalid regexes in your stack.",
40+
"docsLink": "To know more, visit our documentation site on catastrophic-backtracking: https://www.contentstack.com/docs/developers/create-content-types/validation-regex/#prevent-catastrophic-backtracking"
41+
}
2942
}
3043
}

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
{
2-
"name": "cli-cm-regex-validate",
2+
"name": "@contentstack/cli-cm-regex-validate",
33
"description": "Validate Fields with Regex Property of Content Type and Global Field in a Stack",
44
"version": "1.0.0",
55
"author": "Contentstack",
6-
"bin": {
7-
"csdx-regex": "./bin/run"
8-
},
96
"bugs": "https://github.com/contentstack/cli-cm-regex-validate/issues",
107
"devDependencies": {
118
"@oclif/dev-cli": "^1.26.0",
@@ -42,12 +39,15 @@
4239
],
4340
"homepage": "https://github.com/contentstack/cli-cm-regex-validate",
4441
"keywords": [
45-
"oclif-plugin"
42+
"contentstack",
43+
"cli",
44+
"plugin",
45+
"regex"
4646
],
4747
"license": "MIT",
4848
"oclif": {
4949
"commands": "./lib/commands",
50-
"bin": "oclif-example",
50+
"bin": "csdx",
5151
"devPlugins": [
5252
"@oclif/plugin-help"
5353
]

src/commands/cm/stacks/validate-regex.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
1-
import {Command, flags} from '@oclif/command'
1+
import {Command, flags} from '@contentstack/cli-command'
22
import getAuthToken from '../../../utils/get-auth-token'
33
import {inquireAlias, inquireModule} from '../../../utils/interactive'
4-
import getManagementToken from '../../../utils/get-management-token'
54
import connectStack from '../../../utils/connect-stack'
65
const regexMessages = require('../../../../messages/index.json').validateRegex
76

87
export default class CmStacksValidateRegex extends Command {
9-
static description = 'This command is used to check for all the invalid regexes present in the content types and global fields of your stack.'
8+
static description = regexMessages.command.description
109

1110
static flags = {
12-
help: flags.help({char: 'h'}),
13-
alias: flags.string({char: 'a', description: 'Alias to the token for referring it with other commands'}),
14-
contentType: flags.boolean({char: 'c', description: 'To check invalid regexes within the content types'}),
15-
globalField: flags.boolean({char: 'g', description: 'To check invalid regexes within the global fields'}),
16-
filePath: flags.string({char: 'f', description: 'The path or the location in your file system where the CSV output file should be stored, e.g., -f "C:\\Users\\Name\\Desktop\\cli\\csv"'}),
11+
help: flags.help({char: 'h', description: regexMessages.command.help}),
12+
alias: flags.string({char: 'a', description: regexMessages.command.alias}),
13+
contentType: flags.boolean({char: 'c', description: regexMessages.command.contentTypes}),
14+
globalField: flags.boolean({char: 'g', description: regexMessages.command.globalFields}),
15+
filePath: flags.string({char: 'f', description: regexMessages.command.filePath}),
1716
}
1817

19-
static usage = '$ csdx cm:stacks:validate-regex --alias=[ALIAS_NAME] --contentType --globalField --filePath=[FILE_PATH]'
20-
2118
static examples = [
2219
'$ csdx cm:stacks:validate-regex',
23-
'$ csdx cm:stacks:validate-regex --alias=\'Alias Name\' -a \'Alias Name\'',
24-
'$ csdx cm:stacks:validate-regex --contentType -c',
25-
'$ csdx cm:stacks:validate-regex --globalField -g',
26-
'$ csdx cm:stacks:validate-regex --filePath=\'path/to/the/directory/\' -f \'path/to/the/directory/\'',
27-
'$ csdx cm:stacks:validate-regex --alias=\'Alias Name\' --contentType --globalField',
28-
'$ csdx cm:stacks:validate-regex --alias=\'Alias Name\' --contentType --globalField --filePath=\'path/to/the/directory/\'',
29-
'$ csdx cm:stacks:validate-regex -a \'Alias Name\' -c -g',
30-
'$ csdx cm:stacks:validate-regex -a \'Alias Name\' -c -g -f \'path/to/the/directory/\'',
31-
'$ csdx cm:stacks:validate-regex -cga \'Alias Name\'',
32-
'$ csdx cm:stacks:validate-regex -cga \'Alias Name\' -f \'path/to/the/directory/\'',
20+
'$ csdx cm:stacks:validate-regex -a <management_token_alias>',
21+
'$ csdx cm:stacks:validate-regex -c',
22+
'$ csdx cm:stacks:validate-regex -g',
23+
'$ csdx cm:stacks:validate-regex -f <path/to/the/directory>',
24+
'$ csdx cm:stacks:validate-regex -a <management_token_alias> -c -g',
25+
'$ csdx cm:stacks:validate-regex -a <management_token_alias> -c -g -f <path/to/the/directory>',
3326
]
3427

3528
async run() {
@@ -40,26 +33,26 @@ export default class CmStacksValidateRegex extends Command {
4033
authToken = await getAuthToken()
4134
} catch (error) {
4235
this.error(regexMessages.errors.login, {
43-
ref: 'https://www.contentstack.com/docs/developers/cli/authenticate-with-the-cli/#login',
36+
ref: regexMessages.command.login,
4437
})
4538
}
4639

4740
await inquireAlias(flags)
4841

4942
let tokenDetails: any
5043
try {
51-
tokenDetails = await getManagementToken(flags.alias)
44+
tokenDetails = await this.getToken(flags.alias)
5245
} catch (error) {
5346
this.error(regexMessages.errors.tokenNotFound, {
54-
ref: 'https://www.contentstack.com/docs/developers/cli/authenticate-with-the-cli/#add-management-token',
47+
ref: regexMessages.command.addManagementToken,
5548
})
5649
}
5750

5851
await inquireModule(flags)
5952

6053
try {
6154
await connectStack(flags, authToken, tokenDetails.apiKey)
62-
} catch (error: any) {
55+
} catch (error) {
6356
this.error(regexMessages.errors.stack.fetch)
6457
}
6558
}

src/utils/get-management-token.ts

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

src/utils/interactive.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const regexMessages = require('../../messages/index.json').validateRegex
33

44
export const validateAlias = async function (alias: any) {
55
if (!alias || alias.trim() === '') {
6-
return regexMessages.required
6+
return regexMessages.interactive.required
77
}
88
return true
99
}
@@ -13,7 +13,7 @@ export async function inquireAlias(flags: any) {
1313
const input = [{
1414
type: 'input',
1515
name: 'alias',
16-
message: regexMessages.requireToken,
16+
message: regexMessages.interactive.requireToken,
1717
validate: validateAlias,
1818
}]
1919
const response = await inquirer.prompt(input)
@@ -24,7 +24,7 @@ export async function inquireAlias(flags: any) {
2424

2525
export const validateModule = async function (choice: any) {
2626
if (choice.length === 0) {
27-
return regexMessages.selectOne
27+
return regexMessages.interactive.selectOne
2828
}
2929
return true
3030
}
@@ -34,7 +34,7 @@ export async function inquireModule(flags: any) {
3434
const choices = [{
3535
type: 'checkbox',
3636
name: 'choice',
37-
message: regexMessages.selectSchema,
37+
message: regexMessages.interactive.selectSchema,
3838
choices: [
3939
{name: 'Content Type', value: 'contentType', checked: true},
4040
{name: 'Global Field', value: 'globalField'},

src/utils/output.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,31 @@ const regexMessages = require('../../messages/index.json').validateRegex
77
export default async function generateOutput(flags: any, invalidRegex: any, tableData: any) {
88
if (invalidRegex.length > 0) {
99
const resultFile = 'results.csv'
10-
let storagePath = path.resolve(__dirname, '../../', resultFile)
10+
let storagePath = path.resolve(__dirname, '../../results')
1111
if (flags.filePath) {
12-
if (!fs.existsSync(flags.filePath)) {
13-
fs.mkdirSync(flags.filePath, {recursive: true})
14-
}
15-
storagePath = flags.filePath + resultFile
12+
storagePath = flags.filePath
13+
}
14+
if (!fs.existsSync(storagePath)) {
15+
fs.mkdirSync(storagePath, {recursive: true})
1616
}
17+
storagePath = path.resolve(storagePath, resultFile)
1718
jsonexport(invalidRegex, function (error: any, csv: any) {
1819
if (error) {
1920
throw new Error(regexMessages.errors.csvOutput)
2021
}
2122
fs.writeFileSync(storagePath, csv)
2223
})
23-
console.log(regexMessages.tableOutput)
24+
console.log(regexMessages.output.tableOutput)
2425
const table = new Table({
2526
head: ['Module', 'Title', 'UID', 'Invalid Regex Count'],
2627
})
2728
tableData.forEach((row: any) => {
2829
table.push(row)
2930
})
3031
console.log(table.toString())
31-
console.log(regexMessages.csvOutput, storagePath)
32-
console.log(regexMessages.docsLink)
32+
console.log(regexMessages.output.csvOutput, storagePath)
33+
console.log(regexMessages.output.docsLink)
3334
} else {
34-
console.log(regexMessages.noInvalidRegex)
35+
console.log(regexMessages.output.noInvalidRegex)
3536
}
3637
}

src/utils/process-stack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default async function processStack(flags: any, stack: any, startTime: nu
1717
safeRegexCheck(contentType, invalidRegex, tableData, 'Content Type')
1818
})
1919
}).catch(() => {
20-
throw new Error(regexMessages.errors.contentTypes)
20+
throw new Error(regexMessages.errors.stack.contentTypes)
2121
})
2222
}
2323
if (flags.globalField) {
@@ -27,7 +27,7 @@ export default async function processStack(flags: any, stack: any, startTime: nu
2727
safeRegexCheck(globalField, invalidRegex, tableData, 'Global Field')
2828
})
2929
}).catch(() => {
30-
throw new Error(regexMessages.errors.globalFields)
30+
throw new Error(regexMessages.errors.stack.globalFields)
3131
})
3232
}
3333
cli.action.stop(regexMessages.cliAction.processStackStop + (Date.now() - processTime) + ' ms')

0 commit comments

Comments
 (0)