Skip to content

Commit

Permalink
Improving the onBeforeFileWrite callback
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed Dec 5, 2023
1 parent 1b753b9 commit 2eb02e8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
14 changes: 13 additions & 1 deletion apexdocs.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import * as path from 'path';

type OutputDir = {
baseDir: string;
fileDir: string;
};

export default {
onAfterProcess: (files: string[]) => {
onBeforeFileWrite: (dir: OutputDir, fileName: string) => {
console.log('onBefore writing', dir, fileName);
const defaultDir = path.join(dir.baseDir, dir.fileDir);
return { dir: defaultDir, fileName };
},
onAfterProcess: (files: { dir: string; fileName: string }[]) => {
console.log('onAfterProcess files', files);
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cparra/apexdocs",
"version": "2.19.0-alpha.1",
"version": "2.19.0-alpha.2",
"description": "Library with CLI capabilities to generate documentation for Salesforce Apex classes.",
"keywords": [
"apex",
Expand Down
8 changes: 4 additions & 4 deletions src/application/Apexdocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export class Apexdocs {
Transpiler.generate(filteredTypes, processor);
const generatedFiles = processor.fileBuilder().files();

const generatedFilePaths: string[] = [];
FileWriter.write(generatedFiles, (fileName: string) => {
Logger.logSingle(`${fileName} processed.`, false, 'green', false);
generatedFilePaths.push(fileName);
const generatedFilePaths: { dir: string; fileName: string }[] = [];
FileWriter.write(generatedFiles, (file: { dir: string; fileName: string }) => {
Logger.logSingle(`${file.fileName} processed.`, false, 'green', false);
generatedFilePaths.push(file);
});

Settings.getInstance().onAfterProcess(generatedFilePaths);
Expand Down
24 changes: 11 additions & 13 deletions src/service/file-writer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as fs from 'fs';
import * as path from 'path';
import { OutputFile } from '../model/outputFile';
import { Settings } from '../settings';
import { OnBeforeFileWrite, OutputDir, Settings, TargetLocation } from '../settings';

export class FileWriter {
static write(files: OutputFile[], onWriteCallback: (fileName: string) => void) {
const onBeforeFileWrite = (outputDir: string, fileName: string) =>
static write(files: OutputFile[], onWriteCallback: (file: TargetLocation) => void) {
const onBeforeFileWrite = (outputDir: OutputDir, fileName: string) =>
Settings.getInstance().onBeforeFileWrite(outputDir, fileName);
files.forEach((file) => {
const { dir: dirPath, fileName } = this.getTargetLocation(file, onBeforeFileWrite);
Expand All @@ -15,20 +15,18 @@ export class FileWriter {

const filePath = path.join(dirPath, fileName);
fs.writeFileSync(filePath, file.body, 'utf8');
onWriteCallback(filePath);
onWriteCallback({ dir: dirPath, fileName });
});
}

private static getTargetLocation(file: OutputFile, onBeforeFileWrite: OnBeforeFileWrite): TargetLocation {
const outputDir = path.join(Settings.getInstance().outputDir, file.dir);
const fileName = `${file.fileName}${file.fileExtension()}`;
return onBeforeFileWrite(outputDir, fileName);
return onBeforeFileWrite(
{
baseDir: Settings.getInstance().outputDir,
fileDir: file.dir,
},
fileName,
);
}
}

type OnBeforeFileWrite = (outputDir: string, fileName: string) => TargetLocation;

type TargetLocation = {
dir: string;
fileName: string;
};
31 changes: 22 additions & 9 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { GeneratorChoices } from './transpiler/generator-choices';
import * as path from 'path';

export type OnBeforeFileWrite = (outputDir: OutputDir, fileName: string) => TargetLocation;

export type TargetLocation = {
dir: string;
fileName: string;
};

export type OutputDir = {
baseDir: string;
fileDir: string;
};

export interface SettingsConfig {
sourceDirectory: string;
Expand All @@ -15,8 +28,8 @@ export interface SettingsConfig {
openApiFileName: string;
includeMetadata: boolean;
rootDir?: string;
onAfterProcess?: (files: string[]) => void;
onBeforeFileWrite?: (outputDir: string, fileName: string) => { dir: string; fileName: string };
onAfterProcess?: (files: { dir: string; fileName: string }[]) => void;
onBeforeFileWrite?: (outputDir: OutputDir, fileName: string) => { dir: string; fileName: string };
}

export class Settings {
Expand Down Expand Up @@ -98,17 +111,17 @@ export class Settings {
return this.config.rootDir;
}

public onAfterProcess(files: string[]) {
public onAfterProcess(files: { dir: string; fileName: string }[]) {
if (this.config.onAfterProcess) {
this.config.onAfterProcess(files);
}
}

public onBeforeFileWrite(outputDir: string, fileName: string): { dir: string; fileName: string } {
console.log('the config', this.config);
// if (this.config?.onBeforeFileWrite) {
// return this.config.onBeforeFileWrite(outputDir, fileName);
// }
return { dir: outputDir, fileName };
public onBeforeFileWrite(outputDir: OutputDir, fileName: string): { dir: string; fileName: string } {
if (this.config.onBeforeFileWrite) {
return this.config.onBeforeFileWrite(outputDir, fileName);
}
const defaultDir = path.join(outputDir.baseDir, outputDir.fileDir);
return { dir: defaultDir, fileName };
}
}

0 comments on commit 2eb02e8

Please sign in to comment.