@squiz/dxp-cli-next 5.6.0-develop.3 → 5.6.0-develop.5

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.
@@ -21,7 +21,7 @@ describe('upload job', () => {
21
21
  yield program.parseAsync([
22
22
  'node',
23
23
  'dxp-cli',
24
- './src/__tests__/jobrunner/simple-job',
24
+ './src/__tests__/job-runner/simple-job',
25
25
  '-t',
26
26
  'myTenant',
27
27
  '-ou',
@@ -29,7 +29,7 @@ describe('upload job', () => {
29
29
  ]);
30
30
  const opts = program.opts();
31
31
  const args = program.args;
32
- expect(args[0]).toEqual('./src/__tests__/jobrunner/simple-job');
32
+ expect(args[0]).toEqual('./src/__tests__/job-runner/simple-job');
33
33
  expect(opts.tenant).toEqual('myTenant');
34
34
  expect(opts.overrideUrl).toEqual('http://localhost:9999');
35
35
  expect(logSpy).toHaveBeenCalledTimes(4);
@@ -40,7 +40,7 @@ const createListJobContextsCommand = () => {
40
40
  res.data.forEach((context) => {
41
41
  console.log(`${chalk_1.default.green(chalk_1.default.bold(context.contextName))}: ${chalk_1.default.green(context.description)}`);
42
42
  for (const [key, value] of Object.entries(context.environment)) {
43
- console.log(`${chalk_1.default.bold(key)}: ${value}`);
43
+ console.log(`${chalk_1.default.bold(key)}: ${JSON.stringify(value)}`);
44
44
  }
45
45
  (0, utils_1.writeLineSeparator)();
46
46
  });
@@ -57,8 +57,8 @@ describe('listJobContexts', () => {
57
57
  expect(opts.overrideUrl).toEqual('http://localhost:9999');
58
58
  expect(logSpy).toHaveBeenCalledTimes(9);
59
59
  expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.green(chalk_1.default.bold('dev'))}: ${chalk_1.default.green('dev context')}`);
60
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('something')}: cool`);
61
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('somethingElse')}: cooler`);
60
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('something')}: ${JSON.stringify('cool')}`);
61
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('somethingElse')}: ${JSON.stringify('cooler')}`);
62
62
  expect(logSpy).toHaveBeenCalledWith(chalk_1.default.cyan('------------------------------'));
63
63
  }));
64
64
  });
@@ -22,29 +22,34 @@ const createBeginJobCommand = () => {
22
22
  .name('beginJob')
23
23
  .description('Add a job to the queue')
24
24
  .addOption(new commander_1.Option('-t, --tenant <string>', 'Tenant ID to deploy to. If not provided will use configured tenant from login'))
25
+ .addOption(new commander_1.Option('-i, --inputFile <string>', 'Path to the .json input file for the job execution e.g. documents/inputs/myInput.json'))
25
26
  .addArgument(new commander_1.Argument('<jobName>', 'Name of the job to pass to the request'))
26
27
  .addArgument(new commander_1.Argument('<contextName>', 'Name of the job context to pass to the request'))
27
- .addArgument(new commander_1.Argument('<inputSource>', 'Path to the .json input file for the job execution'))
28
28
  .configureOutput({
29
29
  outputError(str, write) {
30
30
  write(chalk_1.default.red(str));
31
31
  },
32
32
  })
33
- .action((jobName, contextName, inputSource, options) => __awaiter(void 0, void 0, void 0, function* () {
33
+ .action((jobName, contextName, options) => __awaiter(void 0, void 0, void 0, function* () {
34
34
  yield (0, utils_1.throwErrorIfNotLoggedIn)(beginJobCommand);
35
35
  const spinner = (0, ora_1.default)('Queueing job').start();
36
36
  console.log('');
37
+ let input;
37
38
  try {
39
+ if (options.inputFile) {
40
+ input = yield (0, utils_1.readInputFile)(options.inputFile);
41
+ }
38
42
  const apiService = new ApiService_1.ApiService(utils_1.validateAxiosStatus);
39
43
  const beginJobUrl = `${yield (0, utils_1.buildJobRunnerUrl)(options.tenant, options.overrideUrl)}/executions/begin`;
40
44
  const res = yield apiService.client.post(beginJobUrl, {
41
45
  jobName: jobName,
42
46
  context: contextName,
47
+ input: input ? input : null,
43
48
  });
44
49
  spinner.succeed('Job queued successfully');
45
50
  (0, utils_1.writeLineSeparator)();
46
51
  for (const [key, value] of Object.entries(res.data)) {
47
- console.log(`${chalk_1.default.bold([key])}: ${value}`);
52
+ console.log(`${chalk_1.default.bold([key])}: ${JSON.stringify(value)}`);
48
53
  }
49
54
  return;
50
55
  }
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const nock_1 = __importDefault(require("nock"));
16
16
  const beginJob_1 = __importDefault(require("./beginJob"));
17
17
  const chalk_1 = __importDefault(require("chalk"));
18
+ const path_1 = __importDefault(require("path"));
18
19
  const logSpy = jest.spyOn(global.console, 'log');
19
20
  describe('beginJob', () => {
20
21
  it('correctly displays the console logs for beginning a job', () => __awaiter(void 0, void 0, void 0, function* () {
@@ -37,7 +38,8 @@ describe('beginJob', () => {
37
38
  'dxp-cli',
38
39
  'simple-job',
39
40
  'dev',
40
- 'myinputs/input',
41
+ '-i',
42
+ path_1.default.join('./src/__tests__/job-runner/inputs/input.json'),
41
43
  '-t',
42
44
  'myTenant',
43
45
  '-ou',
@@ -47,15 +49,14 @@ describe('beginJob', () => {
47
49
  const args = program.args;
48
50
  expect(args[0]).toEqual('simple-job');
49
51
  expect(args[1]).toEqual('dev');
50
- expect(args[2]).toEqual('myinputs/input');
52
+ expect(opts.inputFile).toEqual(path_1.default.join('./src/__tests__/job-runner/inputs/input.json'));
51
53
  expect(opts.tenant).toEqual('myTenant');
52
54
  expect(opts.overrideUrl).toEqual('http://localhost:9999');
53
55
  expect(logSpy).toHaveBeenCalledTimes(12);
54
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('status')}: queued`);
55
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('id')}: d0aab852-365e-42de-a642-eac11bb95ccc`);
56
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('jobName')}: simple-job`);
57
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('status')}: queued`);
58
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('timeQueued')}: 2023-06-23T01:36:44.792Z`);
59
56
  expect(logSpy).toHaveBeenCalledWith(chalk_1.default.cyan('------------------------------'));
