Skip to content

Commit

Permalink
Merge pull request #88 from meowtec/dev
Browse files Browse the repository at this point in the history
Version 0.6.0
  • Loading branch information
meowtec authored Apr 30, 2021
2 parents fd9e4b2 + 174832c commit 785663b
Show file tree
Hide file tree
Showing 64 changed files with 858 additions and 630 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,10 @@ chmod a+x Imagine-x.y.z-x86_64.AppImage # make executable
- Batch optimization
- i18n (English, 简体中文, Nederlands, Español, Français, Italiano, Deutsch)

## 'ImageMagick required'

Since version 0.3.0, you can choose the format that you want to convert to. For example, if you pick a PNG file, you can convert it to JPEG and compress it with MozJPEG optimizer.

But there is an exception: pngquant on windows can't read JPEG files, so if you want to convert JPEG to PNG, you should install ImageMagick firstly, and add it to your PATH environment variable.

Imagine use this command below to check if ImageMagick installed, so make sure it work.

```bash
magick -version
```

## Build and Contribute

```bash
git clone https://github.com/meowtec/Imagine.git
# node 8 is recommended
npm install
npm run dev
```
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<div id="app"></div>
<script>
(() => {
const url = process.env.NODE_ENV === 'development'
const url = process.env.IMAGINE_ENV === 'development'
? 'http://localhost:9999/dist/app.js'
: 'dist/app.js'
: 'dist/web/app.js'
const script = `<script src="${url}"><\/script>`
document.write(script)
})()
Expand Down
2 changes: 1 addition & 1 deletion modules/__tests__/backend/optimize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'path'
import optimize from '../../backend/optimize'
import { saveFilesTmp, getFilePath } from '../../common/file-utils'
import { fullDiff } from '../_tools/image-diff'
import { IImageFile, SupportedExt } from '../../common/constants'
import { IImageFile, SupportedExt } from '../../common/types'

const relPath = (file: string) => path.resolve(__dirname, file)

Expand Down
2 changes: 1 addition & 1 deletion modules/__tests__/common/file-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
flattenFiles,
reext,
} from '../../common/file-utils'
import { IImageFile, SupportedExt } from '../../common/constants'
import { IImageFile, SupportedExt } from '../../common/types'

const relPath = (file: string) => path.resolve(__dirname, file)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ import '../_tools/before-test'

import * as path from 'path'
import { createStore } from '../../renderer/store/store'
import Daemon from '../../renderer/store/daemon'
import JobRunner from '../../renderer/store/job-runner'
import actions from '../../renderer/store/actionCreaters'
import app from '../../backend/app'
import { saveFilesTmp } from '../../common/file-utils'
import { sleep } from '../../common/utils'
import {
IImageFile, IOptimizeOptions, ITaskItem, TaskStatus,
} from '../../common/constants'
IImageFile, TaskStatus,
} from '../../common/types'

jest.mock('../../renderer/apis/')

