Skip to content

Commit

Permalink
📋Add deno check and lint scripts (#132)
Browse files Browse the repository at this point in the history
* Start work for issue #128

* ci: add cicd script linting status check workflow

* chore: add new utils function to replace the doesNotExist function

* ci: improve the Directory class

* ci: create class to run cli commands

* refactor: replace isNullOrEmpty func uses with isNothing func

* chore: add further type restriction to type guard

* refactor: improve containsPathSeparator func

* docs: add code docs to Path class

* ci: create deno check script

* ide: create deno check task and launch config

* config: update deno lock

* ci: create cicd script build status check workflow
  • Loading branch information
CalvinWilkinson authored Nov 6, 2023
1 parent 4925a97 commit 0af60ab
Show file tree
Hide file tree
Showing 19 changed files with 361 additions and 70 deletions.
36 changes: 36 additions & 0 deletions .github/cicd/core/CLI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* A simple CLI command wrapper.
*/
export class CLI {
/**
* Runs the following CLI {@link command}.
* @param command The command to run.
* @returns The output of the command if successful, otherwise an error.
*/
public async runAsync(command: string): Promise<string | Error> {
if (command === undefined || command === null || command === "") {
const errorMsg = "The command parameter cannot be null or empty.";
Deno.exit(1);
}

if (!command.includes(" ")) {
const errorMsg = "The command parameter must include a space.";
Deno.exit(1);
}

const sections: string[] = command.split(" ");

const app = sections[0];
const args = sections.slice(1);

const cmd = new Deno.Command(app, { args: args });

const { code, stdout, stderr } = await cmd.output();

if (code === 0) {
return new TextDecoder().decode(stdout);
} else {
return new Error(new TextDecoder().decode(stderr));
}
}
}
2 changes: 1 addition & 1 deletion .github/cicd/core/CloneRepoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class CloneRepoService {
* @param tagOrBranch The tag or branch name of the repository to clone.
*/
public cloneRepo(tagOrBranch: string): void {
if (Utils.isNullOrEmpty(tagOrBranch)) {
if (Utils.isNothing(tagOrBranch)) {
throw Error("The tag or branch name must not be null, undefined, or empty.");
}

Expand Down
6 changes: 3 additions & 3 deletions .github/cicd/core/DefaultDocTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export class DefaultDocTool {
outputDirPath: string,
configFilePath: string,
): Promise<void> {
Utils.isNullOrEmpty(assemblyPath);
Utils.isNullOrEmpty(outputDirPath);
Utils.isNullOrEmpty(configFilePath);
Utils.isNothing(assemblyPath);
Utils.isNothing(outputDirPath);
Utils.isNothing(configFilePath);

await this.dotNetToolService.setupDotNetTools(this.defaultDocToolName, this.defaultDocToolVersion);

Expand Down
53 changes: 27 additions & 26 deletions .github/cicd/core/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,43 @@ export class Directory {
}

/**
* Returns a list of all of the files in the given directory.
* @param {string} dirPath The path to the directory to the files of.
* @param {string} extension The extension filter of all of the files to get. If undefined or empty, all files will be returned. If not undefined or empty, the extension must start with a period. If the extension does not start with a period, it will be added automatically.
* @returns {string[]} The files in the given directory,
* Gets a list of files in the given {@link dirPath}. This will search recursively
* if {@link recursive} is true.
* @param dirPath The path of the directory start searching.
* @param extension The file extension to search for.
* @param recursive True to search recursively, otherwise false.
* @returns {string[]} A list of files in the given {@link dirPath}.
*/
public static getFiles(dirPath: string, extension = "*.*"): string[] {
Guard.isNotUndefinedOrEmpty(dirPath);
public static getFiles(dirPath: string, extension:string, recursive = false): string[] {
let files: string[] = [];

if (Path.isNotDirPath(dirPath)) {
throw new Error(`The path '${dirPath}' is not a directory path.`);
}
extension = Utils.isNothing(extension) ? "*.*" : extension;
extension = extension.startsWith(".") ? extension : `.${extension}`;

if (this.doesNotExist(dirPath)) {
throw new Error(`The path '${dirPath}' does not exist.`);
if (dirPath === undefined || dirPath === null || dirPath === "") {
const errorMsg = "The dirPath parameter cannot be null or empty.";
Deno.exit(1);
}

// Default value if undefined or empty
extension = Utils.isNullOrEmpty(extension) ? "*.*" : extension;

extension = extension != "*.*" && extension.startsWith("*") ? extension.replace("*", "") : extension;

dirPath = Path.normalizeSeparators(dirPath);

const filePaths: string[] = [];
dirPath = dirPath === "." || dirPath === "/" ? "." : dirPath;

for (const dirEntry of Deno.readDirSync(dirPath)) {
if (dirEntry.isFile) {
const fileExtension: string = Path.getExtension(dirEntry.name);

if (extension === "*.*" || extension === fileExtension) {
filePaths.push(`${dirPath}${dirEntry.name}`);
const entry = dirPath + "/" + dirEntry.name;

if (recursive && dirEntry.isDirectory) {
files = [...files, ...(Directory.getFiles(entry, extension, recursive))];
} else if (dirEntry.isFile) {
if (extension === "*.*") {
files.push(entry);
} else {
if (entry.endsWith(extension)) {
files.push(entry);
}
}
}
}

return filePaths;
return files;
}

/**
Expand Down Expand Up @@ -130,7 +131,7 @@ export class Directory {
* @param {string} dirPath The path to the directory to delete.
*/
public static delete(dirPath: string): void {
if (Utils.isNullOrEmpty(dirPath)) {
if (Utils.isNothing(dirPath)) {
return;
}

Expand Down
8 changes: 4 additions & 4 deletions .github/cicd/core/DocProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export class DocProcessor {
* @param releaseTag The Velaptor release tag.
*/
public async generateFromTag(apiDocDirPath: string, releaseTag: string): Promise<void> {
if (Utils.isNullOrEmpty(apiDocDirPath)) {
if (Utils.isNothing(apiDocDirPath)) {
console.log(chalk.red("The API doc dir path is required."));
Deno.exit();
}

if (Utils.isNullOrEmpty(releaseTag)) {
if (Utils.isNothing(releaseTag)) {
console.log(chalk.red("The release tag is required."));
Deno.exit();
}
Expand All @@ -64,12 +64,12 @@ export class DocProcessor {
* @param branchName The name of the branch to generate the API documentation from.
*/
public async generateFromBranch(apiDocDirPath: string, branchName: string): Promise<void> {
if (Utils.isNullOrEmpty(apiDocDirPath)) {
if (Utils.isNothing(apiDocDirPath)) {
console.log(chalk.red("The API doc dir path is required."));
Deno.exit();
}

if (Utils.isNullOrEmpty(branchName)) {
if (Utils.isNothing(branchName)) {
console.log(chalk.red("The branch name is required."));
Deno.exit();
}
Expand Down
2 changes: 1 addition & 1 deletion .github/cicd/core/DotNetToolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class DotNetToolService {
* @returns The lines with empty lines removed.
*/
private toLines(value: string): string[] {
if (Utils.isNullOrEmpty(value)) {
if (Utils.isNothing(value)) {
return [];
}

Expand Down
6 changes: 3 additions & 3 deletions .github/cicd/core/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export class File {
public static writeTextFileSync(filePath: string, fileContent: string): void {
Guard.isNotUndefinedOrEmpty(filePath, "filePath");

if (Utils.isNullOrEmpty(fileContent)) {
if (Utils.isNothing(fileContent)) {
return;
}

if (Utils.isNullOrEmpty(filePath)) {
if (Utils.isNothing(filePath)) {
throw new Error(`The parameter '${filePath}' must not be null or empty.`);
}

Expand Down Expand Up @@ -87,7 +87,7 @@ export class File {
* @param {string} filePath The file to delete.
*/
public static deleteFile(filePath: string): void {
if (Utils.isNullOrEmpty(filePath)) {
if (Utils.isNothing(filePath)) {
throw new Error(`The 'filePath' parameter must not be null or empty.`);
}

Expand Down
2 changes: 1 addition & 1 deletion .github/cicd/core/HTMLService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class HTMLService {
}

public isHTMLLink(value: string): boolean {
if (Utils.isNullOrEmpty(value)) {
if (Utils.isNothing(value)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion .github/cicd/core/MarkdownFileContentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class MarkdownFileContentService {
}

private processHeaderAngleBrackets(fileContent: string): string {
if (Utils.isNullOrEmpty(fileContent)) {
if (Utils.isNothing(fileContent)) {
return "";
}

Expand Down
8 changes: 4 additions & 4 deletions .github/cicd/core/MarkdownService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class MarkdownService {
public headerExists(name: string, lines: string[]): boolean;
public headerExists(name: string, content: string): boolean;
public headerExists(name: string, linesOrContent: string[] | string): boolean {
if (Utils.isNullOrEmpty(name)) {
if (Utils.isNothing(name)) {
return false;
}

Expand Down Expand Up @@ -191,7 +191,7 @@ export class MarkdownService {
}

public isHeaderLine(line: string): boolean {
if (Utils.isNullOrEmpty(line)) {
if (Utils.isNothing(line)) {
return false;
}

Expand All @@ -200,7 +200,7 @@ export class MarkdownService {
}

public containsMarkdownLink(markDownLink: string): boolean {
if (Utils.isNullOrEmpty(markDownLink)) {
if (Utils.isNothing(markDownLink)) {
return false;
}

Expand All @@ -220,7 +220,7 @@ export class MarkdownService {
}

public createFrontMatter(title: string): string {
if (Utils.isNullOrEmpty(title)) {
if (Utils.isNothing(title)) {
throw new Error("The 'title' parameter must not be null or empty when creating front matter.");
}

Expand Down
Loading

0 comments on commit 0af60ab

Please sign in to comment.