Skip to content

Commit

Permalink
feat: ordering by configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSS committed Jun 30, 2022
1 parent cd79f13 commit c39c7e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/cliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export const optionDefinitions = [
multiple: true,
description: 'The prefix of custom user libraries',
},
{
name: 'groupOrder',
alias: 'g',
type: String,
multiple: true,
description: 'The group order should be organized',
},
{
name: 'staged',
type: Boolean,
Expand Down
8 changes: 3 additions & 5 deletions src/conductor/format-import-statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { ImportCategories } from '../types';

type CategoryEntry = [string, Map<string, string>];

const categoriesOrder = ['thirdParty', 'userLibrary', 'differentModule', 'sameModule']; // <<<<

export function formatImportStatements(importCategories: ImportCategories, lineEnding: string) {
const { separator } = getConfig();
const [first, ...otherCategories] = Object.entries(importCategories)
.filter(hasImports)
.sort(byCategoriesOrder)
.sort(byCategoriesOrder(getConfig().groupOrder))
.map((imports) => toImportBlock(imports, lineEnding));

let result = first || '';
Expand All @@ -21,8 +19,8 @@ export function formatImportStatements(importCategories: ImportCategories, lineE
return result;
}

function byCategoriesOrder([a]: CategoryEntry, [b]: CategoryEntry): number {
return categoriesOrder.indexOf(a) - categoriesOrder.indexOf(b);
function byCategoriesOrder(categoriesOrder: string[]) {
return ([a]: CategoryEntry, [b]: CategoryEntry): number => categoriesOrder.indexOf(a) - categoriesOrder.indexOf(b);
}

function hasImports([, imports]: CategoryEntry) {
Expand Down

0 comments on commit c39c7e8

Please sign in to comment.