test('optimize daemon', async () => {
test('optimize JobRunner', async () => {
const images = await saveFilesTmp(
['600_600.png', 'qr.png'].map((x) => path.resolve(__dirname, '../_files', x)),
)
const store = createStore()
new Daemon().watch(store)
new JobRunner().watch(store)
let state

store.dispatch(actions.taskAdd(images as IImageFile[]))
Expand Down
2 changes: 1 addition & 1 deletion modules/__tests__/renderer/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ITaskItem,
TaskStatus,
SupportedExt,
} from '../../common/constants'
} from '../../common/types'

const image1: IImageFile = {
id: '01',
Expand Down
12 changes: 4 additions & 8 deletions modules/backend/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@ import {
IpcChannel,
SaveType,
IBackendState,
} from '../common/constants'
} from '../common/types'
import * as fu from '../common/file-utils'
import { listenIpc } from '../ipc-bridge/backend'
import optimize from './optimize'
import { saveFiles, saveFile } from './save'
import Menu from './menu'
import { detectImageMagick } from './imagemagick'
import __ from '../locales'
import { isDev } from '../common/env'
import { IS_DEV } from '../common/env'

if (isDev) {
if (IS_DEV) {
// eslint-disable-next-line global-require
require('./dev')
}

const url = `file://${path.resolve('index.html')}`
const url = `file://${path.resolve(app.getAppPath(), 'index.html')}`

class App {
private windows: number[] = []
Expand Down Expand Up @@ -173,8 +171,6 @@ class App {
listenIpc() {
listenIpc<IOptimizeRequest, IImageFile>(IpcChannel.OPTIMIZE, ({ image, exportExt, options }) => optimize(image, options))

listenIpc<void, boolean>(IpcChannel.DETECT_IMAGEMAGICK, detectImageMagick)

ipcMain.on(IpcChannel.FILE_SELECT, () => {
this.menu.open()
})
Expand Down
5 changes: 5 additions & 0 deletions modules/backend/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Jimp from 'jimp'

export default function imageConvert(sourceFile: string, targetFile: string) {
return Jimp.read(sourceFile).then((image) => image.write(targetFile))
}
30 changes: 0 additions & 30 deletions modules/backend/imagemagick.ts

This file was deleted.

6 changes: 3 additions & 3 deletions modules/backend/menu.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as os from 'os'
import { EventEmitter } from 'events'
import { dialog, Menu, shell } from 'electron'
import { SaveType } from '../common/constants'
import { isDev } from '../common/env'
import { SaveType } from '../common/types'
import { IS_DEV } from '../common/env'
import __ from '../locales'
import pkg from '../../package.json'

Expand Down Expand Up @@ -125,7 +125,7 @@ class AppMenu extends EventEmitter {
},
]

if (isDev) {
if (IS_DEV) {
menuTemplates.push({
label: 'Debug',
submenu: [
Expand Down
5 changes: 2 additions & 3 deletions modules/backend/optimize.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as os from 'os'
import * as path from 'path'
import * as fs from 'fs-extra'
import log from 'electron-log'
import { IImageFile, IOptimizeOptions, SupportedExt } from '../common/constants'
import { IImageFile, IOptimizeOptions, SupportedExt } from '../common/types'
import * as fu from '../common/file-utils'
import {
pngquant, mozjpeg, cwebp, IOptimizeMethod,
} from '../optimizers'
import { convert } from './imagemagick'
import { getFileUrl } from '../common/file-utils'
import convert from './convert'

const platform = os.platform()

Expand Down
2 changes: 1 addition & 1 deletion modules/backend/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path'
import * as fs from 'fs-extra'
import { noop } from '../common/utils'
import * as fu from '../common/file-utils'
import { SaveType, IImageFile } from '../common/constants'
import { SaveType, IImageFile } from '../common/types'

export async function saveFiles(images: IImageFile[], type: SaveType, dirname?: string) {
if (type === SaveType.NEW_DIR && !dirname) return
Expand Down
2 changes: 1 addition & 1 deletion modules/backend/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import log from 'electron-log'
import {
IpcChannel,
IUpdateInfo,
} from '../common/constants'
} from '../common/types'
import __ from '../locales'
import app from './app'

Expand Down
2 changes: 1 addition & 1 deletion modules/common/env.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const isDev = process.env.NODE_ENV === 'development'
export const IS_DEV = process.env.IMAGINE_ENV === 'development'
7 changes: 2 additions & 5 deletions modules/common/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import * as crypto from 'crypto'
import FileType from 'file-type'
import rawBody from 'raw-body'
import log from 'electron-log'
import { IImageFile, IOptimizeOptions, SupportedExt } from './constants'
import { isDev } from './env'
import { IImageFile, SupportedExt, SupportedExtAlias } from './types'

export const tmpdir = path.resolve(os.tmpdir(), 'imageOptimizer')

export const isSupportedExt = (type: string): type is SupportedExt => type in SupportedExt

export const cleanTmpdir = () => fs.emptyDirSync(tmpdir)

const takeSubHash = (hash: string) => hash.replace(/[/+=]/g, '').slice(0, 32)

export function md5(text: string) {
return crypto.createHash('md5').update(text).digest('hex')
}
Expand Down Expand Up @@ -116,7 +113,7 @@ export const reext = (filename: string, ext: SupportedExt) => filename.replace(/
$1 = $1.toLowerCase()

// make sure `x.PNG` not be transformed to `x.png`
if ($1 === ext) {
if ($1 === ext || SupportedExtAlias[$1] === ext) {
return $0
}

Expand Down
5 changes: 5 additions & 0 deletions modules/common/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ITaskItem } from './types'

export const isTaskSizeIncreased = ({ image, optimized }: ITaskItem) => (
optimized && optimized.size >= image.size
)
23 changes: 22 additions & 1 deletion modules/common/constants.ts → modules/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export enum SupportedExt {
webp = 'webp',
}

export const SupportedExtAlias: Record<string, SupportedExt> = {
jpeg: SupportedExt.jpg,
}

export const enum TaskStatus {
PENDING = 'PENDING',
PROCESSING = 'PROCESSING',
Expand All @@ -21,7 +25,6 @@ export const enum IpcChannel {
SYNC = 'SYNC',
APP_UPDATE = 'APP_UPDATE',
READY = 'READY',
DETECT_IMAGEMAGICK = 'DETECT_IMAGEMAGICK',
}

export const enum SaveType {
Expand Down Expand Up @@ -88,3 +91,21 @@ export interface IUpdateInfo {
stagingPercentage: number
version: string
}

export interface IDefaultOptions {
jpg: IOptimizeOptions
png: IOptimizeOptions
webp: IOptimizeOptions
}

export interface IGlobals {
activeId?: string
updateInfo?: IUpdateInfo
optionsVisible: boolean
defaultOptions: IDefaultOptions
}

export interface IState {
tasks: ITaskItem[]
globals: IGlobals
}
2 changes: 1 addition & 1 deletion modules/ipc-bridge/backend.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ipcMain } from 'electron'
import log from 'electron-log'
import { IElectronResponse, IpcChannel } from '../common/constants'
import { IElectronResponse, IpcChannel } from '../common/types'

/**
* make cross process method call easier.
Expand Down
2 changes: 1 addition & 1 deletion modules/ipc-bridge/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ipcRenderer } from 'electron'
import { randomId } from '../common/utils'
import { IElectronResponse, IpcChannel } from '../common/constants'
import { IElectronResponse, IpcChannel } from '../common/types'

/**
* make cross process method call easier.
Expand Down
1 change: 0 additions & 1 deletion modules/locales/ar-SA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ export default {
after_optimized: 'بعد',
new_version: 'تحديث',
apply_now: 'تطبيق التغييرات',
imagemagick_required: 'ImageMagick مطلوب',
}
1 change: 0 additions & 1 deletion modules/locales/de-DE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ export default {
after_optimized: 'Nachher',
new_version: 'Aktualisieren',
apply_now: 'Anwenden',
imagemagick_required: 'ImageMagick benötigt',
}
2 changes: 1 addition & 1 deletion modules/locales/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
save_as: 'Save as',
save_success: 'Save successfully',
clear: 'Clear all',
clear_increased: 'Clear increased files',
choose_images: 'Choose images',
drag_files: 'Drag pictures here from the local folder',
colors: 'Colors',
Expand All @@ -20,5 +21,4 @@ export default {
after_optimized: 'After',
new_version: 'Upgrade',
apply_now: 'Apply now',
imagemagick_required: 'ImageMagick required',
}
1 change: 0 additions & 1 deletion modules/locales/es-ES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ export default {
after_optimized: 'Después',
new_version: 'Actualizar',
apply_now: 'Apply now', // TODO: translate
imagemagick_required: 'ImageMagick required', // TODO: translate
}
1 change: 0 additions & 1 deletion modules/locales/fr-FR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ export default {
after_optimized: 'optimisé',
new_version: 'Mettre à jour',
apply_now: 'Appliquer maintenant',
imagemagick_required: 'ImageMagick requis',
}
1 change: 0 additions & 1 deletion modules/locales/it-IT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ export default {
after_optimized: 'Dopo',
new_version: 'Aggiorna',
apply_now: 'Applica',
imagemagick_required: 'ImageMagick richiesto',
}
1 change: 0 additions & 1 deletion modules/locales/nl-NL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ export default {
after_optimized: 'na',
new_version: 'Nieuwe versie beschikbaar!', // TODO: shorter || Note: In Dutch, this can't be shorter :(
apply_now: 'Meld je nu aan!', // TODO: translate
imagemagick_required: 'ImageMagick is verplicht', // TODO: translate
}
2 changes: 1 addition & 1 deletion modules/locales/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
save_as: '另存为',
save_success: '保存成功',
clear: '清空',
clear_increased: '清空增大的文件',
choose_images: '选择图片文件',
drag_files: '从本地文件夹拖动图片到这里',
colors: '色彩',
Expand All @@ -20,5 +21,4 @@ export default {
after_optimized: '优化后',
new_version: '有新版本',
apply_now: '立即应用',
imagemagick_required: '需要安装 ImageMagick',
}
2 changes: 1 addition & 1 deletion modules/optimizers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawn } from 'child-process-promise'
import log from 'electron-log'
import * as bins from './bin'
import { IOptimizeOptions, SupportedExt } from '../common/constants'
import { IOptimizeOptions, SupportedExt } from '../common/types'

const createEnv = () => ({
...process.env,
Expand Down
2 changes: 2 additions & 0 deletions modules/renderer/App.less
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
position: absolute;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;

&.-drag::after {
content: '';
Expand Down
Loading

0 comments on commit 785663b

Please sign in to comment.