Skip to content

Commit

Permalink
chore: run dependency check on commit hook (#5412)
Browse files Browse the repository at this point in the history
* chore: run dependency check on commit hook

* chore: add dep-check script to precommit hook
  • Loading branch information
kuhe committed Oct 24, 2023
1 parent 9468188 commit 8a65e9f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"lint:ci": "lerna exec --since origin/main --exclude-dependents --ignore '@aws-sdk/client-*' --ignore '@aws-sdk/aws-*' 'eslint --quiet src/**/*.ts'",
"lint:release": "lerna exec --ignore '@aws-sdk/client-*' --ignore '@aws-sdk/aws-*' 'eslint --quiet src/**/*.ts'",
"lint:versions": "node scripts/runtime-dependency-version-check/runtime-dep-version-check.js",
"lint:dependencies": "node scripts/runtime-dependency-version-check/check-dependencies.js",
"local-publish": "node ./scripts/verdaccio-publish/index.js",
"test:all": "yarn build:all && jest --coverage --passWithNoTests && lerna run test --scope '@aws-sdk/{fetch-http-handler,hash-blob-browser}' && yarn test:versions && yarn test:integration",
"test:ci": "lerna run test --since origin/main",
Expand Down Expand Up @@ -146,7 +147,7 @@
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && yarn lint:versions",
"pre-commit": "lint-staged && yarn lint:versions && yarn lint:dependencies",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/credential-provider-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@smithy/protocol-http": "^3.0.8",
"@smithy/smithy-client": "^2.1.12",
"@smithy/types": "^2.4.0",
"@smithy/util-stream": "^2.0.17",
"tslib": "^2.5.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/token-providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@aws-sdk/middleware-logger": "*",
"@aws-sdk/middleware-recursion-detection": "*",
"@aws-sdk/middleware-user-agent": "*",
"@aws-sdk/region-config-resolver": "*",
"@aws-sdk/types": "*",
"@aws-sdk/util-endpoints": "*",
"@aws-sdk/util-user-agent-browser": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const packages = path.join(root, "packages");
const walk = require("../utils/walk");

(async () => {
const errors = [];

for (const folder of fs.readdirSync(packages)) {
if (folder === "util-dynamodb") {
// exempt
Expand All @@ -32,9 +34,24 @@ const walk = require("../utils/walk");
continue;
}

const importedDependencies = [];
importedDependencies.push(
...new Set(
[...(contents.toString().match(/from "(@(aws-sdk|smithy)\/.*?)"/g) || [])]
.slice(1)
.map((_) => _.replace(/from "/g, "").replace(/"$/, ""))
)
);

for (const dependency of importedDependencies) {
if (!(dependency in pkgJson.dependencies) && dependency !== pkgJson.name) {
errors.push(`${dependency} undeclared but imported in ${pkgJson.name} ${file}}`);
}
}

for (const [dep, version] of Object.entries(pkgJson.devDependencies ?? {})) {
if ((dep.startsWith("@smithy") || dep.startsWith("@aws-sdk")) && contents.includes(`from "${dep}";`)) {
console.warn(`${dep} incorrectly declared in devDependencies of ${folder}`);
errors.push(`${dep} incorrectly declared in devDependencies of ${folder}`);
delete pkgJson.devDependencies[dep];
if (!pkgJson.dependencies) {
pkgJson.dependencies = {};
Expand All @@ -46,4 +63,8 @@ const walk = require("../utils/walk");
}
}
}

if (errors.length) {
throw new Error(errors.join("\n"));
}
})();

0 comments on commit 8a65e9f

Please sign in to comment.