@squiz/dxp-cli-next 5.10.0-develop.6 → 5.10.0-develop.8

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,7 @@ describe('dxp', () => {
15
15
  'auth Authenticate into the DXP CLI',
16
16
  'cmp Component Service Commands',
17
17
  'job-runner Job Runner Service Commands',
18
- // 'datastore Datastore Commands',
18
+ 'datastore Datastore Commands',
19
19
  ]);
20
20
  });
21
21
  });
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ declare const createPauseCommand: () => Command;
3
+ export default createPauseCommand;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const commander_1 = require("commander");
16
+ const chalk_1 = __importDefault(require("chalk"));
17
+ const ora_1 = __importDefault(require("ora"));
18
+ const utils_1 = require("../../utils");
19
+ const utils_2 = require("../utils");
20
+ const createPauseCommand = () => {
21
+ const pauseCommand = new commander_1.Command('pause')
22
+ .name('pause')
23
+ .description('Pauses the simulator')
24
+ .addOption(new commander_1.Option('-b, --blueprint <string>', 'Path to your datastore API yaml file.'))
25
+ .configureOutput({
26
+ outputError(str, write) {
27
+ write(chalk_1.default.red(str));
28
+ },
29
+ })
30
+ .action((options) => __awaiter(void 0, void 0, void 0, function* () {
31
+ yield (0, utils_1.throwErrorIfNotLoggedIn)(pauseCommand);
32
+ console.log('');
33
+ if (options.blueprint !== undefined) {
34
+ const { fullPath, dirPath, yamlFile, containerName } = (0, utils_2.getSimulatorDetails)(options.blueprint);
35
+ const command = `docker stop ${containerName}`;
36
+ const spinner = (0, ora_1.default)();
37
+ try {
38
+ yield (0, utils_2.checkUpdateAvailable)();
39
+ spinner.start('Pausing simulator.');
40
+ yield (0, utils_2.checkDockerInstalled)();
41
+ yield (0, utils_2.executeCommand)(command);
42
+ spinner.succeed(`Done! Container ID “${containerName}” paused.`);
43
+ return;
44
+ }
45
+ catch (error) {
46
+ const errorMsg = 'Did not pause because of an unexpected error. Ask for help in our support forums.';
47
+ (0, utils_1.logDebug)(`ERROR: ${JSON.stringify(error)}`);
48
+ spinner.fail(errorMsg);
49
+ (0, utils_1.handleCommandError)(pauseCommand, error);
50
+ }
51
+ }
52
+ else {
53
+ const command = 'docker ps --filter "name=datastore-sim-*" --filter "status=running" --format "{{.Names}}"';
54
+ const spinner = (0, ora_1.default)();
55
+ try {
56
+ yield (0, utils_2.checkUpdateAvailable)();
57
+ spinner.start('Pausing all simulators.');
58
+ yield (0, utils_2.checkDockerInstalled)();
59
+ const containerCommand = yield (0, utils_2.executeCommand)(command);
60
+ if (containerCommand === '') {
61
+ throw new Error('Sorry, all simulators are already paused.');
62
+ }
63
+ const containers = containerCommand.split(/\s+/).join(' ');
64
+ const results = yield (0, utils_2.executeCommand)(`docker stop ${containers}`);
65
+ results.split(/\s+/).forEach((result) => {
66
+ if (result !== '') {
67
+ spinner.succeed(`Done! Container ID “${result}” paused.`);
68
+ }
69
+ });
70
+ return;
71
+ }
72
+ catch (error) {
73
+ const errorMsg = 'Did not pause because of an unexpected error. Ask for help in our support forums.';
74
+ (0, utils_1.logDebug)(`ERROR: ${JSON.stringify(error)}`);
75
+ spinner.fail(errorMsg);
76
+ (0, utils_1.handleCommandError)(pauseCommand, error);
77
+ }
78
+ }
79
+ }));
80
+ if (process.env.ENABLE_OVERRIDE_DATASTORE_URL === 'true') {
81
+ pauseCommand.addOption(new commander_1.Option('-i, --image <string>', 'Override the docker image'));
82
+ }
83
+ return pauseCommand;
84
+ };
85
+ exports.default = createPauseCommand;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const pause_1 = __importDefault(require("./pause"));
39
+ const utils = __importStar(require("../utils"));
40
+ const logSpy = jest.spyOn(global.console, 'log');
41
+ const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {
42
+ return undefined;
43
+ }); // prevent process exit on error
44
+ beforeEach(() => {
45
+ jest.resetAllMocks();
46
+ });
47
+ describe('datastoreSimulatorPause', () => {
48
+ it('should pause a simulator', () => __awaiter(void 0, void 0, void 0, function* () {
49
+ const checkUpdateAvailableSpy = jest
50
+ .spyOn(utils, 'checkUpdateAvailable')
51
+ .mockImplementation(() => {
52
+ return Promise.resolve(null);
53
+ });
54
+ const forcePromptSpy = jest
55
+ .spyOn(utils, 'forcePrompt')
56
+ .mockResolvedValue(true);
57
+ let executeCommandCount = 0;
58
+ const executeCommandSpy = jest
59
+ .spyOn(utils, 'executeCommand')
60
+ .mockImplementation((cmd, opt) => {
61
+ executeCommandCount++;
62
+ switch (executeCommandCount) {
63
+ case 1:
64
+ expect(cmd).toStrictEqual('docker -v');
65
+ return Promise.resolve('Docker version 20.10.23, build 7155243');
66
+ case 2:
67
+ expect(cmd).toStrictEqual('docker stop datastore-sim-119726bb');
68
+ return Promise.resolve('');
69
+ }
70
+ return Promise.reject(false);
71
+ });
72
+ const program = (0, pause_1.default)();
73
+ const commandErrorSpy = jest.spyOn(program, 'error');
74
+ yield program.parseAsync([
75
+ 'node',
76
+ 'dxp-cli',
77
+ 'datastore',
78
+ 'simulator',
79
+ 'pause',
80
+ '-b',
81
+ '/path/to/blueprint.yaml',
82
+ ]);
83
+ const opts = program.opts();
84
+ expect(opts.blueprint).toEqual('/path/to/blueprint.yaml');
85
+ expect(checkUpdateAvailableSpy).toHaveBeenCalledTimes(1);
86
+ expect(commandErrorSpy).not.toHaveBeenCalled();
87
+ expect(logSpy).toHaveBeenCalledTimes(1);
88
+ expect(executeCommandSpy).toHaveBeenCalledTimes(2);
89
+ }));
90
+ });
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ declare const createResumeCommand: () => Command;
3
+ export default createResumeCommand;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const commander_1 = require("commander");
16
+ const chalk_1 = __importDefault(require("chalk"));
17
+ const ora_1 = __importDefault(require("ora"));
18
+ const utils_1 = require("../../utils");
19
+ const utils_2 = require("../utils");
20
+ const createResumeCommand = () => {
21
+ const resumeCommand = new commander_1.Command('resume')
22
+ .name('resume')
23
+ .description('Resumes the simulator')
24
+ .addOption(new commander_1.Option('-b, --blueprint <string>', 'Path to your datastore API yaml file.'))
25
+ .configureOutput({
26
+ outputError(str, write) {
27
+ write(chalk_1.default.red(str));
28
+ },
29
+ })
30
+ .action((options) => __awaiter(void 0, void 0, void 0, function* () {
31
+ yield (0, utils_1.throwErrorIfNotLoggedIn)(resumeCommand);
32
+ console.log('');
33
+ if (options.blueprint !== undefined) {
34
+ const { fullPath, dirPath, yamlFile, containerName } = (0, utils_2.getSimulatorDetails)(options.blueprint);
35
+ const command = `docker start ${containerName}`;
36
+ const spinner = (0, ora_1.default)();
37
+ try {
38
+ yield (0, utils_2.checkUpdateAvailable)();
39
+ spinner.start('Resuming simulator.');
40
+ yield (0, utils_2.checkDockerInstalled)();
41
+ yield (0, utils_2.executeCommand)(command);
42
+ spinner.succeed(`Done! Container ID “${containerName}” resumed.`);
43
+ return;
44
+ }
45
+ catch (error) {
46
+ const errorMsg = 'Did not resume because of an unexpected error. Ask for help in our support forums.';
47
+ (0, utils_1.logDebug)(`ERROR: ${JSON.stringify(error)}`);
48
+ spinner.fail(errorMsg);
49
+ (0, utils_1.handleCommandError)(resumeCommand, error);
50
+ }
51
+ }
52
+ else {
53
+ const command = 'docker ps --filter "name=datastore-sim-*" --filter "status=exited" --format "{{.Names}}"';
54
+ const spinner = (0, ora_1.default)();
55
+ try {
56
+ yield (0, utils_2.checkUpdateAvailable)();
57
+ spinner.start('Resuming all simulators.');
58
+ yield (0, utils_2.checkDockerInstalled)();
59
+ const containerCommand = yield (0, utils_2.executeCommand)(command);
60
+ if (containerCommand === '') {
61
+ throw new Error('Sorry, all simulators are already running.');
62
+ }
63
+ const containers = containerCommand.split(/\s+/).join(' ');
64
+ const results = yield (0, utils_2.executeCommand)(`docker start ${containers}`);
65
+ results.split(/\s+/).forEach((result) => {
66
+ if (result !== '') {
67
+ spinner.succeed(`Done! Container ID “${result}” paused.`);
68
+ }
69
+ });
70
+ return;
71
+ }
72
+ catch (error) {
73
+ const errorMsg = 'Did not resume because of an unexpected error. Ask for help in our support forums.';
74
+ (0, utils_1.logDebug)(`ERROR: ${JSON.stringify(error)}`);
75
+ spinner.fail(errorMsg);
76
+ (0, utils_1.handleCommandError)(resumeCommand, error);
77
+ }
78
+ }
79
+ }));
80
+ if (process.env.ENABLE_OVERRIDE_DATASTORE_URL === 'true') {
81
+ resumeCommand.addOption(new commander_1.Option('-i, --image <string>', 'Override the docker image'));
82
+ }
83
+ return resumeCommand;
84
+ };
85
+ exports.default = createResumeCommand;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const resume_1 = __importDefault(require("./resume"));
39
+ const utils = __importStar(require("../utils"));
40
+ const logSpy = jest.spyOn(global.console, 'log');
41
+ const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {
42
+ return undefined;
43
+ }); // prevent process exit on error
44
+ beforeEach(() => {
45
+ jest.resetAllMocks();
46
+ });
47
+ describe('datastoreSimulatorResume', () => {
48
+ it('should resume a simulator', () => __awaiter(void 0, void 0, void 0, function* () {
49
+ const checkUpdateAvailableSpy = jest
50
+ .spyOn(utils, 'checkUpdateAvailable')
51
+ .mockImplementation(() => {
52
+ return Promise.resolve(null);
53
+ });
54
+ const forcePromptSpy = jest
55
+ .spyOn(utils, 'forcePrompt')
56
+ .mockResolvedValue(true);
57
+ let executeCommandCount = 0;
58
+ const executeCommandSpy = jest
59
+ .spyOn(utils, 'executeCommand')
60
+ .mockImplementation((cmd, opt) => {
61
+ executeCommandCount++;
62
+ switch (executeCommandCount) {
63
+ case 1:
64
+ expect(cmd).toStrictEqual('docker -v');
65
+ return Promise.resolve('Docker version 20.10.23, build 7155243');
66
+ case 2:
67
+ expect(cmd).toStrictEqual('docker start datastore-sim-119726bb');
68
+ return Promise.resolve('');
69
+ }
70
+ return Promise.reject(false);
71
+ });
72
+ const program = (0, resume_1.default)();
73
+ const commandErrorSpy = jest.spyOn(program, 'error');
74
+ yield program.parseAsync([
75
+ 'node',
76
+ 'dxp-cli',
77
+ 'datastore',
78
+ 'simulator',
79
+ 'resume',
80
+ '-b',
81
+ '/path/to/blueprint.yaml',
82
+ ]);
83
+ const opts = program.opts();
84
+ expect(opts.blueprint).toEqual('/path/to/blueprint.yaml');
85
+ expect(checkUpdateAvailableSpy).toHaveBeenCalledTimes(1);
86
+ expect(commandErrorSpy).not.toHaveBeenCalled();
87
+ expect(logSpy).toHaveBeenCalledTimes(1);
88
+ expect(executeCommandSpy).toHaveBeenCalledTimes(2);
89
+ }));
90
+ });
@@ -9,6 +9,8 @@ const remove_1 = __importDefault(require("./remove/remove"));
9
9
  const list_1 = __importDefault(require("./list/list"));
