@prismatic-io/spectral
Version:
Utility library for building Prismatic connectors and code-native integrations
88 lines (87 loc) • 4.14 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createActions = void 0;
const path_1 = __importDefault(require("path"));
const createImport_1 = require("../utils/createImport");
const createTemplate_1 = require("../utils/createTemplate");
const createTypeInterface_1 = require("../utils/createTypeInterface");
const getImports_1 = require("./getImports");
const getInputs_1 = require("./getInputs");
const helpers_1 = require("./helpers");
const createActions = (_a) => __awaiter(void 0, [_a], void 0, function* ({ component, dryRun, verbose, sourceDir, destinationDir, }) {
var _b, _c;
if (verbose) {
console.info("Creating actions...");
}
const actionIndex = yield renderActionsIndex({
imports: Object.entries((_b = component.actions) !== null && _b !== void 0 ? _b : {}).map(([actionKey, action]) => {
var _a;
return {
import: (0, createImport_1.createImport)((_a = action.key) !== null && _a !== void 0 ? _a : actionKey),
};
}),
dryRun,
verbose,
sourceDir,
destinationDir,
});
const actions = yield Promise.all(Object.entries((_c = component.actions) !== null && _c !== void 0 ? _c : {}).map((_a) => __awaiter(void 0, [_a], void 0, function* ([actionKey, action]) {
var _b, _c;
const inputs = (0, getInputs_1.getInputs)({
inputs: action.inputs,
});
const imports = (0, getImports_1.getImports)({ inputs });
return yield renderAction({
action: Object.assign(Object.assign({ typeInterface: (0, createTypeInterface_1.createTypeInterface)((_b = action.key) !== null && _b !== void 0 ? _b : actionKey), import: (0, createImport_1.createImport)((_c = action.key) !== null && _c !== void 0 ? _c : actionKey), key: action.key || actionKey, label: action.display.label, description: action.display.description, inputs }, (action.examplePayload ? { examplePayload: action.examplePayload } : {})), { componentKey: component.key }),
imports,
dryRun,
verbose,
sourceDir,
destinationDir,
});
})));
if (verbose) {
console.info("");
}
return Promise.resolve({
actionIndex,
actions,
});
});
exports.createActions = createActions;
const renderActionsIndex = (_a) => __awaiter(void 0, [_a], void 0, function* ({ imports, dryRun, verbose, sourceDir, destinationDir, }) {
return yield (0, createTemplate_1.createTemplate)({
source: path_1.default.join(sourceDir, "actions", "index.ts.ejs"),
destination: path_1.default.join(destinationDir, "actions", "index.ts"),
data: {
imports,
},
dryRun,
verbose,
});
});
const renderAction = (_a) => __awaiter(void 0, [_a], void 0, function* ({ action, dryRun, imports, verbose, sourceDir, destinationDir, }) {
return yield (0, createTemplate_1.createTemplate)({
source: path_1.default.join(sourceDir, "actions", "action.ts.ejs"),
destination: path_1.default.join(destinationDir, "actions", `${action.import}.ts`),
data: {
action,
helpers: helpers_1.helpers,
imports,
},
dryRun,
verbose,
});
});