Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧Add try catch around JSON parsing #39

Merged
merged 7 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions .github/cicd/core/VersionPuller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,31 @@ export class VersionPuller {
const configFiles = [...entries].map((entry) => entry);

if (configFiles.length === 0) {
const errorMsg = `No '${this.denoConfig}' files found.`;
Utils.printNotice(errorMsg);
const errorMsg = `::error::No '${this.denoConfig}' files found.`;
Utils.printGitHubNotice(errorMsg);
Deno.exit(1);
}

const fileName = configFiles[0].name;
const filePath = configFiles[0].path;

const fileData = Deno.readTextFileSync(filePath);
const jsonObj = JSON.parse(fileData);

// If the object contains a property with the name version
if (jsonObj.version === undefined) {
const errorMsg = `The file '${fileName}' does not contain a version property.`;
Utils.printError(errorMsg);
try {
const jsonObj = JSON.parse(fileData);

// If the object contains a property with the name version
if (jsonObj.version === undefined) {
const errorMsg = `::error::The file '${fileName}' does not contain a version property.`;
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

return jsonObj.version;
} catch (error) {
const errorMsg = `There was a problem parsing the file '${fileName}'.\n${error.message}`;
console.log(`::error::${errorMsg}`);
Deno.exit(1);
}

return jsonObj.version;
}
}
12 changes: 6 additions & 6 deletions .github/cicd/scripts/check-release-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ const scriptFileName = new URL(import.meta.url).pathname.split("/").pop();
const versionType = (Deno.env.get("RELEASE_TYPE") ?? "").trim().toLowerCase();

if (versionType === "") {
Utils.printError(`The 'RELEASE_TYPE' environment variable is require.\n\tFileName: ${scriptFileName}`);
Utils.printGitHubError(`The 'RELEASE_TYPE' environment variable is require.\n\tFileName: ${scriptFileName}`);
Deno.exit(1);
}

let version = (Deno.env.get("VERSION") ?? "").trim().toLowerCase();

if (version === "") {
Utils.printError(`The 'VERSION' environment variable is require.\n\tFileName: ${scriptFileName}`);
Utils.printGitHubError(`The 'VERSION' environment variable is require.\n\tFileName: ${scriptFileName}`);
Deno.exit(2);
}

if (versionType != "production" && versionType != "preview") {
Utils.printError(`The version type must be either 'preview' or 'release' but received '${versionType}'.`);
Utils.printGitHubError(`The version type must be either 'preview' or 'release' but received '${versionType}'.`);
Deno.exit(200);
}

Expand All @@ -28,14 +28,14 @@ let releaseNotesDirName = "";

if (versionType === "preview") {
if (Utils.isNotValidPreviewVersion(version)) {
Utils.printError(`The preview version '${version}' is not valid.`);
Utils.printGitHubError(`The preview version '${version}' is not valid.`);
Deno.exit(300);
}

releaseNotesDirName = "preview-releases";
} else if (versionType === "production") {
if (Utils.isNotValidProdVersion(version)) {
Utils.printError(`The production version '${version}' is not valid.`);
Utils.printGitHubError(`The production version '${version}' is not valid.`);
Deno.exit(400);
}

Expand Down Expand Up @@ -63,6 +63,6 @@ const configFiles = [...entries]

if (configFiles.length === 0) {
const errorMsg = `The release notes '${releaseNotesFileName}' file could not be found.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}
16 changes: 11 additions & 5 deletions .github/cicd/scripts/deno-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { existsSync } from "https://deno.land/[email protected]/fs/exists.ts";
import { Guards } from "../../../src/core/guards.ts";
import { DenoBuildSettings } from "./deno-build-settings.ts";
import { checkAll } from "./deno-check.ts";
import { crayon } from "https://deno.land/x/[email protected]/mod.ts";
import { ConsoleLogColor } from "../../../src/core/console-log-color.ts";

let arg = (Deno.args[0] ?? "").trim();

Expand All @@ -23,9 +23,15 @@ const getSettings = (settingsFilePath: string): DenoBuildSettings | undefined=>

const settingJsonData = Deno.readTextFileSync(settingsFilePath);

const settings = JSON.parse(settingJsonData);

return validSettingsObj(settings) ? settings : undefined;
try {
const settings = JSON.parse(settingJsonData);

return validSettingsObj(settings) ? settings : undefined;
} catch (error) {
const errorMsg = `There was a problem parsing the file '${settingsFilePath}'.\n${error.message}`;
console.log(errorMsg);
Deno.exit(1);
}
}

const validSettingsObj = (settingsObj: unknown): settingsObj is DenoBuildSettings => {
Expand All @@ -48,7 +54,7 @@ const settings = getSettings(settingsFilePath);

const ignores = settings?.ignoreExpressions.map((expression) => new RegExp(expression));

console.log(crayon.cyan(`Checking all files in '${Deno.cwd()}' . . .\n`));
ConsoleLogColor.cyan(`Checking all files in '${Deno.cwd()}' . . .\n`);

const results = await checkAll(Deno.cwd(), {
noNpm: false,
Expand Down
2 changes: 1 addition & 1 deletion .github/cicd/scripts/get-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const outputFilePath = Deno.env.get("GITHUB_OUTPUT") ?? "";

if (outputFilePath === "") {
const errorMsg = `The environment variable 'GITHUB_OUTPUT' does not exist.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

Expand Down
22 changes: 11 additions & 11 deletions .github/cicd/scripts/version-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ const ownerName = (Deno.env.get("OWNER_NAME") ?? "").trim();

if (ownerName === "") {
const errorMsg = `The 'OWNER_NAME' environment variable is required.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

const repoName = (Deno.env.get("REPO_NAME") ?? "").trim();

if (repoName === "") {
const errorMsg = `The 'REPO_NAME' environment variable is required.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

const versionType = (Deno.env.get("VERSION_TYPE") ?? "").trim().toLowerCase();

if (versionType === "") {
const errorMsg = `The 'VERSION_TYPE' environment variable is required.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

let version = (Deno.env.get("VERSION") ?? "").trim().toLowerCase();

if (version === "") {
const errorMsg = `The 'VERSION' environment variable is required.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

Expand All @@ -41,44 +41,44 @@ const userCLient: UsersClient = new UsersClient(ownerName, repoName, token);

if (!await userCLient.userExists(ownerName)) {
const errorMsg = `The user '${ownerName}' does not exist.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

const repoClient: RepoClient = new RepoClient(ownerName, repoName, token);

if (!await repoClient.exists()) {
const errorMsg = `The repository '${repoName}' does not exist.`;
Utils.printError(errorMsg);
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

const tagClient: TagClient = new TagClient(ownerName, repoName, token);

if (await tagClient.tagExists(version)) {
const errorMsg = `The tag '${version}' already exists.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

if (versionType != "preview" && versionType != "production") {
const errorMsg = `The version type '${versionType}' is not valid. Valid values are 'preview' or 'production' version type.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

// Verify that the version is a valid preview or production version
if (versionType === "preview") {
if (Utils.isNotValidPreviewVersion(version)) {
const errorMsg = `The version '${version}' is not valid. Please provide a valid preview version.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}
} else if (versionType === "production") {
if (Utils.isNotValidProdVersion(version)) {
const errorMsg = `The version '${version}' is not valid. Please provide a valid production version.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}
}
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"version": "v1.0.0-preview.3",
"tasks": {
"build": "./.github/cicd/scripts/deno-build.ts",
"tests": "deno test ./Tests/*Tests.ts"
"tests": "deno test ./Tests/*Tests.ts",
"reload-cache": "deno cache --reload --lock=deno.lock --lock-write \"./deps.ts\""
},
"lint": {
"include": [
Expand Down
Loading
Loading