Skip to content

Commit

Permalink
Merge pull request #12 from dolliejs/refactor/origin-loaders
Browse files Browse the repository at this point in the history
refactor: 💡 refactor origin params and origin loader
  • Loading branch information
lenconda authored Jul 6, 2021
2 parents 33c8074 + 0b53ba6 commit a2864bb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/@dollie/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"dependencies": {
"@dollie/core": "^3.0.0",
"@dollie/origins": "^3.0.0",
"chalk": "^4.1.1",
"commander": "^7.2.0",
"figlet": "^1.5.0",
Expand Down
5 changes: 4 additions & 1 deletion packages/@dollie/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import _ from 'lodash';
import inquirer from 'inquirer';
import chalk from 'chalk';
import figlet from 'figlet';
import { loadOrigins } from '../../../origins/lib';

export type ConflictSolveApproachType = 'simple' | 'select' | 'edit' | 'ignore';
export type ManualResult = 'all' | 'none' | 'former' | 'current';
Expand Down Expand Up @@ -217,9 +218,11 @@ export default (config: DollieCLIConfigSchema) => {
const errorLogger = new ErrorLogger();
const infoLogger = new InfoLogger();

infoLogger.log('Loading origins...');

const context = new Context(name, template, {
generator: {
origins: config.origins || {},
origins: await loadOrigins(config.origins || {}),
origin: config.origin || {},
loader: _.get(config, 'loader'),
getTemplateProps: async (questions) => {
Expand Down
13 changes: 9 additions & 4 deletions packages/@dollie/core/src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const diff = (originalContent: string, currentContent?: string): DiffChange[] =>
return result;
};

const merge = (originalChanges: DiffChange[], diffList: DiffChange[][], pathname: string): DiffChange[] => {
const merge = (originalChanges: DiffChange[], diffList: DiffChange[][]): DiffChange[] => {
if (!originalChanges) {
return [];
}
Expand Down Expand Up @@ -55,6 +55,7 @@ const merge = (originalChanges: DiffChange[], diffList: DiffChange[][], pathname
}
}
}

const addedChangeLineNumbers = currentDiff
.filter((change) => change.added)
.map((change) => change.lineNumber);
Expand Down Expand Up @@ -95,12 +96,16 @@ const merge = (originalChanges: DiffChange[], diffList: DiffChange[][], pathname
}
}

return blocks.reduce((result, currentBlock) => {
return blocks.reduce((currentResult, currentBlock) => {
const currentPatchItem = patches.shift();

const result = currentResult.concat(currentBlock);

if (!currentPatchItem) {
return result.concat(currentBlock);
return result;
}
return result.concat(currentBlock).concat(currentPatchItem.changes);

return result.concat(currentPatchItem.changes);
}, []);
};

Expand Down
6 changes: 2 additions & 4 deletions packages/@dollie/core/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ class Generator {
}

public async initialize() {
this.messageHandler('Initializing origins...');

this.origins = await loadOrigins(this.config.origins);
this.origins = _.get(this, 'config.origins') || [];

if (_.isString(this.templateOriginName)) {
if (!this.templateOriginName.includes(':')) {
Expand Down Expand Up @@ -273,7 +271,7 @@ class Generator {
} else {
const originalDiffChanges = diffs[0];
const forwardDiffChangesGroup = diffs.slice(1);
const mergedDiffChanges = merge(originalDiffChanges, forwardDiffChangesGroup, entityPathname);
const mergedDiffChanges = merge(originalDiffChanges, forwardDiffChangesGroup);
this.mergeTable[entityPathname] = parseDiffToMergeBlocks(mergedDiffChanges);
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions packages/@dollie/core/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
DollieOrigin,
DollieOriginConfig,
DollieOriginMap,
} from '@dollie/origins';
import { Change } from 'diff';
import { Volume } from 'memfs';
Expand Down Expand Up @@ -35,7 +34,7 @@ export type ConflictSolveResult = MergeBlock | 'ignored' | null;

export interface DollieGeneratorConfig {
origin?: DollieOriginConfig;
origins?: DollieOriginMap;
origins?: DollieOrigin[];
loader?: LoaderConfig;
getTemplateProps?: (questions: DollieQuestion[]) => Promise<DollieAnswers>;
conflictsSolver?: (data: ConflictSolverData) => Promise<ConflictSolveResult>;
Expand Down

0 comments on commit a2864bb

Please sign in to comment.