Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
/yarn.lock
node_modules
/coverage
results.csv
/results
2 changes: 1 addition & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fileignoreconfig:
- filename: messages/index.json
checksum: 625754a9f5ac2ddf6b55f4566b02a9b5ddfc8538eb92ecc6a03b5810f3aca198
checksum: 3f5de31da9c8956bf1d313a027faa78c1695eff7d6c0104d48e9f6aa45d7a083
- filename: package-lock.json
checksum: 4b09083eb562350533e3055723cf09722e5e415ea44884b62991bb718206f21b
- filename: src/commands/cm/stacks/validate-regex.ts
Expand Down
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,48 @@ Validate Fields with Regex Property of Content Type and Global Field in a Stack
# Usage
<!-- usage -->
```sh-session
$ npm install -g cli-cm-regex-validate
$ oclif-example COMMAND
$ npm install -g @contentstack/cli-cm-regex-validate
$ csdx COMMAND
running command...
$ oclif-example (-v|--version|version)
cli-cm-regex-validate/1.0.0 linux-x64 node-v16.2.0
$ oclif-example --help [COMMAND]
$ csdx (-v|--version|version)
@contentstack/cli-cm-regex-validate/1.0.0 linux-x64 node-v16.2.0
$ csdx --help [COMMAND]
USAGE
$ oclif-example COMMAND
$ csdx COMMAND
...
```
<!-- usagestop -->
# Commands
<!-- commands -->
* [`oclif-example cm:stacks:validate-regex [FILE]`](#oclif-example-cmstacksvalidate-regex-file)
* [`csdx cm:stacks:validate-regex`](#csdx-cmstacksvalidate-regex)

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

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

```
USAGE
$ oclif-example cm:stacks:validate-regex [FILE]
$ csdx cm:stacks:validate-regex

OPTIONS
-f, --force
-h, --help show CLI help
-n, --name=name name to print
-a, --alias=alias Alias (name) assigned to the management token
-c, --contentType To find invalid regexes within the content types

-f, --filePath=filePath [optional] The path or the location in your file system where the CSV output file should be
stored.

-g, --globalField To find invalid regexes within the global fields

-h, --help To show details for the CLI command

EXAMPLES
$ csdx cm:stacks:validate-regex
$ csdx cm:stacks:validate-regex -a <management_token_alias>
$ csdx cm:stacks:validate-regex -c
$ csdx cm:stacks:validate-regex -g
$ csdx cm:stacks:validate-regex -f <path/to/the/directory>
$ csdx cm:stacks:validate-regex -a <management_token_alias> -c -g
$ csdx cm:stacks:validate-regex -a <management_token_alias> -c -g -f <path/to/the/directory>
```

_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)_
Expand Down
53 changes: 33 additions & 20 deletions messages/index.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
{
"validateRegex": {
"requireToken": "Enter Management Token Alias:",
"required": "Required",
"tokenDetails": "Token Details: ",
"selectSchema": "Select the module you need to check.",
"selectOne": "Select atleast one option",
"command":{
"description": "This command is used to find all the invalid regexes present in the content types and global fields of your stack.",
"alias": "Alias (name) assigned to the management token",
"contentTypes": "To find invalid regexes within the content types",
"globalFields": "To find invalid regexes within the global fields",
"filePath": "[optional] The path or the location in your file system where the CSV output file should be stored.",
"help": "To show the flags that can be used with this CLI command",
"login": "https://www.contentstack.com/docs/developers/cli/authenticate-with-the-cli/#login",
"addManagementToken": "https://www.contentstack.com/docs/developers/cli/authenticate-with-the-cli/#add-management-token"
},
"interactive": {
"requireToken": "Enter management token alias:",
"required": "Required.",
"selectSchema": "Select the module you need to check.",
"selectOne": "Select at least one option."
},
"cliAction": {
"connectStackStart": "Connecting Stack",
"connectStackStop": "Stack Connection Established. Time taken: ",
"processStackStart": "Processing Stack",
"processStackStop": "Stack Processing Completed. Time taken: "
"connectStackStart": "Connecting stack",
"connectStackStop": "Stack connection established. Time taken: ",
"processStackStart": "Processing stack",
"processStackStop": "Stack processing completed. Time taken: "
},
"errors": {
"login": "Authorization failed, you're not logged in. Login using csdx auth:login",
"tokenNotFound": "Token not found, add token using csdx auth:tokens:add",
"login": "Could not log in as authorization failed. Log in using csdx auth:login",
"tokenNotFound": "Token not found. Add a token using csdx auth:tokens:add",
"stack": {
"fetch": "Error in connecting to the stack",
"apiKey": "Invalid stack API Key"
"fetch": "Error in connecting to the stack. Please try again.",
"apiKey": "Invalid stack API Key provided.",
"contentTypes": "Error in querying content types.",
"globalFields": "Error in querying global fields."
},
"contentTypes": "Error in querying content types.",
"globalFields": "Error in querying global fields.",
"csvOutput": "Generate CSV Output failed"
"csvOutput": "Failed to generate CSV output."
},
"tableOutput": "The following table shows the invalid regexes present in your stack.",
"csvOutput": "Csv output stored successfully at:",
"noInvalidRegex": "There are no invalid regexes in your stack.",
"docsLink": "To know more, visit our documentation site: https://www.contentstack.com/docs/developers/create-content-types/validation-regex/#prevent-catastrophic-backtracking"
"output":{
"tableOutput": "The following table shows the invalid regexes present in your stack.",
"csvOutput": "CSV output stored successfully at:",
"noInvalidRegex": "There are no invalid regexes in your stack.",
"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"
}
}
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"name": "cli-cm-regex-validate",
"name": "@contentstack/cli-cm-regex-validate",
"description": "Validate Fields with Regex Property of Content Type and Global Field in a Stack",
"version": "1.0.0",
"author": "Contentstack",
"bin": {
"csdx-regex": "./bin/run"
},
"bugs": "https://github.com/contentstack/cli-cm-regex-validate/issues",
"devDependencies": {
"@oclif/dev-cli": "^1.26.0",
Expand Down Expand Up @@ -42,12 +39,15 @@
],
"homepage": "https://github.com/contentstack/cli-cm-regex-validate",
"keywords": [
"oclif-plugin"
"contentstack",
"cli",
"plugin",
"regex"
],
"license": "MIT",
"oclif": {
"commands": "./lib/commands",
"bin": "oclif-example",
"bin": "csdx",
"devPlugins": [
"@oclif/plugin-help"
]
Expand Down
41 changes: 17 additions & 24 deletions src/commands/cm/stacks/validate-regex.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import {Command, flags} from '@oclif/command'
import {Command, flags} from '@contentstack/cli-command'
import getAuthToken from '../../../utils/get-auth-token'
import {inquireAlias, inquireModule} from '../../../utils/interactive'
import getManagementToken from '../../../utils/get-management-token'
import connectStack from '../../../utils/connect-stack'
const regexMessages = require('../../../../messages/index.json').validateRegex

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

static flags = {
help: flags.help({char: 'h'}),
alias: flags.string({char: 'a', description: 'Alias to the token for referring it with other commands'}),
contentType: flags.boolean({char: 'c', description: 'To check invalid regexes within the content types'}),
globalField: flags.boolean({char: 'g', description: 'To check invalid regexes within the global fields'}),
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"'}),
help: flags.help({char: 'h', description: regexMessages.command.help}),
alias: flags.string({char: 'a', description: regexMessages.command.alias}),
contentType: flags.boolean({char: 'c', description: regexMessages.command.contentTypes}),
globalField: flags.boolean({char: 'g', description: regexMessages.command.globalFields}),
filePath: flags.string({char: 'f', description: regexMessages.command.filePath}),
}

static usage = '$ csdx cm:stacks:validate-regex --alias=[ALIAS_NAME] --contentType --globalField --filePath=[FILE_PATH]'

static examples = [
'$ csdx cm:stacks:validate-regex',
'$ csdx cm:stacks:validate-regex --alias=\'Alias Name\' -a \'Alias Name\'',
'$ csdx cm:stacks:validate-regex --contentType -c',
'$ csdx cm:stacks:validate-regex --globalField -g',
'$ csdx cm:stacks:validate-regex --filePath=\'path/to/the/directory/\' -f \'path/to/the/directory/\'',
'$ csdx cm:stacks:validate-regex --alias=\'Alias Name\' --contentType --globalField',
'$ csdx cm:stacks:validate-regex --alias=\'Alias Name\' --contentType --globalField --filePath=\'path/to/the/directory/\'',
'$ csdx cm:stacks:validate-regex -a \'Alias Name\' -c -g',
'$ csdx cm:stacks:validate-regex -a \'Alias Name\' -c -g -f \'path/to/the/directory/\'',
'$ csdx cm:stacks:validate-regex -cga \'Alias Name\'',
'$ csdx cm:stacks:validate-regex -cga \'Alias Name\' -f \'path/to/the/directory/\'',
'$ csdx cm:stacks:validate-regex -a <management_token_alias>',
'$ csdx cm:stacks:validate-regex -c',
'$ csdx cm:stacks:validate-regex -g',
'$ csdx cm:stacks:validate-regex -f <path/to/the/directory>',
'$ csdx cm:stacks:validate-regex -a <management_token_alias> -c -g',
'$ csdx cm:stacks:validate-regex -a <management_token_alias> -c -g -f <path/to/the/directory>',
]

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

await inquireAlias(flags)

let tokenDetails: any
try {
tokenDetails = await getManagementToken(flags.alias)
tokenDetails = await this.getToken(flags.alias)
} catch (error) {
this.error(regexMessages.errors.tokenNotFound, {
ref: 'https://www.contentstack.com/docs/developers/cli/authenticate-with-the-cli/#add-management-token',
ref: regexMessages.command.addManagementToken,
})
}

await inquireModule(flags)

try {
await connectStack(flags, authToken, tokenDetails.apiKey)
} catch (error: any) {
} catch (error) {
this.error(regexMessages.errors.stack.fetch)
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/utils/get-management-token.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/utils/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const regexMessages = require('../../messages/index.json').validateRegex

export const validateAlias = async function (alias: any) {
if (!alias || alias.trim() === '') {
return regexMessages.required
return regexMessages.interactive.required
}
return true
}
Expand All @@ -13,7 +13,7 @@ export async function inquireAlias(flags: any) {
const input = [{
type: 'input',
name: 'alias',
message: regexMessages.requireToken,
message: regexMessages.interactive.requireToken,
validate: validateAlias,
}]
const response = await inquirer.prompt(input)
Expand All @@ -24,7 +24,7 @@ export async function inquireAlias(flags: any) {

export const validateModule = async function (choice: any) {
if (choice.length === 0) {
return regexMessages.selectOne
return regexMessages.interactive.selectOne
}
return true
}
Expand All @@ -34,7 +34,7 @@ export async function inquireModule(flags: any) {
const choices = [{
type: 'checkbox',
name: 'choice',
message: regexMessages.selectSchema,
message: regexMessages.interactive.selectSchema,
choices: [
{name: 'Content Type', value: 'contentType', checked: true},
{name: 'Global Field', value: 'globalField'},
Expand Down
19 changes: 10 additions & 9 deletions src/utils/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@ const regexMessages = require('../../messages/index.json').validateRegex
export default async function generateOutput(flags: any, invalidRegex: any, tableData: any) {
if (invalidRegex.length > 0) {
const resultFile = 'results.csv'
let storagePath = path.resolve(__dirname, '../../', resultFile)
let storagePath = path.resolve(__dirname, '../../results')
if (flags.filePath) {
if (!fs.existsSync(flags.filePath)) {
fs.mkdirSync(flags.filePath, {recursive: true})
}
storagePath = flags.filePath + resultFile
storagePath = flags.filePath
}
if (!fs.existsSync(storagePath)) {
fs.mkdirSync(storagePath, {recursive: true})
}
storagePath = path.resolve(storagePath, resultFile)
jsonexport(invalidRegex, function (error: any, csv: any) {
if (error) {
throw new Error(regexMessages.errors.csvOutput)
}
fs.writeFileSync(storagePath, csv)
})
console.log(regexMessages.tableOutput)
console.log(regexMessages.output.tableOutput)
const table = new Table({
head: ['Module', 'Title', 'UID', 'Invalid Regex Count'],
})
tableData.forEach((row: any) => {
table.push(row)
})
console.log(table.toString())
console.log(regexMessages.csvOutput, storagePath)
console.log(regexMessages.docsLink)
console.log(regexMessages.output.csvOutput, storagePath)
console.log(regexMessages.output.docsLink)
} else {
console.log(regexMessages.noInvalidRegex)
console.log(regexMessages.output.noInvalidRegex)
}
}
4 changes: 2 additions & 2 deletions src/utils/process-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default async function processStack(flags: any, stack: any, startTime: nu
safeRegexCheck(contentType, invalidRegex, tableData, 'Content Type')
})
}).catch(() => {
throw new Error(regexMessages.errors.contentTypes)
throw new Error(regexMessages.errors.stack.contentTypes)
})
}
if (flags.globalField) {
Expand All @@ -27,7 +27,7 @@ export default async function processStack(flags: any, stack: any, startTime: nu
safeRegexCheck(globalField, invalidRegex, tableData, 'Global Field')
})
}).catch(() => {
throw new Error(regexMessages.errors.globalFields)
throw new Error(regexMessages.errors.stack.globalFields)
})
}
cli.action.stop(regexMessages.cliAction.processStackStop + (Date.now() - processTime) + ' ms')
Expand Down
35 changes: 0 additions & 35 deletions test/utils/get-management-token.test.ts

This file was deleted.

Loading