Skip to content

Commit 1c32877

Browse files
committed
feat: sort library and sub-app names to reduce git merge conflicts
1 parent a0efb93 commit 1c32877

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

src/lib/library/library.factory.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
url,
1414
} from '@angular-devkit/schematics';
1515
import { parse } from 'jsonc-parser';
16-
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
16+
import { inPlaceSortByKeys, normalizeToKebabOrSnakeCase } from '../../utils';
1717
import {
1818
DEFAULT_LANGUAGE,
1919
DEFAULT_LIB_PATH,
@@ -133,6 +133,8 @@ function updateJestConfig(
133133
const packageKeyRegex = '^' + packageKey + '(|/.*)$';
134134
const packageRoot = join('<rootDir>' as Path, distRoot);
135135
jestOptions.moduleNameMapper[packageKeyRegex] = join(packageRoot, '$1');
136+
137+
inPlaceSortByKeys(jestOptions.moduleNameMapper);
136138
}
137139

138140
function updateNpmScripts(
@@ -181,6 +183,8 @@ function updateJestEndToEnd(options: LibraryOptions) {
181183
const packageRoot = '<rootDir>/../' + distRoot;
182184
jestOptions.moduleNameMapper[deepPackagePath] = packageRoot + '/$1';
183185
jestOptions.moduleNameMapper[packageKey] = packageRoot;
186+
187+
inPlaceSortByKeys(jestOptions.moduleNameMapper);
184188
},
185189
);
186190
};
@@ -238,6 +242,8 @@ function updateTsConfig(
238242
tsconfig.compilerOptions.paths[deepPackagePath] = [];
239243
}
240244
tsconfig.compilerOptions.paths[deepPackagePath].push(distRoot + '/*');
245+
246+
inPlaceSortByKeys(tsconfig.compilerOptions.paths);
241247
},
242248
);
243249
};
@@ -284,6 +290,8 @@ function addLibraryToCliOptions(
284290
);
285291
}
286292
optionsFile.projects[projectName] = project;
293+
294+
inPlaceSortByKeys(optionsFile.projects);
287295
},
288296
);
289297
};

src/lib/sub-app/sub-app.factory.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from '@angular-devkit/schematics';
1717
import { existsSync, readFileSync } from 'fs';
1818
import { parse, stringify } from 'comment-json';
19-
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
19+
import { inPlaceSortByKeys, normalizeToKebabOrSnakeCase } from '../../utils';
2020
import {
2121
DEFAULT_APPS_PATH,
2222
DEFAULT_APP_NAME,
@@ -150,6 +150,8 @@ function updateTsConfig() {
150150
if (!tsconfig.compilerOptions.paths) {
151151
tsconfig.compilerOptions.paths = {};
152152
}
153+
154+
inPlaceSortByKeys(tsconfig.compilerOptions.paths);
153155
},
154156
);
155157
};
@@ -324,6 +326,8 @@ function addAppsToCliOptions(
324326
);
325327
}
326328
optionsFile.projects[projectName] = project;
329+
330+
inPlaceSortByKeys(optionsFile.projects);
327331
},
328332
);
329333
};

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './name.parser';
77
export * from './path.solver';
88
export * from './source-root.helpers';
99
export * from './formatting';
10+
export * from './object-sorting';

src/utils/object-sorting.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* In-place sort object entities by their keys so that it can be serialized to json with sorted order.
3+
* @param object
4+
* @returns The original object with modified entities order.
5+
*/
6+
export function inPlaceSortByKeys(object: Record<string, any>): Record<string, any> {
7+
const sorted: Record<string, any> = {};
8+
9+
const keys = Object.keys(object);
10+
keys.sort();
11+
for (const key of keys) {
12+
sorted[key] = object[key];
13+
delete object[key];
14+
}
15+
16+
return Object.assign(object, sorted);
17+
}

test/utils/object-sorting.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { inPlaceSortByKeys } from '../../src/utils/object-sorting';
2+
3+
4+
describe('inPlaceSortByKeys', () => {
5+
it('should in-place sort the entities by their keys', () => {
6+
const input = { z: 'z', b: 'b', c: 'c', a: 'a', };
7+
expect(Object.keys(input)).toEqual(['z', 'b', 'c', 'a']);
8+
9+
const got = inPlaceSortByKeys(input);
10+
11+
expect(got).toBe(input); // Same object
12+
expect(Object.keys(got)).toEqual(['a', 'b', 'c', 'z']);
13+
})
14+
})

0 commit comments

Comments
 (0)