generated from JoshuaKGoldberg/create-typescript-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow blocks in config files (#96)
* feat: allow blocks in config files * Oh also the files * Tests * it
- Loading branch information
1 parent
151c831
commit 05075db
Showing
13 changed files
with
175 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Block } from "../types/blocks.js"; | ||
import { CreatedBlockAddons } from "../types/creations.js"; | ||
import { Preset } from "../types/presets.js"; | ||
|
||
export interface BlockModifications<Options extends object = object> { | ||
add?: Block<object | undefined, Options>[]; | ||
remove?: Block<object | undefined, Options>[]; | ||
} | ||
|
||
export interface CreateConfig { | ||
preset: Preset; | ||
settings?: CreateConfigSettings; | ||
} | ||
|
||
export interface CreateConfigSettings<Options extends object = object> { | ||
addons?: CreatedBlockAddons<object, Options>[]; | ||
blocks?: BlockModifications<Options>; | ||
} |
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
55 changes: 55 additions & 0 deletions
55
packages/create/src/producers/applyBlockModifications.test.ts
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,55 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { z } from "zod"; | ||
|
||
import { createBase } from "../creators/createBase.js"; | ||
import { applyBlockModifications } from "./applyBlockModifications.js"; | ||
|
||
const base = createBase({ | ||
options: { | ||
value: z.string(), | ||
}, | ||
}); | ||
|
||
const blockA = base.createBlock({ | ||
about: { name: "A" }, | ||
produce: vi.fn(), | ||
}); | ||
|
||
const blockB = base.createBlock({ | ||
about: { name: "B" }, | ||
produce: vi.fn(), | ||
}); | ||
|
||
const blockC = base.createBlock({ | ||
about: { name: "C" }, | ||
produce: vi.fn(), | ||
}); | ||
|
||
describe("runPreset", () => { | ||
it("returns the initial blocks when no modifications are provided", () => { | ||
const initial = [blockA, blockB]; | ||
|
||
const actual = applyBlockModifications(initial); | ||
|
||
expect(actual).toBe(initial); | ||
}); | ||
|
||
it("returns the initial blocks when modifications are empty", () => { | ||
const initial = [blockA, blockB]; | ||
|
||
const actual = applyBlockModifications(initial, { add: [], remove: [] }); | ||
|
||
expect(actual).toBe(initial); | ||
}); | ||
|
||
it("applies modifications when they exist", () => { | ||
const initial = [blockA, blockB]; | ||
|
||
const actual = applyBlockModifications(initial, { | ||
add: [blockC], | ||
remove: [blockB], | ||
}); | ||
|
||
expect(actual).toEqual([blockA, blockC]); | ||
}); | ||
}); |
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,23 @@ | ||
import { BlockModifications } from "../config/types.js"; | ||
import { Block } from "../types/blocks.js"; | ||
|
||
export function applyBlockModifications<Options extends object>( | ||
initial: Block<object | undefined, Options>[], | ||
{ add = [], remove = [] }: BlockModifications<Options> = {}, | ||
) { | ||
if (!add.length && !remove.length) { | ||
return initial; | ||
} | ||
|
||
const blocks = new Set(initial); | ||
|
||
for (const added of add) { | ||
blocks.add(added); | ||
} | ||
|
||
for (const removed of remove) { | ||
blocks.delete(removed); | ||
} | ||
|
||
return Array.from(blocks); | ||
} |
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
Oops, something went wrong.