diff --git a/.rczrc.onrelease.js b/.rczrc.onrelease.js new file mode 100644 index 0000000..6ecf4de --- /dev/null +++ b/.rczrc.onrelease.js @@ -0,0 +1,14 @@ +const fs = require("fs"); + +const packageFile = fs.readFileSync("./package.json").toString(); +const newPackageFile = packageFile.replace( + /"version": "[0-9]+.[0-9]+.[0-9]+"/, + `"version": "${__NEW_VERSION__}"` +); + +fs.writeFileSync("./package.json", newPackageFile); + +const indexFile = fs.readFileSync("./src/index.ts").toString(); +const newIndexFile = indexFile.replace(/version\("[0-9]+\.[0-9]+\.[0-9]+"\)/); + +fs.writeFileSync("./src/index.ts", newIndexFile); diff --git a/.vscode/settings.json b/.vscode/settings.json index ba4e57f..99e8483 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,10 @@ { - "cSpell.words": ["Resultium", "Acked", "johndoe", "outro", "rczrc"] + "cSpell.words": [ + "Acked", + "johndoe", + "onrelease", + "outro", + "rczrc", + "Resultium" + ] } diff --git a/README.md b/README.md index 30c0adf..6932ce8 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,26 @@ You can create an `.rczrc`, `.rczrc.json` or `rcz.config.json` file in your root "autoSignReleases": true } ``` + +You are also able to create an onRelease event file, that is going to run before creation of a git tag and a release commit, when `rcz release` is run. The file is to be named `.rczrc.onrelease.js`. A sample can be seen below. + +```js +const fs = require("fs"); // Require fileSystem + +const packageFile = fs.readFileSync("./package.json").toString(); // Get package.json contents from the current working directory +const newPackageFile = packageFile.replace( + /"version": "[0-9]+.[0-9]+.[0-9]+"/, + `"version": "${__NEW_VERSION__}"` +); // Use RegExp to replace package.json version + +fs.writeFileSync("./package.json", newPackageFile); // Write to package.json file its new content + +const indexFile = fs.readFileSync("./src/index.ts").toString(); // Get index.ts file from source code directory +const newIndexFile = indexFile.replace(/version\("[0-9]+\.[0-9]+\.[0-9]+"\)/); // Replace class function call content with the new version + +fs.writeFileSync("./src/index.ts", newIndexFile); // Write to source code index file +``` + +In your onRelease file you are provided with `__NEW_VERSION__` constant string which is the new version submitted in `rcz release` and `__IS_SIGNED__` constant which is a boolean telling whether the release (it's commit and tag) is going to be signed. + +Bear in mind, **this script is going to be executed through `eval()` with no sanitation**, be careful with whatever you write. diff --git a/src/index.ts b/src/index.ts index cbfdc95..7341721 100644 --- a/src/index.ts +++ b/src/index.ts @@ -424,24 +424,40 @@ program const config = await GetConfig(); const sign = config?.autoSignReleases || options.sign ? true : false; - const version = string.replace("v", ""); - const packageFile = JSON.parse( - ( - await fs.promises.readFile(path.join(process.cwd(), "package.json")) - ).toString() - ); - if (!packageFile) { - console.log("[rcz]: this directory does not have a package.json file"); + const onReleaseFile = ( + await fs.promises.readFile( + path.join(process.cwd(), ".rczrc.onrelease.js") + ) + ).toString(); + + if (onReleaseFile) { + const releaseScript = ` + const __NEW_VERSION__ = "${version}"; + const __IS_SIGNED__ = ${sign}; + + ${onReleaseFile}`; + + eval(releaseScript); + } else { + const packageFile = JSON.parse( + ( + await fs.promises.readFile(path.join(process.cwd(), "package.json")) + ).toString() + ); + + if (!packageFile) { + console.log("[rcz]: this directory does not have a package.json file"); + } + + packageFile.version = version; + await fs.promises.writeFile( + path.join(process.cwd(), "package.json"), + JSON.stringify(packageFile, null, 4) + ); } - packageFile.version = version; - await fs.promises.writeFile( - path.join(process.cwd(), "package.json"), - JSON.stringify(packageFile, null, 4) - ); - await simpleGit() .add(".") .commit(`chore(release): v${version}`, sign ? ["-S"] : [])