@nzz/q-cli 1.9.5 → 1.10.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.
@@ -0,0 +1,70 @@
1
+ const schemaService = require('../schemaService.js');
2
+ const configStore = require('../configStore.js');
3
+ const itemService = require('../itemService.js');
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const chalk = require('chalk');
7
+ const errorColor = chalk.red;
8
+ const successColor = chalk.green;
9
+
10
+ module.exports = async function (command) {
11
+ try {
12
+ const qConfigPath = path.resolve(command.config);
13
+
14
+ if (!fs.existsSync(qConfigPath)) {
15
+ console.error(
16
+ errorColor(
17
+ "Couldn't find config file named q.config.json in the current directory. Create a config file in the current directory or pass the path to the config file with the option -c <path>"
18
+ )
19
+ );
20
+ process.exit(1);
21
+ }
22
+
23
+ const qConfig = JSON.parse(fs.readFileSync(qConfigPath));
24
+ const validationResult = schemaService.validateConfig(qConfig, 'createCustomCodeItem');
25
+
26
+ if (!validationResult.isValid) {
27
+ console.error(errorColor(`A problem occured while validating the config file: ${validationResult.errorsText}`));
28
+ process.exit(1);
29
+ }
30
+
31
+ const environmentName = command.environment;
32
+ const config = await configStore.setupStore(qConfig, environmentName, command.reset);
33
+ const items = itemService.getItems(qConfig);
34
+ const firstItem = items?.[0];
35
+
36
+ if (!firstItem) {
37
+ console.error(errorColor('No items found in the config file.'));
38
+ process.exit(1);
39
+ }
40
+
41
+ // Create a new custom code item
42
+ const title = command.title || 'Custom Code item created by Q-cli';
43
+ const newItem = await itemService.createItem(
44
+ { title: title, tool: 'custom_code' },
45
+ { name: environmentName },
46
+ config
47
+ );
48
+
49
+ if (!newItem) {
50
+ console.error(errorColor('Failed to create a new custom code item.'));
51
+ process.exit(1);
52
+ }
53
+
54
+ // Add the new custom code item to the config file
55
+ qConfig.items[0].environments.push({
56
+ id: newItem._id,
57
+ name: environmentName,
58
+ });
59
+
60
+ // Write the updated config file
61
+ fs.writeFileSync(qConfigPath, JSON.stringify(qConfig, null, 2));
62
+
63
+ console.log(
64
+ successColor(`Successfully added new custom code item with id ${newItem._id} on ${environmentName} environment.`)
65
+ );
66
+ } catch (error) {
67
+ console.error(errorColor(`A problem occured while parsing the config file at ${command.config}.`));
68
+ console.error(error);
69
+ }
70
+ };
@@ -0,0 +1,41 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "title": "Q Config",
4
+ "description": "Config used by the Q CLI to update items",
5
+ "type": "object",
6
+ "properties": {
7
+ "items": {
8
+ "description": "Array of Q items",
9
+ "type": "array",
10
+ "minItems": 1,
11
+ "items": {
12
+ "type": "object",
13
+ "properties": {
14
+ "environments": {
15
+ "type": "array",
16
+ "minItems": 1,
17
+ "items": {
18
+ "type": "object",
19
+ "properties": {
20
+ "name": {
21
+ "type": "string",
22
+ "description": "Environment name of Q item"
23
+ },
24
+ "id": {
25
+ "type": "string",
26
+ "description": "Id of Q item"
27
+ }
28
+ },
29
+ "required": ["name", "id"]
30
+ }
31
+ },
32
+ "item": {
33
+ "type": "object"
34
+ }
35
+ },
36
+ "required": ["environments", "item"]
37
+ }
38
+ }
39
+ },
40
+ "required": ["items"]
41
+ }
@@ -25,6 +25,7 @@ async function getToolSchema(qServer, tool) {
25
25
 
26
26
  function getSchemaPathFor(commandName) {
27
27
  const pathFor = {
28
+ createCustomCodeItem: "./createCustomCodeItem/schema.json",
28
29
  copyItem: "./copyItem/copySchema.json",
29
30
  updateItem: "./updateItem/updateSchema.json",
30
31
  };
package/bin/q.js CHANGED
@@ -8,6 +8,7 @@ const runServer = require("./commands/server.js");
8
8
  const bootstrap = require("./commands/bootstrap.js");
9
9
  const updateItem = require("./commands/qItem/updateItem/updateItem.js");
10
10
  const copyItem = require("./commands/qItem/copyItem/copyItem.js");
11
+ const createCustomCodeItem = require("./commands/qItem/createCustomCodeItem/createCustomCodeItem.js");
11
12
 
12
13
  async function main() {
13
14
  program.version(version).description("Q Toolbox cli");
@@ -185,6 +186,27 @@ async function main() {
185
186
  await copyItem(command);
186
187
  });
187
188
 
189
+ program
190
+ .command("create-custom-code-item")
191
+ .description("creates a new q custom code item in the db and adds it to the q config file")
192
+ .option(
193
+ "-c, --config [path]",
194
+ "set config path to q.config.json. defaults to ./q.config.json",
195
+ `${process.cwd()}/q.config.json`
196
+ )
197
+ .option(
198
+ "-e, --environment [env]",
199
+ "set environment where the new q custom code item should be created in"
200
+ )
201
+ .option(
202
+ "-t, --title [title]",
203
+ "set title of the new q custom code item"
204
+ )
205
+ .option("-r, --reset", "reset stored configuration properties")
206
+ .action(async (command) => {
207
+ await createCustomCodeItem(command);
208
+ });
209
+
188
210
  await program.parseAsync(process.argv);
189
211
  }
190
212
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nzz/q-cli",
3
- "version": "1.9.5",
3
+ "version": "1.10.0",
4
4
  "prettier": "@nzz/et-utils-config-prettier",
5
5
  "description": "Cli tool to setup new Q tools, new Q server implementations and start Q dev server to test developing Q tools",
6
6
  "main": "index.js",