Skip to content

Commit 4ff593e

Browse files
committed
misc and testing fixtures
1 parent 6e613a8 commit 4ff593e

File tree

7 files changed

+80
-79
lines changed

7 files changed

+80
-79
lines changed

eslint.config.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import stylistic from '@stylistic/eslint-plugin'
21
import pluginImport from 'eslint-plugin-import'
32
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'
43
import prettierConfig from 'eslint-plugin-prettier/recommended'
54
import promise from 'eslint-plugin-promise'
65
import is from 'eslint-plugin-simple-import-sort'
76
import eslintPluginUnicorn from 'eslint-plugin-unicorn'
7+
88
export default [
99
prettierConfig,
1010
eslintPluginUnicorn.configs['flat/recommended'],
1111
perfectionistNatural,
12-
stylistic.configs['recommended-flat'],
12+
1313
{
1414
files: ['**/*.js'],
1515
languageOptions: {
@@ -20,8 +20,7 @@ export default [
2020
import: pluginImport,
2121
pluginImport,
2222
promise,
23-
'simple-import-sort': is
24-
23+
'simple-import-sort': is,
2524
},
2625

2726
rules: {

index.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import chalk from 'chalk'
2-
31
import { baseLog } from './lib/closest-parent-info.js'
42
import getFilePathInfo from './lib/file-path-info.js'
53
import ignore from './lib/ignore.js'
@@ -11,33 +9,29 @@ import useFdir from './lib/useFdir.js'
119
const logIgnored = (arrayOfFilePaths, lengthBefore, options, files) => {
1210
if (arrayOfFilePaths.length === 0) {
1311
!global.silent &&
14-
console.log(chalk.blue(`no files to rename, as all were ignored.`))
12+
console.log(`no files to rename, as all were ignored.`)
1513
throw new Error('no files to rename.')
1614
}
1715
!global.silent &&
1816
console.log(`ignored ${lengthBefore - arrayOfFilePaths.length} files.`)
1917
!global.silent &&
2018
console.log(
21-
chalk.blue(
22-
`${arrayOfFilePaths.length}/${files.length} ${
23-
options.dryrun ? `would` : `will`
24-
} be renamed.`
25-
)
19+
`${arrayOfFilePaths.length}/${files.length} ${
20+
options.dryrun ? `would` : `will`
21+
} be renamed.`
2622
)
2723
}
2824
const filesFoundInfo = (files, inputString) => {
2925
const numberFilesFoundInGlob = files.length
3026
if (numberFilesFoundInGlob === 1) {
31-
!global.silent && console.log(chalk.green(`found a file: ${files[0]}.`))
27+
!global.silent && console.log(`found a file: ${files[0]}.`)
3228
} else {
3329
!global.silent &&
3430
console.log(
35-
chalk.green(
36-
`found ${numberFilesFoundInGlob} files in ${inputString.path}.`
37-
)
31+
`found ${numberFilesFoundInGlob} files in ${inputString.path}.`
3832
)
3933
if (numberFilesFoundInGlob === 0) {
40-
!global.silent && console.log(chalk.red(`thus, exiting.`))
34+
!global.silent && console.log(`thus, exiting.`)
4135
throw new Error('no files found.')
4236
}
4337
}
@@ -105,7 +99,6 @@ const run = async (globPattern, userOptions) => {
10599
await processOneFile(file, options.rename)
106100
await baseLog(file)
107101
}
108-
// await logChanges(arrayOfFilePaths)
109102
}
110103
}
111104
export default run

lib/file-path-info.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ const getFilePathInfo = async (
2626

2727
const newPath = path.join(currentPath.dir, newName + newExtension)
2828
return {
29-
oldParsed: currentPath,
29+
new: newPath,
3030
newParsed: path.parse(newPath),
3131
old: path.join(currentPath.dir, currentPath.name + currentPath.ext),
32-
new: newPath,
32+
oldParsed: currentPath,
3333
}
3434
} catch (error) {
3535
!global.silent &&

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"dependencies": {
4343
"chalk": "^5.3.0",
4444
"change-case": "^5.4.4",
45+
"elliotisms": "^0.0.14",
4546
"fdir": "^6.1.1",
4647
"picomatch": "^4.0.2",
4748
"rc": "^1.2.8",
@@ -64,6 +65,7 @@
6465
"eslint-plugin-perfectionist": "^2.9.0",
6566
"eslint-plugin-prettier": "^5.1.3",
6667
"eslint-plugin-promise": "^6.1.1",
68+
"eslint-plugin-security-node": "^1.1.4",
6769
"eslint-plugin-simple-import-sort": "^12.1.0",
6870
"eslint-plugin-unicorn": "^52.0.0",
6971
"prettier": "^3.2.5",

test/createDirectoryWithFiles.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import { deleteDirectoryAndFiles, dirPath, names, coolText } from './test.js';
3+
import { deleteDirectoryAndFiles, dirPath } from './test.js';
4+
async function deleteDirectoryAndFiles() {
5+
try {
6+
await fs.promises.rm(dirPath, { recursive: true })
47

8+
console.log('deleted test dir')
9+
} catch {
10+
// console.log(coolText('no folder to delete'))
11+
}
12+
}
13+
export const names = [
14+
'Persian',
15+
'Siamese',
16+
'Maine Coon',
17+
'Ragdoll',
18+
'Sphynx',
19+
'Bengal',
20+
'Abyssinian',
21+
'British Shorthair',
22+
'Scottish Fold',
23+
'Burmese',
24+
'Oriental',
25+
'Siberian',
26+
'Tonkinese',
27+
'Russian Blue',
28+
'Norwegian Forest',
29+
'кошки',
30+
'பூனைகள்',
31+
'✨🌀🌈🐱‍👤🐱‍🚀🐱‍🐉🐱‍💻👾🎃🕺💃🎉🎲🎸🚀🌠🌌🔮💎🎭🎨🖖🌀✨',
32+
]
533
export async function createDirectoryWithFiles() {
634
await deleteDirectoryAndFiles();
735
// Create directory

test/test.js

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,33 @@
1-
import fs from 'fs'
2-
import path from 'path'
3-
import detawks from '../index.js'
4-
import chalk from 'chalk'
5-
import { slugify } from '../lib/slugify.js'
6-
import { exec } from 'node:child_process'
71
import assert from 'assert'
8-
import { fileURLToPath } from 'url'
9-
import { expect } from 'chai'
10-
import { describe, it } from 'mocha'
2+
import chalk from 'chalk'
3+
import fs from 'node:fs'
4+
import execa from 'elliotisms/lib/exec.js'
115
import test from 'node:test'
6+
import path from 'path'
7+
128
import __dirname from '../lib/__dirname.js'
9+
import { slugify } from '../lib/slugify.js'
1310
import { createDirectoryWithFiles } from './createDirectoryWithFiles.js'
11+
1412
export async function deleteDirectoryAndFiles() {
1513
try {
1614
await fs.promises.rm(dirPath, { recursive: true })
1715

18-
console.log(coolText('deleted test dir'))
19-
} catch (e) {
16+
console.log('deleted test dir')
17+
} catch {
2018
// console.log(coolText('no folder to delete'))
2119
}
2220
}
23-
export const coolText = chalk.bgBlue.black //this part is important for the tests to run
24-
export const dirPath = path.resolve(path.join(__dirname, 'test-assets'))
25-
export const names = [
26-
'Persian',
27-
'Siamese',
28-
'Maine Coon',
29-
'Ragdoll',
30-
'Sphynx',
31-
'Bengal',
32-
'Abyssinian',
33-
'British Shorthair',
34-
'Scottish Fold',
35-
'Burmese',
36-
'Oriental',
37-
'Siberian',
38-
'Tonkinese',
39-
'Russian Blue',
40-
'Norwegian Forest',
41-
'кошки',
42-
'பூனைகள்',
43-
'✨🌀🌈🐱‍👤🐱‍🚀🐱‍🐉🐱‍💻👾🎃🕺💃🎉🎲🎸🚀🌠🌌🔮💎🎭🎨🖖🌀✨',
44-
]
4521

22+
export const dirPath = path.resolve(path.join(__dirname, 'test-assets'))
23+
console.log(dirPath)
4624
const main = async () => {
47-
const execa = async (cmd) =>
48-
new Promise((resolve, reject) => {
49-
exec(cmd, (error, stdout, stderr) => {
50-
if (error || stderr) {
51-
throw new Error(error || stderr)
52-
}
53-
resolve(stdout)
54-
})
55-
})
5625
const app = 'node ./cli.js'
5726

5827
//a function that returns true if the string contains no non-ascii characters and npo underscores
59-
function noNonASCIIChars(str) {
60-
for (let i = 0; i < str.length; i++) {
61-
const charCode = str.charCodeAt(i)
28+
function noNonASCIIChars(string_) {
29+
for (let index = 0; index < string_.length; index++) {
30+
const charCode = string_.charCodeAt(index)
6231
if (charCode > 127 || charCode === 95) {
6332
return false
6433
}
@@ -71,8 +40,8 @@ const main = async () => {
7140
try {
7241
let oneFilePath = await createDirectoryWithFiles()
7342
await execa(`${app} "${oneFilePath}" --dryrun --rename --silent`)
74-
} catch (e) {
75-
console.error(e)
43+
} catch (error) {
44+
console.error(error)
7645
failed = true
7746
}
7847
assert.strictEqual(failed, false)
@@ -93,8 +62,8 @@ const main = async () => {
9362
let files = await fs.promises.readdir(dirPath)
9463
//filter out files
9564
console.log(files)
96-
} catch (e) {
97-
console.error(e)
65+
} catch (error) {
66+
console.error(error)
9867
failed = true
9968
}
10069
assert.strictEqual(failed, false)
@@ -106,9 +75,9 @@ const main = async () => {
10675
console.log(coolText('Running tests with command line...'))
10776
console.log(coolText('Creating directory with files...'))
10877
let oneFilePath = await createDirectoryWithFiles()
109-
let opts = { dryrun: false, rename: true, silent: true }
78+
let options = { dryrun: false, rename: true, silent: true }
11079
console.log(coolText('Options for the tests:'))
111-
console.log(coolText(opts.toString()))
80+
console.log(coolText(options.toString()))
11281
console.log(
11382
coolText(
11483
`Running detawks against one file... (${oneFilePath}))`
@@ -126,8 +95,8 @@ const main = async () => {
12695
console.log(coolText('Deleting directory and files...'))
12796

12897
console.log(coolText('Tests finished!'))
129-
} catch (e) {
130-
console.error(e)
98+
} catch (error) {
99+
console.error(error)
131100
failed = true
132101
}
133102
assert.strictEqual(failed, false)
@@ -136,24 +105,24 @@ const main = async () => {
136105
await test('test basic string replacement', async (t) => {
137106
const testCases = [
138107
{
139-
input: 'Hello World',
140108
expected: 'hello-world',
109+
input: 'Hello World',
141110
},
142111
{
143-
input: 'Hello World',
144112
expected: 'hello-world',
113+
input: 'Hello World',
145114
},
146115
{
147-
input: 'Hello World',
148116
expected: 'hello-world',
117+
input: 'Hello World',
149118
},
150119
]
151120

152121
for await (let testCase of testCases) {
153-
const { input, expected } = testCase
122+
const { expected, input } = testCase
154123
const actual = await slugify(input)
155124
assert.strictEqual(expected, actual)
156125
}
157126
})
158127
}
159-
main()
128+

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,11 @@ electron-to-chromium@^1.4.668:
10761076
resolved "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.747.tgz#e37fa5b7b7e4c22607c5f59b5cf78f947266e77d"
10771077
integrity sha512-+FnSWZIAvFHbsNVmUxhEqWiaOiPMcfum1GQzlWCg/wLigVtshOsjXHyEFfmt6cFK6+HkS3QOJBv6/3OPumbBfw==
10781078

1079+
elliotisms@^0.0.14:
1080+
version "0.0.14"
1081+
resolved "https://registry.npmjs.org/elliotisms/-/elliotisms-0.0.14.tgz#b87f733a7b1014be0ca08fa357ee2b93d683eb19"
1082+
integrity sha512-NEheJmioYy/dxXW3MzfFLVYpa21sQXxaje8P4xrsbF/WT7cskIPF9gJKgnJ7r4aOpV+QrVB7m1ksJukRB4uH+w==
1083+
10791084
emoji-regex@^10.3.0:
10801085
version "10.3.0"
10811086
resolved "https://registry.npmjs.org/emoji-regex/-/emoji-regex-10.3.0.tgz#76998b9268409eb3dae3de989254d456e70cfe23"
@@ -1316,6 +1321,11 @@ eslint-plugin-promise@^6.1.1:
13161321
resolved "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.1.1.tgz#269a3e2772f62875661220631bd4dafcb4083816"
13171322
integrity sha512-tjqWDwVZQo7UIPMeDReOpUgHCmCiH+ePnVT+5zVapL0uuHnegBUs2smM13CzOs2Xb5+MHMRFTs9v24yjba4Oig==
13181323

1324+
eslint-plugin-security-node@^1.1.4:
1325+
version "1.1.4"
1326+
resolved "https://registry.npmjs.org/eslint-plugin-security-node/-/eslint-plugin-security-node-1.1.4.tgz#e78dbfa9d4852076d51ca512a28d6b4ead069792"
1327+
integrity sha512-8+agTMb2glNbP1zFhqo/Ixwtz16Hn0TvJW5KgpoHkAzGjDUhQf9iT+D6OgbhvZCMWRKMjc+5FbJ2Lh0UEUz7fQ==
1328+
13191329
eslint-plugin-simple-import-sort@^12.1.0:
13201330
version "12.1.0"
13211331
resolved "https://registry.npmjs.org/eslint-plugin-simple-import-sort/-/eslint-plugin-simple-import-sort-12.1.0.tgz#8186ad55474d2f5c986a2f1bf70625a981e30d05"

0 commit comments

Comments
 (0)