Browse Source

chore: Add script to check for bad core type defs

Relates to #1121
Michael Bromley 4 years ago
parent
commit
7880cb7f76
2 changed files with 84 additions and 1 deletions
  1. 2 1
      package.json
  2. 82 0
      scripts/check-core-type-defs.ts

+ 2 - 1
package.json

@@ -16,12 +16,13 @@
     "docs:update-build-info": "ts-node scripts/docs/update-build-info.ts",
     "docs:update-build-info": "ts-node scripts/docs/update-build-info.ts",
     "docs:build": "yarn docs:generate-graphql-docs && yarn docs:generate-typescript-docs && yarn docs:update-build-info",
     "docs:build": "yarn docs:generate-graphql-docs && yarn docs:generate-typescript-docs && yarn docs:update-build-info",
     "codegen": "tsc -p scripts/codegen/plugins && ts-node scripts/codegen/generate-graphql-types.ts",
     "codegen": "tsc -p scripts/codegen/plugins && ts-node scripts/codegen/generate-graphql-types.ts",
-    "version": "yarn check-imports && yarn check-angular-versions && yarn build && yarn generate-changelog && git add CHANGELOG.md && git add */version.ts",
+    "version": "yarn check-imports && yarn check-angular-versions && yarn build && yarn check-core-type-defs && yarn generate-changelog && git add CHANGELOG.md && git add */version.ts",
     "dev-server:start": "cd packages/dev-server && yarn start",
     "dev-server:start": "cd packages/dev-server && yarn start",
     "test": "lerna run test --stream --no-bail",
     "test": "lerna run test --stream --no-bail",
     "e2e": "lerna run e2e --stream --no-bail",
     "e2e": "lerna run e2e --stream --no-bail",
     "build": "lerna run build",
     "build": "lerna run build",
     "check-imports": "ts-node scripts/check-imports.ts",
     "check-imports": "ts-node scripts/check-imports.ts",
+    "check-core-type-defs": "ts-node scripts/check-core-type-defs.ts",
     "check-angular-versions": "ts-node scripts/check-angular-versions.ts",
     "check-angular-versions": "ts-node scripts/check-angular-versions.ts",
     "generate-changelog": "ts-node scripts/changelogs/generate-changelog.ts",
     "generate-changelog": "ts-node scripts/changelogs/generate-changelog.ts",
     "publish-release": "lerna publish -m \"chore: Publish %s\" --no-push --force-publish",
     "publish-release": "lerna publish -m \"chore: Publish %s\" --no-push --force-publish",

+ 82 - 0
scripts/check-core-type-defs.ts

@@ -0,0 +1,82 @@
+/* tslint:disable:no-console */
+import fs from 'fs';
+import path from 'path';
+
+/**
+ * For some unknown reason, a v1.2.2 of @vendure/core included incorrect type definitions for some types.
+ * Namely, _some_ of the `ConfigurableOperationDef` types had their specific string literal & enum types
+ * replaced with just `string`, which caused the published package to fail to build.
+ *
+ * Example:
+ * [dummy-payment-method-handler.d.ts](https://unpkg.com/@vendure/core@1.2.2/dist/config/payment/dummy-payment-method-handler.d.ts)
+ * ```
+ * export declare const dummyPaymentHandler: PaymentMethodHandler<{
+ *     automaticSettle: {
+ *         type: string;
+ *         label: {
+ *             languageCode: any;
+ *             value: string;
+ *         }[];
+ *         description: {
+ *             languageCode: any;
+ *             value: string;
+ *         }[];
+ *         required: true;
+ *         defaultValue: boolean;
+ *     };
+ * }>;
+ * ```
+ *
+ * Should be:
+ * ```ts
+ * export declare const dummyPaymentHandler: PaymentMethodHandler<{
+ *     automaticSettle: {
+ *         type: "boolean";
+ *         label: {
+ *             languageCode: LanguageCode.en;
+ *             value: string;
+ *         }[];
+ *         description: {
+ *             languageCode: LanguageCode.en;
+ *             value: string;
+ *         }[];
+ *         required: true;
+ *         defaultValue: false;
+ *     };
+ * }>;
+ * ```
+ *
+ * This script should be run before publishing, in order to verify that this is not the case.
+ */
+
+const configPath = path.join(__dirname, '../packages/core/dist/config');
+const filesToCheck = [
+    path.join(configPath, 'payment/dummy-payment-method-handler.d.ts'),
+    path.join(configPath, 'promotion/actions/product-percentage-discount-action.d.ts'),
+    path.join(configPath, 'promotion/conditions/contains-products-condition.d.ts'),
+    path.join(configPath, 'shipping-method/default-shipping-calculator.d.ts'),
+    path.join(configPath, 'shipping-method/default-shipping-eligibility-checker.d.ts'),
+    path.join(configPath, 'fulfillment/manual-fulfillment-handler.d.ts'),
+];
+
+console.log(`Checking core type definitions...`);
+let checkIsOk = true;
+
+for (const filePath of filesToCheck) {
+    const content = fs.readFileSync(filePath, 'utf-8');
+    const matches = content.match(/type: string;|languageCode: any;/gm);
+    if (matches) {
+        console.warn(`\n\nBad type definitions found in file ${filePath}:`);
+        console.warn(`==========`);
+        console.warn(matches.join('\n'));
+        console.warn(`==========`);
+        checkIsOk = false;
+    }
+}
+
+if (!checkIsOk) {
+    process.exit(1);
+} else {
+    console.log(`Type defs ok!`);
+    process.exit(0);
+}