-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: overhaul presets structure (#2446)
- Loading branch information
Showing
131 changed files
with
2,369 additions
and
1,661 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./dist/presets/index"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import createJITI from "jiti"; | ||
import { consola } from "consola"; | ||
import { kebabCase, camelCase, pascalCase, snakeCase } from "scule"; | ||
import { readdirSync, existsSync, writeFileSync, readFileSync } from "node:fs"; | ||
import { resolve } from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
import type { NitroPreset, NitroPresetMeta } from "nitropack"; | ||
import { findTypeExports } from "mlly"; | ||
|
||
const autoGenHeader = /* ts */ `// Auto-generated using gen-presets script\n`; | ||
|
||
// --- Scan presets/ directory --- | ||
const presetsDir = fileURLToPath(new URL("../src/presets", import.meta.url)); | ||
const presetDirs: string[] = readdirSync(presetsDir, { withFileTypes: true }) | ||
.filter( | ||
(dir) => | ||
dir.isDirectory() && | ||
existsSync(resolve(presetsDir, dir.name, "preset.ts")) | ||
) | ||
.map((dir) => dir.name); | ||
|
||
// --- Load presets --- | ||
const jitiRequire = createJITI(presetsDir, { | ||
esmResolve: true, | ||
interopDefault: true, | ||
alias: { | ||
"nitropack": fileURLToPath(new URL("../src/index.ts", import.meta.url)) | ||
} | ||
}); | ||
const allPresets: (NitroPreset & { _meta?: NitroPresetMeta })[] = []; | ||
for (const preset of presetDirs) { | ||
const presetPath = resolve(presetsDir, preset, "preset.ts"); | ||
const _presets = jitiRequire(presetPath); | ||
allPresets.push(..._presets); | ||
} | ||
|
||
// --- Validate names --- | ||
const _names = new Set<string>(); | ||
for (const preset of allPresets) { | ||
if (!preset._meta?.name) { | ||
consola.warn(`Preset ${preset} does not have a name`); | ||
continue; | ||
} | ||
const names = [preset._meta.name, ...(preset._meta.aliases || [])]; | ||
for (const name of names) { | ||
if (_names.has(name)) { | ||
if (!preset._meta.compatibility?.date) { | ||
consola.warn(`Preset ${name} is duplicated`); | ||
} | ||
continue; | ||
} | ||
if (kebabCase(name) !== name) { | ||
consola.warn(`Preset ${name} is not kebab-case`); | ||
} | ||
_names.add(name); | ||
} | ||
} | ||
const names = [..._names].sort(); | ||
consola.log(names.join(", ")); | ||
|
||
// --- Generate presets/_all.gen.ts --- | ||
writeFileSync( | ||
resolve(presetsDir, "_all.gen.ts"), | ||
/* ts */ `${autoGenHeader} | ||
${presetDirs | ||
.map((preset) => `import _${camelCase(preset)} from "./${preset}/preset";`) | ||
.join("\n")} | ||
export default [ | ||
${presetDirs.map((preset) => ` ..._${camelCase(preset)},`).join("\n")} | ||
] as const; | ||
` | ||
); | ||
// --- Generate presets/_types.gen.ts --- | ||
const presetsWithType = presetDirs.filter((presetDir) => { | ||
const presetPath = resolve(presetsDir, presetDir, "preset.ts"); | ||
const content = readFileSync(presetPath, "utf8"); | ||
const typeExports = findTypeExports(content); | ||
return typeExports.some(type => type.name === "PresetOptions") | ||
}); | ||
writeFileSync( | ||
resolve(presetsDir, "_types.gen.ts"), | ||
/* ts */ `${autoGenHeader} | ||
${presetsWithType.map((preset) => `import { PresetOptions as ${pascalCase(preset)}Options } from "./${preset}/preset";`).join("\n")} | ||
export interface PresetOptions { | ||
${presetsWithType.map((preset) => ` ${camelCase(preset)}: ${pascalCase(preset)}Options;`).join("\n")} | ||
} | ||
export type PresetName = ${names.map((name) => `"${name}"`).join(" | ")}; | ||
export type PresetNameInput = ${names.flatMap((name) => [...new Set([kebabCase(name), camelCase(name), snakeCase(name)])].map(n => `"${n}"`)).join(" | ")} | (string & {}); | ||
` | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,58 @@ | ||
import type { NitroPreset } from "./types"; | ||
import type { NitroPreset, NitroPresetMeta } from "./types"; | ||
import { fileURLToPath } from "node:url"; | ||
import { kebabCase } from "scule"; | ||
import { provider } from 'std-env' | ||
|
||
export function defineNitroPreset< | ||
P extends NitroPreset, | ||
M extends NitroPresetMeta, | ||
>(preset: P, meta?: M): P & { _meta: NitroPresetMeta } { | ||
if ( | ||
meta?.url && | ||
typeof preset !== "function" && | ||
preset.entry && | ||
preset.entry.startsWith(".") | ||
) { | ||
preset.entry = fileURLToPath(new URL(preset.entry, meta.url)); | ||
} | ||
return { ...preset, _meta: meta } as P & { _meta: M }; | ||
} | ||
|
||
let _presets: typeof import("./presets")["presets"] | ||
|
||
export async function resolvePreset(name: string, opts: { static?: boolean, compatibilityDate?: string }): Promise<(NitroPreset & { _meta?: NitroPresetMeta }) | undefined> { | ||
if (!_presets) { | ||
_presets = (await import("./presets") as typeof import("./presets")).presets; | ||
} | ||
|
||
const _name = kebabCase(name) || provider; | ||
const _date = new Date(opts.compatibilityDate || 0); | ||
|
||
const matches = _presets.filter((preset) => { | ||
const names = [preset._meta.name, preset._meta.stdName, ...(preset._meta.aliases || [])].filter(Boolean); | ||
if (!names.includes(_name)) { | ||
return false; | ||
} | ||
if (preset._meta.compatibility?.date && new Date(preset._meta.compatibility?.date || 0) > _date) { | ||
return false | ||
} | ||
return true | ||
}).sort((a, b) => { | ||
const aDate = new Date(a._meta.compatibility?.date || 0); | ||
const bDate = new Date(b._meta.compatibility?.date || 0); | ||
return bDate > aDate ? 1 : -1 | ||
}); | ||
|
||
const preset = matches.find(p => (p._meta.static || false) === (opts?.static || false)) || matches[0]; | ||
|
||
if (typeof preset === 'function') { | ||
return preset(); | ||
} | ||
|
||
if (!name && !preset) { | ||
return opts?.static ? resolvePreset('static', opts) : resolvePreset('node-server', opts) | ||
} | ||
|
||
export function defineNitroPreset(preset: NitroPreset) { | ||
return preset; | ||
} | ||
|
Oops, something went wrong.