Skip to content

Commit

Permalink
make it non-breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
sugarmanz committed Jul 12, 2024
1 parent 3bc7bd5 commit c379344
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
13 changes: 11 additions & 2 deletions plugins/version-file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `canary` or `next` to indicate a prerelease version, and let the consumer handle what to do for those types of releases.
- 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

Expand All @@ -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"],
}}
]
}
66 changes: 64 additions & 2 deletions plugins/version-file/__tests__/version-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ describe("Test Release Types", () => {
await hooks.canary.promise({bump: SEMVER.minor, canaryIdentifier: "canary.368.1"})

// check release script was called
expect(execPromise).toHaveBeenNthCalledWith(1, "./tools/release.sh", ["canary"]);
expect(execPromise).toHaveBeenNthCalledWith(1, "./tools/release.sh", ["snapshot"]);

// check local changes were reverted
expect(execPromise).toHaveBeenNthCalledWith(2, "git", ["reset", "--hard", "HEAD"]);
Expand Down Expand Up @@ -293,7 +293,7 @@ describe("Test Release Types", () => {
await hooks.next.promise(["1.0.0"], {bump: SEMVER.major, fullReleaseNotes:"", releaseNotes:"", commits:[]})

// check release script was called
expect(execPromise).toHaveBeenNthCalledWith(1, "./tools/release.sh", ["next"]);
expect(execPromise).toHaveBeenNthCalledWith(1, "./tools/release.sh", ["snapshot"]);

// Check git ops
expect(execPromise).toHaveBeenNthCalledWith(2, "git", ["tag", "v2.0.0-next.0"]);
Expand All @@ -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")
});

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",
]);
})
});
31 changes: 26 additions & 5 deletions plugins/version-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ import { inc, ReleaseType } from "semver";
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

interface ReleaseTypeArgs {
publish: string[];

Check failure on line 11 in plugins/version-file/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc comment
canary: string[];

Check failure on line 12 in plugins/version-file/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc comment
next: string[];

Check failure on line 13 in plugins/version-file/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc comment
}

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<typeof pluginOptions>;
Expand Down Expand Up @@ -55,10 +67,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 ?? {}
};
}


Expand Down Expand Up @@ -113,7 +134,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'])

Check failure on line 137 in plugins/version-file/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

["publish"] is better written in dot notation
} else {
auto.logger.log.info("Skipping calling release script in repo since none was provided");
}
Expand Down Expand Up @@ -141,7 +162,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, ["canary"]);
await execPromise(this.publishScript, this.publishScriptReleaseTypeArgs['canary']);

Check failure on line 165 in plugins/version-file/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

["canary"] is better written in dot notation
} else {
auto.logger.log.info("Skipping calling release script in repo since none was provided");
}
Expand Down Expand Up @@ -188,7 +209,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, ["next"]);
await execPromise(this.publishScript, this.publishScriptReleaseTypeArgs['next']);

Check failure on line 212 in plugins/version-file/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

["next"] is better written in dot notation
} else {
auto.logger.log.info("Skipping calling release script in repo since none was provided");
}
Expand Down

0 comments on commit c379344

Please sign in to comment.