Skip to content

Commit

Permalink
updated to an insane new eslint setup, linted files
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotberry committed Apr 24, 2024
1 parent bd7e9cf commit e379415
Show file tree
Hide file tree
Showing 13 changed files with 1,400 additions and 985 deletions.
35 changes: 35 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import stylistic from '@stylistic/eslint-plugin-js'
import pluginImport from 'eslint-plugin-import'
import perfectionist from 'eslint-plugin-perfectionist'
import prettierConfig from 'eslint-plugin-prettier/recommended'
import is from 'eslint-plugin-simple-import-sort'
import eslintPluginUnicorn from 'eslint-plugin-unicorn'

export default [
prettierConfig,
eslintPluginUnicorn.configs['flat/recommended'],

{
files: ['**/*.js'],
plugins: {
stylistic,
pluginImport,
'simple-import-sort': is,
perfectionist,
import: pluginImport,
},
languageOptions: {
ecmaVersion: 2024,
sourceType: 'module',
},

rules: {
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',

'no-unused-vars': 'warn',
'no-console': 'off',
'unicorn/prefer-top-level-await': 'off',
},
},
]
99 changes: 49 additions & 50 deletions lib/closest-parent-info.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
import { promises as fs } from 'fs';
import path from 'path';
import { promises as fs } from 'node:fs'
import path from 'node:path'

async function findClosestParentDir(path1, path2) {
// Normalize the paths to eliminate any redundant navigation
const absolutePath1 = path.resolve(path1);
const absolutePath2 = path.resolve(path2);

// Split paths into components
const parts1 = absolutePath1.split(path.sep);
const parts2 = absolutePath2.split(path.sep);

// Find common path parts
const length = Math.min(parts1.length, parts2.length);
let i = 0;
while (i < length && parts1[i] === parts2[i]) {
i++;
}

// If there is no common path, return null
if (i === 0) {
return null;
}

// Construct the common directory path
const commonPath = parts1.slice(0, i).join(path.sep);
const commonDirName = parts1[i - 1];

// Check if the common directory exists
try {
await fs.access(commonPath);
} catch (error) {
throw new Error(`The common directory does not exist: ${commonPath}`);
}

// Calculate relative paths
const relativePath1 = path.relative(commonPath, absolutePath1);
const relativePath2 = path.relative(commonPath, absolutePath2);

// Return the result as an object
const obj = {
name: commonDirName,
path: commonPath,
relativePaths: {
a: relativePath1,
b: relativePath2,
},
};
return `.../${obj.name}/${obj.relativePaths.a} -> .../${obj.name}/${obj.relativePaths.b}`;
}
const baseLog = async (fpath) => {
// Normalize the paths to eliminate any redundant navigation
const absolutePath1 = path.resolve(path1)
const absolutePath2 = path.resolve(path2)

// Split paths into components
const parts1 = absolutePath1.split(path.sep)
const parts2 = absolutePath2.split(path.sep)

// Find common path parts
const length = Math.min(parts1.length, parts2.length)
let index = 0
while (index < length && parts1[index] === parts2[index]) {
index++
}

// If there is no common path, return null
if (index === 0) {
return null
}

// Construct the common directory path
const commonPath = parts1.slice(0, index).join(path.sep)
const commonDirName = parts1[index - 1]

// Check if the common directory exists
try {
await fs.access(commonPath)
} catch {
throw new Error(`The common directory does not exist: ${commonPath}`)
}

// Calculate relative paths
const relativePath1 = path.relative(commonPath, absolutePath1)
const relativePath2 = path.relative(commonPath, absolutePath2)

// Return the result as an object
const object = {
name: commonDirName,
path: commonPath,
relativePaths: {
a: relativePath1,
b: relativePath2,
},
}
return `.../${object.name}/${object.relativePaths.a} -> .../${object.name}/${object.relativePaths.b}`
}
const baseLog = async (fpath) => {}

export {findClosestParentDir, baseLog};
export { baseLog, findClosestParentDir }
21 changes: 12 additions & 9 deletions lib/file-path-info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from 'path'
import { slugify } from './slugify.js'
import path from 'node:path'

import chalk from 'chalk'

import { slugify } from './slugify.js'

