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.
|
|
|
|
*/
|
|
|
|
|
2022-03-25 18:43:29 +03:00
|
|
|
// @ts-check
|
|
|
|
|
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');
|
|
|
|
|
2022-03-25 18:43:29 +03:00
|
|
|
const packagesDir = path.normalize(path.join(__dirname, '..', 'packages'));
|
|
|
|
const packages = fs.readdirSync(packagesDir);
|
2022-03-26 00:12:00 +03:00
|
|
|
const peerDependencies = ['electron', 'react', 'react-dom', '@zip.js/zip.js'];
|
2022-03-25 18:43:29 +03:00
|
|
|
|
2020-07-27 23:02:28 +03:00
|
|
|
async function checkDeps() {
|
2022-03-26 00:12:00 +03:00
|
|
|
await innerCheckDeps(path.join(packagesDir, 'recorder'), true, true);
|
|
|
|
await innerCheckDeps(path.join(packagesDir, 'trace-viewer'), true, true);
|
|
|
|
|
2022-03-25 18:43:29 +03:00
|
|
|
const corePackageJson = await innerCheckDeps(path.join(packagesDir, 'playwright-core'), true, true);
|
|
|
|
const testPackageJson = await innerCheckDeps(path.join(packagesDir, 'playwright-test'), true, true);
|
2021-12-02 05:14:13 +03:00
|
|
|
|
|
|
|
let hasVersionMismatch = false;
|
|
|
|
for (const [key, value] of Object.entries(corePackageJson.dependencies)) {
|
|
|
|
const value2 = testPackageJson.dependencies[key];
|
|
|
|
if (value2 && value2 !== value) {
|
|
|
|
hasVersionMismatch = true;
|
|
|
|
console.log(`Dependency version mismatch ${key}: ${value} != ${value2}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
process.exit(hasVersionMismatch ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:43:29 +03:00
|
|
|
async function innerCheckDeps(root, checkDepsFile, checkPackageJson) {
|
|
|
|
console.log('Testing', root);
|
2021-12-02 05:14:13 +03:00
|
|
|
const deps = new Set();
|
|
|
|
const src = path.join(root, 'src');
|
2022-03-25 18:43:29 +03:00
|
|
|
const depsFile = checkDepsFile ? loadDEPSFile(src) : {};
|
2021-09-02 20:56:30 +03:00
|
|
|
const packageJSON = require(path.join(root, 'package.json'));
|
2020-07-27 23:02:28 +03:00
|
|
|
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 = [];
|
2022-03-25 18:43:29 +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));
|
2022-03-25 18:43:29 +03:00
|
|
|
for (const key of Object.keys(depsFile)) {
|
|
|
|
if (!usedDeps.has(key) && depsFile[key].length)
|
2020-12-22 22:01:25 +03:00
|
|
|
errors.push(`Stale DEPS entry "${key}"`);
|
|
|
|
}
|
2021-12-02 05:14:13 +03:00
|
|
|
if (checkDepsFile && errors.length) {
|
|
|
|
for (const error of errors)
|
|
|
|
console.log(error);
|
2020-08-25 00:48:03 +03:00
|
|
|
console.log(`--------------------------------------------------------`);
|
|
|
|
console.log(`Changing the project structure or adding new components?`);
|
2022-03-25 18:43:29 +03:00
|
|
|
console.log(`Update DEPS in ${root}`);
|
2020-08-25 00:48:03 +03:00
|
|
|
console.log(`--------------------------------------------------------`);
|
2021-12-02 05:14:13 +03:00
|
|
|
process.exit(1);
|
2020-08-25 00:48:03 +03:00
|
|
|
}
|
2021-12-02 05:14:13 +03:00
|
|
|
|
2022-03-25 18:43:29 +03:00
|
|
|
if (checkPackageJson) {
|
|
|
|
for (const dep of peerDependencies)
|
2021-12-02 05:14:13 +03:00
|
|
|
deps.delete(dep);
|
2022-03-25 18:43:29 +03:00
|
|
|
for (const dep of deps) {
|
|
|
|
const resolved = require.resolve(dep, { paths: [root] });
|
|
|
|
if (dep === resolved || !resolved.includes('node_modules'))
|
|
|
|
deps.delete(dep);
|
|
|
|
}
|
|
|
|
for (const dep of Object.keys(packageJSON.dependencies || {}))
|
|
|
|
deps.delete(dep);
|
|
|
|
|
|
|
|
if (deps.size) {
|
|
|
|
console.log('Dependencies are not declared in package.json:');
|
|
|
|
for (const dep of deps)
|
|
|
|
console.log(` ${dep}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2021-12-02 05:14:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return packageJSON;
|
2020-07-27 23:02:28 +03:00
|
|
|
|
|
|
|
function visit(node, fileName) {
|
|
|
|
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
2022-03-25 18:43:29 +03:00
|
|
|
if (node.importClause && node.importClause.isTypeOnly)
|
|
|
|
return;
|
2020-07-27 23:02:28 +03:00
|
|
|
const importName = node.moduleSpecifier.text;
|
2022-03-25 18:43:29 +03:00
|
|
|
let importPath;
|
|
|
|
if (importName.startsWith('.')) {
|
|
|
|
importPath = path.resolve(path.dirname(fileName), importName);
|
|
|
|
} else if (importName.startsWith('@')) {
|
|
|
|
const tokens = importName.substring(1).split('/');
|
|
|
|
const package = tokens[0];
|
|
|
|
if (packages.includes(package))
|
|
|
|
importPath = packagesDir + '/' + tokens[0] + '/src/' + tokens.slice(1).join('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (importPath) {
|
|
|
|
if (!fs.existsSync(importPath)) {
|
|
|
|
if (fs.existsSync(importPath + '.ts'))
|
|
|
|
importPath = importPath + '.ts';
|
|
|
|
else if (fs.existsSync(importPath + '.tsx'))
|
|
|
|
importPath = importPath + '.tsx';
|
2022-03-26 00:12:00 +03:00
|
|
|
else if (fs.existsSync(importPath + '.d.ts'))
|
|
|
|
importPath = importPath + '.d.ts';
|
2022-03-25 18:43:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (checkDepsFile && !allowImport(depsFile, fileName, importPath))
|
|
|
|
errors.push(`Disallowed import ${path.relative(root, importPath)} in ${path.relative(root, fileName)}`);
|
|
|
|
return;
|
2021-12-02 05:14:13 +03:00
|
|
|
}
|
2022-03-25 18:43:29 +03:00
|
|
|
|
|
|
|
if (importName.startsWith('@'))
|
|
|
|
deps.add(importName.split('/').slice(0, 2).join('/'));
|
|
|
|
else
|
|
|
|
deps.add(importName.split('/')[0]);
|
|
|
|
|
|
|
|
if (checkDepsFile && !allowExternalImport(importName, packageJSON))
|
2021-09-02 20:56:30 +03:00
|
|
|
errors.push(`Disallowed external dependency ${importName} from ${path.relative(root, fileName)}`);
|
2020-07-27 23:02:28 +03:00
|
|
|
}
|
|
|
|
ts.forEachChild(node, x => visit(x, fileName));
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:43:29 +03:00
|
|
|
function allowImport(depsFile, from, to) {
|
|
|
|
const fromDirectory = path.dirname(from);
|
|
|
|
const toDirectory = path.dirname(to);
|
2020-08-24 07:24:16 +03:00
|
|
|
if (fromDirectory === toDirectory)
|
|
|
|
return true;
|
|
|
|
|
2022-03-25 18:43:29 +03:00
|
|
|
while (!depsFile[from]) {
|
2020-08-24 07:24:16 +03:00
|
|
|
if (from.lastIndexOf('/') === -1)
|
2022-03-29 04:21:19 +03:00
|
|
|
return false;
|
2022-03-25 18:43:29 +03:00
|
|
|
from = from.substring(0, from.lastIndexOf('/'));
|
2020-08-24 07:24:16 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 22:01:25 +03:00
|
|
|
usedDeps.add(from);
|
2022-03-25 18:43:29 +03:00
|
|
|
for (const dep of depsFile[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-09-02 20:56:30 +03:00
|
|
|
|
|
|
|
|
2022-03-25 18:43:29 +03:00
|
|
|
function allowExternalImport(importName, packageJSON) {
|
2021-09-02 20:56:30 +03:00
|
|
|
// Only external imports are relevant. Files in src/web are bundled via webpack.
|
2022-03-25 18:43:29 +03:00
|
|
|
if (importName.startsWith('.') || importName.startsWith('@'))
|
2021-09-02 20:56:30 +03:00
|
|
|
return true;
|
2022-03-25 18:43:29 +03:00
|
|
|
if (peerDependencies.includes(importName))
|
2021-09-02 20:56:30 +03:00
|
|
|
return true;
|
|
|
|
try {
|
2021-10-08 18:01:31 +03:00
|
|
|
const resolvedImport = require.resolve(importName);
|
2021-09-02 20:56:30 +03:00
|
|
|
const resolvedImportRelativeToNodeModules = path.relative(path.join(root, 'node_modules'), resolvedImport);
|
|
|
|
// Filter out internal Node.js modules
|
|
|
|
if (!resolvedImportRelativeToNodeModules.startsWith(importName))
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'MODULE_NOT_FOUND')
|
2021-10-08 18:01:31 +03:00
|
|
|
throw error;
|
2021-09-02 20:56:30 +03:00
|
|
|
}
|
2022-03-29 04:21:19 +03:00
|
|
|
return !!(packageJSON.dependencies || {})[importName];
|
2021-09-02 20:56:30 +03:00
|
|
|
}
|
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 });
|
2021-09-02 20:56:30 +03:00
|
|
|
const result = [];
|
2021-01-02 02:17:27 +03:00
|
|
|
dirs.map(d => {
|
|
|
|
const res = path.resolve(dir, d.name);
|
|
|
|
if (d.isDirectory())
|
|
|
|
result.push(...listAllFiles(res));
|
|
|
|
else
|
|
|
|
result.push(res);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:43:29 +03:00
|
|
|
function loadDEPSFile(src) {
|
|
|
|
const deps = require(path.join(src, 'DEPS'));
|
|
|
|
const resolved = {};
|
|
|
|
for (let [key, values] of Object.entries(deps)) {
|
|
|
|
if (key === '/')
|
|
|
|
key = '';
|
|
|
|
resolved[path.resolve(src, key)] = values.map(v => {
|
|
|
|
if (v.startsWith('@')) {
|
|
|
|
const tokens = v.substring(1).split('/');
|
|
|
|
return path.resolve(packagesDir, tokens[0], 'src', ...tokens.slice(1));
|
|
|
|
}
|
|
|
|
return path.resolve(src, v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return resolved;
|
|
|
|
}
|
2021-08-05 23:36:47 +03:00
|
|
|
|
2020-12-01 01:57:17 +03:00
|
|
|
checkDeps().catch(e => {
|
|
|
|
console.error(e && e.stack ? e.stack : e);
|
|
|
|
process.exit(1);
|
|
|
|
});
|