simple-photo-gallery 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +349 -0
- package/dist/src/index.js +29 -0
- package/dist/src/modules/build/index.js +56 -0
- package/dist/src/modules/build/types/index.js +1 -0
- package/dist/src/modules/build/utils/index.js +7 -0
- package/dist/src/modules/init/const/index.js +4 -0
- package/dist/src/modules/init/index.js +156 -0
- package/dist/src/modules/init/types/index.js +1 -0
- package/dist/src/modules/init/utils/index.js +93 -0
- package/dist/src/modules/thumbnails/index.js +98 -0
- package/dist/src/modules/thumbnails/types/index.js +1 -0
- package/dist/src/modules/thumbnails/utils/index.js +127 -0
- package/dist/src/types/index.js +35 -0
- package/dist/src/utils/index.js +34 -0
- package/dist/tests/cli.test.js +170 -0
- package/package.json +61 -0
- package/src/index.ts +50 -0
- package/src/modules/build/index.ts +68 -0
- package/src/modules/build/types/index.ts +4 -0
- package/src/modules/build/utils/index.ts +9 -0
- package/src/modules/init/const/index.ts +5 -0
- package/src/modules/init/index.ts +193 -0
- package/src/modules/init/types/index.ts +16 -0
- package/src/modules/init/types/node-ffprobe.d.ts +17 -0
- package/src/modules/init/utils/index.ts +98 -0
- package/src/modules/thumbnails/index.ts +121 -0
- package/src/modules/thumbnails/types/index.ts +5 -0
- package/src/modules/thumbnails/utils/index.ts +162 -0
- package/src/types/index.ts +46 -0
- package/src/utils/index.ts +37 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createImageThumbnail, createVideoThumbnail } from './utils';
|
|
4
|
+
import { GalleryDataSchema } from '../../types';
|
|
5
|
+
import { findGalleries } from '../../utils';
|
|
6
|
+
const processGallery = async (galleryDir, size) => {
|
|
7
|
+
const galleryJsonPath = path.join(galleryDir, 'gallery', 'gallery.json');
|
|
8
|
+
const thumbnailsPath = path.join(galleryDir, 'gallery', 'thumbnails');
|
|
9
|
+
console.log(`\nProcessing gallery in: ${galleryDir}`);
|
|
10
|
+
try {
|
|
11
|
+
// Ensure thumbnails directory exists
|
|
12
|
+
fs.mkdirSync(thumbnailsPath, { recursive: true });
|
|
13
|
+
// Read gallery.json
|
|
14
|
+
const galleryContent = fs.readFileSync(galleryJsonPath, 'utf8');
|
|
15
|
+
const galleryData = GalleryDataSchema.parse(JSON.parse(galleryContent));
|
|
16
|
+
// Process all sections and their images
|
|
17
|
+
let processedCount = 0;
|
|
18
|
+
for (const section of galleryData.sections) {
|
|
19
|
+
for (const [index, mediaFile] of section.images.entries()) {
|
|
20
|
+
section.images[index] = await processMediaFile(mediaFile, galleryDir, thumbnailsPath, size);
|
|
21
|
+
}
|
|
22
|
+
processedCount += section.images.length;
|
|
23
|
+
}
|
|
24
|
+
// Write updated gallery.json
|
|
25
|
+
fs.writeFileSync(galleryJsonPath, JSON.stringify(galleryData, null, 2));
|
|
26
|
+
return processedCount;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.error(`Error creating thumbnails for ${galleryDir}:`, error);
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
async function processMediaFile(mediaFile, galleryDir, thumbnailsPath, size) {
|
|
34
|
+
try {
|
|
35
|
+
// Resolve the path relative to the gallery.json file location, not the gallery directory
|
|
36
|
+
const galleryJsonDir = path.join(galleryDir, 'gallery');
|
|
37
|
+
const filePath = path.resolve(path.join(galleryJsonDir, mediaFile.path));
|
|
38
|
+
const fileName = path.basename(filePath);
|
|
39
|
+
const fileNameWithoutExt = path.parse(fileName).name;
|
|
40
|
+
const thumbnailFileName = `${fileNameWithoutExt}.jpg`;
|
|
41
|
+
const thumbnailPath = path.join(thumbnailsPath, thumbnailFileName);
|
|
42
|
+
const relativeThumbnailPath = path.relative(galleryJsonDir, thumbnailPath);
|
|
43
|
+
console.log(`Processing ${mediaFile.type}: ${fileName}`);
|
|
44
|
+
let thumbnailDimensions;
|
|
45
|
+
if (mediaFile.type === 'image') {
|
|
46
|
+
thumbnailDimensions = await createImageThumbnail(filePath, thumbnailPath, size);
|
|
47
|
+
}
|
|
48
|
+
else if (mediaFile.type === 'video') {
|
|
49
|
+
thumbnailDimensions = await createVideoThumbnail(filePath, thumbnailPath, size);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
console.warn(`Unknown media type: ${mediaFile.type}, skipping...`);
|
|
53
|
+
return mediaFile;
|
|
54
|
+
}
|
|
55
|
+
// Update media file with thumbnail information
|
|
56
|
+
const updatedMediaFile = {
|
|
57
|
+
...mediaFile,
|
|
58
|
+
thumbnail: {
|
|
59
|
+
path: relativeThumbnailPath,
|
|
60
|
+
width: thumbnailDimensions.width,
|
|
61
|
+
height: thumbnailDimensions.height,
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
return updatedMediaFile;
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error instanceof Error && error.message === 'FFMPEG_NOT_AVAILABLE') {
|
|
68
|
+
console.warn(`⚠ Skipping video thumbnail (ffmpeg not available): ${path.basename(mediaFile.path)}`);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
console.error(`Error processing ${mediaFile.path}:`, error);
|
|
72
|
+
}
|
|
73
|
+
return mediaFile;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export async function thumbnails(options) {
|
|
77
|
+
const size = Number.parseInt(options.size);
|
|
78
|
+
// Find all gallery directories
|
|
79
|
+
const galleryDirs = findGalleries(options.path, options.recursive);
|
|
80
|
+
// If no galleries are found, exit
|
|
81
|
+
if (galleryDirs.length === 0) {
|
|
82
|
+
console.log('No gallery/gallery.json files found.');
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// Process each gallery directory
|
|
86
|
+
let totalProcessed = 0;
|
|
87
|
+
for (const galleryDir of galleryDirs) {
|
|
88
|
+
const processed = await processGallery(galleryDir, size);
|
|
89
|
+
totalProcessed += processed;
|
|
90
|
+
}
|
|
91
|
+
// Log processing stats
|
|
92
|
+
if (options.recursive) {
|
|
93
|
+
console.log(`Completed processing ${totalProcessed} total media files across ${galleryDirs.length} galleries.`);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
console.log(`Completed processing ${totalProcessed} media files.`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { promises as fs } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import ffprobe from 'node-ffprobe';
|
|
5
|
+
import sharp from 'sharp';
|
|
6
|
+
// Check if ffmpeg is available
|
|
7
|
+
async function checkFfmpegAvailability() {
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
const ffmpeg = spawn('ffmpeg', ['-version']);
|
|
10
|
+
ffmpeg.on('close', (code) => {
|
|
11
|
+
resolve(code === 0);
|
|
12
|
+
});
|
|
13
|
+
ffmpeg.on('error', () => {
|
|
14
|
+
resolve(false);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
// Utility function to resize and save thumbnail
|
|
19
|
+
async function resizeAndSaveThumbnail(image, outputPath, width, height) {
|
|
20
|
+
await image.resize(width, height, { withoutEnlargement: true }).jpeg({ quality: 90 }).toFile(outputPath);
|
|
21
|
+
}
|
|
22
|
+
export async function createImageThumbnail(inputPath, outputPath, height) {
|
|
23
|
+
try {
|
|
24
|
+
// Check if input file exists
|
|
25
|
+
await fs.access(inputPath);
|
|
26
|
+
// Create thumbnail using sharp
|
|
27
|
+
const image = sharp(inputPath);
|
|
28
|
+
const metadata = await image.metadata();
|
|
29
|
+
const originalWidth = metadata.width || 0;
|
|
30
|
+
const originalHeight = metadata.height || 0;
|
|
31
|
+
if (originalWidth === 0 || originalHeight === 0) {
|
|
32
|
+
throw new Error('Invalid image dimensions');
|
|
33
|
+
}
|
|
34
|
+
// Calculate width maintaining aspect ratio
|
|
35
|
+
const aspectRatio = originalWidth / originalHeight;
|
|
36
|
+
const width = Math.round(height * aspectRatio);
|
|
37
|
+
await resizeAndSaveThumbnail(image, outputPath, width, height);
|
|
38
|
+
return { width, height };
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
throw new Error(`Failed to create image thumbnail for ${inputPath}: ${error}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export async function createVideoThumbnail(inputPath, outputPath, height) {
|
|
45
|
+
try {
|
|
46
|
+
// Check if input file exists
|
|
47
|
+
await fs.access(inputPath);
|
|
48
|
+
// Check if ffmpeg is available
|
|
49
|
+
const ffmpegAvailable = await checkFfmpegAvailability();
|
|
50
|
+
if (!ffmpegAvailable) {
|
|
51
|
+
console.warn(`Warning: ffmpeg is not available. Skipping thumbnail creation for video: ${path.basename(inputPath)}`);
|
|
52
|
+
throw new Error('FFMPEG_NOT_AVAILABLE');
|
|
53
|
+
}
|
|
54
|
+
// Get video metadata using ffprobe
|
|
55
|
+
const videoData = await ffprobe(inputPath);
|
|
56
|
+
const videoStream = videoData.streams.find((stream) => stream.codec_type === 'video');
|
|
57
|
+
if (!videoStream) {
|
|
58
|
+
throw new Error('No video stream found');
|
|
59
|
+
}
|
|
60
|
+
const originalWidth = videoStream.width || 0;
|
|
61
|
+
const originalHeight = videoStream.height || 0;
|
|
62
|
+
if (originalWidth === 0 || originalHeight === 0) {
|
|
63
|
+
throw new Error('Invalid video dimensions');
|
|
64
|
+
}
|
|
65
|
+
// Calculate width maintaining aspect ratio
|
|
66
|
+
const aspectRatio = originalWidth / originalHeight;
|
|
67
|
+
const width = Math.round(height * aspectRatio);
|
|
68
|
+
// Use ffmpeg to extract first frame as a temporary file, then process with sharp
|
|
69
|
+
const tempFramePath = `${outputPath}.temp.png`;
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
// Extract first frame using ffmpeg
|
|
72
|
+
const ffmpeg = spawn('ffmpeg', [
|
|
73
|
+
'-i',
|
|
74
|
+
inputPath,
|
|
75
|
+
'-vframes',
|
|
76
|
+
'1',
|
|
77
|
+
'-y', // Overwrite output file
|
|
78
|
+
tempFramePath,
|
|
79
|
+
]);
|
|
80
|
+
ffmpeg.stderr.on('data', (data) => {
|
|
81
|
+
// FFmpeg writes normal output to stderr, so we don't treat this as an error
|
|
82
|
+
console.debug(`ffmpeg: ${data.toString()}`);
|
|
83
|
+
});
|
|
84
|
+
ffmpeg.on('close', async (code) => {
|
|
85
|
+
if (code === 0) {
|
|
86
|
+
try {
|
|
87
|
+
// Process the extracted frame with sharp
|
|
88
|
+
const frameImage = sharp(tempFramePath);
|
|
89
|
+
await resizeAndSaveThumbnail(frameImage, outputPath, width, height);
|
|
90
|
+
// Clean up temporary file
|
|
91
|
+
try {
|
|
92
|
+
await fs.unlink(tempFramePath);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
// Ignore cleanup errors
|
|
96
|
+
}
|
|
97
|
+
resolve({ width, height });
|
|
98
|
+
}
|
|
99
|
+
catch (sharpError) {
|
|
100
|
+
reject(new Error(`Failed to process extracted frame: ${sharpError}`));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
reject(new Error(`ffmpeg exited with code ${code}`));
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
ffmpeg.on('error', (error) => {
|
|
108
|
+
reject(new Error(`Failed to start ffmpeg: ${error.message}`));
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error instanceof Error && error.message === 'FFMPEG_NOT_AVAILABLE') {
|
|
114
|
+
// Re-throw the specific error for graceful handling upstream
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
if (typeof error === 'object' &&
|
|
118
|
+
error !== null &&
|
|
119
|
+
'message' in error &&
|
|
120
|
+
typeof error.message === 'string' &&
|
|
121
|
+
(error.message.includes('ffmpeg') ||
|
|
122
|
+
error.message.includes('ffprobe'))) {
|
|
123
|
+
throw new Error(`Error: ffmpeg is required to process videos. Please install ffmpeg and ensure it is available in your PATH. Failed to process: ${path.basename(inputPath)}`);
|
|
124
|
+
}
|
|
125
|
+
throw new Error(`Failed to create video thumbnail for ${inputPath}: ${error}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export const ThumbnailSchema = z.object({
|
|
3
|
+
path: z.string(),
|
|
4
|
+
width: z.number(),
|
|
5
|
+
height: z.number(),
|
|
6
|
+
});
|
|
7
|
+
export const MediaFileSchema = z.object({
|
|
8
|
+
type: z.enum(['image', 'video']),
|
|
9
|
+
path: z.string(),
|
|
10
|
+
alt: z.string().optional(),
|
|
11
|
+
width: z.number(),
|
|
12
|
+
height: z.number(),
|
|
13
|
+
thumbnail: ThumbnailSchema.optional(),
|
|
14
|
+
});
|
|
15
|
+
export const GallerySectionSchema = z.object({
|
|
16
|
+
title: z.string().optional(),
|
|
17
|
+
description: z.string().optional(),
|
|
18
|
+
images: z.array(MediaFileSchema),
|
|
19
|
+
});
|
|
20
|
+
export const SubGallerySchema = z.object({
|
|
21
|
+
title: z.string(),
|
|
22
|
+
headerImage: z.string(),
|
|
23
|
+
path: z.string(),
|
|
24
|
+
});
|
|
25
|
+
export const GalleryDataSchema = z.object({
|
|
26
|
+
title: z.string(),
|
|
27
|
+
description: z.string(),
|
|
28
|
+
headerImage: z.string(),
|
|
29
|
+
metadata: z.object({
|
|
30
|
+
ogUrl: z.string(),
|
|
31
|
+
}),
|
|
32
|
+
galleryOutputPath: z.string().optional(),
|
|
33
|
+
sections: z.array(GallerySectionSchema),
|
|
34
|
+
subGalleries: z.object({ title: z.string(), galleries: z.array(SubGallerySchema) }),
|
|
35
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* Finds all gallery directories that contain a gallery/gallery.json file.
|
|
5
|
+
*
|
|
6
|
+
* @param basePath - The base directory to search from
|
|
7
|
+
* @param recursive - Whether to search subdirectories recursively
|
|
8
|
+
* @returns Array of paths to directories containing gallery/gallery.json files
|
|
9
|
+
*/
|
|
10
|
+
export const findGalleries = (basePath, recursive) => {
|
|
11
|
+
const galleryDirs = [];
|
|
12
|
+
// Check basePath itself
|
|
13
|
+
const galleryJsonPath = path.join(basePath, 'gallery', 'gallery.json');
|
|
14
|
+
if (fs.existsSync(galleryJsonPath)) {
|
|
15
|
+
galleryDirs.push(basePath);
|
|
16
|
+
}
|
|
17
|
+
// If recursive, search all subdirectories
|
|
18
|
+
if (recursive) {
|
|
19
|
+
try {
|
|
20
|
+
const entries = fs.readdirSync(basePath, { withFileTypes: true });
|
|
21
|
+
for (const entry of entries) {
|
|
22
|
+
if (entry.isDirectory() && entry.name !== 'gallery') {
|
|
23
|
+
const subPath = path.join(basePath, entry.name);
|
|
24
|
+
const subResults = findGalleries(subPath, recursive);
|
|
25
|
+
galleryDirs.push(...subResults);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// Silently ignore errors when reading directories
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return galleryDirs;
|
|
34
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { execSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, rmSync, readFileSync, readdirSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { copySync } from 'fs-extra';
|
|
6
|
+
import { GalleryDataSchema, MediaFileSchema, ThumbnailSchema } from '../src/types';
|
|
7
|
+
const testDir = process.cwd();
|
|
8
|
+
const tsxPath = path.resolve(testDir, 'node_modules', '.bin', 'tsx');
|
|
9
|
+
const cliPath = path.resolve(testDir, 'src', 'index.ts');
|
|
10
|
+
const singleFixturePath = path.resolve(testDir, 'tests', 'fixtures', 'single');
|
|
11
|
+
const singleTestPath = path.resolve(testDir, 'tests', 'fixtures', 'test', 'single');
|
|
12
|
+
const multiFixturePath = path.resolve(testDir, 'tests', 'fixtures', 'multi');
|
|
13
|
+
const multiTestPath = path.resolve(testDir, 'tests', 'fixtures', 'test', 'multi');
|
|
14
|
+
const MediaFileWithThumbnailSchema = MediaFileSchema.extend({
|
|
15
|
+
thumbnail: ThumbnailSchema,
|
|
16
|
+
});
|
|
17
|
+
// Helper functions for gallery validation
|
|
18
|
+
function validateGalleryStructure(galleryPath, expectedImageCount, expectedSubGalleryCount = 0) {
|
|
19
|
+
// Check that gallery.json exists
|
|
20
|
+
const galleryJsonPath = path.resolve(galleryPath, 'gallery.json');
|
|
21
|
+
expect(existsSync(galleryJsonPath)).toBe(true);
|
|
22
|
+
// Read and parse gallery.json
|
|
23
|
+
const galleryContent = readFileSync(galleryJsonPath, 'utf8');
|
|
24
|
+
const galleryData = JSON.parse(galleryContent);
|
|
25
|
+
// Validate with schema
|
|
26
|
+
expect(() => GalleryDataSchema.parse(galleryData)).not.toThrow();
|
|
27
|
+
const validatedData = GalleryDataSchema.parse(galleryData);
|
|
28
|
+
// Check image count
|
|
29
|
+
expect(validatedData.sections).toHaveLength(1);
|
|
30
|
+
expect(validatedData.sections[0].images).toHaveLength(expectedImageCount);
|
|
31
|
+
// Check subgallery count
|
|
32
|
+
expect(validatedData.subGalleries.galleries).toHaveLength(expectedSubGalleryCount);
|
|
33
|
+
return validatedData;
|
|
34
|
+
}
|
|
35
|
+
function validateThumbnails(galleryPath, expectedThumbnailCount) {
|
|
36
|
+
// Check that thumbnails directory exists
|
|
37
|
+
const thumbnailsPath = path.resolve(galleryPath, 'thumbnails');
|
|
38
|
+
expect(existsSync(thumbnailsPath)).toBe(true);
|
|
39
|
+
// Check thumbnail file count
|
|
40
|
+
const thumbnailFiles = readdirSync(thumbnailsPath);
|
|
41
|
+
expect(thumbnailFiles).toHaveLength(expectedThumbnailCount);
|
|
42
|
+
// Read and validate gallery.json with thumbnails
|
|
43
|
+
const galleryJsonPath = path.resolve(galleryPath, 'gallery.json');
|
|
44
|
+
const galleryContent = readFileSync(galleryJsonPath, 'utf8');
|
|
45
|
+
const galleryData = JSON.parse(galleryContent);
|
|
46
|
+
const validatedData = GalleryDataSchema.parse(galleryData);
|
|
47
|
+
// Validate all images have thumbnails
|
|
48
|
+
for (const image of validatedData.sections[0].images) {
|
|
49
|
+
expect(() => MediaFileWithThumbnailSchema.parse(image)).not.toThrow();
|
|
50
|
+
}
|
|
51
|
+
return validatedData;
|
|
52
|
+
}
|
|
53
|
+
function validateBuildOutput(testPath, galleryPath) {
|
|
54
|
+
// Check that index.html exists
|
|
55
|
+
const indexPath = path.resolve(testPath, 'index.html');
|
|
56
|
+
expect(existsSync(indexPath)).toBe(true);
|
|
57
|
+
// Check that gallery files still exist
|
|
58
|
+
const galleryJsonPath = path.resolve(galleryPath, 'gallery.json');
|
|
59
|
+
const galleryFiles = readdirSync(galleryPath);
|
|
60
|
+
expect(galleryFiles).toContain('thumbnails');
|
|
61
|
+
expect(galleryFiles).toContain('gallery.json');
|
|
62
|
+
// Validate gallery.json is still valid
|
|
63
|
+
const galleryContent = readFileSync(galleryJsonPath, 'utf8');
|
|
64
|
+
const galleryData = JSON.parse(galleryContent);
|
|
65
|
+
expect(() => GalleryDataSchema.parse(galleryData)).not.toThrow();
|
|
66
|
+
}
|
|
67
|
+
describe('Single-folder gallery', () => {
|
|
68
|
+
beforeAll(() => {
|
|
69
|
+
if (existsSync(singleTestPath)) {
|
|
70
|
+
rmSync(singleTestPath, { recursive: true, force: true });
|
|
71
|
+
}
|
|
72
|
+
copySync(singleFixturePath, singleTestPath);
|
|
73
|
+
});
|
|
74
|
+
afterAll(() => {
|
|
75
|
+
if (existsSync(singleTestPath)) {
|
|
76
|
+
rmSync(singleTestPath, { recursive: true, force: true });
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
describe('init command', () => {
|
|
80
|
+
test('should create gallery.json with correct structure and content', () => {
|
|
81
|
+
const galleryPath = path.resolve(singleTestPath, 'gallery');
|
|
82
|
+
// Run init command
|
|
83
|
+
execSync(`${tsxPath} ${cliPath} init --path ${singleTestPath}`);
|
|
84
|
+
// Validate gallery structure
|
|
85
|
+
validateGalleryStructure(galleryPath, 3, 0);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
describe('thumbnails command', () => {
|
|
89
|
+
test('should create thumbnails for all images and update gallery.json', () => {
|
|
90
|
+
const galleryPath = path.resolve(singleTestPath, 'gallery');
|
|
91
|
+
// Run thumbnails command (init should have been run by previous test)
|
|
92
|
+
execSync(`${tsxPath} ${cliPath} thumbnails --path ${singleTestPath}`);
|
|
93
|
+
// Validate thumbnails using helper
|
|
94
|
+
validateThumbnails(galleryPath, 3);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
describe('build command', () => {
|
|
98
|
+
test('should create static HTML files and gallery assets', () => {
|
|
99
|
+
const galleryPath = path.resolve(singleTestPath, 'gallery');
|
|
100
|
+
// Run build command (init and thumbnails should have been run by previous tests)
|
|
101
|
+
execSync(`${tsxPath} ${cliPath} build --path ${singleTestPath}`);
|
|
102
|
+
// Validate build output using helper
|
|
103
|
+
validateBuildOutput(singleTestPath, galleryPath);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
describe('Multi-folder gallery', () => {
|
|
108
|
+
beforeAll(() => {
|
|
109
|
+
if (existsSync(multiTestPath)) {
|
|
110
|
+
rmSync(multiTestPath, { recursive: true, force: true });
|
|
111
|
+
}
|
|
112
|
+
copySync(multiFixturePath, multiTestPath);
|
|
113
|
+
});
|
|
114
|
+
afterAll(() => {
|
|
115
|
+
if (existsSync(multiTestPath)) {
|
|
116
|
+
rmSync(multiTestPath, { recursive: true, force: true });
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
describe('init command with recursive option', () => {
|
|
120
|
+
test('should create gallery.json files with subgalleries for multi-folder structure', () => {
|
|
121
|
+
// Run init command with recursive option
|
|
122
|
+
execSync(`${tsxPath} ${cliPath} init --path ${multiTestPath} -r`);
|
|
123
|
+
// Validate main gallery (3 root images + 2 subgalleries)
|
|
124
|
+
const mainGalleryPath = path.resolve(multiTestPath, 'gallery');
|
|
125
|
+
const mainValidatedData = validateGalleryStructure(mainGalleryPath, 3, 2);
|
|
126
|
+
// Check subgalleries metadata
|
|
127
|
+
expect(mainValidatedData.subGalleries.galleries).toHaveLength(2);
|
|
128
|
+
const subGalleryTitles = mainValidatedData.subGalleries.galleries.map((sg) => sg.title);
|
|
129
|
+
expect(subGalleryTitles).toContain('First');
|
|
130
|
+
expect(subGalleryTitles).toContain('Second');
|
|
131
|
+
// Validate first subgallery (2 images)
|
|
132
|
+
const firstGalleryPath = path.resolve(multiTestPath, 'first', 'gallery');
|
|
133
|
+
validateGalleryStructure(firstGalleryPath, 2, 0);
|
|
134
|
+
// Validate second subgallery (2 images)
|
|
135
|
+
const secondGalleryPath = path.resolve(multiTestPath, 'second', 'gallery');
|
|
136
|
+
validateGalleryStructure(secondGalleryPath, 2, 0);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
describe('thumbnails command with recursive option', () => {
|
|
140
|
+
test('should create thumbnails for all galleries recursively', () => {
|
|
141
|
+
// Run thumbnails command with recursive option
|
|
142
|
+
execSync(`${tsxPath} ${cliPath} thumbnails --path ${multiTestPath} -r`);
|
|
143
|
+
// Validate thumbnails for main gallery
|
|
144
|
+
const mainGalleryPath = path.resolve(multiTestPath, 'gallery');
|
|
145
|
+
validateThumbnails(mainGalleryPath, 3);
|
|
146
|
+
// Validate thumbnails for first subgallery
|
|
147
|
+
const firstGalleryPath = path.resolve(multiTestPath, 'first', 'gallery');
|
|
148
|
+
validateThumbnails(firstGalleryPath, 2);
|
|
149
|
+
// Validate thumbnails for second subgallery
|
|
150
|
+
const secondGalleryPath = path.resolve(multiTestPath, 'second', 'gallery');
|
|
151
|
+
validateThumbnails(secondGalleryPath, 2);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
describe('build command with recursive option', () => {
|
|
155
|
+
test('should build static files for all galleries recursively', () => {
|
|
156
|
+
// Run build command with recursive option
|
|
157
|
+
execSync(`${tsxPath} ${cliPath} build --path ${multiTestPath} -r`);
|
|
158
|
+
// Validate build output for main gallery
|
|
159
|
+
const mainGalleryPath = path.resolve(multiTestPath, 'gallery');
|
|
160
|
+
validateBuildOutput(multiTestPath, mainGalleryPath);
|
|
161
|
+
// Validate build output for subgalleries
|
|
162
|
+
const firstGalleryPath = path.resolve(multiTestPath, 'first', 'gallery');
|
|
163
|
+
const firstTestPath = path.resolve(multiTestPath, 'first');
|
|
164
|
+
validateBuildOutput(firstTestPath, firstGalleryPath);
|
|
165
|
+
const secondGalleryPath = path.resolve(multiTestPath, 'second', 'gallery');
|
|
166
|
+
const secondTestPath = path.resolve(multiTestPath, 'second');
|
|
167
|
+
validateBuildOutput(secondTestPath, secondGalleryPath);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "simple-photo-gallery",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Simple Photo Gallery CLI",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Vladimir Haltakov, Tomasz Rusin",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/SimplePhotoGallery/core"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://simple.photo",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"src"
|
|
15
|
+
],
|
|
16
|
+
"bin": {
|
|
17
|
+
"gallery": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"gallery": "tsx src/index.ts",
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"check": "tsc --noEmit && eslint . && prettier --check .",
|
|
23
|
+
"lint": "eslint .",
|
|
24
|
+
"lint:fix": "eslint . --fix",
|
|
25
|
+
"format": "prettier --write .",
|
|
26
|
+
"format:fix": "prettier --write .",
|
|
27
|
+
"test": "jest"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@simple-photo-gallery/theme-modern": "0.0.1",
|
|
31
|
+
"commander": "^12.0.0",
|
|
32
|
+
"exif-reader": "^2.0.1",
|
|
33
|
+
"node-ffprobe": "^3.0.0",
|
|
34
|
+
"sharp": "^0.33.0",
|
|
35
|
+
"zod": "^4.0.14"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
39
|
+
"@eslint/js": "^9.30.1",
|
|
40
|
+
"@types/fs-extra": "^11.0.4",
|
|
41
|
+
"@types/jest": "^30.0.0",
|
|
42
|
+
"@types/node": "^24.0.10",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
|
44
|
+
"@typescript-eslint/parser": "^8.38.0",
|
|
45
|
+
"eslint": "^9.30.1",
|
|
46
|
+
"eslint-config-prettier": "^10.1.5",
|
|
47
|
+
"eslint-import-resolver-alias": "^1.1.2",
|
|
48
|
+
"eslint-import-resolver-typescript": "^4.4.4",
|
|
49
|
+
"eslint-plugin-import": "^2.31.0",
|
|
50
|
+
"eslint-plugin-prettier": "^5.5.1",
|
|
51
|
+
"eslint-plugin-unicorn": "^59.0.1",
|
|
52
|
+
"fs-extra": "^11.3.0",
|
|
53
|
+
"jest": "^30.0.5",
|
|
54
|
+
"prettier": "^3.4.2",
|
|
55
|
+
"ts-jest": "^29.4.1",
|
|
56
|
+
"tsx": "^4.20.3",
|
|
57
|
+
"typescript": "^5.9.0",
|
|
58
|
+
"typescript-eslint": "^8.38.0"
|
|
59
|
+
},
|
|
60
|
+
"type": "module"
|
|
61
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
|
|
5
|
+
import { Command } from 'commander';
|
|
6
|
+
|
|
7
|
+
import { build } from './modules/build';
|
|
8
|
+
import { init } from './modules/init';
|
|
9
|
+
import { thumbnails } from './modules/thumbnails';
|
|
10
|
+
|
|
11
|
+
const program = new Command();
|
|
12
|
+
|
|
13
|
+
program.name('gallery').description('Simple Photo Gallery CLI').version('0.0.1');
|
|
14
|
+
|
|
15
|
+
program
|
|
16
|
+
.command('init')
|
|
17
|
+
.description('Initialize a gallery by scaning a folder for images and videos')
|
|
18
|
+
.option(
|
|
19
|
+
'-p, --path <path>',
|
|
20
|
+
'Path where the gallery should be initialized. Default: current working directory',
|
|
21
|
+
process.cwd(),
|
|
22
|
+
)
|
|
23
|
+
.option('-o, --output <path>', 'Output directory for the gallery.json file', '')
|
|
24
|
+
.option('-r, --recursive', 'Scan subdirectories recursively', false)
|
|
25
|
+
.action(init);
|
|
26
|
+
|
|
27
|
+
program
|
|
28
|
+
.command('thumbnails')
|
|
29
|
+
.description('Create thumbnails for all media files in the gallery')
|
|
30
|
+
.option(
|
|
31
|
+
'-p, --path <path>',
|
|
32
|
+
'Path to the folder containing the gallery.json file. Default: current working directory',
|
|
33
|
+
process.cwd(),
|
|
34
|
+
)
|
|
35
|
+
.option('-s, --size <size>', 'Thumbnail height in pixels', '200')
|
|
36
|
+
.option('-r, --recursive', 'Scan subdirectories recursively', false)
|
|
37
|
+
.action(thumbnails);
|
|
38
|
+
|
|
39
|
+
program
|
|
40
|
+
.command('build')
|
|
41
|
+
.description('Build the HTML gallery in the specified directory')
|
|
42
|
+
.option(
|
|
43
|
+
'-p, --path <path>',
|
|
44
|
+
'Path to the folder containing the gallery.json file. Default: current working directory',
|
|
45
|
+
process.cwd(),
|
|
46
|
+
)
|
|
47
|
+
.option('-r, --recursive', 'Scan subdirectories recursively', false)
|
|
48
|
+
.action(build);
|
|
49
|
+
|
|
50
|
+
program.parse();
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { execSync } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
|
|
6
|
+
import { findGalleries } from '../../utils';
|
|
7
|
+
|
|
8
|
+
import type { BuildOptions } from './types';
|
|
9
|
+
|
|
10
|
+
function buildGallery(galleryDir: string, templateDir: string) {
|
|
11
|
+
// Make sure the gallery.json file exists
|
|
12
|
+
const galleryJsonPath = path.join(galleryDir, 'gallery', 'gallery.json');
|
|
13
|
+
if (!fs.existsSync(galleryJsonPath)) {
|
|
14
|
+
console.log(`No gallery/gallery.json found in ${galleryDir}`);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Build the template
|
|
19
|
+
const originalEnv = { ...process.env };
|
|
20
|
+
try {
|
|
21
|
+
// Set the environment variable for the gallery.json path that will be used by the template
|
|
22
|
+
process.env.GALLERY_JSON_PATH = galleryJsonPath;
|
|
23
|
+
process.env.GALLERY_OUTPUT_DIR = path.join(galleryDir, 'gallery');
|
|
24
|
+
|
|
25
|
+
execSync('yarn build', { cwd: templateDir, stdio: 'inherit' });
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.error(error);
|
|
28
|
+
console.error(`Build failed for ${galleryDir}`);
|
|
29
|
+
return;
|
|
30
|
+
} finally {
|
|
31
|
+
// Restore original environment and gallery.json
|
|
32
|
+
process.env = originalEnv;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Copy the build output to the output directory
|
|
36
|
+
const outputDir = path.join(galleryDir, 'gallery');
|
|
37
|
+
const buildDir = path.join(outputDir, '_build');
|
|
38
|
+
fs.cpSync(buildDir, outputDir, { recursive: true });
|
|
39
|
+
|
|
40
|
+
// Move the index.html to the gallery directory
|
|
41
|
+
fs.copyFileSync(path.join(outputDir, 'index.html'), path.join(galleryDir, 'index.html'));
|
|
42
|
+
fs.rmSync(path.join(outputDir, 'index.html'));
|
|
43
|
+
|
|
44
|
+
// Clean up the _build directory
|
|
45
|
+
console.log('Cleaning up build directory...');
|
|
46
|
+
fs.rmSync(buildDir, { recursive: true, force: true });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function build(options: BuildOptions): Promise<void> {
|
|
50
|
+
// Get the template directory
|
|
51
|
+
// Resolve the theme-modern package directory
|
|
52
|
+
const themePath = await import.meta.resolve('@simple-photo-gallery/theme-modern/package.json');
|
|
53
|
+
const themeDir = path.dirname(new URL(themePath).pathname);
|
|
54
|
+
|
|
55
|
+
// Find all gallery directories
|
|
56
|
+
const galleryDirs = findGalleries(options.path, options.recursive);
|
|
57
|
+
|
|
58
|
+
// If no galleries are found, exit
|
|
59
|
+
if (galleryDirs.length === 0) {
|
|
60
|
+
console.log('No gallery/gallery.json files found.');
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Process each gallery
|
|
65
|
+
for (const dir of galleryDirs) {
|
|
66
|
+
buildGallery(path.resolve(dir), themeDir);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
// __dirname workaround for ESM modules
|
|
4
|
+
const __dirname = path.dirname(new URL(import.meta.url).pathname);
|
|
5
|
+
|
|
6
|
+
// Helper function to resolve paths relative to current file
|
|
7
|
+
export const resolveFromCurrentDir = (...segments: string[]): string => {
|
|
8
|
+
return path.resolve(__dirname, ...segments);
|
|
9
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Image extensions
|
|
2
|
+
export const IMAGE_EXTENSIONS = new Set(['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.tiff', '.tif', '.svg']);
|
|
3
|
+
|
|
4
|
+
// Video extensions
|
|
5
|
+
export const VIDEO_EXTENSIONS = new Set(['.mp4', '.avi', '.mov', '.wmv', '.flv', '.webm', '.mkv', '.m4v', '.3gp']);
|