10
10
  const clear_1 = __importDefault(require("./clear/clear"));
11
11
  const upgrade_1 = __importDefault(require("./upgrade/upgrade"));
12
+ const pause_1 = __importDefault(require("./pause/pause"));
13
+ const resume_1 = __importDefault(require("./resume/resume"));
12
14
  const createSimulatorCommand = () => {
13
15
  const datastoreSimulatorCommand = new commander_1.Command('simulator');
14
16
  datastoreSimulatorCommand
@@ -17,7 +19,9 @@ const createSimulatorCommand = () => {
17
19
  .addCommand((0, remove_1.default)())
18
20
  .addCommand((0, list_1.default)())
19
21
  .addCommand((0, clear_1.default)())
20
- .addCommand((0, upgrade_1.default)());
22
+ .addCommand((0, upgrade_1.default)())
23
+ .addCommand((0, pause_1.default)())
24
+ .addCommand((0, resume_1.default)());
21
25
  return datastoreSimulatorCommand;
22
26
  };
23
27
  exports.default = createSimulatorCommand;
package/lib/dxp.js CHANGED
@@ -14,6 +14,7 @@ validateNodeVersion();
14
14
  const auth_1 = __importDefault(require("./auth"));
15
15
  const cmp_1 = __importDefault(require("./cmp"));
16
16
  const job_runner_1 = __importDefault(require("./job-runner"));
17
+ const datastore_1 = __importDefault(require("./datastore"));
17
18
  const program = new commander_1.default.Command();
18
19
  const packageJson = require('../package.json');
19
20
  const version = packageJson.version;
@@ -24,7 +25,7 @@ program
24
25
  .addCommand(auth_1.default)
25
26
  .addCommand(cmp_1.default)
26
27
  .addCommand(job_runner_1.default)
27
- // .addCommand(datastoreCommand)
28
+ .addCommand(datastore_1.default)
28
29
  .action(() => {
29
30
  program.help();
30
31
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/dxp-cli-next",
3
- "version": "5.10.0-develop.6",
3
+ "version": "5.10.0-develop.8",
4
4
  "repository": {
5
5
  "url": "https://gitlab.squiz.net/dxp/dxp-cli-next"
6
6
  },
@@ -39,7 +39,7 @@
39
39
  "codecov"
40
40
  ],
41
41
  "dependencies": {
42
- "@squiz/component-cli-lib": "1.63.1-alpha.7",
42
+ "@squiz/component-cli-lib": "1.63.1",
43
43
  "@apidevtools/swagger-parser": "10.1.0",
44
44
  "axios": "1.1.3",
45
45
  "cli-color": "2.0.3",