|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { findUp } from 'find-up-simple' |
| 3 | +import type { |
| 4 | + PickOverridableOptions, |
| 5 | + Project, |
| 6 | + GitRepositoryHosting, |
| 7 | + ReleaserOptions, |
| 8 | + ReleaserCheckoutOptions, |
| 9 | + ReleaserCommitOptions, |
| 10 | + ReleaserTagOptions, |
| 11 | + ReleaserPushOptions |
| 12 | +} from '@simple-release/core' |
| 13 | + |
| 14 | +const VARIANTS = [ |
| 15 | + '.simple-release.js', |
| 16 | + '.simple-release.mjs', |
| 17 | + '.simple-release.cjs' |
| 18 | +] |
| 19 | + |
| 20 | +export interface SimpleReleaseConfig< |
| 21 | + P extends Project = Project, |
| 22 | + G extends GitRepositoryHosting = GitRepositoryHosting |
| 23 | +> { |
| 24 | + project?: P |
| 25 | + hosting?: G |
| 26 | + releaser?: Omit<ReleaserOptions, 'project' | 'hosting'> |
| 27 | + checkout?: { |
| 28 | + branch?: string |
| 29 | + } & ReleaserCheckoutOptions |
| 30 | + bump?: PickOverridableOptions<P['bump']> |
| 31 | + commit?: ReleaserCommitOptions |
| 32 | + tag?: ReleaserTagOptions |
| 33 | + push?: ReleaserPushOptions |
| 34 | + publish?: PickOverridableOptions<P['publish']> |
| 35 | + release?: PickOverridableOptions<G['createRelease']> |
| 36 | + pullRequest?: PickOverridableOptions<G['createPullRequest']> |
| 37 | +} |
| 38 | + |
| 39 | +export type SimpleReleaseConfigRequirements = { |
| 40 | + config?: boolean |
| 41 | +} & { |
| 42 | + [K in keyof SimpleReleaseConfig]?: boolean |
| 43 | +} |
| 44 | + |
| 45 | +type ApplyRequirements<T extends Record<string, any>, R extends Record<string, any>> = { |
| 46 | + [K in keyof T as K extends keyof R ? R[K] extends true ? K : never : never]-?: Exclude<T[K], undefined> |
| 47 | +} & { |
| 48 | + [K in keyof T as K extends keyof R ? R[K] extends true ? never : K : K]: T[K] |
| 49 | +} |
| 50 | + |
| 51 | +type ApplyConfigRequirement<R extends { config?: boolean }, T> = R extends { config: true } |
| 52 | + ? T |
| 53 | + : T | null |
| 54 | + |
| 55 | +type Result< |
| 56 | + P extends Project = Project, |
| 57 | + G extends GitRepositoryHosting = GitRepositoryHosting, |
| 58 | + R extends SimpleReleaseConfigRequirements = SimpleReleaseConfigRequirements |
| 59 | +> = ApplyRequirements<SimpleReleaseConfig<P, G>, R> extends infer C |
| 60 | + ? ApplyConfigRequirement<R, C> |
| 61 | + : never |
| 62 | + |
| 63 | +function validate(target: Record<string, any>, rules: Record<string, any>) { |
| 64 | + for (const [rule, required] of Object.entries(rules)) { |
| 65 | + if (required && target[rule] === undefined) { |
| 66 | + throw new Error(`Faild to load config: '${rule}' is required`) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Load simple-release config. |
| 73 | + * @param requirements |
| 74 | + * @returns simple-release config |
| 75 | + */ |
| 76 | +export async function load< |
| 77 | + P extends Project = Project, |
| 78 | + G extends GitRepositoryHosting = GitRepositoryHosting, |
| 79 | + R extends SimpleReleaseConfigRequirements = SimpleReleaseConfigRequirements |
| 80 | +>(requirements?: R): Promise<Result<P, G, R>> |
| 81 | + |
| 82 | +export async function load(requirements: Record<string, any> = {}) { |
| 83 | + console.log(requirements) |
| 84 | + |
| 85 | + const { |
| 86 | + config: configRequired, |
| 87 | + ...reqs |
| 88 | + } = requirements |
| 89 | + |
| 90 | + for (const variant of VARIANTS) { |
| 91 | + const foundPath = await findUp(variant) |
| 92 | + |
| 93 | + if (foundPath) { |
| 94 | + console.log(foundPath) |
| 95 | + |
| 96 | + try { |
| 97 | + const module = await import(foundPath) as SimpleReleaseConfig | { default: SimpleReleaseConfig } |
| 98 | + const config = 'default' in module ? module.default : module |
| 99 | + |
| 100 | + console.log(config) |
| 101 | + |
| 102 | + validate(config, reqs) |
| 103 | + |
| 104 | + return config |
| 105 | + } catch (err) { |
| 106 | + if (configRequired) { |
| 107 | + throw err |
| 108 | + } |
| 109 | + |
| 110 | + return null |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (configRequired) { |
| 116 | + throw new Error('Config not found') |
| 117 | + } |
| 118 | + |
| 119 | + return null |
| 120 | +} |
0 commit comments