Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 92 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"expect": "^26.4.2",
"extract-zip": "^2.0.1",
"https-proxy-agent": "^5.0.0",
"jest-matcher-utils": "^26.4.2",
"jpeg-js": "^0.4.2",
"mime": "^2.4.6",
"minimatch": "^3.0.3",
Expand Down
34 changes: 32 additions & 2 deletions utils/check_deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const path = require('path');
async function checkDeps() {
const root = path.normalize(path.join(__dirname, '..'));
const src = path.normalize(path.join(__dirname, '..', 'src'));
const packageJSON = require(path.join(root, 'package.json'));
const program = ts.createProgram({
options: {
allowJs: true,
Expand All @@ -44,7 +45,7 @@ async function checkDeps() {
if (errors.length) {
console.log(`--------------------------------------------------------`);
console.log(`Changing the project structure or adding new components?`);
console.log(`Update DEPS in //${path.relative(root, __filename)}.`);
console.log(`Update DEPS in ./${path.relative(root, __filename)}`);
console.log(`--------------------------------------------------------`);
}
process.exit(errors.length ? 1 : 0);
Expand All @@ -55,6 +56,8 @@ async function checkDeps() {
const importPath = path.resolve(path.dirname(fileName), importName) + '.ts';
if (!allowImport(fileName, importPath))
errors.push(`Disallowed import from ${path.relative(root, fileName)} to ${path.relative(root, importPath)}`);
if (!alllowExternalImport(fileName, importPath, importName))
errors.push(`Disallowed external dependency ${importName} from ${path.relative(root, fileName)}`);
}
ts.forEachChild(node, x => visit(x, fileName));
}
Expand Down Expand Up @@ -91,11 +94,38 @@ async function checkDeps() {
}
return false;
}


function alllowExternalImport(from, importPath, importName) {
const EXTERNAL_IMPORT_ALLOWLIST = ['electron'];
// Only external imports are relevant. Files in src/web are bundled via webpack.
if (importName.startsWith('.') || importPath.startsWith(path.join(src, 'web')))
return true;
if (EXTERNAL_IMPORT_ALLOWLIST.includes(importName))
return true;
try {
const resolvedImport = require.resolve(importName)
const resolvedImportRelativeToNodeModules = path.relative(path.join(root, 'node_modules'), resolvedImport);
// Filter out internal Node.js modules
if (!resolvedImportRelativeToNodeModules.startsWith(importName))
return true;
const resolvedImportRelativeToNodeModulesParts = resolvedImportRelativeToNodeModules.split(path.sep);
if (packageJSON.dependencies[resolvedImportRelativeToNodeModulesParts[0]])
return true;
// handle e.g. @babel/code-frame
if (resolvedImportRelativeToNodeModulesParts.length >= 2 && packageJSON.dependencies[resolvedImportRelativeToNodeModulesParts.splice(0, 2).join(path.sep)])
return true;
return false;
} catch (error) {
if (error.code !== 'MODULE_NOT_FOUND')
throw error
}
}
}

function listAllFiles(dir) {
const dirs = fs.readdirSync(dir, { withFileTypes: true });
const result = [];
const result = [];
dirs.map(d => {
const res = path.resolve(dir, d.name);
if (d.isDirectory())
Expand Down