-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: load presets from unbundled
nitropack/presets
(#2459)
- Loading branch information
Showing
9 changed files
with
52 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { NitroPreset, NitroPresetMeta } from "nitropack"; | ||
import { kebabCase } from "scule"; | ||
import { provider } from 'std-env' | ||
import allPresets from "./_all.gen"; | ||
|
||
export async function resolvePreset(name: string, opts: { static?: boolean, compatibilityDate?: string }): Promise<(NitroPreset & { _meta?: NitroPresetMeta }) | undefined> { | ||
const _name = kebabCase(name) || provider; | ||
const _date = new Date(opts.compatibilityDate || 0); | ||
|
||
const matches = allPresets.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) | ||
} | ||
|
||
return preset; | ||
} | ||
|
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,3 +1,2 @@ | ||
export { default as presets } from "./_all.gen" | ||
|
||
export type { PresetOptions, PresetName, PresetNameInput } from "./_types.gen"; | ||
export { resolvePreset } from './_resolve' |
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 { resolvePreset } from './_resolve' |
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