@teambit/workspace-config-files 1.0.106 → 1.0.108

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,146 @@
1
+ import { Environment, ExecutionContext } from '@teambit/envs';
2
+ import { EnvMapValue } from './workspace-config-files.main.runtime';
3
+ import { WrittenConfigFile } from './writers';
4
+
5
+ export type ConfigFile = {
6
+ /**
7
+ * Name of the config file.
8
+ * supports also using `{hash}` in the name, which will be replaced by the hash of the config file.
9
+ */
10
+ name: string;
11
+ /**
12
+ * Content of the config file.
13
+ * I.E the content of the tsconfig.json file.
14
+ */
15
+ content: string;
16
+ /**
17
+ * Hash of the config file.
18
+ */
19
+ hash?: string;
20
+ };
21
+
22
+ export type ExtendingConfigFileAdditionalProp = {
23
+ /**
24
+ * the config file that this config file extends.
25
+ */
26
+ extendingTarget: WrittenConfigFile;
27
+
28
+ /**
29
+ * When replacing the config file name with the actual path of the config file, use absolute paths.
30
+ */
31
+ useAbsPaths?: boolean;
32
+ };
33
+
34
+ export type ExtendingConfigFile = ConfigFile & ExtendingConfigFileAdditionalProp;
35
+
36
+ export type PostProcessExtendingConfigFilesArgs = {
37
+ workspaceDir: string;
38
+ configsRootDir: string;
39
+ extendingConfigFile: ExtendingConfigFile;
40
+ /**
41
+ * Paths that the file will be written to.
42
+ */
43
+ paths: string[];
44
+ envMapValue: EnvMapValue;
45
+ /**
46
+ * This is a flag for backward compatibility
47
+ * We used to return string from the post process, so old versions of bit only knows to handle string results
48
+ * while in new version we support getting array of objects
49
+ * we need to know if bit the user is using support the new format or not
50
+ */
51
+ supportSpecificPathChange?: boolean;
52
+ };
53
+
54
+ export type GenerateExtendingConfigFilesArgs = {
55
+ workspaceDir: string;
56
+ configsRootDir: string;
57
+ writtenConfigFiles: WrittenConfigFile[];
58
+ envMapValue: EnvMapValue;
59
+ };
60
+
61
+ export type PostProcessExtendingConfigFilesOneFile = {
62
+ path: string;
63
+ content: string;
64
+ };
65
+
66
+ export type MergeConfigFilesFunc = (configFile: ConfigFile, configFile2: ConfigFile) => string;
67
+ export interface ConfigWriterEntry {
68
+ /**
69
+ * Id is used for few things:
70
+ * 1. merge/post process different configs files (from different envs) together.
71
+ * 2. filter the config writer by the cli when using --writers flag.
72
+ */
73
+ id: string;
74
+
75
+ /**
76
+ * Name of the config writer.
77
+ * used for outputs and logging.
78
+ */
79
+ name: string;
80
+
81
+ /**
82
+ * Get's the component env and return the config file content
83
+ * for example the eslint config to tsconfig.
84
+ * This also enable to return a hash of the config file, which will be used to determine if
85
+ * 2 config files are the same.
86
+ * If the hash is not provided, the content will be used as the hash.
87
+ * This enables the specific config type to ignore specific fields when calculating the
88
+ * hash in order to ignore theses fields when determining if 2 config files are the same.
89
+ * The calc function also get the target directory of the config file (calculated by this aspect) as sometime there
90
+ * is a need to change the config file content based on the target directory.
91
+ * for example, change the includes/excludes paths to be relative to the target directory.
92
+ * The calc can return undefined if the config file is not relevant for the component. or not supported by the subscriber.
93
+ * for example if the component uses babel to compile, then tsconfig is not relevant.
94
+ * @param env
95
+ */
96
+ calcConfigFiles(executionContext: ExecutionContext, env: Environment, dir: string): ConfigFile[] | undefined;
97
+
98
+ /**
99
+ * Provide a function that knows how to merge 2 config files together.
100
+ * This is used when 2 different envs generate the same config file hash.
101
+ * sometime we want to merge the 2 config files together.
102
+ * @param configFile
103
+ * @param configFile2
104
+ */
105
+ mergeConfigFiles?: MergeConfigFilesFunc;
106
+
107
+ /**
108
+ * This will be used to generate an extending file content.
109
+ * For example, the tsconfig.json file will extend the real tsconfig.{hash}.json file (that were coming from the env).
110
+ * That way we can avoid writing the same config file multiple times.
111
+ * It also reduces the risk of the user manually change the config file and then the changes will be lost.
112
+ * This function support returning a file with content with a dsl using `{}` to replace the config file name.
113
+ * for example:
114
+ * content = `{
115
+ * "extends": {configFile.name},
116
+ * }`
117
+ */
118
+ generateExtendingFile(args: GenerateExtendingConfigFilesArgs): ExtendingConfigFile | undefined;
119
+
120
+ /**
121
+ * This enables the writer to do some post processing after the extending config files were calculated and deduped.
122
+ * this is important in case when we need to change a config file / extending config file after it was calculated
123
+ * based on all the environments in the ws
124
+ * or based on other config files that were written.
125
+ * @param args
126
+ */
127
+ postProcessExtendingConfigFiles?(
128
+ args: PostProcessExtendingConfigFilesArgs
129
+ ): Promise<string | Array<PostProcessExtendingConfigFilesOneFile> | undefined>;
130
+
131
+ /**
132
+ * Find all the files that are relevant for the config type.
133
+ * This is used to clean / delete these files
134
+ * This should return an array of glob patterns (that will passed to the globby/minimatch library)
135
+ * @param dir
136
+ */
137
+ patterns: string[];
138
+
139
+ /**
140
+ * A function to determine if a file was generated by bit.
141
+ * This is useful to check if the config file was generated by bit to prevent delete user's file.
142
+ * @param filePath
143
+ * @returns
144
+ */
145
+ isBitGenerated?: (filePath: string) => boolean;
146
+ }
@@ -0,0 +1,84 @@
1
+ import { EnvContext, EnvHandler } from '@teambit/envs';
2
+ import { findIndex } from 'lodash';
3
+ import { ConfigWriterEntry } from './config-writer-entry';
4
+
5
+ export type ConfigWriterHandler = {
6
+ handler: EnvHandler<ConfigWriterEntry>;
7
+ name: string;
8
+ };
9
+
10
+ /**
11
+ * create and maintain config writer list for component dev environments.
12
+ */
13
+ export class ConfigWriterList {
14
+ constructor(private _writers: ConfigWriterHandler[]) {}
15
+
16
+ /**
17
+ * list all congig writer handlers in the list.
18
+ */
19
+ get writers() {
20
+ return this._writers;
21
+ }
22
+
23
+ private initiateWriters(writers: ConfigWriterHandler[], context: EnvContext): ConfigWriterEntry[] {
24
+ return writers.map((task) => {
25
+ return task.handler(context);
26
+ });
27
+ }
28
+
29
+ /**
30
+ * add writers to the list.
31
+ */
32
+ add(writers: ConfigWriterHandler[]) {
33
+ this._writers = this._writers.concat(writers);
34
+ return this;
35
+ }
36
+
37
+ /**
38
+ * remove writers from the list.
39
+ */
40
+ remove(writerNames: string[]) {
41
+ this._writers = this._writers.filter((writer) => {
42
+ return !writerNames.includes(writer.name);
43
+ });
44
+ return this;
45
+ }
46
+
47
+ /**
48
+ * replace writers in the list.
49
+ */
50
+ replace(writers: ConfigWriterHandler[]) {
51
+ writers.forEach((writer) => {
52
+ // Find Writer index using _.findIndex
53
+ const matchIndex = findIndex(this._writers, (origWriter) => {
54
+ return origWriter.name === writer.name;
55
+ });
56
+ if (matchIndex !== -1) {
57
+ // Replace Writer at index using native splice
58
+ this._writers.splice(matchIndex, 1, writer);
59
+ }
60
+ });
61
+ return this;
62
+ }
63
+
64
+ /**
65
+ * return a new list with the writers from the args added.
66
+ * @param pipeline
67
+ * @returns
68
+ */
69
+ concat(writerList: ConfigWriterList) {
70
+ return new ConfigWriterList(this._writers.concat(writerList.writers));
71
+ }
72
+
73
+ /**
74
+ * compute the list.
75
+ */
76
+ compute(context: EnvContext): ConfigWriterEntry[] {
77
+ const writerEntries = this.initiateWriters(this._writers, context);
78
+ return writerEntries;
79
+ }
80
+
81
+ static from(writersHandlers: ConfigWriterHandler[]): ConfigWriterList {
82
+ return new ConfigWriterList(writersHandlers);
83
+ }
84
+ }
@@ -0,0 +1,349 @@
1
+ import { DedupedPaths, dedupePaths } from './dedup-paths';
2
+ import { ExtendingConfigFilesMap } from './writers';
3
+
4
+ const envCompsDirsMap = {
5
+ 'teambit.harmony/node': {
6
+ id: 'teambit.harmony/node',
7
+ paths: [
8
+ 'react/apps/react-app-types',
9
+ 'compilation/babel-compiler',
10
+ 'compilation/compiler-task',
11
+ 'mdx/compilers/mdx-compiler',
12
+ 'mdx/compilers/mdx-multi-compiler',
13
+ 'compilation/compilers/multi-compiler',
14
+ 'defender/eslint-linter',
15
+ 'mdx/generator/mdx-starters',
16
+ 'mdx/generator/mdx-templates',
17
+ 'react/generator/react-starters',
18
+ 'react/generator/react-templates',
19
+ 'defender/jest-tester',
20
+ 'react/jest/react-jest',
21
+ 'defender/linter-task',
22
+ 'defender/mocha-tester',
23
+ 'compilation/modules/babel-file-transpiler',
24
+ 'defender/prettier-formatter',
25
+ 'preview/react-preview',
26
+ 'defender/tester-task',
27
+ 'typescript/typescript-compiler',
28
+ 'webpack/webpack-bundler',
29
+ 'webpack/webpack-dev-server',
30
+ 'react/webpack/react-webpack',
31
+ ],
32
+ },
33
+ 'teambit.react/react-env@0.0.44': {
34
+ id: 'teambit.react/react-env@0.0.44',
35
+ paths: ['test-new-envs-app/apps/test-app', 'test-new-env/ui/button2'],
36
+ },
37
+ 'teambit.react/react': {
38
+ id: 'teambit.react/react',
39
+ paths: ['docs/docs-template', 'react/mounter'],
40
+ },
41
+ 'teambit.envs/env': {
42
+ id: 'teambit.envs/env',
43
+ paths: [
44
+ 'react/examples/my-react-env',
45
+ 'mdx/mdx-env',
46
+ 'node/node',
47
+ 'node/node-env-extension',
48
+ 'react/react-env',
49
+ 'react/react-env-extension',
50
+ ],
51
+ },
52
+ 'teambit.mdx/mdx-env@0.0.6': {
53
+ id: 'teambit.mdx/mdx-env@0.0.6',
54
+ paths: ['test-new-env/mdx/content-comp'],
55
+ },
56
+ 'teambit.node/node@0.0.16': {
57
+ id: 'teambit.node/node@0.0.16',
58
+ paths: ['test-new-env/node/node-comp1', 'test-new-env/node/node-comp2'],
59
+ },
60
+ 'teambit.react/examples/my-react-env@0.0.39': {
61
+ id: 'teambit.react/examples/my-react-env@0.0.39',
62
+ paths: ['test-new-env/ui/button'],
63
+ },
64
+ };
65
+
66
+ const tsExtendingConfigFilesMap: ExtendingConfigFilesMap = {
67
+ '816b7f584991ff21a7682ef8a4229ebf312c457f': {
68
+ extendingConfigFile: {
69
+ useAbsPaths: false,
70
+ content:
71
+ '// bit-generated-typescript-config\n' +
72
+ '\n' +
73
+ '{\n' +
74
+ ' "extends": "/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/tsconfig.bit.4957ea3122e57ea3302aadd885eed84127d9c54b.json"\n' +
75
+ '}',
76
+ name: 'tsconfig.json',
77
+ extendingTarget: {
78
+ hash: '4957ea3122e57ea3302aadd885eed84127d9c54b',
79
+ content: 'does not matter',
80
+ name: 'tsconfig.bit.4957ea3122e57ea3302aadd885eed84127d9c54b.json',
81
+ filePath:
82
+ '/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/tsconfig.bit.4957ea3122e57ea3302aadd885eed84127d9c54b.json',
83
+ },
84
+
85
+ hash: '816b7f584991ff21a7682ef8a4229ebf312c457f',
86
+ },
87
+ envIds: ['teambit.harmony/node', 'teambit.react/react'],
88
+ },
89
+ '00330a9c547353867d519c34f47e17ddc9f161d1': {
90
+ extendingConfigFile: {
91
+ useAbsPaths: false,
92
+ content:
93
+ '// bit-generated-typescript-config\n' +
94
+ '\n' +
95
+ '{\n' +
96
+ ' "extends": "/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/tsconfig.bit.17b99a1071b1d2d86ed04ebce68903b82403f278.json"\n' +
97
+ '}',
98
+ name: 'tsconfig.json',
99
+ extendingTarget: {
100
+ hash: '17b99a1071b1d2d86ed04ebce68903b82403f278',
101
+ content: 'does not matter',
102
+ name: 'tsconfig.bit.17b99a1071b1d2d86ed04ebce68903b82403f278.json',
103
+ filePath:
104
+ '/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/tsconfig.bit.17b99a1071b1d2d86ed04ebce68903b82403f278.json',
105
+ },
106
+ hash: '00330a9c547353867d519c34f47e17ddc9f161d1',
107
+ },
108
+ envIds: ['teambit.react/react-env@0.0.44', 'teambit.react/examples/my-react-env@0.0.39'],
109
+ },
110
+ '3c5960bcad98570b98834a5c37f47dd4729dbef0': {
111
+ extendingConfigFile: {
112
+ useAbsPaths: false,
113
+ content:
114
+ '// bit-generated-typescript-config\n' +
115
+ '\n' +
116
+ '{\n' +
117
+ ' "extends": "/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/tsconfig.bit.dab521d0914afe64de248743e05a169cf9b4b50c.json"\n' +
118
+ '}',
119
+ name: 'tsconfig.json',
120
+ extendingTarget: {
121
+ hash: 'dab521d0914afe64de248743e05a169cf9b4b50c',
122
+ content: 'does not matter',
123
+ name: 'tsconfig.bit.dab521d0914afe64de248743e05a169cf9b4b50c.json',
124
+ filePath:
125
+ '/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/tsconfig.bit.dab521d0914afe64de248743e05a169cf9b4b50c.json',
126
+ },
127
+ hash: '3c5960bcad98570b98834a5c37f47dd4729dbef0',
128
+ },
129
+ envIds: ['teambit.node/node@0.0.16'],
130
+ },
131
+ };
132
+
133
+ const tsExpectedDedupedPaths: DedupedPaths = [
134
+ {
135
+ fileHash: '00330a9c547353867d519c34f47e17ddc9f161d1',
136
+ paths: ['test-new-envs-app', 'test-new-env/ui'],
137
+ },
138
+ {
139
+ fileHash: '3c5960bcad98570b98834a5c37f47dd4729dbef0',
140
+ paths: ['test-new-env/node'],
141
+ },
142
+ {
143
+ fileHash: '816b7f584991ff21a7682ef8a4229ebf312c457f',
144
+ paths: ['.'],
145
+ },
146
+ ];
147
+
148
+ const eslintExtendingConfigFilesMap: ExtendingConfigFilesMap = {
149
+ '8810839e74a9c41694bb5a2a8587dcae71dc389d': {
150
+ extendingConfigFile: {
151
+ useAbsPaths: false,
152
+ content:
153
+ '// bit-generated-eslint-config\n' +
154
+ '{\n' +
155
+ ' "extends": [\n' +
156
+ ' "/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.eslintrc.bit.be8facfbdcf1db685d5020f8612d8c2c3ac2eb7c.json"\n' +
157
+ ' ]\n' +
158
+ '}',
159
+ name: '.eslintrc.json',
160
+ extendingTarget: {
161
+ hash: 'be8facfbdcf1db685d5020f8612d8c2c3ac2eb7c',
162
+ content: 'does not matter',
163
+ name: '.eslintrc.bit.be8facfbdcf1db685d5020f8612d8c2c3ac2eb7c.json',
164
+ filePath:
165
+ '/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.eslintrc.bit.be8facfbdcf1db685d5020f8612d8c2c3ac2eb7c.json',
166
+ },
167
+ hash: '8810839e74a9c41694bb5a2a8587dcae71dc389d',
168
+ },
169
+ envIds: ['teambit.harmony/node', 'teambit.react/react', 'teambit.envs/env'],
170
+ },
171
+ ff6ea265e6f21ce25f7985570beef37dd957d0dc: {
172
+ extendingConfigFile: {
173
+ useAbsPaths: false,
174
+ content:
175
+ '// bit-generated-eslint-config\n' +
176
+ '{\n' +
177
+ ' "extends": [\n' +
178
+ ' "/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.eslintrc.bit.e5ca5528c64e0442b05b949fea42cdda4243d840.json"\n' +
179
+ ' ]\n' +
180
+ '}',
181
+ name: '.eslintrc.json',
182
+ extendingTarget: {
183
+ hash: 'e5ca5528c64e0442b05b949fea42cdda4243d840',
184
+ content: 'does not matter',
185
+ name: '.eslintrc.bit.e5ca5528c64e0442b05b949fea42cdda4243d840.json',
186
+ filePath:
187
+ '/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.eslintrc.bit.e5ca5528c64e0442b05b949fea42cdda4243d840.json',
188
+ },
189
+ hash: 'ff6ea265e6f21ce25f7985570beef37dd957d0dc',
190
+ },
191
+ envIds: ['teambit.react/react-env@0.0.44'],
192
+ },
193
+ '91021f2c973a2940c70b75447639e4ea2a799955': {
194
+ extendingConfigFile: {
195
+ useAbsPaths: false,
196
+ content:
197
+ '// bit-generated-eslint-config\n' +
198
+ '{\n' +
199
+ ' "extends": [\n' +
200
+ ' "/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.eslintrc.bit.7f229bf5ab41e8bfe61916de6c68f9c14c76f23e.json"\n' +
201
+ ' ]\n' +
202
+ '}',
203
+ name: '.eslintrc.json',
204
+ extendingTarget: {
205
+ hash: '7f229bf5ab41e8bfe61916de6c68f9c14c76f23e',
206
+ content: 'does not matter',
207
+ name: '.eslintrc.bit.7f229bf5ab41e8bfe61916de6c68f9c14c76f23e.json',
208
+ filePath:
209
+ '/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.eslintrc.bit.7f229bf5ab41e8bfe61916de6c68f9c14c76f23e.json',
210
+ },
211
+ hash: '91021f2c973a2940c70b75447639e4ea2a799955',
212
+ },
213
+ envIds: ['teambit.mdx/mdx-env@0.0.6', 'teambit.react/examples/my-react-env@0.0.39'],
214
+ },
215
+ f1743b227e588db0c59d4a43171653e6c4262816: {
216
+ extendingConfigFile: {
217
+ useAbsPaths: false,
218
+ content:
219
+ '// bit-generated-eslint-config\n' +
220
+ '{\n' +
221
+ ' "extends": [\n' +
222
+ ' "/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.eslintrc.bit.c503f7386e2a637d91c74f7df90d8fafe79c4378.json"\n' +
223
+ ' ]\n' +
224
+ '}',
225
+ name: '.eslintrc.json',
226
+ extendingTarget: {
227
+ hash: 'c503f7386e2a637d91c74f7df90d8fafe79c4378',
228
+ content: 'does not matter',
229
+ name: '.eslintrc.bit.c503f7386e2a637d91c74f7df90d8fafe79c4378.json',
230
+ filePath:
231
+ '/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.eslintrc.bit.c503f7386e2a637d91c74f7df90d8fafe79c4378.json',
232
+ },
233
+ hash: 'f1743b227e588db0c59d4a43171653e6c4262816',
234
+ },
235
+ envIds: ['teambit.node/node@0.0.16'],
236
+ },
237
+ };
238
+
239
+ const eslintExpectedDedupedPaths: DedupedPaths = [
240
+ {
241
+ fileHash: 'ff6ea265e6f21ce25f7985570beef37dd957d0dc',
242
+ paths: ['test-new-envs-app', 'test-new-env/ui/button2'],
243
+ },
244
+ {
245
+ fileHash: '91021f2c973a2940c70b75447639e4ea2a799955',
246
+ paths: ['test-new-env/mdx', 'test-new-env/ui/button'],
247
+ },
248
+ {
249
+ fileHash: 'f1743b227e588db0c59d4a43171653e6c4262816',
250
+ paths: ['test-new-env/node'],
251
+ },
252
+ {
253
+ fileHash: '8810839e74a9c41694bb5a2a8587dcae71dc389d',
254
+ paths: ['.'],
255
+ },
256
+ ];
257
+
258
+ const prettierExtendingConfigFilesMap: ExtendingConfigFilesMap = {
259
+ '082f546b2555ea89e7063b20de47c039d387fc74': {
260
+ extendingConfigFile: {
261
+ useAbsPaths: false,
262
+ content:
263
+ '// bit-generated-prettier-config\n' +
264
+ 'module.exports = {\n' +
265
+ " ...require('/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.prettierrc.bit.e4882af8861bcf5b0147891d8b70b40a10428881.cjs')\n" +
266
+ '}',
267
+ name: '.prettierrc.cjs',
268
+ extendingTarget: {
269
+ hash: 'e4882af8861bcf5b0147891d8b70b40a10428881',
270
+ content: 'does not matter',
271
+ name: '.prettierrc.bit.e4882af8861bcf5b0147891d8b70b40a10428881.cjs',
272
+ filePath:
273
+ '/Users/giladshoham/dev/temp/new-react-18-config-files/node_modules/.cache/.prettierrc.bit.e4882af8861bcf5b0147891d8b70b40a10428881.cjs',
274
+ },
275
+ hash: '082f546b2555ea89e7063b20de47c039d387fc74',
276
+ },
277
+ envIds: [
278
+ 'teambit.harmony/node',
279
+ 'teambit.react/react-env@0.0.44',
280
+ 'teambit.react/react',
281
+ 'teambit.envs/env',
282
+ 'teambit.mdx/mdx-env@0.0.6',
283
+ 'teambit.node/node@0.0.16',
284
+ 'teambit.react/examples/my-react-env@0.0.39',
285
+ ],
286
+ },
287
+ };
288
+
289
+ const prettierExpectedDedupedPaths: DedupedPaths = [
290
+ {
291
+ fileHash: '082f546b2555ea89e7063b20de47c039d387fc74',
292
+ paths: ['.'],
293
+ },
294
+ ];
295
+
296
+ describe('Workspace Config files - dedupe paths', function () {
297
+ describe('dedupePaths', () => {
298
+ describe('ts example', () => {
299
+ let result: DedupedPaths;
300
+ beforeAll(async () => {
301
+ // @ts-ignore (we don't really care about the env itself here)
302
+ result = dedupePaths(tsExtendingConfigFilesMap, envCompsDirsMap);
303
+ });
304
+
305
+ it('should reduce files to minimum necessary', async () => {
306
+ expect(result.length).toEqual(tsExpectedDedupedPaths.length);
307
+ });
308
+
309
+ it('should place files in correct folders', async () => {
310
+ // @ts-ignore
311
+ expect(result).toMatchObject(tsExpectedDedupedPaths);
312
+ });
313
+ });
314
+
315
+ describe('eslint example', () => {
316
+ let result: DedupedPaths;
317
+ beforeAll(async () => {
318
+ // @ts-ignore (we don't really care about the env itself here)
319
+ result = dedupePaths(eslintExtendingConfigFilesMap, envCompsDirsMap);
320
+ });
321
+
322
+ it('should reduce files to minimum necessary', async () => {
323
+ expect(result.length).toEqual(eslintExpectedDedupedPaths.length);
324
+ });
325
+
326
+ it('should place files in correct folders', async () => {
327
+ // @ts-ignore
328
+ expect(result).toMatchObject(eslintExpectedDedupedPaths);
329
+ });
330
+ });
331
+
332
+ describe('prettier example', () => {
333
+ let result: DedupedPaths;
334
+ beforeAll(async () => {
335
+ // @ts-ignore (we don't really care about the env itself here)
336
+ result = dedupePaths(prettierExtendingConfigFilesMap, envCompsDirsMap);
337
+ });
338
+
339
+ it('should reduce files to minimum necessary', async () => {
340
+ expect(result.length).toEqual(prettierExpectedDedupedPaths.length);
341
+ });
342
+
343
+ it('should place files in correct folders', async () => {
344
+ // @ts-ignore
345
+ expect(result).toMatchObject(prettierExpectedDedupedPaths);
346
+ });
347
+ });
348
+ });
349
+ });
package/dedup-paths.ts ADDED
@@ -0,0 +1,120 @@
1
+ import { invertBy, uniq } from 'lodash';
2
+ import { dirname } from 'path';
3
+ import { PathLinuxRelative } from '@teambit/legacy/dist/utils/path';
4
+ import { CompPathExtendingHashMap, EnvCompsDirsMap } from './workspace-config-files.main.runtime';
5
+ import { ExtendingConfigFilesMap } from './writers';
6
+
7
+ export type DedupedPaths = Array<{
8
+ fileHash: string;
9
+ paths: string[];
10
+ }>;
11
+
12
+ function getAllPossibleDirsFromPaths(paths: PathLinuxRelative[]): PathLinuxRelative[] {
13
+ const dirs = paths.map((p) => getAllParentsDirOfPath(p)).flat();
14
+ dirs.push('.'); // add the root dir
15
+ return uniq(dirs);
16
+ }
17
+
18
+ function getAllParentsDirOfPath(p: PathLinuxRelative): PathLinuxRelative[] {
19
+ const all: string[] = [];
20
+ let current = p;
21
+ while (current !== '.') {
22
+ all.push(current);
23
+ current = dirname(current);
24
+ }
25
+ return all;
26
+ }
27
+
28
+ export function buildCompPathExtendingHashMap(
29
+ extendingConfigFilesMap: ExtendingConfigFilesMap,
30
+ envCompsDirsMap: EnvCompsDirsMap
31
+ ): CompPathExtendingHashMap {
32
+ const map = Object.entries(extendingConfigFilesMap).reduce((acc, [hash, { envIds }]) => {
33
+ envIds.forEach((envId) => {
34
+ const envCompDirs = envCompsDirsMap[envId];
35
+ envCompDirs.paths.forEach((compPath) => {
36
+ acc[compPath] = hash;
37
+ });
38
+ });
39
+ return acc;
40
+ }, {});
41
+ return map;
42
+ }
43
+
44
+ /**
45
+ * easier to understand by an example:
46
+ * input:
47
+ * [
48
+ * { fileHash: hash1, paths: [ui/button, ui/form] },
49
+ * { fileHash: hash2, paths: [p/a1, p/a2] },
50
+ * { fileHash: hash3, paths: [p/n1] },
51
+ * ]
52
+ *
53
+ * output:
54
+ * [
55
+ * { fileHash: hash1, paths: [ui] },
56
+ * { fileHash: hash2, paths: [p] },
57
+ * { fileHash: hash3, paths: [p/n1] },
58
+ * ]
59
+ *
60
+ * the goal is to minimize the amount of files to write per env if possible.
61
+ * when multiple components of the same env share a root-dir, then, it's enough to write a file in that shared dir.
62
+ * if in a shared-dir, some components using env1 and some env2, it finds the env that has the max number of
63
+ * components, this env will be optimized. other components, will have the files written inside their dirs.
64
+ */
65
+ export function dedupePaths(
66
+ extendingConfigFilesMap: ExtendingConfigFilesMap,
67
+ envCompsDirsMap: EnvCompsDirsMap
68
+ ): DedupedPaths {
69
+ const rootDir = '.';
70
+
71
+ const compPathExtendingHashMap = buildCompPathExtendingHashMap(extendingConfigFilesMap, envCompsDirsMap);
72
+ const allPaths = Object.keys(compPathExtendingHashMap);
73
+ const allPossibleDirs = getAllPossibleDirsFromPaths(allPaths);
74
+
75
+ const allPathsPerFileHash: { [path: string]: string | null } = {}; // null when parent-dir has same amount of comps per env.
76
+
77
+ const calculateBestFileForDir = (dir: string) => {
78
+ if (compPathExtendingHashMap[dir]) {
79
+ // it's the component dir, so it's the file that should be written.
80
+ allPathsPerFileHash[dir] = compPathExtendingHashMap[dir];
81
+ return;
82
+ }
83
+ const allPathsShareSameDir = dir === rootDir ? allPaths : allPaths.filter((p) => p.startsWith(`${dir}/`));
84
+ const countPerFileHash: { [fileHash: string]: number } = {};
85
+ allPathsShareSameDir.forEach((p) => {
86
+ const fileHash = compPathExtendingHashMap[p];
87
+ if (countPerFileHash[fileHash]) countPerFileHash[fileHash] += 1;
88
+ else countPerFileHash[fileHash] = 1;
89
+ });
90
+ const max = Math.max(...Object.values(countPerFileHash));
91
+ const fileHashWithMax = Object.keys(countPerFileHash).filter((fileHash) => countPerFileHash[fileHash] === max);
92
+ if (!fileHashWithMax.length) throw new Error(`must be at least one fileHash related to path "${dir}"`);
93
+ if (fileHashWithMax.length > 1) allPathsPerFileHash[dir] = null;
94
+ else allPathsPerFileHash[dir] = fileHashWithMax[0];
95
+ };
96
+
97
+ allPossibleDirs.forEach((dirPath) => {
98
+ calculateBestFileForDir(dirPath);
99
+ });
100
+
101
+ // this is the actual deduping. if found a shorter path with the same env, then no need for this path.
102
+ // in other words, return only the paths that their parent is null or has a different env.
103
+ const dedupedPathsPerFileHash = Object.keys(allPathsPerFileHash).reduce((acc, current) => {
104
+ if (allPathsPerFileHash[current] && allPathsPerFileHash[dirname(current)] !== allPathsPerFileHash[current]) {
105
+ acc[current] = allPathsPerFileHash[current];
106
+ }
107
+
108
+ return acc;
109
+ }, {});
110
+ // rootDir parent is always rootDir, so leave it as is.
111
+ if (allPathsPerFileHash[rootDir]) dedupedPathsPerFileHash[rootDir] = allPathsPerFileHash[rootDir];
112
+
113
+ const fileHashPerDedupedPaths = invertBy(dedupedPathsPerFileHash);
114
+
115
+ const dedupedPaths = Object.keys(fileHashPerDedupedPaths).map((fileHash) => ({
116
+ fileHash,
117
+ paths: fileHashPerDedupedPaths[fileHash],
118
+ }));
119
+ return dedupedPaths;
120
+ }