@constructor-io/constructorio-connect-cli
Version:
CLI tool to enable users to interface with the Constructor Connect Ecosystem
80 lines (79 loc) โข 4.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const prompts_1 = require("@inquirer/prompts");
const core_1 = require("@oclif/core");
const fs_extra_1 = require("fs-extra");
const path_2 = require("../customer/path");
const file_exists_1 = require("../helpers/file-exists");
const ux_action_1 = require("../helpers/ux-action");
const get_fixture_1 = require("../http/get-fixture");
const render_prompt_1 = require("../prompt-data/render-prompt");
const render_tip_1 = require("../rendering/render-tip");
const types_1 = require("../types");
const check_if_connection_needs_mapping_1 = require("../helpers/check-if-connection-needs-mapping");
const select_connections_1 = require("../prompt-data/select-connections");
const refresh_connections_command_1 = require("./refresh-connections-command");
class GenerateFixture extends refresh_connections_command_1.RefreshConnectionsCommand {
static args = {};
static description = `
This command will fetch one fixture from the server and save it into a specified file. This fixture file will be an example of how the data would be passed to the connector when executing the templates.\n
You are expected to customize this file to match the data you expect to cover in a template execution (e.g. an item with stock, a variation with missing data), etc. Note that you can have multiple fixtures of the same type to cover different scenarios.\n
Finally, if the file already exists you'll be prompted to overwrite it.
`;
static examples = ["$ <%= config.bin %> generate-fixture"];
async runCommand() {
const connection = await (0, select_connections_1.selectConnections)("Choose the connection to generate the fixture for");
const choices = Object.keys(types_1.CatalogFixtureType).map((catalogFixtureType) => ({
name: catalogFixtureType,
value: Object(types_1.CatalogFixtureType)[catalogFixtureType],
}));
if (!(0, check_if_connection_needs_mapping_1.checkIfConnectionNeedsMapping)(connection.partner)) {
const index = choices.findIndex((choice) => choice.value === "mapping");
choices.splice(index, 1);
}
const type = await (0, render_prompt_1.renderPrompt)({
promptMessage: "Choose the type of fixture you want to generate",
choices,
});
const filename = await (0, prompts_1.input)({
message: "Enter the filename",
default: `${type}.json`,
});
const filepath = this.getFixturePath(type, filename);
const shouldAskReplace = (0, file_exists_1.fileExists)(filepath, true);
if (shouldAskReplace) {
const shouldRefresh = await (0, prompts_1.confirm)({
message: `This fixture already exists. Do you want to refresh it?`,
default: false,
});
if (!shouldRefresh) {
(0, render_tip_1.renderTip)([
"Your file was not refreshed!",
"If you want to generate the fixture, please execute the command again",
]);
return;
}
}
try {
await (0, ux_action_1.uxAction)("๐งถ Generating fixtures...", async () => {
const response = await (0, get_fixture_1.getFixture)({
connectionId: connection.id,
type: type,
});
await (0, fs_extra_1.outputFile)(filepath, JSON.stringify(response.data, null, 2));
})();
core_1.ux.stdout(`๐ ${type} fixture generated at ${filepath}`);
}
catch (err) {
core_1.ux.error(`๐ ${err.message}`);
}
}
getFixturePath(type, filename) {
return (0, path_2.getCustomerOSSpecificPath)(path_1.default.join("src", "fixtures", type, filename.replace(/\.[^/.]+$/, "") + ".json"));
}
}
exports.default = GenerateFixture;