瀏覽代碼

chore: Add check for bad imports before publish

Michael Bromley 6 年之前
父節點
當前提交
d45463441f
共有 3 個文件被更改,包括 16 次插入1 次删除
  1. 3 1
      package.json
  2. 1 0
      scripts/check-imports.ts
  3. 12 0
      yarn.lock

+ 3 - 1
package.json

@@ -14,7 +14,7 @@
     "format": "prettier --write --html-whitespace-sensitivity ignore",
     "lint:packages": "yarn tslint --fix",
     "lint:admin-ui": "cd admin-ui && yarn lint --fix",
-    "version": "yarn build && yarn generate-changelog && git add CHANGELOG.md",
+    "version": "yarn check-imports && yarn build && yarn generate-changelog && git add CHANGELOG.md",
     "dev-server:start": "cd packages/dev-server && yarn start",
     "dev-server:populate": "cd packages/dev-server && yarn populate",
     "test:all": "cd admin-ui && yarn test --watch=false --browsers=ChromeHeadlessCI --progress=false && cd ../ && yarn test:common && yarn test:core && yarn test:email-plugin && yarn test:elasticsearch-plugin && yarn test:e2e",
@@ -25,6 +25,7 @@
     "test:e2e": "jest --config packages/core/e2e/config/jest-e2e.json --runInBand",
     "test:admin-ui": "cd admin-ui && yarn test --watch=false --browsers=ChromeHeadlessCI --progress=false",
     "build": "lerna run build",
+    "check-imports": "ts-node scripts/check-imports.ts",
     "generate-changelog": "ts-node scripts/changelogs/generate-changelog.ts",
     "publish-release": "lerna publish -m \"chore: Publish %s\" --no-push"
   },
@@ -42,6 +43,7 @@
     "@types/node": "^10.11.5",
     "concurrently": "^4.1.0",
     "conventional-changelog-core": "^3.2.2",
+    "find": "^0.3.0",
     "graphql": "^14.1.1",
     "graphql-tools": "^4.0.0",
     "husky": "^3.0.0",

+ 1 - 0
scripts/check-imports.ts

@@ -0,0 +1 @@
+/* tslint:disable:no-console */
import fs from 'fs';
import path from 'path';
// tslint:disable-next-line:no-var-requires
const find = require('find');

/**
 * An array of regular expressions defining illegal import patterns to be checked in the
 * source files of the monorepo packages. This prevents bad imports (which work locally
 * and go undetected) from getting into published releases of Vendure.
 */
const illegalImportPatters: RegExp[] = [
    /@vendure\/common\/src/,
];

findInFiles(illegalImportPatters, path.join(__dirname, '../packages'), /\.ts$/);

function findInFiles(patterns: RegExp[], directory: string, fileFilter: RegExp) {
    find.file(fileFilter, directory, async (files: string[]) => {
        const matches = await getMatchedFiles(patterns, files);
        if (matches.length) {
            console.error(`Found illegal imports in the following files:`);
            console.error(matches.join('\n'));
            process.exitCode = 1;
        } else {
            console.log('Imports check ok!');
        }
    });
}

async function getMatchedFiles(patterns: RegExp[], files: string[]) {
    const matchedFiles = [];
    for (let i = files.length - 1; i >= 0; i--) {
        const content = await readFile(files[i]);
        for (const pattern of patterns) {
            if (pattern.test(content)) {
                matchedFiles.push(files[i]);
                continue;
            }
        }
    }
    return matchedFiles;
}

function readFile(filePath: string): Promise<string> {
    return new Promise((resolve, reject) => {
        fs.readFile(filePath, 'utf-8', (err, data) => {
            if (err) {
                reject(err);
            } else {
                resolve(data);
            }
        });
    });
}

+ 12 - 0
yarn.lock

@@ -4821,6 +4821,13 @@ find-up@^4.0.0:
   dependencies:
     locate-path "^5.0.0"
 
+find@^0.3.0:
+  version "0.3.0"
+  resolved "http://localhost:4873/find/-/find-0.3.0.tgz#4082e8fc8d8320f1a382b5e4f521b9bc50775cb8"
+  integrity sha512-iSd+O4OEYV/I36Zl8MdYJO0xD82wH528SaCieTVHhclgiYNe9y+yPKSwK+A7/WsmHL1EZ+pYUJBXWTL5qofksw==
+  dependencies:
+    traverse-chain "~0.1.0"
+
 findup-sync@^2.0.0:
   version "2.0.0"
   resolved "https://registry.npmjs.org/findup-sync/-/findup-sync-2.0.0.tgz#9326b1488c22d1a6088650a86901b2d9a90a2cbc"
@@ -11134,6 +11141,11 @@ tr46@^1.0.1:
   dependencies:
     punycode "^2.1.0"
 
+traverse-chain@~0.1.0:
+  version "0.1.0"
+  resolved "http://localhost:4873/traverse-chain/-/traverse-chain-0.1.0.tgz#61dbc2d53b69ff6091a12a168fd7d433107e40f1"
+  integrity sha1-YdvC1Ttp/2CRoSoWj9fUMxB+QPE=
+
 tree-kill@^1.1.0:
   version "1.2.1"
   resolved "https://registry.npmjs.org/tree-kill/-/tree-kill-1.2.1.tgz#5398f374e2f292b9dcc7b2e71e30a5c3bb6c743a"