Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotberry committed Apr 24, 2024
1 parent e379415 commit 6e613a8
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 134 deletions.
40 changes: 18 additions & 22 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node
import chalk from 'chalk'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'

import detawks from './index.js'
import { stringModificationFunctions } from './lib/slugify.js'
import chalk from 'chalk'

const main = async () => {
const parser = await yargs(hideBin(process.argv))
Expand Down Expand Up @@ -63,18 +64,13 @@ const main = async () => {
.alias('h', 'help')

const argv = await parser.parse()
let globPattern;
let globPattern

if (argv._.length === 1) {
globPattern = argv._[0]
}
else {
globPattern = argv._
}
globPattern = argv._.length === 1 ? argv._[0] : argv._
if (argv.l) {
for (const [key] of Object.entries(stringModificationFunctions)) {
let a = stringModificationFunctions[key]
console.log(`${a.name}${a.description ? ': ' + a.description : ''}`)
const a = stringModificationFunctions[key]
console.log(`${a.name}${a.description ? `: ${a.description}` : ''}`)
}

process.exit(0)
Expand All @@ -84,13 +80,13 @@ const main = async () => {
process.exit(1)
}

let dryrun = argv.d
let directories = argv.f
let rename = argv.r
let silent = argv.s
let verbose = argv.v
let numbered = argv.n
let userOpts = {
const dryrun = argv.d
const directories = argv.f
const rename = argv.r
const silent = argv.s
const verbose = argv.v
const numbered = argv.n
const userOptions = {
numbered,
verbose,
dryrun,
Expand All @@ -99,14 +95,14 @@ const main = async () => {
silent,
}
if (argv.m) {
let maxDepth = parseInt(argv.m)
if (!isNaN(maxDepth)) {
userOpts.maxDepth = maxDepth
const maxDepth = Number.parseInt(argv.m)
if (!Number.isNaN(maxDepth)) {
userOptions.maxDepth = maxDepth
}
}
let opts = userOpts
const options = userOptions
//console.log(argv)
await detawks(globPattern, opts)
await detawks(globPattern, options)
}
}
main()
45 changes: 30 additions & 15 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
import stylistic from '@stylistic/eslint-plugin-js'
import stylistic from '@stylistic/eslint-plugin'
import pluginImport from 'eslint-plugin-import'
import perfectionist from 'eslint-plugin-perfectionist'
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'
import prettierConfig from 'eslint-plugin-prettier/recommended'
import promise from 'eslint-plugin-promise'
import is from 'eslint-plugin-simple-import-sort'
import eslintPluginUnicorn from 'eslint-plugin-unicorn'

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

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

},

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

'no-unused-vars': 'warn',
'no-console': 'off',
'no-unused-vars': 'warn',

'promise/always-return': 'error',
'promise/avoid-new': 'warn',
'promise/catch-or-return': 'error',
'promise/no-callback-in-promise': 'warn',
'promise/no-native': 'off',
'promise/no-nesting': 'warn',
'promise/no-new-statics': 'error',
'promise/no-promise-in-callback': 'warn',
'promise/no-return-in-finally': 'warn',
'promise/no-return-wrap': 'error',
'promise/param-names': 'error',
'promise/valid-params': 'warn',
'simple-import-sort/exports': 'error',
'simple-import-sort/imports': 'error',
'unicorn/no-null': 'warn',
'unicorn/prefer-top-level-await': 'off',
'unicorn/prevent-abbreviations': 'warn',
},
},
]
67 changes: 36 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,88 @@
import chalk from 'chalk'

import { baseLog } from './lib/closest-parent-info.js'
import getFilePathInfo from './lib/file-path-info.js'
import ignore from './lib/ignore.js'
import processOneFile from './lib/process-one-file.js'
import useFdir from './lib/useFdir.js'
import { validateAndFormatInput } from './lib/path-validation.js'
import { baseLog } from './lib/closest-parent-info.js'
import chalk from 'chalk'
import processOneFile from './lib/process-one-file.js'
import config from './lib/rc.js'
import useFdir from './lib/useFdir.js'

