Skip to content

Commit

Permalink
adding reminders about imports
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorFries committed Nov 8, 2024
1 parent 6ea44d4 commit ba5e97f
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 126 deletions.
6 changes: 3 additions & 3 deletions .github/helpers/github-api/github-api-requests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function addComment(issueNumber, issueComment) {
issue_number: issueNumber,
body: issueComment,
});
//console.log("add comment: ", request.data);
console.log("add comment: ", request.data);
return response;
}

Expand All @@ -84,7 +84,7 @@ export async function deleteComment(commentID) {
repo: GITHUB_REPO,
comment_id: commentID,
});
//console.log("delete comment: ", response.data);
console.log("delete comment: ", response.data);
return response;
}

Expand All @@ -102,7 +102,7 @@ export async function findIssues(state = "open") {
repo: GITHUB_REPO,
state, // Get issues that are 'open', 'closed' or 'all'.
});
//console.log("found issue: ", response.data);
console.log("found issue: ", response.data);
return response;
}

Expand Down
12 changes: 7 additions & 5 deletions .github/helpers/npm-deps/create-report-issues.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import outputText from "../../../outputText.json";
import {
closeAndCreateIssue,
createAndCloseComment,
} from "../github-api/create-and-close-existing-issue.mjs";
// NOTE: these cant be imports as they are outside a module
const outputText = require(path.resolve(__dirname, `../../../outputText.json`));
const module = await import(
"../github-api/create-and-close-existing-issue.mjs"
);
const closeAndCreateIssue = module.closeAndCreateIssue;
const createAndCloseComment = module.createAndCloseComment;

