@nzz/q-cli 2.0.0-beta.10 → 2.0.0-beta.13

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.
@@ -15,7 +15,11 @@ export default function (command) {
15
15
  const qConfigPath = resolve(command.config);
16
16
  if (!existsSync(qConfigPath)) {
17
17
  logError(`Couldn't find config file: '${qConfigPath}'.\nCreate a config file in the current directory or pass the path to the config file with the option -c <path>`);
18
- return;
18
+ process.exit(1);
19
+ }
20
+ if (!command.environment) {
21
+ logError('Environment is required');
22
+ process.exit(1);
19
23
  }
20
24
  // Get the Q server URL and access token from the environment variables or 1Password
21
25
  const qServerUrl = getQServerUrl(command.environment);
package/dist/enums.js CHANGED
@@ -3,4 +3,5 @@ export var Environment;
3
3
  Environment["PRODUCTION"] = "production";
4
4
  Environment["STAGING"] = "staging";
5
5
  Environment["TEST"] = "test";
6
+ Environment["LOCAL"] = "local";
6
7
  })(Environment || (Environment = {}));
package/dist/index.js CHANGED
@@ -21,34 +21,12 @@ function main() {
21
21
  program.version(version).description('Q Toolbox cli');
22
22
  program
23
23
  .command('new-custom-code')
24
- .option('-d, --dir [path]', 'the base directory to bootstrap the new custom code project in')
24
+ .argument('<path>', 'the base directory to bootstrap the new custom code project in')
25
25
  .description('bootstrap a new custom code project')
26
- .action((command) => {
27
- newCustomCode(command);
26
+ .action(() => {
27
+ const dir = program.args[1];
28
+ newCustomCode({ dir });
28
29
  });
29
- program
30
- .command('new-et-utils-package')
31
- .option('-d, --dir [path]', 'the base directory to bootstrap the new tool in, defaults to the tools name')
32
- .description('bootstrap a new ed-tech utility package')
33
- .action(() => __awaiter(this, void 0, void 0, function* () {
34
- // const name = program.args[1];
35
- // const author = program.args[2] || 'TODO: Set package author name';
36
- // const description = program.args[3] || 'TODO: Write a package description';
37
- // if (!name) {
38
- // console.error(errorColor('no package name given'));
39
- // process.exit(1);
40
- // }
41
- // const baseDir = program.dir || name;
42
- // const textReplacements = [
43
- // { regex: new RegExp('<package-name>', 'g'), replaceWith: name },
44
- // { regex: new RegExp('<author-name>', 'g'), replaceWith: author },
45
- // {
46
- // regex: new RegExp('<package-description>', 'g'),
47
- // replaceWith: description,
48
- // },
49
- // ];
50
- // await bootstrap('et-utils-package', baseDir, textReplacements);
51
- }));
52
30
  program
53
31
  .command('update-item')
54
32
  .description('update q item')
@@ -57,14 +35,6 @@ function main() {
57
35
  .action((command) => __awaiter(this, void 0, void 0, function* () {
58
36
  yield updateItem(command);
59
37
  }));
60
- program
61
- .command('copy-item')
62
- .description('copies an existing q item')
63
- .option('-c, --config [path]', 'set config path which defines the q items to be copied. defaults to ./q.config.json', `${process.cwd()}/q.config.json`)
64
- .option('-e, --environment [env]', 'set environment where the existing q item is found, defaults to copy all items of all environments defined in config')
65
- .action((command) => __awaiter(this, void 0, void 0, function* () {
66
- // await copyItem(command);
67
- }));
68
38
  program
69
39
  .command('create-custom-code-item')
70
40
  .description('creates a new q custom code item in the db and adds it to the q config file')
@@ -14,6 +14,39 @@ import FormData from 'form-data';
14
14
  import fetch from 'node-fetch'; // Use node-fetch instead of native fetch, because native fetch seems to have issues with FormData/uploading files
15
15
  import { getAccessToken, getQServerUrl, logError, logSuccess, updateItem } from './utils.js';
16
16
  import updateSchemaJson from './assets/updateSchema.json' with { type: 'json' };
17
+ import { Environment } from './enums.js';
18
+ const envCredentials = {
19
+ [Environment.LOCAL]: {
20
+ qServerUrl: '',
21
+ accessToken: '',
22
+ },
23
+ [Environment.TEST]: {
24
+ qServerUrl: '',
25
+ accessToken: '',
26
+ },
27
+ [Environment.STAGING]: {
28
+ qServerUrl: '',
29
+ accessToken: '',
30
+ },
31
+ [Environment.PRODUCTION]: {
32
+ qServerUrl: '',
33
+ accessToken: '',
34
+ },
35
+ };
36
+ /**
37
+ * Get the Q server URL and access token from the environment variables or 1Password
38
+ * @param environment The environment to get the credentials for
39
+ * @returns The Q server URL and access token
40
+ */
41
+ function getEnvCredentials(environment) {
42
+ if (envCredentials[environment].qServerUrl === '' || envCredentials[environment].accessToken === '') {
43
+ envCredentials[environment] = {
44
+ qServerUrl: getQServerUrl(environment),
45
+ accessToken: getAccessToken(environment),
46
+ };
47
+ }
48
+ return envCredentials[environment];
49
+ }
17
50
  function validateConfig(metaSchema, qConfig) {
18
51
  const ajv = new Ajv({ allErrors: true });
19
52
  let isValid = true;
@@ -107,16 +140,37 @@ export default function (command) {
107
140
  logError(`A problem occurred while validating the config file: ${validationResult.errorsText}`);
108
141
  process.exit(1);
109
142
  }
110
- // Get the Q server URL and access token from the environment variables or 1Password
111
- const qServerUrl = getQServerUrl(command.environment);
112
- const accessToken = getAccessToken(command.environment);
113
- console.log(`Updating items on server ${qServerUrl}...`);
143
+ let qServerUrl;
144
+ let accessToken;
145
+ let environmentToUpdate;
114
146
  for (const item of qConfig.items) {
115
147
  for (const environment of item.environments) {
116
- // Skip if the environment is not the one we want to update
117
- if (command.environment !== environment.name)
148
+ // If the environment is specified, use that environment
149
+ if (command.environment && !environmentToUpdate) {
150
+ // Skip if the environment is not the one we want to update
151
+ if (command.environment !== environment.name)
152
+ continue;
153
+ environmentToUpdate = command.environment;
154
+ // Get the credentials for the current environment
155
+ const credentials = getEnvCredentials(environmentToUpdate);
156
+ qServerUrl = credentials.qServerUrl;
157
+ accessToken = credentials.accessToken;
158
+ console.log(`Updating items on server ${qServerUrl}...`);
159
+ }
160
+ else if (!command.environment && environmentToUpdate !== environment.name) {
161
+ // Otherwise, use the environment from the config
162
+ environmentToUpdate = environment.name;
163
+ // Get the credentials for the current environment
164
+ const credentials = getEnvCredentials(environmentToUpdate);
165
+ qServerUrl = credentials.qServerUrl;
166
+ accessToken = credentials.accessToken;
167
+ console.log(`Updating items on server ${qServerUrl}...`);
168
+ }
169
+ else {
170
+ // If the environment is not specified, skip the environment
118
171
  continue;
119
- console.log(`Updating item ${environment.id} on ${environment.name} environment...`);
172
+ }
173
+ console.log(`Updating item ${environment.id} on ${environmentToUpdate} environment...`);
120
174
  let qDoc = structuredClone(item.item);
121
175
  qDoc._id = environment.id;
122
176
  // Process the 'path' properties and upload files
@@ -128,7 +182,7 @@ export default function (command) {
128
182
  // Update the item on the Q server
129
183
  const result = yield updateItem(qDoc, qServerUrl, accessToken);
130
184
  if (result.success) {
131
- logSuccess(`Successfully updated item with id ${environment.id} on ${environment.name} environment`);
185
+ logSuccess(`Successfully updated item with id ${environment.id} on ${environmentToUpdate} environment`);
132
186
  }
133
187
  else {
134
188
  logError(result.msg);
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@nzz/q-cli",
3
- "version": "2.0.0-beta.10",
3
+ "version": "2.0.0-beta.13",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "bin": {
8
- "q": "./dist/index.js"
8
+ "Q": "./dist/index.js"
9
9
  },
10
10
  "files": [
11
11
  "dist"