From 2db74b896cc869216c1584c68ab65ad1b4bf8fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?elliot=20b=20=20=3D=5E=2E=5F=2E=5E=3D=20=E2=88=AB?= Date: Thu, 25 Apr 2024 13:39:49 -0400 Subject: [PATCH] small functional updates --- cli.js | 31 +++++++++++++------------------ default.detawksrc | 3 +-- index.js | 2 +- lib/file-path-info.js | 8 +------- lib/ignore.js | 6 +++--- lib/path-validation.js | 7 ++++--- lib/process-one-file.js | 10 +++++----- lib/rc.js | 1 - lib/slugify.js | 12 ++++-------- test/createDirectoryWithFiles.js | 10 +++++++--- test/test.js | 29 ++++++++++++----------------- 11 files changed, 51 insertions(+), 68 deletions(-) diff --git a/cli.js b/cli.js index 3d23482..5784400 100644 --- a/cli.js +++ b/cli.js @@ -11,48 +11,42 @@ const main = async () => { .usage('Usage: $0 [options] ') .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', { @@ -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) @@ -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) @@ -101,7 +96,7 @@ const main = async () => { } } const options = userOptions - //console.log(argv) + await detawks(globPattern, options) } } diff --git a/default.detawksrc b/default.detawksrc index fb483ce..eee5678 100644 --- a/default.detawksrc +++ b/default.detawksrc @@ -8,8 +8,7 @@ "ignores": [ "node_modules", ".DS_Store", - ".git", - ".DS_Store" + ".git" ], "sequence": [ "toString", diff --git a/index.js b/index.js index f8b2c18..871c1ee 100644 --- a/index.js +++ b/index.js @@ -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.`) diff --git a/lib/file-path-info.js b/lib/file-path-info.js index ab78272..c2c5329 100644 --- a/lib/file-path-info.js +++ b/lib/file-path-info.js @@ -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. @@ -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 } diff --git a/lib/ignore.js b/lib/ignore.js index 0039ff4..3cc36fd 100644 --- a/lib/ignore.js +++ b/lib/ignore.js @@ -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 @@ -33,7 +33,7 @@ const init = () => { } const ignore = async (arrayOfFilePaths) => { - ignoreFunction = await init() + ignoreFunction = await ignoreFilterFactory() return typeof ignoreFunction === 'function' ? arrayOfFilePaths.filter(ignoreFunction) : arrayOfFilePaths diff --git a/lib/path-validation.js b/lib/path-validation.js index 6685b58..a66a724 100644 --- a/lib/path-validation.js +++ b/lib/path-validation.js @@ -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}`) } } diff --git a/lib/process-one-file.js b/lib/process-one-file.js index 2614117..7c9222b 100644 --- a/lib/process-one-file.js +++ b/lib/process-one-file.js @@ -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}`) @@ -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}`) diff --git a/lib/rc.js b/lib/rc.js index d610e53..39c7ecc 100644 --- a/lib/rc.js +++ b/lib/rc.js @@ -1,5 +1,4 @@ import fs from 'node:fs' - import rc from 'rc' import __dirname from './__dirname.js' diff --git a/lib/slugify.js b/lib/slugify.js index 262774a..f36fbd7 100644 --- a/lib/slugify.js +++ b/lib/slugify.js @@ -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 @@ -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, }) } } diff --git a/test/createDirectoryWithFiles.js b/test/createDirectoryWithFiles.js index b782be5..6d6f8e1 100644 --- a/test/createDirectoryWithFiles.js +++ b/test/createDirectoryWithFiles.js @@ -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 }) @@ -30,7 +32,7 @@ export const names = [ 'பூனைகள்', '✨🌀🌈🐱‍👤🐱‍🚀🐱‍🐉🐱‍💻👾🎃🕺💃🎉🎲🎸🚀🌠🌌🔮💎🎭🎨🖖🌀✨', ] -export async function createDirectoryWithFiles() { +async function createDirectoryWithFiles() { await deleteDirectoryAndFiles(); // Create directory await fs.promises.mkdir(dirPath); @@ -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 \ No newline at end of file diff --git a/test/test.js b/test/test.js index 0ddc725..1f62906 100644 --- a/test/test.js +++ b/test/test.js @@ -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++) { @@ -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()