diff --git a/plugins/version-file/README.md b/plugins/version-file/README.md index 2043ad867..cebf246dd 100644 --- a/plugins/version-file/README.md +++ b/plugins/version-file/README.md @@ -17,7 +17,12 @@ yarn add -D @auto-it/version-file ## Options - versionFile (optional, default="VERSION"): Path to where the version is stored in the repository. It should be a file containing just the semver. -- releaseScript: (optional, default=None): Path to script that runs the publish actions in your repository. If not supplied nothing will be called. If supplied will be called during the `publish`,`canary` and `next` hooks. For the `publish` hook the first parameter passed to the script will be `release` to indicate that a regular release is being called. For `canary` and `next` hooks the first parameter will be `snapshot` to indicate a prerelease version. +- publishScript: (optional, default=None): Path to script that runs the publish actions in your repository. If not supplied nothing will be called. If supplied will be called during the `publish`,`canary` and `next` hooks with the arguments defined in `publishScriptReleaseTypeArgs` for that release type. +- publishScriptReleaseTypeArgs: (optional, default=```{ + "publish": ["release"], + "canary": ["snapshot"], + "next": ["snapshot"] +}```): Mapping of arguments to pass to the `publishScript` for each release type (`publish`, `canary`, `next`) ## Usage @@ -34,6 +39,10 @@ yarn add -D @auto-it/version-file ```json { "plugins": [ - "version-file", {"versionFile": "./tools/Version.txt", "releaseScript":"./tools/publish.sh"} + "version-file", {"versionFile": "./tools/Version.txt", "publishScript":"./tools/publish.sh", "publishScriptReleaseTypeArgs": { + "publish": ["release"], // (default) + "canary": ["snapshot"], + "next": ["some", "other", "args"], + }} ] -} \ No newline at end of file +} diff --git a/plugins/version-file/__tests__/version-file.test.ts b/plugins/version-file/__tests__/version-file.test.ts index 003bf83a4..6a4a07f06 100644 --- a/plugins/version-file/__tests__/version-file.test.ts +++ b/plugins/version-file/__tests__/version-file.test.ts @@ -302,4 +302,66 @@ describe("Test Release Types", () => { // Check the right version was written expect(fs.readFileSync("VERSION", "utf-8")).toStrictEqual("v2.0.0-next.0") }); -}); \ No newline at end of file + + test("Release type args can be provided to override default args for publish script", async () => { + const prefixRelease: (a: string) => string = (version: string) => { + return `v${version}`; + }; + + mockFs({ + VERSION: `1.0.0`, + }); + const plugin = new BazelPlugin({ + publishScript: "./tools/release.sh", + publishScriptReleaseTypeArgs: { + publish: ["args", "for", "publish"], + canary: ["different", "canary"], + next: ["next"], + }, + }); + const hooks = makeHooks(); + + plugin.apply(({ + hooks, + config: { prereleaseBranches: ["next"] }, + remote: "origin", + baseBranch: "main", + logger: dummyLog(), + prefixRelease, + getCurrentVersion: () => "1.0.0", + git: { + getLastTagNotInBaseBranch: async () => undefined, + getLatestRelease: () => "1.0.0", + getLatestTagInBranch: () => Promise.resolve("1.0.0"), + }, + } as unknown) as Auto); + + + await hooks.publish.promise({ bump: SEMVER.major }); + + expect(execPromise).toHaveBeenNthCalledWith(1, "./tools/release.sh", [ + "args", "for", "publish" + ]); + + await hooks.canary.promise({ + bump: SEMVER.minor, + canaryIdentifier: "canary.368.1", + }); + + expect(execPromise).toHaveBeenNthCalledWith(3, "./tools/release.sh", [ + "different", + "canary", + ]); + + await hooks.next.promise(["1.0.0"], { + bump: SEMVER.major, + fullReleaseNotes: "", + releaseNotes: "", + commits: [], + }); + + expect(execPromise).toHaveBeenNthCalledWith(5, "./tools/release.sh", [ + "next", + ]); + }) +}); diff --git a/plugins/version-file/src/index.ts b/plugins/version-file/src/index.ts index c436f22ba..853d67766 100644 --- a/plugins/version-file/src/index.ts +++ b/plugins/version-file/src/index.ts @@ -7,13 +7,28 @@ import { inc, ReleaseType } from "semver"; const readFile = promisify(fs.readFile); const writeFile = promisify(fs.writeFile); +interface ReleaseTypeArgs { + /** Args to use when invoking the publishScript during the publish hook */ + publish: string[]; + /** Args to use when invoking the publishScript during the canary hook */ + canary: string[]; + /** Args to use when invoking the publishScript during the next hook */ + next: string[]; +} const pluginOptions = t.partial({ /** Path to file (from where auto is executed) where the version is stored */ versionFile: t.string, /** Optional script that executes release pipeline stages */ - publishScript: t.string + publishScript: t.string, + + /** Optional publish script args mapping for each release hook, defaults `publish` to ["release"] and the others to ["snapshot"] */ + publishScriptReleaseTypeArgs: t.partial({ + publish: t.array(t.string), + canary: t.array(t.string), + next: t.array(t.string), + }) }); export type IVersionFilePluginOptions = t.TypeOf; @@ -55,10 +70,19 @@ export default class VersionFilePlugin implements IPlugin { /** Release script location */ readonly publishScript: string | undefined + /** */ + readonly publishScriptReleaseTypeArgs: ReleaseTypeArgs; + /** Initialize the plugin with it's options */ constructor(options: IVersionFilePluginOptions) { this.versionFile = options.versionFile ?? "VERSION"; - this.publishScript = options.publishScript + this.publishScript = options.publishScript; + this.publishScriptReleaseTypeArgs = { + publish: ['release'], + canary: ['snapshot'], + next: ['snapshot'], + ...options.publishScriptReleaseTypeArgs ?? {} + }; } @@ -113,7 +137,7 @@ export default class VersionFilePlugin implements IPlugin { // Call release script if provided if(this.publishScript){ auto.logger.log.info(`Calling release script in repo at ${this.publishScript}`); - await execPromise(this.publishScript, ["release"]) + await execPromise(this.publishScript, this.publishScriptReleaseTypeArgs.publish) } else { auto.logger.log.info("Skipping calling release script in repo since none was provided"); } @@ -141,7 +165,7 @@ export default class VersionFilePlugin implements IPlugin { // Ship canary release if release script is provided if(this.publishScript){ auto.logger.log.info(`Calling release script in repo at ${this.publishScript}`); - await execPromise(this.publishScript, ["snapshot"]); + await execPromise(this.publishScript, this.publishScriptReleaseTypeArgs.canary); } else { auto.logger.log.info("Skipping calling release script in repo since none was provided"); } @@ -188,7 +212,7 @@ export default class VersionFilePlugin implements IPlugin { // ship next release if release script is provided if(this.publishScript){ auto.logger.log.info(`Calling release script in repo at ${this.publishScript}`); - await execPromise(this.publishScript, ["snapshot"]); + await execPromise(this.publishScript, this.publishScriptReleaseTypeArgs.next); } else { auto.logger.log.info("Skipping calling release script in repo since none was provided"); }