Skip to content

Commit

Permalink
feat(cli): check .codefendrc before obfuscation
Browse files Browse the repository at this point in the history
  • Loading branch information
Z-n-o-M committed Jan 5, 2023
1 parent 6c8fa2c commit 728243d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/cli/CodefendCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,39 @@ export class CodefendCLI implements ICodefendCLI {
async executeCheckCommand() {
console.log("checking .codefendrc.json...");
await this.delay(500);
console.log("Check completed. 0 error(s) 0 warning(s) ");
const checkResults: ICheckResults = {
errors: [],
warnings: [],
};

const config = fileSystem.fileReader.readFile("./.codefendrc.json");
if (!config) {
checkResults.errors.push(
".codefendrc.json not found. please run codefend -i to create a new one"
);
}

console.log(
`Check completed. ${checkResults.errors.length} error(s) ${checkResults.warnings.length} warning(s) `
);
checkResults.errors.forEach((error) => {
console.log(`Error: ${error}`);
});
checkResults.warnings.forEach((warning) => {
console.log(`Warning: ${warning}`);
});

const success = !checkResults.errors.length;

return success;
}

async executeObfuscateCommand() {
const checkSuccess = await this.executeCheckCommand();
if (!checkSuccess) {
console.log("Could not start with Obfuscation, check contains errors");
return;
}
console.log("Obfuscation started...");
await this.delay(500);
console.log("Obfuscation completed.");
Expand All @@ -85,3 +114,8 @@ export class CodefendCLI implements ICodefendCLI {
program.outputHelp();
}
}

export interface ICheckResults {
warnings: string[];
errors: string[];
}
12 changes: 12 additions & 0 deletions src/fs/file/reader/CodefendFileReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from "fs";
import { ICodefendFileReader } from "./ICodefendFileReader";

export class CodefendFileReader implements ICodefendFileReader {
readFile(path: string, flag = "r") {
try {
return fs.readFileSync(path, { encoding: "utf8", flag: flag });
} catch (e) {
return null;
}
}
}
3 changes: 3 additions & 0 deletions src/fs/file/reader/ICodefendFileReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ICodefendFileReader {
readFile: (path: string) => string | null;
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { CodefendParser } from "./core/parser/CodefendParser";
import { ICodefendParser } from "./core/parser/ICodefendParser";
import { CodefendReplacer } from "./core/replacer/CodefendReplacer";
import { ICodefendReplacer } from "./core/replacer/ICodefendReplacer";
import { CodefendFileReader } from "./fs/file/reader/CodefendFileReader";
import { ICodefendFileReader } from "./fs/file/reader/ICodefendFileReader";
import { CodefendFileWriter } from "./fs/file/writer/CodefendFileWriter";
import { ICodefendFileWriter } from "./fs/file/writer/ICodefendFileWriter";
import { CodefendLogger } from "./logger/CodefendLogger";
Expand All @@ -17,6 +19,7 @@ export const logger = new CodefendLogger(defaultOptions);
export const cli = new CodefendCLI();
export const fileSystem: ICodefendFileSystem = {
fileWriter: new CodefendFileWriter(),
fileReader: new CodefendFileReader(),
};
export const codefendDefaultOptions = defaultOptions;

Expand Down Expand Up @@ -48,4 +51,5 @@ export interface ICodefendCore {

export interface ICodefendFileSystem {
fileWriter: ICodefendFileWriter;
fileReader: ICodefendFileReader;
}

0 comments on commit 728243d

Please sign in to comment.