-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
110 lines (94 loc) · 2.8 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Modules to control application life and create native browser window
const { app, BrowserWindow, ipcMain, dialog, shell} = require('electron');
const path = require('path');
const Svgo = require('svgo');
const { exec, execSync, spawn, spawnSync } = require('child_process');
const config = require('./config');
const fs = require('fs-extra');
const glob = require('glob');
const svg2html = require('svg2html')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function globPromise(pattern, options) {
return new Promise((resolve, reject) => {
glob(pattern, (err, files) => {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
}
function handlerSvgs(event, svgs, outDir) {
svgo = new Svgo(config);
const handlerSvg = svgs.map((svgFile) => {
const svgCont = fs.readFileSync(svgFile, 'utf-8');
const base = path.parse(svgFile).base;
return svgo.optimize(svgCont, { path: svgFile }).then((result) => {
fs.outputFile(`${outDir}/${base}`, result.data);
const resultData = {
file: svgFile,
content: result.data,
total: svgs.length
};
event.sender.send('outProcess', resultData);
return resultData;
});
});
return Promise.all(handlerSvg).then((values) => {
event.sender.send('outDone', {
total: values.length
});
});
}
function createWindow () {
mainWindow = new BrowserWindow({
width: 1000,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
// mainWindow.webContents.openDevTools()
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
if (mainWindow === null) createWindow()
})
ipcMain.on('open-dialog', function (event, opts) {
dialog.showOpenDialog(opts).then((result) => {
event.sender.send('selectedItem', result.filePaths);
});
});
ipcMain.on('open-dir', function (event, path) {
const [openDir] = path
shell.showItemInFolder(openDir);
});
ipcMain.on('outsvg', async(event, data) => {
const [outDir] = data[data.length - 1].outputDir;
const allSvgPromise = await Promise.all(data.map(async (item) => {
if (item.type == 'file') {
handlerSvgs(event, item.content, item.outputDir);
return item.content;
} else {
const svgs = await globPromise(`${item.content[0]}/**/*.svg`);
handlerSvgs(event, svgs, item.outputDir);
return svgs;
}
}))
const allSvg = allSvgPromise.flat();
svg2html.run({
inDir: allSvg,
outDir,
rmAttr: 'opacity|fill',
});
});