57
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('context')}: "dev"`);
58
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('id')}: "d0aab852-365e-42de-a642-eac11bb95ccc"`);
59
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('jobName')}: "simple-job"`);
60
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('timeQueued')}: "2023-06-23T01:36:44.792Z"`);
60
61
  }));
61
62
  });
@@ -39,7 +39,7 @@ const createListJobExecutionsCommand = () => {
39
39
  (0, utils_1.writeLineSeparator)();
40
40
  res.data.forEach((execution) => {
41
41
  for (const [key, value] of Object.entries(execution)) {
42
- console.log(`${chalk_1.default.bold([key])}: ${value}`);
42
+ console.log(`${chalk_1.default.bold([key])}: ${JSON.stringify(value)}`);
43
43
  }
44
44
  (0, utils_1.writeLineSeparator)();
45
45
  });
@@ -59,8 +59,8 @@ describe('listJobExecutions', () => {
59
59
  expect(opts.overrideUrl).toEqual('http://localhost:9999');
60
60
  expect(logSpy).toHaveBeenCalled();
61
61
  expect(logSpy).toHaveBeenCalledTimes(19);
62
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('status')}: successfully-completed`);
63
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('jobName')}: simple-job`);
62
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('status')}: ${JSON.stringify('successfully-completed')}`);
63
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('jobName')}: ${JSON.stringify('simple-job')}`);
64
64
  expect(logSpy).toHaveBeenCalledWith(chalk_1.default.cyan('------------------------------'));
65
65
  }));
66
66
  });
@@ -44,7 +44,7 @@ const createTerminateJobCommand = () => {
44
44
  reason: reason,
45
45
  });
46
46
  for (const [key, value] of Object.entries(res.data)) {
47
- console.log(`${chalk_1.default.bold([key])}: ${value}`);
47
+ console.log(`${chalk_1.default.bold([key])}: ${JSON.stringify(value)}`);
48
48
  }
49
49
  spinner.succeed('Job terminated');
50
50
  return;
