@contentstack/cli-config 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Contentstack
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ @contentstack/cli-config
2
+ ===
3
+
4
+ The config namespace contains all the commands that you will need to configure the CLI as per your requirements. Contentstack currently supports three regions: North America, Europe and Azure North America. [Configure the CLI documentation](https://www.contentstack.com/docs/developers/cli/configure-the-cli)
5
+
6
+ [![License](https://img.shields.io/npm/l/@contentstack/cli)](https://github.com/contentstack/cli/blob/main/LICENSE)
7
+
8
+ <!-- toc -->
9
+ * [Usage](#usage)
10
+ * [Commands](#commands)
11
+ <!-- tocstop -->
12
+ # Usage
13
+ <!-- usage -->
14
+ ```sh-session
15
+ $ npm install -g @contentstack/cli-config
16
+ $ csdx COMMAND
17
+ running command...
18
+ $ csdx (-v|--version|version)
19
+ @contentstack/cli-config/1.0.0 linux-x64 node-v16.14.2
20
+ $ csdx --help [COMMAND]
21
+ USAGE
22
+ $ csdx COMMAND
23
+ ...
24
+ ```
25
+ <!-- usagestop -->
26
+ # Commands
27
+ <!-- commands -->
28
+ * [`csdx config:get:region`](#csdx-configgetregion)
29
+ * [`csdx config:set:region [REGION]`](#csdx-configsetregion-region)
30
+
31
+ ## `csdx config:get:region`
32
+
33
+ Get current region set for CLI
34
+
35
+ ```
36
+ USAGE
37
+ $ csdx config:get:region
38
+
39
+ EXAMPLE
40
+ $ csdx config:get:region
41
+ ```
42
+
43
+ _See code: [src/commands/config/get/region.ts](https://github.com/contentstack/cli/blob/v1.0.0/src/commands/config/get/region.ts)_
44
+
45
+ ## `csdx config:set:region [REGION]`
46
+
47
+ Set region for CLI
48
+
49
+ ```
50
+ USAGE
51
+ $ csdx config:set:region [REGION]
52
+
53
+ OPTIONS
54
+ -d, --cda=cda Custom host to set for content delivery API, if this flag is added then cma and name flags are
55
+ required
56
+
57
+ -m, --cma=cma Custom host to set for content management API, , if this flag is added then cda and name flags are
58
+ required
59
+
60
+ -n, --name=name Name for the region, if this flag is added then cda and cma flags are required
61
+
62
+ EXAMPLES
63
+ $ csdx config:set:region
64
+ $ csdx config:set:region NA
65
+ $ csdx config:set:region NA
66
+ $ csdx config:set:region --cma <contentstack_cma_endpoint> --cda <contentstack_cda_endpoint> --name "India"
67
+ ```
68
+
69
+ _See code: [src/commands/config/set/region.ts](https://github.com/contentstack/cli/blob/v1.0.0/src/commands/config/set/region.ts)_
70
+ <!-- commandsstop -->
package/bin/run ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('@oclif/command').run()
4
+ .catch(require('@oclif/errors/handle'))
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const cli_command_1 = require("@contentstack/cli-command");
4
+ const cli_utilities_1 = require("@contentstack/cli-utilities");
5
+ class RegionGetCommand extends cli_command_1.Command {
6
+ async run() {
7
+ let currentRegion = this.region;
8
+ if (!currentRegion) {
9
+ cli_utilities_1.logger.error('No region set');
10
+ cli_utilities_1.cliux.error('CLI_CONFIG_GET_REGION_NOT_FOUND');
11
+ this.exit();
12
+ }
13
+ cli_utilities_1.cliux.print(`Currently using ${currentRegion.name} region`);
14
+ cli_utilities_1.cliux.print(`CDA HOST: ${currentRegion.cda}`);
15
+ cli_utilities_1.cliux.print(`CMA HOST: ${currentRegion.cma}`);
16
+ }
17
+ }
18
+ exports.default = RegionGetCommand;
19
+ RegionGetCommand.description = 'Get current region set for CLI';
20
+ RegionGetCommand.examples = ['$ csdx config:get:region'];
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const cli_command_1 = require("@contentstack/cli-command");
4
+ const cli_utilities_1 = require("@contentstack/cli-utilities");
5
+ const utils_1 = require("../../../utils");
6
+ class RegionSetCommand extends cli_command_1.Command {
7
+ async run() {
8
+ const { args, flags } = this.parse(RegionSetCommand);
9
+ let cda = flags.cda;
10
+ let cma = flags.cma;
11
+ let name = flags.name;
12
+ let selectedRegion = args.region;
13
+ if (!(cda && cma && name) && !selectedRegion) {
14
+ selectedRegion = await utils_1.interactive.askRegions();
15
+ }
16
+ if (selectedRegion === 'custom') {
17
+ const selectedCustomRegion = await utils_1.interactive.askCustomRegion();
18
+ name = selectedCustomRegion.name;
19
+ cda = selectedCustomRegion.cda;
20
+ cma = selectedCustomRegion.cma;
21
+ }
22
+ else if (selectedRegion === 'exit') {
23
+ this.exit();
24
+ }
25
+ // Custom flag will get first priority over region argument
26
+ if (cda && cma && name) {
27
+ try {
28
+ let customRegion = { cda, cma, name };
29
+ customRegion = utils_1.regionHandler.setCustomRegion(customRegion);
30
+ cli_utilities_1.cliux.success(`Custom region has been set to ${customRegion.name}`);
31
+ cli_utilities_1.cliux.success(`CMA HOST: ${customRegion.cma}`);
32
+ cli_utilities_1.cliux.success(`CDA HOST: ${customRegion.cda}`);
33
+ }
34
+ catch (error) {
35
+ cli_utilities_1.logger.error('failed to set the region', error);
36
+ cli_utilities_1.cliux.error(`Failed to set region due to: ${error.message}`);
37
+ }
38
+ }
39
+ else if (['NA', 'EU', 'AZURE-NA'].includes(selectedRegion)) {
40
+ const regionDetails = utils_1.regionHandler.setRegion(selectedRegion);
41
+ cli_utilities_1.cliux.success(`Region has been set to ${regionDetails.name}`);
42
+ cli_utilities_1.cliux.success(`CDA HOST: ${regionDetails.cda}`);
43
+ cli_utilities_1.cliux.success(`CMA HOST: ${regionDetails.cma}`);
44
+ }
45
+ else {
46
+ cli_utilities_1.cliux.error(`Invalid region is given`);
47
+ }
48
+ }
49
+ }
50
+ exports.default = RegionSetCommand;
51
+ RegionSetCommand.description = 'Set region for CLI';
52
+ RegionSetCommand.flags = {
53
+ cda: cli_command_1.flags.string({
54
+ char: 'd',
55
+ description: 'Custom host to set for content delivery API, if this flag is added then cma and name flags are required',
56
+ dependsOn: ['cma', 'name'],
57
+ parse: (0, cli_utilities_1.printFlagDeprecation)(['-d'], ['--cda']),
58
+ }),
59
+ cma: cli_command_1.flags.string({
60
+ char: 'm',
61
+ description: 'Custom host to set for content management API, , if this flag is added then cda and name flags are required',
62
+ dependsOn: ['cda', 'name'],
63
+ parse: (0, cli_utilities_1.printFlagDeprecation)(['-m'], ['--cma']),
64
+ }),
65
+ name: cli_command_1.flags.string({
66
+ char: 'n',
67
+ description: 'Name for the region, if this flag is added then cda and cma flags are required',
68
+ dependsOn: ['cda', 'cma'],
69
+ }),
70
+ };
71
+ RegionSetCommand.examples = [
72
+ '$ csdx config:set:region',
73
+ '$ csdx config:set:region NA',
74
+ '$ csdx config:set:region NA',
75
+ '$ csdx config:set:region --cma <contentstack_cma_endpoint> --cda <contentstack_cda_endpoint> --name "India"',
76
+ ];
77
+ RegionSetCommand.args = [
78
+ {
79
+ name: 'region',
80
+ },
81
+ ];
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = {
4
+ logger: {
5
+ level: 'error',
6
+ silent: false,
7
+ },
8
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.interactive = exports.regionHandler = void 0;
4
+ var region_handler_1 = require("./region-handler");
5
+ Object.defineProperty(exports, "regionHandler", { enumerable: true, get: function () { return region_handler_1.default; } });
6
+ exports.interactive = require("./interactive");
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.askCustomRegion = exports.askRegions = void 0;
4
+ const cli_utilities_1 = require("@contentstack/cli-utilities");
5
+ const askRegions = async () => {
6
+ return cli_utilities_1.cliux.inquire({
7
+ type: 'list',
8
+ name: 'selectedRegion',
9
+ message: 'CLI_CONFIG_SELECT_REGION',
10
+ choices: [
11
+ { name: 'NA', value: 'NA' },
12
+ { name: 'EU', value: 'EU' },
13
+ { name: 'AZURE-NA', value: 'AZURE-NA' },
14
+ { name: 'Custom', value: 'custom' },
15
+ { name: 'exit', value: 'exit' },
16
+ ],
17
+ });
18
+ };
19
+ exports.askRegions = askRegions;
20
+ const askCustomRegion = async () => {
21
+ const name = await cli_utilities_1.cliux.inquire({
22
+ type: 'input',
23
+ name: 'name',
24
+ message: 'CLI_CONFIG_INQUIRE_REGION_NAME',
25
+ });
26
+ const cma = await cli_utilities_1.cliux.inquire({
27
+ type: 'input',
28
+ name: 'cma',
29
+ message: 'CLI_CONFIG_INQUIRE_REGION_CMA',
30
+ });
31
+ const cda = await cli_utilities_1.cliux.inquire({
32
+ type: 'input',
33
+ name: 'cda',
34
+ message: 'CLI_CONFIG_INQUIRE_REGION_CDA',
35
+ });
36
+ return { name, cma, cda };
37
+ };
38
+ exports.askCustomRegion = askCustomRegion;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const cli_utilities_1 = require("@contentstack/cli-utilities");
4
+ function validURL(str) {
5
+ const pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
6
+ '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
7
+ '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
8
+ '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
9
+ '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
10
+ '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
11
+ return Boolean(pattern.test(str));
12
+ }
13
+ // Available region list
14
+ const regions = {
15
+ NA: { cma: 'https://api.contentstack.io', cda: 'https://cdn.contentstack.io', name: 'NA' },
16
+ EU: { cma: 'https://eu-api.contentstack.com', cda: 'https://eu-cdn.contentstack.com', name: 'EU' },
17
+ 'AZURE-NA': {
18
+ cma: 'https://azure-na-api.contentstack.com',
19
+ cda: 'https://azure-na-cdn.contentstack.com',
20
+ name: 'AZURE-NA',
21
+ },
22
+ };
23
+ class UserConfig {
24
+ /**
25
+ *
26
+ * Set region to config store
27
+ * @param {string} region It Can be NA, EU
28
+ * @returns {object} region object with cma, cda, region property
29
+ */
30
+ setRegion(region) {
31
+ let selectedRegion = regions[region];
32
+ if (selectedRegion) {
33
+ cli_utilities_1.configHandler.set('region', selectedRegion);
34
+ return selectedRegion;
35
+ }
36
+ }
37
+ /**
38
+ *
39
+ * Get current host set for CLI
40
+ * @returns { object } Object contains url for cma and cda, and region to which it is pointing to
41
+ */
42
+ getRegion() {
43
+ const regionDetails = cli_utilities_1.configHandler.get('region');
44
+ if (regionDetails)
45
+ return regionDetails;
46
+ // returns NA region if not found in config
47
+ return regions.NA;
48
+ }
49
+ /**
50
+ *
51
+ * Set region to config store
52
+ * @param {object} regionObject should contain cma, cda, region property
53
+ * @returns {object} region object with cma, cda, region(name of region) property
54
+ */
55
+ setCustomRegion(regionObject) {
56
+ if (this.validateRegion(regionObject)) {
57
+ regionObject = this.sanitizeRegionObject(regionObject);
58
+ cli_utilities_1.configHandler.set('region', regionObject);
59
+ return regionObject;
60
+ }
61
+ throw new TypeError('Custom region should include valid cma(URL), cda(URL), name(String) (Name for the Region) property.');
62
+ }
63
+ /**
64
+ *
65
+ * Set rateLimit to config store
66
+ * @param {object} rateLimitObject should contain rate limit property
67
+ * @returns {object} ratelimit object with limit property
68
+ */
69
+ // setCustomRateLimit(rateLimitObject) {
70
+ // if(rateLimitObject !== undefined && isNaN(rateLimitObject)) {
71
+ // throw new TypeError(rateLimitObject + " is not a number, Please provide number as a rate limit")
72
+ // } else {
73
+ // config.set('rate-limit', rateLimitObject)
74
+ // return rateLimitObject
75
+ // }
76
+ // }
77
+ /**
78
+ * Validate given region JSON object
79
+ * @param {*} regionObject JSON object needs to be validated
80
+ * @returns {boolean} True if contains cma, cda and region property otherwise false
81
+ */
82
+ validateRegion(regionObject) {
83
+ if (regionObject.cma && regionObject.cda && regionObject.name) {
84
+ if (validURL(regionObject.cma) && validURL(regionObject.cda))
85
+ return true;
86
+ }
87
+ return false;
88
+ }
89
+ /**
90
+ * Sanitize given region JSON object by removing any other properties than cma, cda and region
91
+ * @param { object } regionObject JSON object needs to be sanitized
92
+ * @returns { object } JSON object with only valid keys for region
93
+ */
94
+ sanitizeRegionObject(regionObject) {
95
+ let sanitizedRegion;
96
+ sanitizedRegion = {
97
+ cma: regionObject.cma,
98
+ cda: regionObject.cda,
99
+ name: regionObject.name,
100
+ };
101
+ return sanitizedRegion;
102
+ }
103
+ }
104
+ exports.default = new UserConfig();
@@ -0,0 +1 @@
1
+ {"version":"1.0.0","commands":{"config:get:region":{"id":"config:get:region","description":"Get current region set for CLI","pluginName":"@contentstack/cli-config","pluginType":"core","aliases":[],"examples":["$ csdx config:get:region"],"flags":{},"args":[]},"config:set:region":{"id":"config:set:region","description":"Set region for CLI","pluginName":"@contentstack/cli-config","pluginType":"core","aliases":[],"examples":["$ csdx config:set:region","$ csdx config:set:region NA","$ csdx config:set:region NA","$ csdx config:set:region --cma <contentstack_cma_endpoint> --cda <contentstack_cda_endpoint> --name \"India\""],"flags":{"cda":{"name":"cda","type":"option","char":"d","description":"Custom host to set for content delivery API, if this flag is added then cma and name flags are required"},"cma":{"name":"cma","type":"option","char":"m","description":"Custom host to set for content management API, , if this flag is added then cda and name flags are required"},"name":{"name":"name","type":"option","char":"n","description":"Name for the region, if this flag is added then cda and cma flags are required"}},"args":[{"name":"region"}]}}}
package/package.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "@contentstack/cli-config",
3
+ "description": "Contentstack CLI plugin for configuration",
4
+ "version": "1.0.0",
5
+ "author": "Contentstack",
6
+ "bin": {
7
+ "csdx": "./bin/run"
8
+ },
9
+ "scripts": {
10
+ "build": "npm run clean && npm run compile",
11
+ "clean": "rm -rf ./lib && rm -rf tsconfig.build.tsbuildinfo",
12
+ "compile": "tsc -b tsconfig.json",
13
+ "postpack": "rm -f oclif.manifest.json",
14
+ "prepack": "npm run build && oclif-dev manifest && oclif-dev readme",
15
+ "version": "oclif-dev readme && git add README.md",
16
+ "test:report": "tsc -p test && nyc --reporter=lcov --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
17
+ "pretest": "tsc -p test",
18
+ "test": "nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
19
+ "posttest": "npm run lint",
20
+ "lint": "eslint src/**/*.ts",
21
+ "format": "eslint src/**/*.ts --fix"
22
+ },
23
+ "dependencies": {
24
+ "@contentstack/cli-command": "^1.0.0",
25
+ "@contentstack/cli-utilities": "^1.0.0",
26
+ "@oclif/command": "^1.8.16",
27
+ "@oclif/config": "^1.18.3",
28
+ "chalk": "^4.0.0",
29
+ "debug": "^4.1.1",
30
+ "inquirer": "^7.1.0",
31
+ "winston": "^3.7.2"
32
+ },
33
+ "devDependencies": {
34
+ "@oclif/dev-cli": "^1.22.2",
35
+ "@oclif/plugin-help": "^5.1.12",
36
+ "@oclif/test": "^1.2.8",
37
+ "@types/chai": "^4.2.18",
38
+ "@types/inquirer": "^7.3.1",
39
+ "@types/mkdirp": "^1.0.1",
40
+ "@types/mocha": "^8.2.2",
41
+ "@types/node": "^14.14.32",
42
+ "@types/sinon": "^10.0.2",
43
+ "@types/tar": "^4.0.3",
44
+ "@types/winston": "^2.4.4",
45
+ "chai": "^4.3.4",
46
+ "eslint": "^8.18.0",
47
+ "eslint-config-oclif": "^3.1.0",
48
+ "eslint-config-oclif-typescript": "^0.1.0",
49
+ "globby": "^10.0.2",
50
+ "mocha": "^9.2.2",
51
+ "nyc": "^15.1.0",
52
+ "rimraf": "^2.7.1",
53
+ "sinon": "^11.1.1",
54
+ "tmp": "^0.2.1",
55
+ "ts-node": "^8.10.2",
56
+ "typescript": "^4.7.4"
57
+ },
58
+ "engines": {
59
+ "node": ">=8.0.0"
60
+ },
61
+ "files": [
62
+ "/lib",
63
+ "/npm-shrinkwrap.json",
64
+ "/oclif.manifest.json"
65
+ ],
66
+ "homepage": "https://github.com/contentstack/cli",
67
+ "keywords": [
68
+ "contentstack",
69
+ "cli",
70
+ "plugin"
71
+ ],
72
+ "license": "MIT",
73
+ "oclif": {
74
+ "commands": "./lib/commands",
75
+ "bin": "csdx",
76
+ "devPlugins": [
77
+ "@oclif/plugin-help"
78
+ ],
79
+ "protected": [
80
+ "config:get:region"
81
+ ]
82
+ },
83
+ "husky": {
84
+ "hooks": {
85
+ "pre-commit": "npm run lint"
86
+ }
87
+ },
88
+ "repository": "contentstack/cli"
89
+ }