const logIgnored = (arrayOfFilePaths, lengthBefore, opts, files) => {
const logIgnored = (arrayOfFilePaths, lengthBefore, options, files) => {
if (arrayOfFilePaths.length === 0) {
!global.silent &&
console.log(chalk.blue(`no files to rename, as all were ignored.`))
process.exit(0)
throw new Error('no files to rename.')
}
!global.silent &&
console.log(`ignored ${lengthBefore - arrayOfFilePaths.length} files.`)
!global.silent &&
console.log(
chalk.blue(
`${arrayOfFilePaths.length}/${files.length} ${
opts.dryrun ? `would` : `will`
options.dryrun ? `would` : `will`
} be renamed.`
)
)
}
const filesFoundInfo = (files, inputStr) => {
const filesFoundInfo = (files, inputString) => {
const numberFilesFoundInGlob = files.length
if (numberFilesFoundInGlob === 1) {
!global.silent && console.log(chalk.green(`found a file: ${files[0]}.`))
} else {
!global.silent &&
console.log(
chalk.green(
`found ${numberFilesFoundInGlob} files in ${inputStr.path}.`
`found ${numberFilesFoundInGlob} files in ${inputString.path}.`
)
)
if (numberFilesFoundInGlob === 0) {
!global.silent && console.log(chalk.red(`thus, exiting.`))
process.exit(0)
throw new Error('no files found.')
}
}
}
const run = async (globPattern, userOpts) => {
const opts = userOpts ? Object.assign({}, config, userOpts) : config;
global.verbose = opts.verbose
global.silent = opts.silent
const run = async (globPattern, userOptions) => {
const options = userOptions
? Object.assign({}, config, userOptions)
: config
global.verbose = options.verbose
global.silent = options.silent

global.verbose && console.log(`DEBUG ON`)
global.verbose &&
console.log(`globPattern: ${globPattern}, userOpts: ${userOpts}`)
const inputStr = await validateAndFormatInput(globPattern)
console.log(`globPattern: ${globPattern}, userOpts: ${userOptions}`)
const inputString = await validateAndFormatInput(globPattern)
let files
if (inputStr.type === 'fileArray') {
files = inputStr.files
}
else if (inputStr.type === 'directory' || inputStr.type === 'glob') {
if (inputString.type === 'fileArray') {
files = inputString.files
} else if (
inputString.type === 'directory' ||
inputString.type === 'glob'
) {
files = await useFdir(
inputStr.path,
opts.maxDepth,
opts.directories,
inputStr.type === 'glob'
inputString.path,
options.maxDepth,
options.directories,
inputString.type === 'glob'
)
}
//is single file
else {
files = [inputStr.path]
files = [inputString.path]
}

//console log that we got some shit to process.
filesFoundInfo(files, inputStr)
filesFoundInfo(files, inputString)

//get pending new/old filepaths given our current options loadout
let arrayOfFilePaths = []
let fileNumber = 1
for await (const file of files) {
const filePathInfo = await getFilePathInfo(
file,
opts.fixTildes,
opts.numbered ? fileNumber++ : null
options.fixTildes,
options.numbered ? fileNumber++ : null
)

arrayOfFilePaths.push(filePathInfo)
Expand All @@ -88,16 +93,16 @@ const run = async (globPattern, userOpts) => {

const lengthBefore = arrayOfFilePaths.length
arrayOfFilePaths = await ignore(arrayOfFilePaths)
logIgnored(arrayOfFilePaths, lengthBefore, opts, files)
logIgnored(arrayOfFilePaths, lengthBefore, options, files)

if (opts.dryrun) {
if (options.dryrun) {
for await (const fileShit of arrayOfFilePaths) {
await baseLog(fileShit)
}
// console.log(resp.join('\n'))
} else {
for await (const file of arrayOfFilePaths) {
await processOneFile(file, opts.rename)
await processOneFile(file, options.rename)
await baseLog(file)
}
// await logChanges(arrayOfFilePaths)
Expand Down
6 changes: 3 additions & 3 deletions lib/__dirname.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fileURLToPath } from 'url'
import path from 'path'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))

export default path.resolve(`${__dirname}/../`)
export default path.resolve(`${__dirname}/../`)
11 changes: 11 additions & 0 deletions lib/exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import fs from 'node:fs/promises'
const exists = async (path) => {
try {
await fs.access(path)
return true
} catch {
return false
}
}

export default exists
9 changes: 4 additions & 5 deletions lib/file-path-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ const getFilePathInfo = async (
fixTildes = false,
fileNumber = null
) => {
let returnValue = null
try {
let newName
let currentPath = path.parse(path.resolve(filePath))
const currentPath = path.parse(path.resolve(filePath))
newName =
fileNumber === null
? await slugify(currentPath.name)
Expand All @@ -25,8 +24,8 @@ const getFilePathInfo = async (
newExtension = currentPath.ext.split('~').join('')
}

let newPath = path.join(currentPath.dir, newName + newExtension)
returnValue = {
const newPath = path.join(currentPath.dir, newName + newExtension)
return {
oldParsed: currentPath,
newParsed: path.parse(newPath),
old: path.join(currentPath.dir, currentPath.name + currentPath.ext),
Expand All @@ -40,6 +39,6 @@ const getFilePathInfo = async (
)
)
}
return returnValue
return null
}
export default getFilePathInfo
30 changes: 11 additions & 19 deletions lib/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,25 @@ const init = () => {
global.debug && console.log(`conf: ${config_}`)
if (config_ === null) {
return false
} else {
let arrayOfFns = []
}
const arrayOfFns = []

const shouldFilterOut = (filePath) => {
let nameToCompare = filePath.oldParsed.base
let resp = true
for (let ignore of config_) {
if (ignore === nameToCompare) {
resp = false
break
}
return (filePath) => {
const nameToCompare = filePath.oldParsed.base
for (const ignore of config_) {
if (ignore === nameToCompare) {
return false
}
return resp
}
return shouldFilterOut
return true
}
}

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

export default ignore
Loading

0 comments on commit 6e613a8

Please sign in to comment.