Skip to content

Commit

Permalink
fix: 🐛 module config handler context
Browse files Browse the repository at this point in the history
  • Loading branch information
lenconda committed Sep 2, 2021
1 parent 38ba52d commit 2828ce9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
20 changes: 14 additions & 6 deletions packages/dollie-core/src/generators/module.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
import Generator from '../generator.abstract';
import * as _ from 'lodash';
import {
diff, parseFileTextToMergeBlocks, parseMergeBlocksToText,
diff,
parseFileTextToMergeBlocks,
parseMergeBlocksToText,
parseDiffToMergeBlocks,
} from '../diff';
import {
ParameterInvalidError,
Expand Down Expand Up @@ -152,7 +155,7 @@ class ModuleGenerator extends Generator implements Generator {
lodash: _,
},
exists: this.exists.bind(this),
getEntity: this.getEntity.bind(this),
getFileContent: this.getFileContent.bind(this),
});

if (_.isBoolean(result) && !result) {
Expand Down Expand Up @@ -350,7 +353,7 @@ class ModuleGenerator extends Generator implements Generator {
lodash: _,
},
exists: this.exists.bind(this),
getEntity: this.getEntity.bind(this),
getFileContent: this.getFileContent.bind(this),
};

const patterns = {};
Expand Down Expand Up @@ -385,12 +388,17 @@ class ModuleGenerator extends Generator implements Generator {
return false;
}

private async exists(pathname: string) {
private exists(pathname: string) {
return this.files.findIndex((file) => file.relativeOriginalPathname === pathname) !== -1;
}

private async getEntity(pathname: string) {
return this.files.find((file) => file.absoluteOriginalPathname === pathname);
private getFileContent(pathname: string) {
const diffs = this.cacheTable[pathname];
if (!_.isArray(diffs) || diffs.length === 0) {
return '';
}
const currentFileDiff = diffs[0];
return parseMergeBlocksToText(parseDiffToMergeBlocks(currentFileDiff));
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/dollie-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ export type TemplateCleanUpFunction = (data: TemplateCleanupData) => void;
export type ExtendTemplateConfig = Record<string, Omit<TemplateConfig, 'extendTemplates'>>;

export type ModuleEntityAlias = Record<string, string>;
export type EntityExistenceChecker = (pathname: string) => Promise<boolean>;
export type EntityReader = (pathname: string) => Promise<TemplateFileItem>;
export type EntityExistenceChecker = (pathname: string) => boolean;
export type EntityReader = (pathname: string) => FileContent;

export interface ModuleConfigHandlerContext {
moduleId: string;
props: InquirerAnswers;
context: GeneralHandlerContext;
exists: EntityExistenceChecker;
getEntity: EntityReader;
getFileContent: EntityReader;
}

export type ModuleDeleteConfigHandler = (data: ModuleConfigHandlerContext) => Promise<string | string[]>;
Expand Down

0 comments on commit 2828ce9

Please sign in to comment.