sanity
Version:
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
207 lines (206 loc) • 8.25 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__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(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
mod
));
var path = require("node:path"), chalk = require("chalk"), helpers = require("yargs/helpers"), yargs = require("yargs/yargs"), _internal = require("./_internal.js"), runtime = require("./runtime.js"), checkRequiredDependencies = require("./checkRequiredDependencies.js"), servers = require("./servers.js"), timing = require("./timing.js");
function _interopDefaultCompat(e) {
return e && typeof e == "object" && "default" in e ? e : { default: e };
}
var path__default = /* @__PURE__ */ _interopDefaultCompat(path), chalk__default = /* @__PURE__ */ _interopDefaultCompat(chalk), yargs__default = /* @__PURE__ */ _interopDefaultCompat(yargs);
async function startDevServer(options) {
const {
cwd,
httpPort,
httpHost,
basePath,
reactStrictMode,
vite: extendViteConfig,
reactCompiler,
entry,
isApp,
skipStartLog
} = options, startTime = Date.now();
runtime.debug("Writing Sanity runtime files"), await runtime.writeSanityRuntime({
cwd,
reactStrictMode,
watch: !0,
basePath,
entry,
isApp
}), runtime.debug("Resolving vite config");
const mode = "development";
let viteConfig = await runtime.getViteConfig({
basePath,
mode: "development",
server: {
port: httpPort,
host: httpHost
},
cwd,
reactCompiler,
isApp
});
extendViteConfig && (viteConfig = await runtime.extendViteConfigWithUserConfig({
command: "serve",
mode
}, viteConfig, extendViteConfig)), runtime.debug("Creating vite server");
const {
createServer
} = await import("vite"), server = await createServer(viteConfig), info = server.config.logger.info;
if (runtime.debug("Listening on specified port"), await server.listen(), !skipStartLog) {
const startupDuration = Date.now() - startTime, url = `http://${httpHost || "localhost"}:${httpPort || "3333"}${basePath}`;
info(`${isApp ? "Sanity application" : "Sanity Studio"} using ${chalk__default.default.cyan(`vite@${require("vite/package.json").version}`)} ready in ${chalk__default.default.cyan(`${Math.ceil(startupDuration)}ms`)} and running at ${chalk__default.default.cyan(url)}`);
}
return {
close: () => server.close()
};
}
const debug = _internal.debug.extend("dev"), getDefaultCoreURL = ({
organizationId,
url
}) => {
const params = new URLSearchParams({
url
});
return process.env.SANITY_INTERNAL_ENV === "staging" ? `https://sanity.work/@${organizationId}?${params.toString()}` : `https://sanity.io/@${organizationId}?${params.toString()}`;
}, getCoreApiURL = () => process.env.SANITY_INTERNAL_ENV === "staging" ? "https://sanity.work" : "https://sanity.io", getCoreURL = async ({
fetchFn = globalThis.fetch,
timeout = 5e3,
organizationId,
url
}) => {
const abortController = new AbortController(), timer = setTimeout(() => abortController.abort(), timeout);
try {
const queryParams = new URLSearchParams({
organizationId,
url
}), res = await fetchFn(`${getCoreApiURL()}/api/dashboard/mode/development/resolve-url?${queryParams.toString()}`, {
signal: abortController.signal
});
return res.ok ? (await res.json()).url : (debug(`Failed to fetch core URL: ${res.statusText}`), getDefaultCoreURL({
organizationId,
url
}));
} catch (err) {
return debug(`Failed to fetch core URL: ${err.message}`), getDefaultCoreURL({
organizationId,
url
});
} finally {
clearTimeout(timer);
}
}, getCoreAppURL = async ({
organizationId,
httpHost = "localhost",
httpPort = 3333
}) => await getCoreURL({
organizationId,
url: `http://${httpHost}:${httpPort}`
});
function parseCliFlags(args) {
return yargs__default.default(helpers.hideBin(args.argv || process.argv).slice(1)).options("host", {
type: "string"
}).options("port", {
type: "number"
}).option("load-in-dashboard", {
type: "boolean",
default: !1
}).argv;
}
async function startSanityDevServer(args, context) {
const timers = timing.getTimer(), flags = await parseCliFlags(args), {
output,
apiClient,
workDir,
cliConfig
} = context, {
loadInDashboard
} = flags;
if (timers.start("checkStudioDependencyVersions"), checkRequiredDependencies.checkStudioDependencyVersions(workDir), timers.end("checkStudioDependencyVersions"), (await checkRequiredDependencies.checkRequiredDependencies(context)).didInstall)
return;
const config = getDevServerConfig({
flags,
workDir,
cliConfig,
output
}), projectId = cliConfig?.api?.projectId;
let organizationId;
if (loadInDashboard) {
projectId || (output.error("Project Id is required to load in dashboard"), process.exit(1));
const client = apiClient({
requireUser: !0,
requireProject: !0
});
try {
organizationId = (await client.request({
uri: `/projects/${projectId}`
})).organizationId;
} catch (err) {
debug(`Failed to get organization Id from project Id: ${err}`), output.error("Failed to get organization Id from project Id"), process.exit(1);
}
}
try {
const spinner = output.spinner("Starting dev server").start();
await startDevServer({
...config,
skipStartLog: loadInDashboard
}), spinner.succeed(), loadInDashboard && (organizationId || (output.error("Organization Id not found for project"), process.exit(1)), output.print(`Dev server started on ${config.httpPort} port`), output.print("View your app in the Sanity dashboard here:"), output.print(chalk__default.default.blue(chalk__default.default.underline(await getCoreAppURL({
organizationId,
httpHost: config.httpHost,
httpPort: config.httpPort
})))));
} catch (err) {
debug(`Failed to start dev server: ${err}`), servers.gracefulServerDeath("dev", config.httpHost, config.httpPort, err);
}
}
function getDevServerConfig({
flags,
workDir,
cliConfig,
output
}) {
const configSpinner = output.spinner("Checking configuration files..."), baseConfig = servers.getSharedServerConfig({
flags: {
host: flags.host,
port: flags.port
},
workDir,
cliConfig
});
configSpinner.succeed();
const env = process.env, reactStrictMode = env.SANITY_STUDIO_REACT_STRICT_MODE ? env.SANITY_STUDIO_REACT_STRICT_MODE === "true" : !!cliConfig?.reactStrictMode;
return env.SANITY_STUDIO_BASEPATH && cliConfig?.project?.basePath && output.warn(`Overriding configured base path (${cliConfig.project.basePath}) with value from environment variable (${env.SANITY_STUDIO_BASEPATH})`), {
...baseConfig,
staticPath: path__default.default.join(workDir, "static"),
reactStrictMode,
reactCompiler: cliConfig && "reactCompiler" in cliConfig ? cliConfig.reactCompiler : void 0
};
}
var devAction = /* @__PURE__ */ Object.freeze({
__proto__: null,
default: startSanityDevServer,
getCoreAppURL,
getCoreURL,
getDevServerConfig
});
exports.devAction = devAction;
exports.getCoreAppURL = getCoreAppURL;
exports.getDevServerConfig = getDevServerConfig;
exports.startDevServer = startDevServer;
//# sourceMappingURL=devAction2.js.map