/**
* THIS FILE DOES NOT REQUIRE ANY EDITING.
Expand Down
264 changes: 146 additions & 118 deletions .github/helpers/npm-deps/parse-npm-deps.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

const https = require('https');
const path = require('path');
// NOTE: these cant be imports as they are outside a module
const https = require("https");
const path = require("path");

const LOCAL_TEST = false;
const TEST_PACKAGEJSON_PATHS = ['src/frontend', 'src/backend'];
const TEST_PACKAGEJSON_PATHS = ["src/frontend", "src/backend"];

/**
* THIS FILE DOES NOT REQUIRE ANY EDITING.
Expand All @@ -18,129 +18,157 @@ const TEST_PACKAGEJSON_PATHS = ['src/frontend', 'src/backend'];

// Get package.json paths from env.
const packageJsonPaths = LOCAL_TEST
? TEST_PACKAGEJSON_PATHS
: JSON.parse(process.env.packageJsonPaths);
? TEST_PACKAGEJSON_PATHS
: JSON.parse(process.env.packageJsonPaths);

// Save results to json.
let results = {};
const results = {};

// Save information on the dependency including latestVersion to the above results arrays.
const saveDependencyResults = (packagePath, isDevDep, dependency, version, latestVersion) => {
// Create arrays of version triplets ie. 2.7.4 >> [2, 7, 4]
const versionTriplet = version.split('.');
const latestVersionTriplet = latestVersion.split('.');

// Determine version change.
let versionChange = 'patch';
if (versionTriplet[0] !== latestVersionTriplet[0]) versionChange = 'major';
else if (versionTriplet[1] !== latestVersionTriplet[1]) versionChange = 'minor';

// Save results.
const saveInfo = {
dependency,
version,
latestVersion,
};

if (isDevDep)
results[packagePath].devDeps[versionChange].push(saveInfo); // devDep.
else results[packagePath].deps[versionChange].push(saveInfo); // dep.
const saveDependencyResults = (
packagePath,
isDevDep,
dependency,
version,
latestVersion,
) => {
// Create arrays of version triplets ie. 2.7.4 >> [2, 7, 4]
const versionTriplet = version.split(".");
const latestVersionTriplet = latestVersion.split(".");

// Determine version change.
let versionChange = "patch";
if (versionTriplet[0] !== latestVersionTriplet[0]) versionChange = "major";
else if (versionTriplet[1] !== latestVersionTriplet[1])
versionChange = "minor";

// Save results.
const saveInfo = {
dependency,
version,
latestVersion,
};

if (isDevDep)
results[packagePath].devDeps[versionChange].push(saveInfo); // devDep.
else results[packagePath].deps[versionChange].push(saveInfo); // dep.
};

// Check the latest version of each dependency.
const checkVersions = async (dependencyList, packagePath, isDevDep) => {
// For each dependency in the dependencyList.
for (let key in dependencyList) {
const [dependency, version] = dependencyList[key];
const url = `https://registry.npmjs.org/${dependency}/latest`;

// Add to total.
if (isDevDep) ++results[packagePath].devDeps.total;
else ++results[packagePath].deps.total;

try {
// Make an http request to the npm registry.
const data = await new Promise((resolve, reject) => {
https.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
res.on('error', (error) => {
reject(error);
});
});
});

// Parse response data for latest version.
const latestVersion = JSON.parse(data).version;

// Check if theres a difference in version and latestVersion.
if (latestVersion && latestVersion !== '0.0.0' && latestVersion !== version) {
if (!latestVersion.includes('-'))
saveDependencyResults(packagePath, isDevDep, dependency, version, latestVersion);
else {
// Latest version includes '-'.
const data = await new Promise((resolve, reject) => {
https.get(`https://registry.npmjs.org/${dependency}`, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
res.on('error', (error) => {
reject(error);
});
});
});

const versions = Object.keys(JSON.parse(data).versions);
// Remove all versions containing '-' and select the last item in the array.
const filteredLatestVersions = versions.filter((item) => !item.includes('-'));
const latestVersion = filteredLatestVersions[filteredLatestVersions.length - 1];

if (latestVersion !== version)
saveDependencyResults(packagePath, isDevDep, dependency, version, latestVersion);
}

// Add to outdated sum.
if (isDevDep) ++results[packagePath].devDeps.outdated;
else ++results[packagePath].deps.outdated;
}
} catch (error) {
console.error(`Error checking ${dependency}: ${error.message}`);
}
}
// For each dependency in the dependencyList.
for (let key in dependencyList) {
const [dependency, version] = dependencyList[key];
const url = `https://registry.npmjs.org/${dependency}/latest`;

// Add to total.
if (isDevDep) ++results[packagePath].devDeps.total;
else ++results[packagePath].deps.total;

try {
// Make an http request to the npm registry.
const data = await new Promise((resolve, reject) => {
https.get(url, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve(data);
});
res.on("error", (error) => {
reject(error);
});
});
});

// Parse response data for latest version.
const latestVersion = JSON.parse(data).version;

// Check if theres a difference in version and latestVersion.
if (
latestVersion &&
latestVersion !== "0.0.0" &&
latestVersion !== version
) {
if (!latestVersion.includes("-"))
saveDependencyResults(
packagePath,
isDevDep,
dependency,
version,
latestVersion,
);
else {
// Latest version includes '-'.
const data = await new Promise((resolve, reject) => {
https.get(`https://registry.npmjs.org/${dependency}`, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve(data);
});
res.on("error", (error) => {
reject(error);
});
});
});

const versions = Object.keys(JSON.parse(data).versions);
// Remove all versions containing '-' and select the last item in the array.
const filteredLatestVersions = versions.filter(
(item) => !item.includes("-"),
);
const latestVersion =
filteredLatestVersions[filteredLatestVersions.length - 1];

if (latestVersion !== version)
saveDependencyResults(
packagePath,
isDevDep,
dependency,
version,
latestVersion,
);
}

// Add to outdated sum.
if (isDevDep) ++results[packagePath].devDeps.outdated;
else ++results[packagePath].deps.outdated;
}
} catch (error) {
console.error(`Error checking ${dependency}: ${error.message}`);
}
}
};

(async () => {
// Create an array of promises for each packageJsonPath.
const promises = packageJsonPaths.map(async (packagePath) => {
const packageJson = require(path.resolve(__dirname, `../../../${packagePath}/package.json`));
results[packagePath] = {
deps: { total: 0, outdated: 0, major: [], minor: [], patch: [] },
devDeps: { total: 0, outdated: 0, major: [], minor: [], patch: [] },
};

// Read the package.json file and get the list of dependencies and devDependencies.
const deps = Object.entries(packageJson.dependencies) ?? [];
const devDeps = Object.entries(packageJson.devDependencies) ?? [];

// Await the completion of version checks for both dependencies and devDependencies.
await Promise.all([
checkVersions(deps, packagePath, false),
checkVersions(devDeps, packagePath, true),
]);
});

// Wait for all package checks to complete.
await Promise.all(promises);

// Once all promises are resolved, log the results.
console.log(JSON.stringify(results, null, 2));
// Create an array of promises for each packageJsonPath.
const promises = packageJsonPaths.map(async (packagePath) => {
const packageJson = require(
path.resolve(__dirname, `../../../${packagePath}/package.json`),
);
results[packagePath] = {
deps: { total: 0, outdated: 0, major: [], minor: [], patch: [] },
devDeps: { total: 0, outdated: 0, major: [], minor: [], patch: [] },
};

// Read the package.json file and get the list of dependencies and devDependencies.
const deps = Object.entries(packageJson.dependencies) ?? [];
const devDeps = Object.entries(packageJson.devDependencies) ?? [];

// Await the completion of version checks for both dependencies and devDependencies.
await Promise.all([
checkVersions(deps, packagePath, false),
checkVersions(devDeps, packagePath, true),
]);
});

// Wait for all package checks to complete.
await Promise.all(promises);

// Once all promises are resolved, log the results.
console.log(JSON.stringify(results, null, 2));
})();

0 comments on commit ba5e97f

Please sign in to comment.