UNPKG

zenstack

Version:

FullStack enhancement for Prisma ORM: seamless integration from database to UI

88 lines 4.22 kB
"use strict"; 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.generate = generate; const sdk_1 = require("@zenstackhq/sdk"); const ast_1 = require("@zenstackhq/sdk/ast"); const colors_1 = __importDefault(require("colors")); const path_1 = __importDefault(require("path")); const cli_error_1 = require("../cli-error"); const cli_util_1 = require("../cli-util"); const plugin_runner_1 = require("../plugin-runner"); /** * CLI action for generating code from schema */ function generate(projectPath, options) { return __awaiter(this, void 0, void 0, function* () { if (options.dependencyCheck) { (0, cli_util_1.checkRequiredPackage)('prisma', cli_util_1.requiredPrismaVersion); (0, cli_util_1.checkRequiredPackage)('@prisma/client', cli_util_1.requiredPrismaVersion); } // check for multiple versions of Zenstack packages const packages = (0, cli_util_1.getZenStackPackages)(projectPath); if (packages.length > 0) { const versions = new Set(packages.map((p) => p.version).filter((v) => !!v)); if (versions.size > 1) { console.warn(colors_1.default.yellow('WARNING: Multiple versions of Zenstack packages detected. Run "zenstack info" to see details.')); } } yield runPlugins(options); // note that we can't run online jobs concurrently with plugins because // plugins are CPU-bound and can cause false timeout const postJobs = []; if (options.versionCheck && !options.offline) { postJobs.push((0, cli_util_1.checkNewVersion)()); } if (!options.offline) { postJobs.push((0, cli_util_1.showNotification)()); } yield Promise.all(postJobs); }); } function runPlugins(options) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const schema = (_a = options.schema) !== null && _a !== void 0 ? _a : (0, cli_util_1.getDefaultSchemaLocation)(); const model = yield (0, cli_util_1.loadDocument)(schema); for (const name of [...((_b = options.withPlugins) !== null && _b !== void 0 ? _b : []), ...((_c = options.withoutPlugins) !== null && _c !== void 0 ? _c : [])]) { const pluginDecl = model.declarations.find((d) => (0, ast_1.isPlugin)(d) && d.name === name); if (!pluginDecl) { console.error(colors_1.default.red(`Plugin "${name}" not found in schema.`)); throw new cli_error_1.CliError(`Plugin "${name}" not found in schema.`); } } const runnerOpts = { schema: model, schemaPath: path_1.default.resolve(schema), withPlugins: options.withPlugins, withoutPlugins: options.withoutPlugins, defaultPlugins: options.defaultPlugins, output: options.output, compile: options.compile, }; try { yield new plugin_runner_1.PluginRunner().run(runnerOpts); } catch (err) { if (err instanceof sdk_1.PluginError) { console.error(colors_1.default.red(`${err.plugin}: ${err.message}`)); throw new cli_error_1.CliError(err.message); } else { throw err; } } }); } //# sourceMappingURL=generate.js.map