-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
95 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@kosko/cli": minor | ||
--- | ||
|
||
Implement the plugin system. |
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,75 @@ | ||
import type { PluginConfig } from "@kosko/config"; | ||
import type { Plugin, PluginContext } from "@kosko/plugin"; | ||
import { importPath } from "@kosko/require"; | ||
import resolveFrom from "resolve-from"; | ||
import { excludeFalsyInArray } from "../../utils"; | ||
|
||
async function loadPlugin({ | ||
name, | ||
path, | ||
...ctx | ||
}: PluginContext & { name: string; path: string }): Promise<Plugin> { | ||
const mod = await importPath(path); | ||
const plugin = await mod(ctx); | ||
|
||
if (typeof plugin !== "object" || plugin == null) { | ||
throw new Error(`Plugin "${name}" must export an object`); | ||
} | ||
|
||
if ( | ||
plugin.transformManifest != null && | ||
typeof plugin.transformManifest !== "function" | ||
) { | ||
throw new Error( | ||
`Expected "transformManifest" to be a function in plugin "${name}"` | ||
); | ||
} | ||
|
||
return plugin; | ||
} | ||
|
||
function composeTransforms( | ||
transformers: readonly Required<Plugin>["transformManifest"][] | ||
): Plugin["transformManifest"] { | ||
return async (manifest) => { | ||
let data = manifest.data; | ||
|
||
for (const transform of transformers) { | ||
data = await transform({ ...manifest, data }); | ||
|
||
// Stop if the return value is undefined or null | ||
if (data == null) return data; | ||
} | ||
|
||
return data; | ||
}; | ||
} | ||
|
||
export async function loadPlugins( | ||
cwd: string, | ||
configs: readonly PluginConfig[] | ||
): Promise<Plugin> { | ||
if (!configs.length) return {}; | ||
|
||
// eslint-disable-next-line no-restricted-globals | ||
if (process.env.BUILD_TARGET !== "node") { | ||
throw new Error("Plugins are only supported on Node.js"); | ||
} | ||
|
||
const plugins: Plugin[] = []; | ||
|
||
for (const conf of configs) { | ||
const path = resolveFrom(cwd, conf.name); | ||
const plugin = await loadPlugin({ ...conf, path, cwd }); | ||
|
||
plugins.push(plugin); | ||
} | ||
|
||
const transformers = excludeFalsyInArray( | ||
plugins.map((p) => p.transformManifest) | ||
); | ||
|
||
return { | ||
transformManifest: composeTransforms(transformers) | ||
}; | ||
} |
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,3 @@ | ||
export function excludeFalsyInArray<T>(input: (T | undefined | null)[]): T[] { | ||
return input.filter(Boolean) as T[]; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.