-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧Improve update json version process (#22)
* Start work for issue #21 * enhance: add ability to use a json key path to the json version value * cleanup: remove unused files * refactor: change code to meeting coding standards * chore: improve the setPropertyValue function
- Loading branch information
1 parent
7f35ca6
commit de793ab
Showing
7 changed files
with
60 additions
and
111 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,45 +1,71 @@ | ||
import { existsSync } from "../deps.ts"; | ||
import { ParamGuards } from "./core/param-guards.ts"; | ||
import { PrepareReleaseSettings } from "./prepare-release-settings.ts"; | ||
|
||
/** | ||
* Updates the version in a JSON file. | ||
*/ | ||
export class JsonVersionUpdater { | ||
/** | ||
* Updates the version in the JSON version file at the given {@link versionFilePath}. | ||
* @param versionFilePath The full or relative file path to the version file. | ||
* @param settings The prepare release settings. | ||
* @param newVersion The new version. | ||
* @throws {Error} Thrown if the version file does not exist or the version file does not contain a 'version' property. | ||
*/ | ||
public updateVersion(versionFilePath: string, newVersion: string) { | ||
ParamGuards.isNothing(versionFilePath, "versionFilePath null, empty, or undefined."); | ||
public updateVersion(settings: PrepareReleaseSettings, newVersion: string) { | ||
ParamGuards.isNothing(newVersion, "newVersion null, empty, or undefined."); | ||
|
||
const versionFilePath = settings.versionFilePath ?? ""; | ||
|
||
if (!existsSync(versionFilePath, { isFile: true })) { | ||
throw new Error(`The version file path does not exist: ${versionFilePath}`); | ||
} | ||
|
||
const versionFileContent = Deno.readTextFileSync(versionFilePath); | ||
const versionConfig = JSON.parse(versionFileContent); | ||
|
||
const versionConfig: { version: string } = JSON.parse(versionFileContent); | ||
const propChain = settings.versionJSONKeyPath?.split(".").map((i) => i.trim()) ?? ["version"]; | ||
|
||
if (!this.containsVersionProp(versionConfig)) { | ||
throw new Error(`The version file does not contain a version property: ${versionFilePath}`); | ||
} | ||
const result = this.setPropertyValue(versionConfig, propChain, newVersion); | ||
|
||
versionConfig.version = newVersion; | ||
if (result[0] === false) { | ||
console.log(`%c${result[1]}`, "color: red;"); | ||
Deno.exit(1); | ||
} | ||
|
||
Deno.writeTextFileSync(versionFilePath, `${JSON.stringify(versionConfig, null, 4)}\n`); | ||
} | ||
|
||
/** | ||
* Validates that the given {@link obj} contains a version property. | ||
* @param obj The object to validate. | ||
* @returns True if the object contains a version property; otherwise, false. | ||
* Sets a property in the given {@link propChain} to the given {@link newValue}. | ||
* @param obj The object that might contains the property to update. | ||
* @param propChain The property chain path to the property to update. | ||
* @param newValue The new value to set the last prop in the chain to. | ||
* @returns A tuple where the first item is a boolean indicating if the property was set and the second item is an error message if the property was not set. | ||
*/ | ||
private containsVersionProp(obj: unknown): obj is { version: string } { | ||
return obj !== null && | ||
obj !== undefined && | ||
typeof obj === "object" && "version" in obj; | ||
private setPropertyValue<T extends { [key: string]: unknown }>( | ||
obj: T, | ||
propChain: string[], | ||
newValue: string | number, | ||
): [boolean, string | undefined] { | ||
let currentObj: Record<string, unknown> = obj; // Start with the entire object | ||
|
||
for (let i = 0; i < propChain.length - 1; i++) { | ||
const propertyName = propChain[i]; | ||
if (!(propertyName in currentObj)) { | ||
return [false, `The property '${propertyName}' does not exist.`]; | ||
} | ||
|
||
currentObj = currentObj[propertyName] as Record<string, unknown>; | ||
} | ||
|
||
const finalPropName = propChain[propChain.length - 1]; | ||
|
||
if (finalPropName in currentObj) { | ||
(currentObj as Record<string, unknown>)[finalPropName] = newValue; // Set the new value | ||
return [true, undefined]; | ||
} | ||
|
||
return [false, `The property '${finalPropName}' does not exist.`]; | ||
} | ||
} |
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