Skip to content

Commit

Permalink
Convert project to TypeScript including type declarations for node-ex…
Browse files Browse the repository at this point in the history
…iftool (first pass)
  • Loading branch information
szTheory committed Apr 28, 2020
1 parent dba10ce commit 60f71e7
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 110 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/common/config.js → src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Store = require("electron-store");

module.exports = new Store({
export const store = new Store({
defaults: {
favoriteAnimal: "🦄"
}
Expand Down
File renamed without changes.
14 changes: 6 additions & 8 deletions src/main/app_setup.js → src/main/app_setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BrowserWindow } from "electron";

const { app } = require("electron");
const { createMainWindow } = require("./window_setup");

Expand All @@ -7,7 +9,7 @@ function preventMultipleAppInstances() {
}
}

function openMinimizedIfAlreadyExists({ win }) {
function openMinimizedIfAlreadyExists({ win }: { win: BrowserWindow }) {
app.on("second-instance", () => {
if (win) {
if (win.isMinimized()) {
Expand All @@ -24,21 +26,17 @@ function quitOnWindowsAllClosed() {
});
}

function createWindowOnActivate({ win }) {
function createWindowOnActivate({ win }: { win: BrowserWindow }) {
app.on("activate", () => {
if (!win) {
win = createMainWindow();
}
});
}

const setupApp = function({ win }) {
export function setupApp({ win }: { win: BrowserWindow }) {
preventMultipleAppInstances();
openMinimizedIfAlreadyExists({ win: win });
quitOnWindowsAllClosed();
createWindowOnActivate({ win: win });
};

module.exports = {
setupApp
};
}
6 changes: 3 additions & 3 deletions src/main/index.js → src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// electron-webpack HMR for development
// const is = require("electron-util");
import { is } from "electron-util";
import { BrowserWindow } from "electron";

// electron-webpack HMR for development
if (is.development && module.hot) {
module.hot.accept();
}
Expand All @@ -13,7 +13,7 @@ const { createMainWindow, setupMainWindow } = require("./window_setup");

// Maintain reference to window to
// prevent it from being garbage collected
var win = null;
var win: BrowserWindow;

async function setup() {
init({ win: win });
Expand Down
10 changes: 3 additions & 7 deletions src/main/init.js → src/main/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app } = require("electron");
import { app, BrowserWindow } from "electron";
const unhandled = require("electron-unhandled");
const contextMenu = require("electron-context-menu");
const packageJson = require("../../package.json");
Expand All @@ -17,13 +17,9 @@ function setupUserModelId() {
app.setAppUserModelId(packageJson.build.appId);
}

const init = function({ win }) {
export function init({ win }: { win: BrowserWindow }) {
setupErrorHandling();
setupContextMenu();
setupUserModelId();
setupApp({ win: win });
};

module.exports = {
init
};
}
File renamed without changes.
18 changes: 8 additions & 10 deletions src/main/menu_file_open.js → src/main/menu_file_open.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const { dialog } = require("electron");
import { dialog, BrowserWindow, WebContents } from "electron";

const EVENT_FILE_OPEN_ADD_FILES = "file-open-add-files";
export const EVENT_FILE_OPEN_ADD_FILES = "file-open-add-files";

function fileOpenClick(event, focusedWindow, focusedWebContents) {
export function fileOpenClick(
event: KeyboardEvent,
focusedWindow: BrowserWindow,
focusedWebContents: WebContents
) {
dialog
.showOpenDialog(focusedWindow, {
properties: ["openFile", "multiSelections"]
Expand All @@ -17,16 +21,10 @@ function fileOpenClick(event, focusedWindow, focusedWebContents) {
});
}

function fileMenuOpenItem() {
export function fileMenuOpenItem() {
return {
label: "Open…",
accelerator: "CmdOrCtrl+O",
click: fileOpenClick
};
}

module.exports = {
fileOpenClick,
fileMenuOpenItem,
EVENT_FILE_OPEN_ADD_FILES
};
20 changes: 8 additions & 12 deletions src/main/menu_help.js → src/main/menu_help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ const GITHUB_PROJECTNAME = "exifcleaner";
const SOURCE_CODE_URL = `https://github.com/${GITHUB_USERNAME}/${GITHUB_PROJECTNAME}`;
const COPYRIGHT_TEXT = `Copyright © ${GITHUB_USERNAME}`;

function aboutMenuIconPath() {
if (is.linux) {
return path.join(__dirname, "../../exifcleaner.png");
} else {
return path.join(__dirname, "static", "icon.png");
}
}

function buildHelpSubmenu() {
export function buildHelpSubmenu() {
let submenu = [
openUrlMenuItem({
label: "Website",
Expand Down Expand Up @@ -67,6 +59,10 @@ function buildHelpSubmenu() {
return submenu;
}

module.exports = {
buildHelpSubmenu
};
function aboutMenuIconPath() {
if (is.linux) {
return path.join(__dirname, "../../exifcleaner.png");
} else {
return path.join(__dirname, "static", "icon.png");
}
}
25 changes: 8 additions & 17 deletions src/main/window_setup.js → src/main/window_setup.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
const { BrowserWindow, app } = require("electron");
import { BrowserWindow, app } from "electron";
const { is } = require("electron-util");
const url = require("url");
const path = require("path");

const DEFAULT_WINDOW_WIDTH = 580;
const DEFAULT_WINDOW_HEIGHT = 312;

function setupMainWindowClose({ win }) {
function setupMainWindowClose({ win }: { win: BrowserWindow }) {
win.on("closed", () => {
// Dereference the window
// For multiple windows store them in an array
win = null;

// Close application on window quit
// (for Mac File -> Close Window)
app.quit();
});
}

function showWindowOnReady({ win }) {
function showWindowOnReady({ win }: { win: BrowserWindow }) {
win.once("ready-to-show", () => {
win.show();
});
Expand All @@ -41,13 +37,13 @@ function urlForLoad() {
}
}

function mainWindowLoadUrl({ win }) {
function mainWindowLoadUrl({ win }: { win: BrowserWindow }) {
const url = urlForLoad();

win.loadURL(url);
}

const createMainWindow = async function() {
export async function createMainWindow() {
let options = {
title: app.name,
show: false,
Expand All @@ -65,15 +61,10 @@ const createMainWindow = async function() {
}

return new BrowserWindow(options);
};
}

const setupMainWindow = function({ win }) {
export function setupMainWindow({ win }: { win: BrowserWindow }) {
setupMainWindowClose({ win: win });
showWindowOnReady({ win: win });
mainWindowLoadUrl({ win: win });
};

module.exports = {
createMainWindow,
setupMainWindow
};
}
79 changes: 54 additions & 25 deletions src/renderer/add_files.js → src/renderer/add_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const {
updateRowWithExif,
updateRowWithCleanerSpinner
} = require("./table");
const exiftool = require("node-exiftool");
const { exiftoolBinPath } = require("../common/binaries");
import exiftool, { ExiftoolProcess } from "node-exiftool";
import { exiftoolBinPath } from "../common/binaries";

async function addFiles({ filePaths }) {
export async function addFiles({ filePaths }: { filePaths: string[] }) {
for (const filePath of filePaths) {
addFile({ filePath: filePath });
}
Expand All @@ -16,32 +16,48 @@ function newExifToolProcess() {
return new exiftool.ExiftoolProcess(exiftoolBinPath);
}

async function showExifBeforeClean({ trNode, filePath }) {
async function showExifBeforeClean({
trNode,
filePath
}: {
trNode: HTMLTableRowElement;
filePath: string;
}) {
const tdBeforeNode = trNode.querySelector("td:nth-child(2)");
const ep = newExifToolProcess();
const exifData = await getExif({ ep: ep, filePath: filePath }).then(val => {
const exifData = await getExif({
exiftoolProcess: ep,
filePath: filePath
}).then(val => {
ep.close();
return val;
});

updateRowWithExif({ tdNode: tdBeforeNode, exifData: exifData });
}

async function showExifAfterClean({ trNode, filePath }) {
async function showExifAfterClean({
trNode,
filePath
}: {
trNode: HTMLTableRowElement;
filePath: string;
}) {
const tdAfterNode = trNode.querySelector("td:nth-child(3)");
const ep = newExifToolProcess();
const newExifData = await getExif({ ep: ep, filePath: filePath }).then(
val => {
ep.close();
return val;
}
);
const newExifData = await getExif({
exiftoolProcess: ep,
filePath: filePath
}).then(val => {
ep.close();
return val;
});

updateRowWithExif({ tdNode: tdAfterNode, exifData: newExifData });
return Promise.resolve();
}

async function addFile({ filePath }) {
async function addFile({ filePath }: { filePath: string }) {
// add row
const trNode = addTableRow({ filePath: filePath });

Expand All @@ -62,12 +78,18 @@ async function addFile({ filePath }) {
.catch(console.error);
}

function cleanExifData(exifHash) {
function cleanExifData(exifHash: any) {
// remove basic file info that is part of
// exiftools output, but not metadata
delete exifHash.SourceFile;
delete exifHash.ImageSize;
delete exifHash.Megapixels;
if (exifHash.SourceFile) {
delete exifHash.SourceFile;
}
if (exifHash.ImageSize) {
delete exifHash.ImageSize;
}
if (exifHash.Megapixels) {
delete exifHash.Megapixels;
}

return exifHash;
}
Expand All @@ -90,7 +112,7 @@ function cleanExifData(exifHash) {
// .then(() => ep.close())
// .then(() => console.log('Closed exiftool'))
// .catch(console.error)
async function removeExif({ ep, filePath }) {
async function removeExif({ ep, filePath }: { ep: any; filePath: string }) {
const exifData = ep
.open()
// .then((pid) => console.log('Started exiftool process %s', pid))
Expand All @@ -104,12 +126,19 @@ async function removeExif({ ep, filePath }) {

// Read the exif data using the exiftool bin.
// This should also have the perl processes cleaned up after.
async function getExif({ ep, filePath }) {
const exifData = ep
async function getExif({
exiftoolProcess,
filePath
}: {
exiftoolProcess: ExiftoolProcess;
filePath: string;
}) {
const exifData = exiftoolProcess
.open()
// .then((pid) => console.log('Started exiftool process %s', pid))
.then(() => {
return ep
// readMetadata(ep, filePath);
return exiftoolProcess
.readMetadata(filePath, [
"-File:all",
"-ExifToolVersion",
Expand All @@ -118,6 +147,10 @@ async function getExif({ ep, filePath }) {
])
.then(
exifData => {
if (exifData.data === null) {
return {};
}

const hash = exifData.data[0];
return cleanExifData(hash);
},
Expand All @@ -130,7 +163,3 @@ async function getExif({ ep, filePath }) {

return exifData;
}

module.exports = {
addFiles
};
2 changes: 1 addition & 1 deletion src/renderer/drag.js → src/renderer/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ document.addEventListener("dragover", event => {
event.stopPropagation();
});

function filePaths({ fileList }) {
function filePaths({ fileList }: { fileList: FileList }) {
let paths = [];
for (const file of fileList) {
paths.push(file.path);
Expand Down
6 changes: 1 addition & 5 deletions src/renderer/empty_pane.js → src/renderer/empty_pane.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function hideEmptyPane() {
export function hideEmptyPane() {
const pane = emptyPane();
if (!pane) {
throw "Could not find empty pane to hide";
Expand All @@ -10,7 +10,3 @@ function hideEmptyPane() {
function emptyPane() {
return document.getElementById("empty");
}

module.exports = {
hideEmptyPane
};
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
eraseSelectedFilesPane
} = require("./selected_files");

export function selectFiles({ filePaths }) {
export function selectFiles({ filePaths }: { filePaths: string[] }) {
if (filePaths.length > 0) {
hideEmptyPane();
eraseSelectedFilesPane();
Expand Down
Loading

0 comments on commit 60f71e7

Please sign in to comment.