chore: lint errors in scripts

This commit is contained in:
Thijs Louisse 2023-01-02 14:33:32 +01:00 committed by Thijs Louisse
parent 6007ca46f7
commit a92622f753
4 changed files with 39 additions and 10 deletions

View file

@ -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) {

View file

@ -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');

View file

@ -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(

View file

@ -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();