Skip to content

Commit

Permalink
Converted src/meta/dependencies.js to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
brycez1 committed Jan 24, 2024
1 parent 21a237a commit 196d321
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 48 deletions.
113 changes: 65 additions & 48 deletions src/meta/dependencies.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,88 @@
'use strict';

const path = require('path');
const fs = require('fs');

const semver = require('semver');
const winston = require('winston');
const chalk = require('chalk');

const pkg = require('../../package.json');
const { paths, pluginNamePattern } = require('../constants');

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const semver_1 = __importDefault(require("semver"));
const winston_1 = __importDefault(require("winston"));
const chalk_1 = __importDefault(require("chalk"));
const constants_1 = require("../constants");
// added "resolveJsonModule": true to tsconfig.json
const package_json_1 = __importDefault(require("../../package.json"));
const Dependencies = module.exports;

let depsMissing = false;
let depsOutdated = false;

Dependencies.check = async function () {
const modules = Object.keys(pkg.dependencies);

winston.verbose('Checking dependencies for outdated modules');

await Promise.all(modules.map(module => Dependencies.checkModule(module)));

if (depsMissing) {
throw new Error('dependencies-missing');
} else if (depsOutdated && global.env !== 'development') {
throw new Error('dependencies-out-of-date');
}
Dependencies.check = function () {
return __awaiter(this, void 0, void 0, function* () {
const modules = Object.keys(package_json_1.default.dependencies);
winston_1.default.verbose('Checking dependencies for outdated modules');
yield Promise.all(modules.map(module => Dependencies.checkModule(module)));
if (depsMissing) {
throw new Error('dependencies-missing');
}
else if (depsOutdated && global.env !== 'development') {
throw new Error('dependencies-out-of-date');
}
});
};

Dependencies.checkModule = async function (moduleName) {
try {
let pkgData = await fs.promises.readFile(path.join(paths.nodeModules, moduleName, 'package.json'), 'utf8');
pkgData = Dependencies.parseModuleData(moduleName, pkgData);

const satisfies = Dependencies.doesSatisfy(pkgData, pkg.dependencies[moduleName]);
return satisfies;
} catch (err) {
if (err.code === 'ENOENT' && pluginNamePattern.test(moduleName)) {
winston.warn(`[meta/dependencies] Bundled plugin ${moduleName} not found, skipping dependency check.`);
return true;
Dependencies.checkModule = function (moduleName) {
return __awaiter(this, void 0, void 0, function* () {
try {
let pkgData = yield fs_1.default.promises.readFile(path_1.default.join(constants_1.paths.nodeModules, moduleName, 'package.json'), 'utf8');
pkgData = Dependencies.parseModuleData(moduleName, pkgData) || '';
const satisfies = Dependencies.doesSatisfy(pkgData, package_json_1.default.dependencies[moduleName]);
return satisfies;
}
throw err;
}
catch (err) {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
if (err.code === 'ENOENT' && constants_1.pluginNamePattern.test(moduleName)) {
winston_1.default.warn(`[meta/dependencies] Bundled plugin ${moduleName} not found, skipping dependency check.`);
return true;
}
throw err;
}
});
};

Dependencies.parseModuleData = function (moduleName, pkgData) {
try {
pkgData = JSON.parse(pkgData);
} catch (e) {
winston.warn(`[${chalk.red('missing')}] ${chalk.bold(moduleName)} is a required dependency but could not be found\n`);
}
catch (e) {
winston_1.default.warn(`[${chalk_1.default.red('missing')}] ${chalk_1.default.bold(moduleName)} is a required dependency but could not be found\n`);
depsMissing = true;
return null;
}
return pkgData;
};

Dependencies.doesSatisfy = function (moduleData, packageJSONVersion) {
if (!moduleData) {
return false;
}
const versionOk = !semver.validRange(packageJSONVersion) ||
semver.satisfies(moduleData.version, packageJSONVersion);
const githubRepo = moduleData._resolved && moduleData._resolved.includes('//github.com');
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const version_name = moduleData.version;
const versionOk = !semver_1.default.validRange(packageJSONVersion) ||
semver_1.default.satisfies(version_name, packageJSONVersion);
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const githubRepo = (moduleData._resolved && moduleData._resolved.includes('//github.com'));
const satisfies = versionOk || githubRepo;
if (!satisfies) {
winston.warn(`[${chalk.yellow('outdated')}] ${chalk.bold(moduleData.name)} installed v${moduleData.version}, package.json requires ${packageJSONVersion}\n`);
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
winston_1.default.warn(`[${chalk_1.default.yellow('outdated')}] ${chalk_1.default.bold(moduleData.name)} installed v${version_name}, package.json requires ${packageJSONVersion}\n`);
depsOutdated = true;
}
return satisfies;
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true
},
"include": [
"public/src/**/*",
Expand Down

0 comments on commit 196d321

Please sign in to comment.