From a92622f753453cb1d4ef93bb36c6d627f215f3e3 Mon Sep 17 00:00:00 2001 From: Thijs Louisse Date: Mon, 2 Jan 2023 14:33:32 +0100 Subject: [PATCH] chore: lint errors in scripts --- scripts/check-npm-dist-tag.mjs | 10 ++++++++-- scripts/lint-versions.js | 18 +++++++++++++++++- scripts/lock-scan.js | 2 +- scripts/rewrite-links.js | 19 +++++++++++++------ 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/scripts/check-npm-dist-tag.mjs b/scripts/check-npm-dist-tag.mjs index 5f9807f32..a55864519 100644 --- a/scripts/check-npm-dist-tag.mjs +++ b/scripts/check-npm-dist-tag.mjs @@ -7,11 +7,14 @@ import { readdirSync } from 'fs'; const exec = promisify(execCallback); -const getDirectories = source => +const getDirectories = (/** @type {string} */ source) => readdirSync(source, { withFileTypes: true }) .filter(pathMeta => pathMeta.isDirectory()) .map(pathMeta => pathMeta.name); +/** + * @param {string} folder + */ async function checkNpmDistTag(folder) { const actions = []; console.log('| Name | Local Version | NPM dist tag latest | Check |'); @@ -26,7 +29,10 @@ async function checkNpmDistTag(folder) { const latestVersion = stdout.trim(); console.log( - `| ${name.padEnd(24, ' ')} | ${version.padEnd(13, ' ')} | ${latestVersion.padEnd(19, ' ')} | ${version !== latestVersion ? ' ❌ ' : ' ✓ '} |`, + `| ${name.padEnd(24, ' ')} | ${version.padEnd(13, ' ')} | ${latestVersion.padEnd( + 19, + ' ', + )} | ${version !== latestVersion ? ' ❌ ' : ' ✓ '} |`, ); if (version !== latestVersion) { diff --git a/scripts/lint-versions.js b/scripts/lint-versions.js index b994d88d5..8b38ecf05 100644 --- a/scripts/lint-versions.js +++ b/scripts/lint-versions.js @@ -1,11 +1,14 @@ const { readdirSync, existsSync, readFileSync } = require('fs'); const semver = require('semver'); -const getDirectories = source => +const getDirectories = (/** @type {string} */ source) => readdirSync(source, { withFileTypes: true }) .filter(pathMeta => pathMeta.isDirectory()) .map(pathMeta => pathMeta.name); +/** + * @param {string} filePath + */ function readPackageJsonDeps(filePath) { if (existsSync(filePath)) { const jsonData = JSON.parse(readFileSync(filePath, 'utf-8')); @@ -24,6 +27,9 @@ function readPackageJsonDeps(filePath) { return {}; } +/** + * @param {string} filePath + */ function readPackageJsonNameVersion(filePath) { if (existsSync(filePath)) { const jsonData = JSON.parse(readFileSync(filePath, 'utf-8')); @@ -34,6 +40,12 @@ function readPackageJsonNameVersion(filePath) { return {}; } +/** + * + * @param {object} versionsA + * @param {object} versionsB + * @returns + */ function compareVersions(versionsA, versionsB) { let output = ''; const newVersions = { ...versionsA }; @@ -46,6 +58,7 @@ function compareVersions(versionsA, versionsB) { const rangeA = semver.validRange(versionsA[dep]); const rangeB = semver.validRange(versionsB[dep]); if ( + // @ts-ignore !(rangeA && rangeB && (semver.subset(rangeA, rangeB) || semver.subset(rangeB, rangeA))) ) { output += ` - "${dep}" "${versionsB[dep]}" does not seem semver compatible with "${versionsA[dep]}" and probably one of them needs a bump"\n`; @@ -64,6 +77,9 @@ function compareVersions(versionsA, versionsB) { } let endReturn = 0; +/** + * @param {string} folder + */ function lintVersions(folder) { let currentVersions = readPackageJsonDeps('./package.json'); diff --git a/scripts/lock-scan.js b/scripts/lock-scan.js index d1f0a8f2e..b65a968f8 100644 --- a/scripts/lock-scan.js +++ b/scripts/lock-scan.js @@ -5,7 +5,7 @@ const lockFileContent = fs.readFileSync(path.resolve('./package-lock.json'), 'ut const allowedRegistries = ['registry.yarnpkg.com', 'registry.npmjs.org']; const resolvedUrls = lockFileContent.match(/"resolved": "https:.*"/g); -resolvedUrls.forEach(url => { +resolvedUrls?.forEach(url => { const [, registry] = url.match(/^"resolved": "https:\/\/(.*?)\/.*"$/) || []; if (!allowedRegistries.includes(registry)) { throw new Error( diff --git a/scripts/rewrite-links.js b/scripts/rewrite-links.js index 122d89444..2495041f1 100644 --- a/scripts/rewrite-links.js +++ b/scripts/rewrite-links.js @@ -22,10 +22,10 @@ const gatherFilesConfig = { * @param {string} startPath - local filesystem path * @param {object} cfg - configuration object * @param {string} cfg.extension - file extension like '.md' - * @param {array} cfg.excludeFiles - file names filtered out - * @param {array} cfg.excludeFolders - folder names filtered out - * @param {array} result - list of file paths - * @returns {array} result list of file paths + * @param {string[]} cfg.excludeFiles - file names filtered out + * @param {string[]} cfg.excludeFolders - folder names filtered out + * @param {string[]} result - list of file paths + * @returns {string[]} result list of file paths */ function gatherFilesFromDir(startPath, cfg = gatherFilesConfig, result = []) { const files = fs.readdirSync(startPath); @@ -59,7 +59,7 @@ function gatherFilesFromDir(startPath, cfg = gatherFilesConfig, result = []) { * @returns {string} adjusted contents of input md file (mdContent) */ function rewriteLinksInMdContent(mdContent, filePath, cfg = rewriteLinksConfig) { - const rewrite = href => { + const rewrite = (/** @type {string} */ href) => { let newHref = href; const isRelativeUrlPattern = /^(\.\/|\.\.\/)/; // starts with './' or '../' if (href.match(isRelativeUrlPattern)) { @@ -74,8 +74,13 @@ function rewriteLinksInMdContent(mdContent, filePath, cfg = rewriteLinksConfig) return newHref; }; - const mdLink = (href, title, text) => `[${text}](${rewrite(href)}${title ? ` ${title}` : ''})`; + const mdLink = ( + /** @type {string} */ href, + /** @type {string} */ title, + /** @type {string} */ text, + ) => `[${text}](${rewrite(href)}${title ? ` ${title}` : ''})`; + /** @type {string[]} */ const resultLinks = []; // /^!?\[(label)\]\(href(?:\s+(title))?\s*\)/ const linkPattern = '!?\\[(.*)\\]\\(([^|\\s]*)( +(.*))?\\s*\\)'; // eslint-disable-line @@ -93,6 +98,7 @@ function rewriteLinksInMdContent(mdContent, filePath, cfg = rewriteLinksConfig) // Now that we have our rewritten links, stitch back together the desired result const tokenPattern = /!?\[.*\]\([^|\s]*(?: +.*)?\s*\)/; const tokens = mdContent.split(new RegExp(tokenPattern, 'g')); + /** @type {string[]} */ const resultTokens = []; tokens.forEach((token, i) => { resultTokens.push(token + (resultLinks[i] || '')); @@ -105,6 +111,7 @@ function rewriteLinksInMdContent(mdContent, filePath, cfg = rewriteLinksConfig) * Main code */ function main({ dryRun } = { dryRun: false }) { + /** @type {string[]} */ const mdFilePaths = gatherFilesFromDir(process.cwd()); // [path.resolve(__dirname, '../', 'packages/field/README.md')]; mdFilePaths.forEach(filePath => { const content = fs.readFileSync(filePath).toString();