2020-07-27 23:02:28 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/**
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-01-02 02:17:27 +03:00
|
|
|
const fs = require('fs');
|
2020-07-27 23:02:28 +03:00
|
|
|
const ts = require('typescript');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
async function checkDeps() {
|
|
|
|
const root = path.normalize(path.join(__dirname, '..'));
|
|
|
|
const src = path.normalize(path.join(__dirname, '..', 'src'));
|
|
|
|
const program = ts.createProgram({
|
|
|
|
options: {
|
|
|
|
allowJs: true,
|
|
|
|
target: ts.ScriptTarget.ESNext,
|
|
|
|
strict: true,
|
|
|
|
},
|
2021-01-02 02:17:27 +03:00
|
|
|
rootNames: listAllFiles(src),
|
2020-07-27 23:02:28 +03:00
|
|
|
});
|
|
|
|
const sourceFiles = program.getSourceFiles();
|
|
|
|
const errors = [];
|
2020-12-22 22:01:25 +03:00
|
|
|
const usedDeps = new Set();
|
2020-07-27 23:02:28 +03:00
|
|
|
sourceFiles.filter(x => !x.fileName.includes('node_modules')).map(x => visit(x, x.fileName));
|
2020-12-22 22:01:25 +03:00
|
|
|
for (const key of Object.keys(DEPS)) {
|
|
|
|
if (!usedDeps.has(key) && DEPS[key].length)
|
|
|
|
errors.push(`Stale DEPS entry "${key}"`);
|
|
|
|
}
|
2020-07-27 23:02:28 +03:00
|
|
|
for (const error of errors)
|
|
|
|
console.log(error);
|
2020-08-25 00:48:03 +03:00
|
|
|
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(`--------------------------------------------------------`);
|
|
|
|
}
|
2020-07-27 23:02:28 +03:00
|
|
|
process.exit(errors.length ? 1 : 0);
|
|
|
|
|
|
|
|
function visit(node, fileName) {
|
|
|
|
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
|
|
const importName = node.moduleSpecifier.text;
|
2020-08-24 07:24:16 +03:00
|
|
|
const importPath = path.resolve(path.dirname(fileName), importName) + '.ts';
|
2020-07-27 23:02:28 +03:00
|
|
|
if (!allowImport(fileName, importPath))
|
|
|
|
errors.push(`Disallowed import from ${path.relative(root, fileName)} to ${path.relative(root, importPath)}`);
|
|
|
|
}
|
|
|
|
ts.forEachChild(node, x => visit(x, fileName));
|
|
|
|
}
|
|
|
|
|
|
|
|
function allowImport(from, to) {
|
2020-12-22 22:01:25 +03:00
|
|
|
if (!to.startsWith(src + path.sep))
|
2020-12-10 02:06:57 +03:00
|
|
|
return true;
|
2021-01-06 20:31:42 +03:00
|
|
|
if (!fs.existsSync(to))
|
|
|
|
return true;
|
2020-12-22 22:01:25 +03:00
|
|
|
from = path.relative(root, from).replace(/\\/g, '/');
|
|
|
|
to = path.relative(root, to).replace(/\\/g, '/');
|
2020-08-24 07:24:16 +03:00
|
|
|
const fromDirectory = from.substring(0, from.lastIndexOf('/') + 1);
|
2020-08-23 01:46:42 +03:00
|
|
|
const toDirectory = to.substring(0, to.lastIndexOf('/') + 1);
|
2020-08-24 07:24:16 +03:00
|
|
|
if (fromDirectory === toDirectory)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
while (!DEPS[from]) {
|
|
|
|
if (from.endsWith('/'))
|
|
|
|
from = from.substring(0, from.length - 1);
|
|
|
|
if (from.lastIndexOf('/') === -1)
|
2020-08-25 00:48:03 +03:00
|
|
|
throw new Error(`Cannot find DEPS for ${fromDirectory}`);
|
2020-08-24 07:24:16 +03:00
|
|
|
from = from.substring(0, from.lastIndexOf('/') + 1);
|
|
|
|
}
|
|
|
|
|
2020-12-22 22:01:25 +03:00
|
|
|
usedDeps.add(from);
|
2020-08-25 00:48:03 +03:00
|
|
|
for (const dep of DEPS[from]) {
|
2020-08-24 07:24:16 +03:00
|
|
|
if (to === dep || toDirectory === dep)
|
|
|
|
return true;
|
|
|
|
if (dep.endsWith('**')) {
|
|
|
|
const parent = dep.substring(0, dep.length - 2);
|
|
|
|
if (to.startsWith(parent))
|
2020-08-22 17:07:13 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 07:24:16 +03:00
|
|
|
return false;
|
2020-07-27 23:02:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 02:17:27 +03:00
|
|
|
function listAllFiles(dir) {
|
|
|
|
const dirs = fs.readdirSync(dir, { withFileTypes: true });
|
|
|
|
const result = [];
|
|
|
|
dirs.map(d => {
|
|
|
|
const res = path.resolve(dir, d.name);
|
|
|
|
if (d.isDirectory())
|
|
|
|
result.push(...listAllFiles(res));
|
|
|
|
else
|
|
|
|
result.push(res);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-24 07:24:16 +03:00
|
|
|
const DEPS = {};
|
|
|
|
|
|
|
|
DEPS['src/protocol/'] = ['src/utils/'];
|
|
|
|
|
2020-08-24 16:51:51 +03:00
|
|
|
// Client depends on chromium protocol for types.
|
2021-06-24 04:01:48 +03:00
|
|
|
DEPS['src/client/'] = ['src/common/', 'src/utils/', 'src/protocol/', 'src/server/chromium/protocol.d.ts'];
|
2021-02-12 20:05:32 +03:00
|
|
|
DEPS['src/outofprocess.ts'] = ['src/client/', 'src/protocol/'];
|
2020-08-24 07:24:16 +03:00
|
|
|
|
2021-02-11 08:50:29 +03:00
|
|
|
DEPS['src/dispatchers/'] = ['src/common/', 'src/utils/', 'src/protocol/', 'src/server/**'];
|
2020-08-25 00:48:03 +03:00
|
|
|
|
|
|
|
// Generic dependencies for server-side code.
|
2020-08-24 16:51:51 +03:00
|
|
|
DEPS['src/server/'] = [
|
2021-02-11 05:52:28 +03:00
|
|
|
'src/common/',
|
2020-08-24 16:51:51 +03:00
|
|
|
'src/utils/',
|
|
|
|
'src/generated/',
|
2020-08-25 00:48:03 +03:00
|
|
|
// Can depend on files directly in the server directory.
|
|
|
|
'src/server/',
|
|
|
|
// Can depend on any files in these subdirectories.
|
|
|
|
'src/server/common/**',
|
|
|
|
'src/server/injected/**',
|
2021-01-25 06:21:19 +03:00
|
|
|
'src/server/supplements/**',
|
2021-01-26 01:49:26 +03:00
|
|
|
'src/protocol/**',
|
2020-08-24 16:51:51 +03:00
|
|
|
];
|
2020-08-24 07:24:16 +03:00
|
|
|
|
2020-08-25 00:48:03 +03:00
|
|
|
// No dependencies for code shared between node and page.
|
|
|
|
DEPS['src/server/common/'] = [];
|
|
|
|
// Strict dependencies for injected code.
|
2020-08-25 01:30:45 +03:00
|
|
|
DEPS['src/server/injected/'] = ['src/server/common/'];
|
2021-01-25 06:21:19 +03:00
|
|
|
|
2020-11-07 03:31:11 +03:00
|
|
|
// Electron and Clank use chromium internally.
|
2020-12-22 22:01:25 +03:00
|
|
|
DEPS['src/server/android/'] = [...DEPS['src/server/'], 'src/server/chromium/', 'src/protocol/'];
|
2020-08-25 00:48:03 +03:00
|
|
|
DEPS['src/server/electron/'] = [...DEPS['src/server/'], 'src/server/chromium/'];
|
2020-08-24 07:24:16 +03:00
|
|
|
|
2021-04-24 04:34:52 +03:00
|
|
|
DEPS['src/server/playwright.ts'] = [...DEPS['src/server/'], 'src/server/chromium/', 'src/server/webkit/', 'src/server/firefox/', 'src/server/android/', 'src/server/electron/'];
|
2021-04-27 21:07:07 +03:00
|
|
|
DEPS['src/server/browserContext.ts'] = [...DEPS['src/server/'], 'src/server/trace/recorder/tracing.ts'];
|
2020-12-29 02:44:24 +03:00
|
|
|
DEPS['src/cli/driver.ts'] = DEPS['src/inprocess.ts'] = DEPS['src/browserServerImpl.ts'] = ['src/**'];
|
2020-08-22 17:07:13 +03:00
|
|
|
|
2020-09-05 02:31:52 +03:00
|
|
|
// Tracing is a client/server plugin, nothing should depend on it.
|
2021-02-18 01:05:41 +03:00
|
|
|
DEPS['src/web/recorder/'] = ['src/common/', 'src/web/', 'src/web/components/', 'src/server/supplements/recorder/recorderTypes.ts'];
|
2021-02-25 00:39:51 +03:00
|
|
|
DEPS['src/web/traceViewer/'] = ['src/common/', 'src/web/'];
|
2021-07-03 02:45:09 +03:00
|
|
|
DEPS['src/web/traceViewer/ui/'] = ['src/common/', 'src/protocol/', 'src/web/traceViewer/', 'src/web/', 'src/server/trace/viewer/', 'src/server/trace/', 'src/server/trace/common/', 'src/server/snapshot/snapshotTypes.ts', 'src/protocol/channels.ts'];
|
2020-12-01 01:57:17 +03:00
|
|
|
// The service is a cross-cutting feature, and so it depends on a bunch of things.
|
2021-07-07 22:14:16 +03:00
|
|
|
DEPS['src/remote/'] = ['src/client/', 'src/debug/', 'src/dispatchers/', 'src/server/', 'src/server/supplements/', 'src/server/electron/', 'src/server/trace/', 'src/utils/**'];
|
2020-12-01 01:57:17 +03:00
|
|
|
|
2020-12-23 01:54:13 +03:00
|
|
|
// CLI should only use client-side features.
|
2021-07-07 06:59:16 +03:00
|
|
|
DEPS['src/cli/'] = ['src/cli/**', 'src/client/**', 'src/generated/', 'src/server/injected/', 'src/debug/injected/', 'src/server/trace/**', 'src/utils/**'];
|
2020-12-23 01:54:13 +03:00
|
|
|
|
2021-02-12 21:11:30 +03:00
|
|
|
DEPS['src/server/supplements/recorder/recorderApp.ts'] = ['src/common/', 'src/utils/', 'src/server/', 'src/server/chromium/'];
|
2021-02-26 00:09:26 +03:00
|
|
|
DEPS['src/server/supplements/recorderSupplement.ts'] = ['src/server/snapshot/', ...DEPS['src/server/']];
|
2021-07-03 02:45:09 +03:00
|
|
|
DEPS['src/utils/'] = ['src/common/', 'src/protocol/'];
|
2021-02-01 03:37:13 +03:00
|
|
|
|
2021-02-25 00:39:51 +03:00
|
|
|
// Trace viewer
|
2021-02-25 20:33:32 +03:00
|
|
|
DEPS['src/server/trace/common/'] = ['src/server/snapshot/', ...DEPS['src/server/']];
|
|
|
|
DEPS['src/server/trace/recorder/'] = ['src/server/trace/common/', ...DEPS['src/server/trace/common/']];
|
2021-07-03 00:33:38 +03:00
|
|
|
DEPS['src/server/trace/viewer/'] = ['src/server/trace/common/', 'src/server/trace/recorder/', 'src/server/chromium/', ...DEPS['src/server/trace/common/']];
|
2021-07-07 22:14:16 +03:00
|
|
|
DEPS['src/test/'] = ['src/test/**', 'src/utils/utils.ts', 'src/utils/**'];
|
2021-06-07 03:09:53 +03:00
|
|
|
|
2021-08-05 23:36:47 +03:00
|
|
|
// HTML report
|
|
|
|
DEPS['src/web/htmlReport/'] = ['src/test/**', 'src/web/'];
|
|
|
|
|
|
|
|
|
2020-12-01 01:57:17 +03:00
|
|
|
checkDeps().catch(e => {
|
|
|
|
console.error(e && e.stack ? e.stack : e);
|
|
|
|
process.exit(1);
|
|
|
|
});
|