// Takes a file path and an optional boolean flag as input.
// Extracts information about the file, such as the directory, file name, and file extension.
// Creates a new file name that is a URL-friendly version of the original file name and returns an object that contains the old file path and the new file path if the new file name is different from the original file name. Otherwise, it returns null.
Expand All @@ -14,11 +16,10 @@ const getFilePathInfo = async (
try {
let newName
let currentPath = path.parse(path.resolve(filePath))
if (fileNumber !== null) {
newName = `${fileNumber}`
} else {
newName = await slugify(currentPath.name)
}
newName =
fileNumber === null
? await slugify(currentPath.name)
: `${fileNumber}`
let newExtension = currentPath.ext
if (fixTildes) {
newExtension = currentPath.ext.split('~').join('')
Expand All @@ -31,10 +32,12 @@ const getFilePathInfo = async (
old: path.join(currentPath.dir, currentPath.name + currentPath.ext),
new: newPath,
}
} catch (e) {
} catch (error) {
!global.silent &&
console.warn(
chalk.yellow(`error processing ${filePath}: ${e.toString()}`)
chalk.yellow(
`error processing ${filePath}: ${error.toString()}`
)
)
}
return returnValue
Expand Down
39 changes: 18 additions & 21 deletions lib/ignore.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import config from './rc.js'
let ignoreFn
let ignoreFunction

const getIgnores = () => {
let ret = null
let returnValue = null
const ignores = config.ignores
if (Array.isArray(ignores)) {
if (ignores.length > 0) {
ret = ignores
}
if (Array.isArray(ignores) && ignores.length > 0) {
returnValue = ignores
}
if (ret === null) {
if (returnValue === null) {
throw new Error(`ignore config in the config file is not valid`)
}
return ret
return returnValue
}

const init = () => {
const conf = getIgnores()
global.debug && console.log(`conf: ${conf}`)
if (conf !== null) {
const config_ = getIgnores()
global.debug && console.log(`conf: ${config_}`)
if (config_ === null) {
return false
} else {
let arrayOfFns = []


const shouldFilterOut = (filePath) => {
let nameToCompare = filePath.oldParsed.base
let resp = true
for (let ignore of conf) {
for (let ignore of config_) {
if (ignore === nameToCompare) {
resp = false
break
Expand All @@ -34,19 +33,17 @@ const init = () => {
return resp
}
return shouldFilterOut
} else {
return false
}
}

const ignore = async (arrayOfFilePaths) => {
ignoreFn = await init()
if (typeof ignoreFn !== 'function') {
return arrayOfFilePaths
}
else {
let newArrayOfFilePathsFiltered = arrayOfFilePaths.filter(ignoreFn)
ignoreFunction = await init()
if (typeof ignoreFunction === 'function') {
let newArrayOfFilePathsFiltered =
arrayOfFilePaths.filter(ignoreFunction)
return newArrayOfFilePathsFiltered
} else {
return arrayOfFilePaths
}
}

Expand Down
40 changes: 22 additions & 18 deletions lib/log-changes.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import fs from 'fs';
import path from 'path';
import os from 'os';
import chalk from 'chalk';
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'

import chalk from 'chalk'

//
// Logchanged filenames to disk. not currently used.
//


const logDir = path.join(os.homedir(), '.config');
const logDir = path.join(os.homedir(), '.config')

// Ensure the log directory exists
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
fs.mkdirSync(logDir)
}

function logChanges(arr) {
const currentDate = new Date();
const logFileName = `${currentDate.toISOString().split('T')[0]}.log`;
const logFilePath = path.join(logDir, logFileName);
function logChanges(array) {
const currentDate = new Date()
const logFileName = `${currentDate.toISOString().split('T')[0]}.log`
const logFilePath = path.join(logDir, logFileName)

// Write the array and the current date to the log file
const logContent = `Date: ${currentDate.toISOString()}\nData: ${JSON.stringify(arr, null, 2)}\n`;
fs.writeFileSync(logFilePath, logContent);
const logContent = `Date: ${currentDate.toISOString()}\nData: ${JSON.stringify(array, null, 2)}\n`
fs.writeFileSync(logFilePath, logContent)

// Check for older logs and truncate if there are more than 10
const logFiles = fs.readdirSync(logDir).filter(file => file.endsWith('.log')).sort();
const logFiles = fs
.readdirSync(logDir)
.filter((file) => file.endsWith('.log'))
.sort()
while (logFiles.length > 10) {
const oldestLog = logFiles.shift();
fs.unlinkSync(path.join(logDir, oldestLog));
const oldestLog = logFiles.shift()
fs.unlinkSync(path.join(logDir, oldestLog))
}
!global.silent && console.log(chalk.green(`logged changes to ${logFilePath}`));
!global.silent &&
console.log(chalk.green(`logged changes to ${logFilePath}`))
}

export default logChanges;
export default logChanges
Loading

0 comments on commit e379415

Please sign in to comment.