Skip to content

Commit

Permalink
feat: use simple-git for staging support
Browse files Browse the repository at this point in the history
Replaced staged-git-files with simple-git for handling staged files in imagemin-guard. Added a new test to verify image compression of staged files. Updated dependencies to remove staged-git-files in favor of simple-git.

(This commit message was AI-generated.)

Signed-off-by: Jens Oliver Meiert <[email protected]>
  • Loading branch information
j9t committed Oct 5, 2024
1 parent abcb30b commit ac45a9a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 37 deletions.
38 changes: 33 additions & 5 deletions bin/imagemin-guard.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')
const simpleGit = require('simple-git')
const testFolder = path.join(__dirname, '../media/test')
const tempFolder = path.join(__dirname, '../media/temp')
const testFolderGit = path.join(__dirname, '../media/test-git')
const imageminGuardScript = path.join(__dirname, '../bin/imagemin-guard.js')
// Crutch to avoid files like .DS_Store to sneak in
// @@ Consolidate with package, to keep image definitions DRY
Expand Down Expand Up @@ -55,27 +56,54 @@ function areImagesAlreadyCompressed(dir) {
describe('Imagemin Guard', () => {
beforeAll(() => {
// Backup original images
copyFiles(testFolder, tempFolder)
copyFiles(testFolder, testFolderGit)
})

afterAll(() => {
// Clean up temporary folder
fs.rmSync(tempFolder, { recursive: true })
fs.rmSync(testFolderGit, { recursive: true })
})

test('Compress images from media/test folder (in temp location)', () => {
// Ensure images in temp folder are not already compressed
expect(areImagesAlreadyCompressed(tempFolder)).toBe(true)
expect(areImagesAlreadyCompressed(testFolderGit)).toBe(true)

// Run imagemin-guard script
execSync(`node ${imageminGuardScript} --ignore=media/test`)

// Verify images are compressed
const { allCompressed, uncompressedFiles } = areImagesCompressed(tempFolder)
const { allCompressed, uncompressedFiles } = areImagesCompressed(testFolderGit)
if (uncompressedFiles.length > 0) {
// @@ Ensure all compressed files are listed
console.log('The following files were not compressed:', uncompressedFiles.join(', '))
}
expect(allCompressed).toBe(true)
})

test('Compress only staged images from media/test folder (in temp location)', async () => {
const git = simpleGit(testFolderGit)

// Ensure the temp folder exists
if (!fs.existsSync(testFolderGit)) {
fs.mkdirSync(testFolderGit, { recursive: true })
}

// Initialize a temporary Git repository
await git.init()
await git.addConfig('user.name', 'Test User')
await git.addConfig('user.email', '[email protected]')

// Stage files
await git.add('.')

// Run imagemin-guard script with --staged option
execSync(`node ${imageminGuardScript} --staged`, { cwd: testFolderGit })

// Verify images are compressed
const { allCompressed, uncompressedFiles } = areImagesCompressed(testFolderGit)
if (uncompressedFiles.length > 0) {
console.log('The following files were not compressed:', uncompressedFiles.join(', '))
}
expect(allCompressed).toBe(true)
})
})
74 changes: 55 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
"imagemin-mozjpeg": "^10.0.0",
"imagemin-webp": "^8.0.0",
"rimraf": "^6.0.1",
"staged-git-files": "^1.3.0",
"simple-git": "^3.27.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"jest": "^29.7.0"
}
}
}
19 changes: 8 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { rimraf } from 'rimraf'
import { globbySync } from 'globby'
import sgf from 'staged-git-files'
import simpleGit from 'simple-git'
import { utils } from './utils.js'
import _yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
Expand Down Expand Up @@ -70,17 +70,14 @@ import { hideBin } from 'yargs/helpers'

// Search for staged files
if (argv.staged) {
await sgf('A', async function (err, results) {
if (err) {
return console.error(err)
}

compressionFiles = results
.map(result => result.filename)
.filter(filename => files.includes(filename))

const git = simpleGit()
try {
const status = await git.status()
compressionFiles = status.staged.filter(filename => files.includes(filename))
await compress(compressionFiles, argv.dry)
})
} catch (err) {
console.error(err)
}
} else {
await compress(compressionFiles, argv.dry)
}
Expand Down

0 comments on commit ac45a9a

Please sign in to comment.