Skip to content

Commit de2f642

Browse files
Setup a New Single Package (#697)
* feat: publish single package * refactor: debugging export * refactor: group quotes related models * refactor(quote): add missing types export * chore: add rest of the models * refactor(models): remove unnecessary config * refactor: revert changes to the quote models * fix(models): adjust imports * refactor(models): remove debug files * refactor(presets): use workspace dependencies * feat(generators): include the new created package in the all-packages preset * fix(generators): fix ts errors * refactor(core): remove unnecessary config * refactor(all-packages): add missing files * refactor(core): remove unnecessary config * chore: add changesets * fix(all-packages): adjust readme --------- Co-authored-by: Brahma Reddy <brahmareddychlkl@gmail.com>
1 parent 4731cf9 commit de2f642

107 files changed

Lines changed: 7487 additions & 5224 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/curly-boats-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools-test-data/generators': minor
3+
---
4+
5+
Extend the test data model generator so the created ones are registered in the new single aggregated package.

.changeset/friendly-deers-allow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@commercetools-test-data/product-type': patch
3+
'@commercetools-test-data/type': patch
4+
---
5+
6+
Fix incorrect `import` statement.

.changeset/friendly-rice-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools-test-data/product': patch
3+
---
4+
5+
Use Graphql generated Typescript type instead of a manual one.

.changeset/pretty-rules-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools/composable-commerce-test-data': minor
3+
---
4+
5+
New package which acts as an aggregator for all the test data models so consumers just need to use one dependency.

generators/src/new-test-model/new-test-model.generator.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { execSync } from 'node:child_process';
12
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
23
import { dirname, join } from 'node:path';
34
import snakeCase from 'lodash/snakeCase';
@@ -6,6 +7,12 @@ import { render as renderTemplate } from 'squirrelly';
67
import { CodeGenerator } from '../types';
78
import { packageTemplatesData, modelTemplatesData } from './templates';
89

10+
type TExecCommandError = Error & {
11+
status?: number;
12+
stdout?: Buffer;
13+
stderr?: Buffer;
14+
};
15+
916
const servicesToTypePrefixMap = {
1017
core: 'TCore',
1118
ctp: 'TCtp',
@@ -114,7 +121,7 @@ export const newTestModelGenerator: CodeGenerator = {
114121
join(__dirname, '..', '..', '..', 'core', 'package.json')
115122
);
116123

117-
// 2. Generate the files
124+
// 2. Generate the model files
118125
const templatesData = {
119126
modelName,
120127
modelCodename,
@@ -188,6 +195,61 @@ export const newTestModelGenerator: CodeGenerator = {
188195
});
189196
}
190197

198+
// 3. Update the single package preset
199+
if (generationType === 'standalone') {
200+
const presetDirectoryPath = join(
201+
__dirname,
202+
'..',
203+
'..',
204+
'..',
205+
'presets',
206+
'all-packages'
207+
);
208+
// Generate the re-export proxy file
209+
writeFileSync(
210+
join(presetDirectoryPath, 'src', `${modelCodename}.ts`),
211+
`export * from '@commercetools-test-data/${modelCodename}';`
212+
);
213+
// Update package.json
214+
const presetPackageJson = (
215+
await import(join(presetDirectoryPath, 'package.json'))
216+
).default;
217+
presetPackageJson.files.push(modelCodename);
218+
presetPackageJson.preconstruct.entrypoints.push(`${modelCodename}.ts`);
219+
presetPackageJson.dependencies[
220+
`@commercetools-test-data/${modelCodename}`
221+
] = 'workspace:*';
222+
writeFileSync(
223+
join(presetDirectoryPath, 'package.json'),
224+
JSON.stringify(presetPackageJson, null, 2)
225+
);
226+
227+
// Run preconstruct to generate the proxy package.json file
228+
const rootDirectoryPath = join(__dirname, '..', '..', '..');
229+
try {
230+
execSync('npx preconstruct fix', {
231+
cwd: rootDirectoryPath,
232+
});
233+
} catch (err) {
234+
const error = err as TExecCommandError;
235+
throw new Error(
236+
`Failed to run "preconstruct fix": ${error.message} - Exit code: ${error.status} - stdout: ${error.stdout?.toString()} - stderr: ${error.stderr?.toString()} - cwd: ${rootDirectoryPath}`
237+
);
238+
}
239+
240+
// Run pnpm install to install the new package
241+
try {
242+
execSync('pnpm install', {
243+
cwd: rootDirectoryPath,
244+
});
245+
} catch (err) {
246+
const error = err as TExecCommandError;
247+
throw new Error(
248+
`Failed to run "pnpm install": ${error.message} - Exit code: ${error.status} - stdout: ${error.stdout?.toString()} - stderr: ${error.stderr?.toString()} - cwd: ${rootDirectoryPath}`
249+
);
250+
}
251+
}
252+
191253
console.log(
192254
"\n 🚀 All set! We've generated all files. You can now adjust them to your needs."
193255
);

models/product-type/src/attribute-localized-enum-value/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AttributeLocalizedEnumValue } from '@commercetools/platform-sdk';
2-
import { TLocalizedStringGraphql } from '@commercetools-test-data/commons/src';
2+
import { TLocalizedStringGraphql } from '@commercetools-test-data/commons';
33
import type { TBuilder } from '@commercetools-test-data/core';
44

55
export type TAttributeLocalizedEnumValue = AttributeLocalizedEnumValue;

models/product/src/product-data/types.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import {
33
ProductData,
44
ProductVariant,
55
SearchKeyword,
6-
CategoryOrderHints,
76
Category,
87
} from '@commercetools/platform-sdk';
98
import { TLocalizedStringGraphql } from '@commercetools-test-data/commons';
109
import type { TBuilder } from '@commercetools-test-data/core';
11-
import { ValueOf } from '@commercetools-test-data/core/src/@jackfranklin/test-data-bot';
10+
import { TCtpCategoryOrderHint } from '@commercetools-test-data/graphql-types';
1211

1312
// The base generator model. Consumers configure these fields.
1413
export type TProductData = Omit<ProductData, 'categories'> & {
@@ -25,11 +24,7 @@ export type TProductDataRest = Omit<ProductData, 'categories'> & {
2524
};
2625

2726
// This type only appears in the GraphQL representation
28-
export type TCategoryOrderHintGraphql = {
29-
categoryId: keyof CategoryOrderHints;
30-
orderHint: ValueOf<CategoryOrderHints>;
31-
__typename: 'CategoryOrderHint';
32-
};
27+
export type TCategoryOrderHintGraphql = TCtpCategoryOrderHint;
3328

3429
export type TCategoryReferenceGraphql = CategoryReference & {
3530
__typename: 'Reference';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Modules to generate test data for commercetools APIs",
55
"private": true,
66
"preconstruct": {
7-
"packages": ["core", "graphql-types", "models/*", "utils", "generators"]
7+
"packages": ["core", "graphql-types", "models/*", "utils", "generators", "presets/*"]
88
},
99
"scripts": {
1010
"preinstall": "npx only-allow pnpm",

0 commit comments

Comments
 (0)