forked from sumcumo/imagemin-merlin
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use simple-git for staging support
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
Showing
4 changed files
with
98 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters