polen
Version:
A framework for delightful GraphQL developer portals
159 lines • 5.51 kB
JavaScript
import { assertPathAbsolute } from '#lib/kit-temp';
import { packagePaths } from '#package-paths';
import { Err, Manifest, Path, Str } from '@wollybeard/kit';
import { z } from 'zod';
export const BuildArchitectureEnum = {
ssg: `ssg`,
spa: `spa`,
ssr: `ssr`,
};
export const BuildArchitecture = z.nativeEnum(BuildArchitectureEnum);
const buildPaths = (rootDir) => {
if (!Path.isAbsolute(rootDir))
throw new Error(`Root dir path must be absolute: ${rootDir}`);
const rootAbsolute = Path.ensureAbsoluteWith(rootDir);
const buildAbsolutePath = rootAbsolute(`build`);
const buildAbsolute = Path.ensureAbsoluteWith(buildAbsolutePath);
const publicAbsolutePath = rootAbsolute(`public`);
const publicAbsolute = Path.ensureAbsoluteWith(publicAbsolutePath);
return {
project: {
rootDir,
relative: {
build: {
root: `build`,
relative: {
serverEntrypoint: `app.js`,
assets: `assets`,
},
},
pages: `pages`,
public: {
root: `public`,
logo: `logo.svg`,
},
},
absolute: {
pages: rootAbsolute(`pages`),
build: {
root: buildAbsolute(`.`),
serverEntrypoint: buildAbsolute(`app.js`),
assets: buildAbsolute(`assets`),
},
public: {
root: publicAbsolute(`.`),
logo: publicAbsolute(`logo.svg`),
},
},
},
framework: packagePaths,
};
};
const configInputDefaults = {
_input: {},
templateVariables: {
title: `My Developer Portal`,
},
schemaAugmentations: [],
watch: {
also: [],
},
build: {
architecture: BuildArchitecture.enum.ssg,
base: `/`,
},
server: {
port: 3000,
},
schema: null,
ssr: {
enabled: true,
},
warnings: {
interactiveWithoutSchema: {
enabled: true,
},
},
paths: buildPaths(process.cwd()),
advanced: {
isSelfContainedMode: false,
debug: false,
explorer: false,
},
};
export const normalizeInput = async (configInput,
/**
* If the input has a relative root path, then resolve it relative to this path.
*
* We tell users relative paths are resolved to the config file directory.
* Config loaders should pass the directory of the config file here to ensure that happens.
*
* If this is omitted, then relative root paths will throw an error.
*/
baseRootDirPath) => {
assertPathAbsolute(baseRootDirPath);
const config = structuredClone(configInputDefaults);
if (configInput) {
config._input = configInput;
}
if (configInput?.build?.architecture) {
config.build.architecture = configInput.build.architecture;
}
if (configInput?.build?.base !== undefined) {
// Validate base path
const base = configInput.build.base;
if (!base.startsWith(`/`)) {
throw new Error(`Base path must start with "/". Provided: ${base}`);
}
if (!base.endsWith(`/`)) {
throw new Error(`Base path must end with "/". Provided: ${base}`);
}
config.build.base = base;
}
if (configInput?.advanced?.debug !== undefined) {
config.advanced.debug = configInput.advanced.debug;
}
// Always use the baseRootDirPath as the project root
// This is either the --project directory or the config file directory
config.paths = buildPaths(baseRootDirPath);
// Try to read package.json name as fallback for title
if (!configInput?.templateVariables?.title) {
const packageJson = await Manifest.resource.read(config.paths.project.rootDir);
// todo: let the user know there was an error...
if (!Err.is(packageJson) && packageJson.name) {
// Package name will be used as default, but can still be overridden below
config.templateVariables.title = Str.Case.title(packageJson.name);
}
}
if (configInput?.advanced?.vite) {
config.advanced.vite = configInput.advanced.vite;
}
if (configInput?.schemaAugmentations) {
config.schemaAugmentations = configInput.schemaAugmentations;
}
config.templateVariables = {
...config.templateVariables,
...configInput?.templateVariables,
};
if (configInput?.schema) {
config.schema = configInput.schema;
}
if (configInput?.advanced?.isSelfContainedMode !== undefined) {
config.advanced.isSelfContainedMode = configInput.advanced.isSelfContainedMode;
}
if (configInput?.advanced?.explorer !== undefined) {
config.advanced.explorer = configInput.advanced.explorer;
}
if (configInput?.advanced?.watch?.also) {
config.watch.also = configInput.advanced.watch.also;
}
if (configInput?.server?.port !== undefined) {
config.server.port = configInput.server.port;
}
// Process warnings configuration
if (configInput?.warnings?.interactiveWithoutSchema?.enabled !== undefined) {
config.warnings.interactiveWithoutSchema.enabled = configInput.warnings.interactiveWithoutSchema.enabled;
}
return config;
};
//# sourceMappingURL=configurator.js.map