datocms-client
Version:
For new DatoCMS users, we recommend @datocms/cma-client-node
75 lines (60 loc) • 3.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = runPendingMigrations;
var _ora = _interopRequireDefault(require("ora"));
var _SiteClient = _interopRequireDefault(require("../site/SiteClient"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function runPendingMigrations(_x) {
return _runPendingMigrations.apply(this, arguments);
}
function _runPendingMigrations() {
_runPendingMigrations = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref) {
var sourceEnvId, destinationEnvId, cmaBaseUrl, tokenByArg, token, client, allEnvironments, sourceEnv, forkSpinner, existingEnvironment;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
sourceEnvId = _ref.sourceEnvId, destinationEnvId = _ref.destinationEnvId, cmaBaseUrl = _ref.cmaBaseUrl, tokenByArg = _ref.token;
token = tokenByArg || process.env.DATO_MANAGEMENT_API_TOKEN;
client = new _SiteClient["default"](token, {
baseUrl: cmaBaseUrl
});
_context.next = 5;
return client.environments.all();
case 5:
allEnvironments = _context.sent;
_context.next = 8;
return client.environments.find(sourceEnvId);
case 8:
sourceEnv = _context.sent;
forkSpinner = (0, _ora["default"])("Creating a fork of `".concat(sourceEnv.id, "` called `").concat(destinationEnvId, "`...")).start();
existingEnvironment = allEnvironments.find(function (env) {
return env.id === destinationEnvId;
});
if (!existingEnvironment) {
_context.next = 14;
break;
}
forkSpinner.fail();
throw new Error("Environment ".concat(destinationEnvId, " already exists!"));
case 14:
_context.next = 16;
return client.environments.fork(sourceEnv.id, {
id: destinationEnvId
});
case 16:
forkSpinner.succeed();
process.stdout.write('Done!\n');
case 18:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _runPendingMigrations.apply(this, arguments);
}