Skip to content

Commit

Permalink
small functional updates
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotberry committed Apr 25, 2024
1 parent 4ff593e commit 2db74b8
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 68 deletions.
31 changes: 13 additions & 18 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,42 @@ const main = async () => {
.usage('Usage: $0 [options] <glob / file / dir>')
.option('s', {
alias: 'silent',
default: false,
describe:
'silent mode; e.g no console.log describing new file names',
type: 'boolean',
default: false,
})
.option('v', {
alias: 'verbose',
default: false,
describe:
'verbose mode; logs files renamed, as well as other useful information',
type: 'boolean',
default: false,
})
.option('d', {
alias: 'dryrun',
default: false,
describe: 'dry run, showing what files would be renamed',
type: 'boolean',
})
.option('r', {
alias: 'rename',
default: true,
describe:
'if overwrite possible, rename files AUTOMAGICALLY without prompting',
'if overwrite is possible, rename files AUTOMAGICALLY without prompting',
type: 'boolean',
})
.option('n', {
alias: 'numbered',

type: 'boolean',
default: false,
describe:
'numbered mode: fuck all the other renaming schemes, and just numbered all files 1-??.',
})
.option('f', {
alias: 'dirs',
default: false,
describe: 'include directories',
type: 'boolean',
default: false,
})
.option('m', {
alias: 'max-depth',
default: -1,
describe: 'max depth',
type: 'number',
default: -1,
})
//options to list all available string modification functions
.option('l', {
Expand All @@ -71,6 +65,7 @@ const main = async () => {
for (const [key] of Object.entries(stringModificationFunctions)) {
const a = stringModificationFunctions[key]
console.log(`${a.name}${a.description ? `: ${a.description}` : ''}`)

}

process.exit(0)
Expand All @@ -85,14 +80,14 @@ const main = async () => {
const rename = argv.r
const silent = argv.s
const verbose = argv.v
const numbered = argv.n

const userOptions = {
numbered,
verbose,
dryrun,
directories,
dryrun,
numbered,
rename,
silent,
verbose,
}
if (argv.m) {
const maxDepth = Number.parseInt(argv.m)
Expand All @@ -101,7 +96,7 @@ const main = async () => {
}
}
const options = userOptions
//console.log(argv)

await detawks(globPattern, options)
}
}
Expand Down
3 changes: 1 addition & 2 deletions default.detawksrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"ignores": [
"node_modules",
".DS_Store",
".git",
".DS_Store"
".git"
],
"sequence": [
"toString",
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const filesFoundInfo = (files, inputString) => {
} else {
!global.silent &&
console.log(
`found ${numberFilesFoundInGlob} files in ${inputString.path}.`
`found ${numberFilesFoundInGlob} files in ${inputString.path ? inputString.path : `'${inputString.input}'`}.`
)
if (numberFilesFoundInGlob === 0) {
!global.silent && console.log(`thus, exiting.`)
Expand Down
8 changes: 1 addition & 7 deletions lib/file-path-info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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.
Expand Down Expand Up @@ -33,11 +31,7 @@ const getFilePathInfo = async (
}
} catch (error) {
!global.silent &&
console.warn(
chalk.yellow(
`error processing ${filePath}: ${error.toString()}`
)
)
console.warn(`error processing ${filePath}: ${error.toString()}`)
}
return null
}
Expand Down
6 changes: 3 additions & 3 deletions lib/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const getIgnores = () => {
return returnValue
}

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


return (filePath) => {
const nameToCompare = filePath.oldParsed.base
Expand All @@ -33,7 +33,7 @@ const init = () => {
}

const ignore = async (arrayOfFilePaths) => {
ignoreFunction = await init()
ignoreFunction = await ignoreFilterFactory()
return typeof ignoreFunction === 'function'
? arrayOfFilePaths.filter(ignoreFunction)
: arrayOfFilePaths
Expand Down
7 changes: 4 additions & 3 deletions lib/path-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ export const validateAndFormatInput = async (
path: path.resolve(inputString),
type,
parent: parentdir,
input: inputString,
}
} else if (type === 'glob') {
return { path: inputString, type: 'glob' }
return { path: inputString, type: 'glob', input: inputString }
} else {
throw new Error(chalk.red(`invalid input: ${inputString}`))
throw new Error(`invalid input: ${inputString}`)
}
} catch {
throw new Error(chalk.red(`invalid input: ${inputString}`))
throw new Error(`invalid input: ${inputString}`)
}
}
10 changes: 5 additions & 5 deletions lib/process-one-file.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fsp from 'node:fs/promises'
import fs from 'node:fs/promises'
import path from 'node:path'

import exists from './exists.js'

const processOneFile = async (fileObject, rename = true, fixTildes = false) => {
const processOneFile = async (fileObject, rename = true) => {
try {
// !global.silent && console.log(`${f.old} -> ${f.new}`)

Expand All @@ -18,14 +18,14 @@ const processOneFile = async (fileObject, rename = true, fixTildes = false) => {
newName = `${parsed.name}-${index}${parsed.ext}`
}
fileObject.new = path.resolve(path.join(parsed.dir, newName))
await fsp.rename(fileObject.old, fileObject.new)
await fs.rename(fileObject.old, fileObject.new)
} else {
console.log(
console.warn(
`file already exists, doing nothing: ${fileObject.new}`
)
}
} else {
await fsp.rename(fileObject.old, fileObject.new)
await fs.rename(fileObject.old, fileObject.new)
}
} catch (error) {
console.log(`error processing ${fileObject.old}: ${error}`)
Expand Down
1 change: 0 additions & 1 deletion lib/rc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'node:fs'

import rc from 'rc'

import __dirname from './__dirname.js'
Expand Down
12 changes: 4 additions & 8 deletions lib/slugify.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import chalk from 'chalk'

import config from './rc.js'
import stringModificationFunctions from './string-modification-functions.js'
let arrayOfFunctions;
let slugifyFunction;
let arrayOfFunctions
let slugifyFunction

//
// async/does an array of functions on a string in a row, and accumulates the result
Expand All @@ -22,14 +20,12 @@ const init = () => {
for (const item of slugifyConfig) {
if (stringModificationFunctions[item.name] === undefined) {
console.warn(
chalk.red(
`String modification Function ${item.name} is not defined`
)
`String modification Function ${item.name} is not defined`
)
} else {
finalFunctions.push({
fn: stringModificationFunctions[item.name].fn,
args: item.args || {},
fn: stringModificationFunctions[item.name].fn,
})
}
}
Expand Down
10 changes: 7 additions & 3 deletions test/createDirectoryWithFiles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fs from 'fs';
import path from 'path';
import { deleteDirectoryAndFiles, dirPath } from './test.js';
import __dirname from '../lib/__dirname.js'

const dirPath = path.resolve(path.join(__dirname, 'test-assets'))
async function deleteDirectoryAndFiles() {
try {
await fs.promises.rm(dirPath, { recursive: true })
Expand Down Expand Up @@ -30,7 +32,7 @@ export const names = [
'பூனைகள்',
'✨🌀🌈🐱‍👤🐱‍🚀🐱‍🐉🐱‍💻👾🎃🕺💃🎉🎲🎸🚀🌠🌌🔮💎🎭🎨🖖🌀✨',
]
export async function createDirectoryWithFiles() {
async function createDirectoryWithFiles() {
await deleteDirectoryAndFiles();
// Create directory
await fs.promises.mkdir(dirPath);
Expand Down Expand Up @@ -59,6 +61,8 @@ export async function createDirectoryWithFiles() {
oneFilePath = filePath;
}
}
console.log(coolText(`created ${testFilesMade} test files`));
console.log(`created ${testFilesMade} test files`)
return oneFilePath;
}

export default createDirectoryWithFiles
29 changes: 12 additions & 17 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
import assert from 'assert'
import chalk from 'chalk'
import execa from 'elliotisms/lib/exec.js'
import fs from 'node:fs'
import execa from 'elliotisms/lib/exec.js'
import test from 'node:test'
import path from 'path'

import __dirname from '../lib/__dirname.js'
import { slugify } from '../lib/slugify.js'
import { createDirectoryWithFiles } from './createDirectoryWithFiles.js'
import createDirectoryWithFiles from './createDirectoryWithFiles.js'

export async function deleteDirectoryAndFiles() {
try {
await fs.promises.rm(dirPath, { recursive: true })

console.log('deleted test dir')
} catch {
// console.log(coolText('no folder to delete'))
}
}

export const dirPath = path.resolve(path.join(__dirname, 'test-assets'))
console.log(dirPath)
const dirPath = path.resolve(path.join(__dirname, 'test-assets'))
const app = 'node ./cli.js'
const main = async () => {
const app = 'node ./cli.js'

//a function that returns true if the string contains no non-ascii characters and npo underscores
function noNonASCIIChars(string_) {
for (let index = 0; index < string_.length; index++) {
Expand Down Expand Up @@ -126,3 +113,11 @@ const main = async () => {
})
}

const manualTest = async () => {
await createDirectoryWithFiles()
await execa(
`${app} "/Users/eberry/projects/___MY-SCRIPTS/detawks/test-assets" --dryrun`
)
}

manualTest()

0 comments on commit 2db74b8

Please sign in to comment.