Skip to content

Commit

Permalink
switch to xslx and some modernization
Browse files Browse the repository at this point in the history
  • Loading branch information
btargac committed May 17, 2023
1 parent 726afa1 commit 5a0a009
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 772 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:

steps:
- name: Check out Git repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Install Node.js, NPM and Yarn
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
Expand Down
1,110 changes: 371 additions & 739 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "excel-parser-processor",
"productName": "Excel Parser Processor",
"version": "1.3.1",
"description": "Does the tedious processing over all items of a given excel file by converting the rows to an array and process all items of that array recursively",
"version": "1.4.0",
"description": "Does the tedious processing over all items of a given excel file by converting the rows to an array and processing them all",
"main": "./dist/index.bundle.js",
"scripts": {
"build-main": "cross-env NODE_ENV=production PROCESS_TYPE=main webpack --config webpack.prod.js",
Expand Down Expand Up @@ -72,9 +72,9 @@
"sass-loader": "^13.2.2",
"style-loader": "^3.3.2",
"uglifyjs-webpack-plugin": "^2.2.0",
"webpack": "^5.78.0",
"webpack-cli": "^5.0.1",
"webpack-merge": "^5.7.3"
"webpack": "^5.80.0",
"webpack-cli": "^5.0.2",
"webpack-merge": "^5.8.0"
},
"dependencies": {
"@babel/runtime-corejs3": "^7.21.0",
Expand All @@ -84,8 +84,8 @@
"is-url": "^1.2.4",
"jquery": "^3.6.4",
"mime-types": "^2.1.35",
"node-xlsx": "^0.21.0",
"normalize.css": "^8.0.1"
"normalize.css": "^8.0.1",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.19.2/xlsx-0.19.2.tgz"
},
"build": {
"appId": "com.Targac.ExcelParserProcessor",
Expand Down
22 changes: 12 additions & 10 deletions src/dialogs.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { dialog } from 'electron';

export const showOpenDialog = (browserWindow, defaultPath, cb) => {
dialog.showOpenDialog(browserWindow, {
buttonLabel: "Choose",
defaultPath,
title: "Choose an output folder",
properties: ['openDirectory', 'createDirectory']
}).then(({ canceled, filePaths }) => {
export const showOpenDialog = async (browserWindow, data, cb) => {
try {
const { canceled, filePaths } = await dialog.showOpenDialog(browserWindow, {
buttonLabel: "Choose",
defaultPath: data.path,
title: "Choose an output folder",
properties: ['openDirectory', 'createDirectory']
});

if( !canceled && filePaths?.length) {
cb(defaultPath, filePaths[0], browserWindow);
cb(data.file, filePaths[0], browserWindow);
}
}).catch(err => {
} catch (err) {
console.log(err);
})
}
};
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const createWindow = () => {
show: false,
webPreferences: {
contextIsolation: true,
devTools: false,
enableRemoteModule: false,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
Expand All @@ -31,10 +32,10 @@ const createWindow = () => {
};

app.on('ready', () => {
ipcMain.on('file-dropped', (event, filePath) => {
ipcMain.on('file-dropped', (event, data) => {
const [window] = BrowserWindow.getAllWindows();

showOpenDialog(window, filePath, processFile);
showOpenDialog(window, data, processFile);
});

createWindow();
Expand Down
7 changes: 5 additions & 2 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ outEvents.forEach(event => drop.addEventListener(event, handleOut));

const excelFileRegex = /(vnd\.ms-excel|vnd\.openxmlformats-officedocument\.spreadsheetml\.sheet)/i;

const handleFileSelect = event => {
const handleFileSelect = async event => {
const files = event.target.files;

for (let file of files) {
Expand All @@ -59,7 +59,10 @@ const handleFileSelect = event => {

window.postMessage({
type: 'file-dropped',
data: file.path
data: {
file: await file.arrayBuffer(),
path: file.path
}
}, '*');
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/generateFileName.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const extensionRegex = /\.([a-zA-Z0-9]+)$/ig
const trailingDotRegex = /\.$/g

const trimTrailingDot = name => {
const hasTrailingDot = trailingDotRegex.test(name);

return hasTrailingDot ? name.replace(trailingDotRegex, '') : name;
}

export default (name, extension) => {
const hasExtension = name.match(extensionRegex);
name = trimTrailingDot(name);

return hasExtension ? name: `${name}.${extension}`;
}
9 changes: 5 additions & 4 deletions src/utils/generateFileName.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ test('should generate the correct file name when file name is extension free', (
expect(fileName).toBe('sample.jpg');
});


test('should generate the correct file name when file name has an extension', () => {
const fileName = generateFileName('sample.avif', 'avif');

let fileName = generateFileName('sample.avif', 'avif');
expect(fileName).toBe('sample.avif');

fileName = generateFileName('sample.gif');
expect(fileName).toBe('sample.gif');
});

test('should generate the correct file name when file name is erroneous', () => {
const fileName = generateFileName('sample.', 'gif');

expect(fileName).toBe('sample..gif');
expect(fileName).toBe('sample.gif');
});
18 changes: 12 additions & 6 deletions src/utils/processItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pipeline } from 'stream';
import { promisify } from 'util';
import fetch from 'electron-fetch';
import { URL } from 'url';
import xlsx from 'node-xlsx';
import { read, utils } from 'xlsx';
import isUrl from 'is-url';
import mime from 'mime-types';

Expand Down Expand Up @@ -126,15 +126,21 @@ const processItems = async (rowItems, outputPath, win) => {
logFileStream.end();
};

export const processFile = async (filePath, outputPath, browserWindow) => {
export const processFile = async (file, outputPath, browserWindow) => {

_resetProcessData();

const workSheetsFromFile = xlsx.parse(filePath);
const dataRows = workSheetsFromFile.flatMap(page => page.data).filter(item => item.length);
const validRows = dataRows.filter(row => row.some(text => isUrl(text)));
const workbook = read(file);
// get all the work Sheets in the file
const wsNames = workbook.SheetNames;
// filter out the sheets without no rows
const AllSheetsData = wsNames.map(
name => utils.sheet_to_json(workbook.Sheets[name], { header: 1, blankrows: false })
).filter(item => item.length).flat();

incompatibleItems = dataRows.filter(row => !row.some(text => isUrl(text)));
const validRows = AllSheetsData.filter(row => row.some(text => isUrl(text)));

incompatibleItems = AllSheetsData.filter(row => !row.some(text => isUrl(text)));

initialItemsLength = validRows.length;

Expand Down

0 comments on commit 5a0a009

Please sign in to comment.