diff --git a/plugins/platforms/github/mod.test.ts b/plugins/platforms/github/mod.test.ts new file mode 100644 index 0000000..31db2fe --- /dev/null +++ b/plugins/platforms/github/mod.test.ts @@ -0,0 +1,48 @@ +import { context } from "../../../src/plugin/mod.ts"; +import { assertEquals, deepMerge } from "../../../deps.ts"; +import { RenderedTemplate } from "../../../src/platform/plugin.ts"; + +import { github } from "./mod.ts"; + +const FAKE_VERSION = "v10.10.10"; + +// FIXME +// This doesn't work: +// deepMerge(context, { version: FAKE_VERSION }); +// Why? +const fakeContext = deepMerge(context, {}); +fakeContext.version = FAKE_VERSION; + +const fakeYaml = ` +name: Runner check +on: pull_request +jobs: + stage: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: ls +`; + +const fakeYamlWithHeader = `# Generated with pipelinit ${FAKE_VERSION} +# https://pipelinit.com/ +${fakeYaml}`; + +Deno.test("Plugins > GitHub - prepends a header to generated files", async () => { + async function* templatesFixture(): AsyncIterableIterator { + yield { + name: "pipelinit.stage", + content: fakeYaml, + }; + return; + } + const result = await github(fakeContext, templatesFixture()); + assertEquals(result.length, 1); + assertEquals( + result, + [{ + path: ".github/workflows/pipelinit.stage.yaml", + content: fakeYamlWithHeader, + }], + ); +}); diff --git a/plugins/platforms/github/mod.ts b/plugins/platforms/github/mod.ts index 37410f4..9c932df 100644 --- a/plugins/platforms/github/mod.ts +++ b/plugins/platforms/github/mod.ts @@ -1,12 +1,17 @@ -import { ensureFile, log, PlatformWriterFn } from "../deps.ts"; +import { PlatformWriterFn } from "../deps.ts"; -export const github: PlatformWriterFn = async (templates) => { - const logger = log.getLogger("main"); +export const github: PlatformWriterFn = async (context, templates) => { + const header = ` +# Generated with pipelinit ${context.version} +# https://pipelinit.com/ +`.trimStart(); + const configurationFiles = []; for await (const template of templates) { const { name, content } = template; - const filename = `.github/workflows/${name}.yaml`; - logger.info(`Writing ${filename}`); - await ensureFile(filename); - await Deno.writeTextFile(filename, content); + configurationFiles.push({ + path: `.github/workflows/${name}.yaml`, + content: `${header}${content}`, + }); } + return configurationFiles; }; diff --git a/src/cli/commands/default.ts b/src/cli/commands/default.ts index ff59155..a87ac9c 100644 --- a/src/cli/commands/default.ts +++ b/src/cli/commands/default.ts @@ -4,15 +4,24 @@ import { renderTemplates } from "../../template/mod.ts"; import { prelude } from "../prelude/mod.ts"; import { GlobalOptions } from "../types.ts"; import { outputErrors } from "../../plugin/errors.ts"; +import { context } from "../../plugin/mod.ts"; +import { ensureFile } from "../../../deps.ts"; type DefaultOptions = GlobalOptions; export default async function (opts: DefaultOptions): Promise { await prelude(opts); + const logger = context.getLogger("main"); const detected = await introspect(); const platform = "github"; - await platformWriters[platform]( + const files = await platformWriters[platform]( + context, renderTemplates(platform, detected), ); + for (const { path, content } of files) { + logger.info(`Writing ${path}`); + await ensureFile(path); + await Deno.writeTextFile(path, content); + } outputErrors(); } diff --git a/src/platform/plugin.ts b/src/platform/plugin.ts index 4dd781d..9e41876 100644 --- a/src/platform/plugin.ts +++ b/src/platform/plugin.ts @@ -1,8 +1,16 @@ +import { Context } from "../plugin/mod.ts"; + export interface RenderedTemplate { name: string; content: string; } +export interface CiConfigurationFile { + path: string; + content: string; +} + export type PlatformWriterFn = ( + context: Context, templates: AsyncIterableIterator, -) => Promise; +) => Promise>; diff --git a/src/plugin/mod.ts b/src/plugin/mod.ts index 02a05e8..9c00fc8 100644 --- a/src/plugin/mod.ts +++ b/src/plugin/mod.ts @@ -1,6 +1,7 @@ +import { log, semver } from "../../deps.ts"; import { each, includes, readJSON, readLines, readToml } from "./files.ts"; import { errors } from "./errors.ts"; -import { log, semver } from "../../deps.ts"; +import { VERSION } from "../version.ts"; export const context = { getLogger: log.getLogger, @@ -16,6 +17,7 @@ export const context = { }, semver, suggestDefault: true, + version: VERSION, }; export type Context = typeof context;