zenstack
Version:
FullStack enhancement for Prisma ORM: seamless integration from database to UI
136 lines • 5.89 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CorePlugins = exports.ALL_OPERATION_KINDS = void 0;
exports.getNodeModulesFolder = getNodeModulesFolder;
exports.ensureDefaultOutputFolder = ensureDefaultOutputFolder;
exports.getDefaultOutputFolder = getDefaultOutputFolder;
exports.getPluginCustomOutputFolder = getPluginCustomOutputFolder;
const runtime_1 = require("@zenstackhq/runtime");
const sdk_1 = require("@zenstackhq/sdk");
const ast_1 = require("@zenstackhq/sdk/ast");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const version_utils_1 = require("../utils/version-utils");
exports.ALL_OPERATION_KINDS = ['create', 'update', 'postUpdate', 'read', 'delete'];
/**
* Gets the nearest "node_modules" folder by walking up from start path.
*/
function getNodeModulesFolder(startPath) {
startPath = startPath !== null && startPath !== void 0 ? startPath : process.cwd();
if (startPath.endsWith('node_modules')) {
return startPath;
}
else if (fs_1.default.existsSync(path_1.default.join(startPath, 'node_modules'))) {
return path_1.default.join(startPath, 'node_modules');
}
else if (startPath !== '/') {
const parent = path_1.default.join(startPath, '..');
return getNodeModulesFolder(parent);
}
else {
return undefined;
}
}
/**
* Ensure the default output folder is initialized.
*/
function ensureDefaultOutputFolder(options) {
var _a;
const output = options.output ? path_1.default.resolve(options.output) : getDefaultOutputFolder();
if (output) {
(0, sdk_1.ensureEmptyDir)(output);
if (!options.output) {
const pkgJson = {
name: 'zenstack-generated',
version: (_a = (0, version_utils_1.getVersion)()) !== null && _a !== void 0 ? _a : '1.0.0',
exports: {
'./enhance': {
types: './enhance.d.ts',
default: './enhance.js',
},
'./enhance-edge': {
types: './enhance-edge.d.ts',
default: './enhance-edge.js',
},
'./zod': {
types: './zod/index.d.ts',
default: './zod/index.js',
},
'./zod/models': {
types: './zod/models/index.d.ts',
default: './zod/models/index.js',
},
'./zod/input': {
types: './zod/input/index.d.ts',
default: './zod/input/index.js',
},
'./zod/objects': {
types: './zod/objects/index.d.ts',
default: './zod/objects/index.js',
},
'./model-meta': {
types: './model-meta.d.ts',
default: './model-meta.js',
},
'./models': {
types: './models.d.ts',
},
},
};
// create stubs for zod exports to make bundlers that statically
// analyze imports (like Next.js) happy
for (const zodFolder of ['models', 'input', 'objects']) {
fs_1.default.mkdirSync(path_1.default.join(output, 'zod', zodFolder), { recursive: true });
fs_1.default.writeFileSync(path_1.default.join(output, 'zod', zodFolder, 'index.js'), '');
}
fs_1.default.writeFileSync(path_1.default.join(output, 'package.json'), JSON.stringify(pkgJson, undefined, 4));
}
}
return output;
}
/**
* Gets the default node_modules/.zenstack output folder for plugins.
* @returns
*/
function getDefaultOutputFolder(globalOptions) {
if (typeof (globalOptions === null || globalOptions === void 0 ? void 0 : globalOptions.output) === 'string') {
return path_1.default.resolve(globalOptions.output);
}
// for testing, use the local node_modules
if (process.env.ZENSTACK_TEST === '1') {
return path_1.default.join(process.cwd(), 'node_modules', runtime_1.DEFAULT_RUNTIME_LOAD_PATH);
}
// find the real runtime module path, it might be a symlink in pnpm
let runtimeModulePath = require.resolve('@zenstackhq/runtime');
// start with the parent folder of @zenstackhq, supposed to be a node_modules folder
while (!runtimeModulePath.endsWith('@zenstackhq') && runtimeModulePath !== '/') {
runtimeModulePath = path_1.default.join(runtimeModulePath, '..');
}
runtimeModulePath = path_1.default.join(runtimeModulePath, '..');
const modulesFolder = getNodeModulesFolder(runtimeModulePath);
return modulesFolder ? path_1.default.join(modulesFolder, runtime_1.DEFAULT_RUNTIME_LOAD_PATH) : undefined;
}
/**
* Core plugin providers
*/
var CorePlugins;
(function (CorePlugins) {
CorePlugins["Prisma"] = "@core/prisma";
CorePlugins["Zod"] = "@core/zod";
CorePlugins["Enhancer"] = "@core/enhancer";
})(CorePlugins || (exports.CorePlugins = CorePlugins = {}));
/**
* Gets the custom output folder for a plugin.
*/
function getPluginCustomOutputFolder(zmodel, provider) {
const plugin = zmodel.declarations.find((d) => (0, ast_1.isPlugin)(d) && d.fields.some((f) => f.name === 'provider' && (0, sdk_1.getLiteral)(f.value) === provider));
if (!plugin) {
return undefined;
}
const output = plugin.fields.find((f) => f.name === 'output');
return output && (0, sdk_1.getLiteral)(output.value);
}
//# sourceMappingURL=plugin-utils.js.map
;