@vendure/cli
Version:
A modern, headless ecommerce framework
140 lines • 5.69 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateCommand = migrateCommand;
const prompts_1 = require("@clack/prompts");
const picocolors_1 = __importDefault(require("picocolors"));
const utils_1 = require("../../utilities/utils");
const migration_operations_1 = require("./migration-operations");
const cancelledMessage = 'Migrate cancelled.';
async function migrateCommand(options) {
if ((options === null || options === void 0 ? void 0 : options.generate) || (options === null || options === void 0 ? void 0 : options.run) || (options === null || options === void 0 ? void 0 : options.revert)) {
await handleNonInteractiveMode(options);
return;
}
await handleInteractiveMode(options === null || options === void 0 ? void 0 : options.config);
}
async function handleNonInteractiveMode(options) {
try {
process.env.VENDURE_RUNNING_IN_CLI = 'true';
if (options.generate) {
const result = await (0, migration_operations_1.generateMigrationOperation)({
name: options.generate,
outputDir: options.outputDir,
config: options.config,
});
if (result.success) {
prompts_1.log.success(result.message);
}
else {
prompts_1.log.error(result.message);
process.exit(1);
}
}
else if (options.run) {
const result = await (0, migration_operations_1.runMigrationsOperation)(options.config);
if (result.success) {
prompts_1.log.success(result.message);
}
else {
prompts_1.log.error(result.message);
process.exit(1);
}
}
else if (options.revert) {
const result = await (0, migration_operations_1.revertMigrationOperation)(options.config);
if (result.success) {
prompts_1.log.success(result.message);
}
else {
prompts_1.log.error(result.message);
process.exit(1);
}
}
process.env.VENDURE_RUNNING_IN_CLI = undefined;
}
catch (e) {
prompts_1.log.error(e.message);
if (e.stack) {
prompts_1.log.error(e.stack);
}
process.exit(1);
}
}
async function handleInteractiveMode(configFile) {
console.log(`\n`);
(0, prompts_1.intro)(picocolors_1.default.blue('🛠️️ Vendure migrations'));
const action = await (0, utils_1.withInteractiveTimeout)(async () => {
return await (0, prompts_1.select)({
message: 'What would you like to do?',
options: [
{ value: 'generate', label: 'Generate a new migration' },
{ value: 'run', label: 'Run pending migrations' },
{ value: 'revert', label: 'Revert the last migration' },
],
});
});
if ((0, prompts_1.isCancel)(action)) {
(0, prompts_1.cancel)(cancelledMessage);
process.exit(0);
}
try {
process.env.VENDURE_RUNNING_IN_CLI = 'true';
if (action === 'generate') {
const { generateMigrationCommand } = await Promise.resolve().then(() => __importStar(require('./generate-migration/generate-migration')));
await generateMigrationCommand.run({ configFile });
}
if (action === 'run') {
const { runMigrationCommand } = await Promise.resolve().then(() => __importStar(require('./run-migration/run-migration')));
await runMigrationCommand.run({ configFile });
}
if (action === 'revert') {
const { revertMigrationCommand } = await Promise.resolve().then(() => __importStar(require('./revert-migration/revert-migration')));
await revertMigrationCommand.run();
}
(0, prompts_1.outro)('✅ Done!');
process.env.VENDURE_RUNNING_IN_CLI = undefined;
}
catch (e) {
prompts_1.log.error(e.message);
if (e.stack) {
prompts_1.log.error(e.stack);
}
}
}
//# sourceMappingURL=migrate.js.map