convex
Version:
Client for the Convex Cloud
298 lines (297 loc) • 9.43 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var codegen_exports = {};
__export(codegen_exports, {
doCodegen: () => doCodegen,
doInitCodegen: () => doInitCodegen
});
module.exports = __toCommonJS(codegen_exports);
var import_path = __toESM(require("path"));
var import_prettier = __toESM(require("prettier"));
var import_esbuild = __toESM(require("esbuild"));
var import_utils = require("./utils.js");
var import_react = require("../codegen_templates/react.js");
var import_dataModel = require("../codegen_templates/dataModel.js");
var import_server = require("../codegen_templates/server.js");
var import_typecheck = require("./typecheck.js");
var import_tsconfig = require("../codegen_templates/tsconfig.js");
var import_readme = require("../codegen_templates/readme.js");
var import_clientConfig = require("../codegen_templates/clientConfig.js");
var import_bundler = require("../../bundler/index.js");
var import_fs = require("../../bundler/fs.js");
var import_api2 = require("../codegen_templates/api.js");
var import_actionsTsconfig = require("../codegen_templates/actionsTsconfig.js");
function format(source, filetype) {
return import_prettier.default.format(source, { parser: filetype });
}
function compileToCommonJS(source) {
const { code } = import_esbuild.default.transformSync(source, {
format: "cjs",
target: "node14",
minify: false
});
return code;
}
function writeFile(ctx, filename, source, dir, dryRun, debug, quiet, filetype = "typescript") {
const formattedSource = format(source, filetype);
const dest = import_path.default.join(dir.tmpPath, filename);
if (debug) {
console.log(`# ${filename}`);
console.log(formattedSource);
return;
}
if (dryRun) {
if (ctx.fs.exists(dest)) {
const fileText = ctx.fs.readUtf8File(dest);
if (fileText !== formattedSource) {
console.log(`Command would replace file: ${dest}`);
}
} else {
console.log(`Command would create file: ${dest}`);
}
return;
}
if (!quiet) {
console.log(`writing ${filename}`);
}
import_fs.nodeFs.writeUtf8File(dest, formattedSource);
}
function writeJsWithTypes(ctx, name, content, codegenDir, dryRun, debug, quiet, commonjs) {
writeFile(ctx, `${name}.d.ts`, content.DTS, codegenDir, dryRun, debug, quiet);
if (content.JS) {
const js = commonjs ? compileToCommonJS(content.JS) : content.JS;
writeFile(ctx, `${name}.js`, js, codegenDir, dryRun, debug, quiet);
}
}
function doServerCodegen(ctx, codegenDir, dryRun, hasSchemaFile, debug, quiet = false, commonjs = false) {
if (hasSchemaFile) {
writeJsWithTypes(
ctx,
"dataModel",
import_dataModel.dataModel,
codegenDir,
dryRun,
debug,
quiet,
commonjs
);
} else {
writeJsWithTypes(
ctx,
"dataModel",
import_dataModel.dataModelWithoutSchema,
codegenDir,
dryRun,
debug,
quiet,
commonjs
);
}
writeJsWithTypes(
ctx,
"server",
(0, import_server.serverCodegen)(),
codegenDir,
dryRun,
debug,
quiet,
commonjs
);
}
async function doApiCodegen(ctx, functionsDir2, codegenDir, dryRun, debug, quiet = false, commonjs = false) {
const modulePaths = (await (0, import_bundler.allEntryPoints)(ctx.fs, functionsDir2, false)).map(
(entryPoint) => import_path.default.relative(functionsDir2, entryPoint)
);
writeJsWithTypes(
ctx,
"api",
(0, import_api2.apiCodegen)(modulePaths),
codegenDir,
dryRun,
debug,
quiet,
commonjs
);
}
async function doReactCodegen(ctx, codegenDir, dryRun, debug, quiet = false, commonjs = false) {
writeJsWithTypes(
ctx,
"react",
(0, import_react.reactCodegen)(),
codegenDir,
dryRun,
debug,
quiet,
commonjs
);
}
async function doCodegen({
ctx,
projectConfig,
configPath,
typeCheckMode,
deploymentType,
dryRun = false,
debug = false,
quiet = false,
commonjs = false
}) {
const funcDir = (0, import_utils.functionsDir)(configPath, projectConfig);
const legacyCodegenPath = import_path.default.join(funcDir, "_generated.ts");
if (ctx.fs.exists(legacyCodegenPath)) {
if (!dryRun) {
console.log(`Deleting legacy codegen file: ${legacyCodegenPath}}`);
ctx.fs.unlink(legacyCodegenPath);
} else {
console.log(
`Command would delete legacy codegen file: ${legacyCodegenPath}}`
);
}
}
ctx.fs.mkdir(funcDir, { allowExisting: true });
const schemaPath = import_path.default.join(funcDir, "schema.ts");
const hasSchemaFile = ctx.fs.exists(schemaPath);
await (0, import_fs.mkdtemp)("_generated", async (tempCodegenDir) => {
writeJsWithTypes(
ctx,
"clientConfig",
deploymentType === "dev" ? import_clientConfig.devDeploymentConfig : (0, import_clientConfig.prodDeploymentConfig)(projectConfig),
tempCodegenDir,
dryRun,
debug,
quiet,
commonjs
);
doServerCodegen(
ctx,
tempCodegenDir,
dryRun,
hasSchemaFile,
debug,
quiet,
commonjs
);
await doApiCodegen(ctx, funcDir, tempCodegenDir, dryRun, debug, quiet);
await doReactCodegen(ctx, tempCodegenDir, dryRun, debug, quiet, commonjs);
if (!debug && !dryRun) {
const codegenDir = import_path.default.join(funcDir, "_generated");
syncFromTemp(ctx, tempCodegenDir, codegenDir, true);
}
await (0, import_typecheck.processTypeCheckResult)(
ctx,
typeCheckMode,
() => (0, import_typecheck.typeCheckFunctions)(ctx, funcDir)
);
});
}
function syncFromTemp(ctx, tempDir, destDir, eliminateExtras) {
ctx.fs.mkdir(destDir, { allowExisting: true });
const added = /* @__PURE__ */ new Set();
for (const { isDir, path: fpath } of Array.from(
(0, import_bundler.walkDir)(ctx.fs, tempDir.tmpPath)
)) {
const relPath = import_path.default.relative(tempDir.tmpPath, fpath);
const destPath = import_path.default.join(destDir, relPath);
if (ctx.fs.exists(destPath)) {
if (ctx.fs.stat(destPath).isDirectory()) {
if (!isDir) {
ctx.fs.rm(destPath, { recursive: true });
}
} else {
ctx.fs.unlink(destPath);
}
}
if (isDir) {
ctx.fs.mkdir(destPath, { allowExisting: true });
} else {
ctx.fs.renameFile(fpath, destPath);
}
added.add(destPath);
}
if (eliminateExtras) {
const destEntries = Array.from((0, import_bundler.walkDir)(ctx.fs, destDir)).reverse();
for (const { isDir, path: fpath } of destEntries) {
if (!added.has(fpath)) {
if (isDir) {
ctx.fs.rmdir(fpath);
} else {
ctx.fs.unlink(fpath);
}
}
}
}
}
async function doInitCodegen(ctx, functionsDir2, quiet = false, dryRun = false, debug = false) {
const actionsPath = import_path.default.join(functionsDir2, import_bundler.actionsDir);
const hasActionsDir = ctx.fs.exists(actionsPath);
await (0, import_fs.mkdtemp)("convex", async (tempFunctionsDir) => {
doReadmeCodegen(ctx, tempFunctionsDir, dryRun, debug, quiet);
doTsconfigCodegen(ctx, tempFunctionsDir, dryRun, debug, quiet);
if (hasActionsDir) {
ctx.fs.mkdir(import_path.default.join(tempFunctionsDir.tmpPath, import_bundler.actionsDir), {
allowExisting: true
});
doActionsTsconfigCodegen(ctx, tempFunctionsDir, dryRun, debug, quiet);
}
syncFromTemp(ctx, tempFunctionsDir, functionsDir2, false);
});
}
function doReadmeCodegen(ctx, tempFunctionsDir, dryRun = false, debug = false, quiet = false) {
writeFile(
ctx,
"README.md",
(0, import_readme.readmeCodegen)(),
tempFunctionsDir,
dryRun,
debug,
quiet,
"markdown"
);
}
function doTsconfigCodegen(ctx, tempFunctionsDir, dryRun = false, debug = false, quiet = false) {
writeFile(
ctx,
"tsconfig.json",
(0, import_tsconfig.tsconfigCodegen)(),
tempFunctionsDir,
dryRun,
debug,
quiet,
"json"
);
}
function doActionsTsconfigCodegen(ctx, tempFunctionsDir, dryRun = false, debug = false, quiet = false) {
writeFile(
ctx,
import_path.default.join(import_bundler.actionsDir, "tsconfig.json"),
(0, import_actionsTsconfig.actionsTsconfigCodegen)(),
tempFunctionsDir,
dryRun,
debug,
quiet,
"json"
);
}
//# sourceMappingURL=codegen.js.map