@@ -50,11 +50,11 @@ describe('terminateJob', () => {
50
50
  expect(opts.tenant).toEqual('myTenant');
51
51
  expect(opts.overrideUrl).toEqual('http://localhost:9999');
52
52
  expect(logSpy).toHaveBeenCalledTimes(11);
53
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('status')}: terminated`);
54
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('jobName')}: simple-job`);
55
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('id')}: simple-job-d6a418eb-dd63-4594-b299-07410ccd30d5`);
56
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('context')}: dev`);
57
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('timeFinished')}: 2023-06-23T01:49:09.433Z`);
58
- expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('version')}: 1.0.1`);
53
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('status')}: ${JSON.stringify('terminated')}`);
54
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('jobName')}: ${JSON.stringify('simple-job')}`);
55
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('id')}: ${JSON.stringify('simple-job-d6a418eb-dd63-4594-b299-07410ccd30d5')}`);
56
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('context')}: ${JSON.stringify('dev')}`);
57
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('timeFinished')}: ${JSON.stringify('2023-06-23T01:49:09.433Z')}`);
58
+ expect(logSpy).toHaveBeenCalledWith(`${chalk_1.default.bold('version')}: ${JSON.stringify('1.0.1')}`);
59
59
  }));
60
60
  });
@@ -2,5 +2,6 @@ import { Command } from 'commander';
2
2
  export declare function handleCommandError(command: Command, error: Error): void;
3
3
  export declare function throwErrorIfNotLoggedIn(command: Command): Promise<void>;
4
4
  export declare function buildJobRunnerUrl(tenantID?: string, overrideUrl?: string): Promise<string>;
5
+ export declare function readInputFile(inputPath: string): Promise<any>;
5
6
  export declare function writeLineSeparator(): void;
6
7
  export declare function validateAxiosStatus(status: number): boolean;
@@ -12,7 +12,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.validateAxiosStatus = exports.writeLineSeparator = exports.buildJobRunnerUrl = exports.throwErrorIfNotLoggedIn = exports.handleCommandError = void 0;
15
+ exports.validateAxiosStatus = exports.writeLineSeparator = exports.readInputFile = exports.buildJobRunnerUrl = exports.throwErrorIfNotLoggedIn = exports.handleCommandError = void 0;
16
+ const fs_1 = __importDefault(require("fs"));
16
17
  const chalk_1 = __importDefault(require("chalk"));
17
18
  const ApplicationConfig_1 = require("../ApplicationConfig");
18
19
  const axios_1 = __importDefault(require("axios"));
@@ -62,6 +63,27 @@ function buildJobRunnerUrl(tenantID, overrideUrl) {
62
63
  });
63
64
  }
64
65
  exports.buildJobRunnerUrl = buildJobRunnerUrl;
66
+ function readInputFile(inputPath) {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ if (inputPath.endsWith('.json')) {
69
+ if (fs_1.default.existsSync(inputPath)) {
70
+ try {
71
+ return JSON.parse(fs_1.default.readFileSync(inputPath, { encoding: 'utf-8' }));
72
+ }
73
+ catch (e) {
74
+ throw new Error(`Unable to read file ${inputPath} - ${e.message}`);
75
+ }
76
+ }
77
+ else {
78
+ throw new Error('Input could not be found');
79
+ }
80
+ }
81
+ else {
82
+ throw new Error('Input must be a .json file');
83
+ }
84
+ });
85
+ }
86
+ exports.readInputFile = readInputFile;
65
87
  function writeLineSeparator() {
66
88
  console.log(chalk_1.default.cyan('------------------------------'));
67
89
  }
@@ -16,6 +16,7 @@ const utils_1 = require("./utils");
16
16
  const commander_1 = require("commander");
17
17
  const axios_1 = require("axios");
18
18
  const chalk_1 = __importDefault(require("chalk"));
19
+ const path_1 = __importDefault(require("path"));
19
20
  const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {
20
21
  return undefined;
21
22
  }); // prevent process exit on error
@@ -73,3 +74,19 @@ describe('buildJobRunnerUrl', () => {
73
74
  expect(url).toBe('http://localhost:9999/someTenant');
74
75
  }));
75
76
  });
77
+ describe('readInputFile', () => {
78
+ it('correctly reads the file ', () => __awaiter(void 0, void 0, void 0, function* () {
79
+ const res = yield (0, utils_1.readInputFile)(path_1.default.join('./src/__tests__/job-runner/inputs/input.json'));
80
+ expect(res).toStrictEqual({ something: 'amazing' });
81
+ }));
82
+ it('errors if file extension is not .json', () => __awaiter(void 0, void 0, void 0, function* () {
83
+ yield expect((0, utils_1.readInputFile)(path_1.default.join('./src/__tests__/job-runner/simple-job/main.js'))).rejects.toThrow('Input must be a .json file');
84
+ }));
85
+ it('errors if file does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
86
+ yield expect((0, utils_1.readInputFile)(path_1.default.join('./src/__tests__/job-runner/simple-job/input.json'))).rejects.toThrow('Input could not be found');
87
+ }));
88
+ it('errors if file has json extension but cannot be parsed', () => __awaiter(void 0, void 0, void 0, function* () {
89
+ const source = './src/__tests__/job-runner/inputs/badInput.json';
90
+ yield expect((0, utils_1.readInputFile)(path_1.default.join(source))).rejects.toThrow(`Unable to read file ${path_1.default.join(source)} - Unexpected token i in JSON at position 1`);
91
+ }));
92
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/dxp-cli-next",
3
- "version": "5.6.0-develop.3",
3
+ "version": "5.6.0-develop.5",
4
4
  "repository": {
5
5
  "url": "https://gitlab.squiz.net/developer-experience/dxp-cli-next"
6
6
  },
@@ -39,7 +39,7 @@
39
39
  "codecov"
40
40
  ],
41
41
  "dependencies": {
42
- "@squiz/component-cli-lib": "^1.39.1-alpha.24",
42
+ "@squiz/component-cli-lib": "^1.41.1-alpha",
43
43
  "axios": "1.1.3",
44
44
  "cli-color": "2.0.3",
45
45
  "commander": "9.4.0",