diff --git a/main/index.js b/main/index.js index 45c176f..f57200d 100644 --- a/main/index.js +++ b/main/index.js @@ -15,8 +15,10 @@ const { version } = require('../package') const windowList = require('./utils/frames/list') const toggleWindow = require('./utils/frames/toggle') const updater = require('./updates') +const showcase = require('./utils/showcase') const notify = require('./notify') const handleException = require('./utils/exception') +const { error: showError } = require('./dialogs') // Load the app instance from electron const { app } = electron @@ -42,6 +44,17 @@ app.on('window-all-closed', () => { } }) +const filesDropped = async (event, files) => { + event.preventDefault() + + if (process.env.CONNECTION === 'offline') { + showError("You're offline") + return + } + + await showcase(files) +} + // Chrome Command Line Switches app.commandLine.appendSwitch('disable-renderer-backgrounding') @@ -117,6 +130,7 @@ app.on('ready', async () => { } // Define major event listeners for tray + tray.on('drop-files', filesDropped) tray.on('click', toggleActivity) tray.on('double-click', toggleActivity) diff --git a/main/utils/showcase.js b/main/utils/showcase.js new file mode 100644 index 0000000..7957006 --- /dev/null +++ b/main/utils/showcase.js @@ -0,0 +1,84 @@ +// Native +const path = require('path') + +// Packages +const electron = require('electron') +const ejse = require('ejs-electron') +const fs = require('fs-extra') +const {Dataset, File} = require('data.js') +const hri = require('human-readable-ids').hri +const { resolve: resolvePath } = require('app-root-path') + +// Utils +const { error: showError } = require('../dialogs') +const toggleWindow = require('./frames/toggle') + + +const prepareDatasetFromFile = async filePath => { + const pathParts = path.parse(filePath) + const file = await File.load(pathParts.base, {basePath: pathParts.dir}) + + // List of formats that are known as tabular + const knownTabularFormats = ['csv', 'tsv', 'dsv'] + if (knownTabularFormats.includes(file.descriptor.format)) { + await file.addSchema() + } + + let dpName, dpTitle + dpName = file.descriptor.name.replace(/\s+/g, '-').toLowerCase() + // Add human readable id so that this packge does not conflict with other + // packages (name is coming from the file name which could just be + // data.csv) + dpName += '-' + hri.random() + + // Make unslugifies version for title: + dpTitle = dpName.replace(/-+/g, ' ') + dpTitle = dpTitle.charAt(0).toUpperCase() + dpTitle.slice(1) + + const metadata = { + name: dpName, + title: dpTitle, + resources: [] + } + const dataset = await Dataset.load(metadata) + dataset.addResource(file) + return dataset +} + + +module.exports = async (files) => { + if (files.length === 1) { + const path_ = files[0] + if (fs.lstatSync(path_).isFile()) { + const pathParts = path.parse(path_) + if (pathParts.base === 'datapackage.json') { + showError('Sorry, currently you can drop in a single file. Data Package support is coming!') + } + const dataset = await prepareDatasetFromFile(path_) + ejse.data('dataset', dataset.descriptor) + ejse.data('dpId', JSON.stringify(dataset.descriptor).replace(/\\/g, '\\\\').replace(/\'/g, "\\'")) + const win = new electron.BrowserWindow({ + width: 800, + height: 700, + title: 'Welcome to DataHub', + resizable: false, + center: true, + frame: false, + show: false, + fullscreenable: false, + maximizable: false, + titleBarStyle: 'hidden-inset', + webPreferences: { + backgroundThrottling: false, + devTools: true + } + }) + win.loadURL('file://' + resolvePath('./main/pages/showcase.ejs')) + toggleWindow(null, win) + } else { + showError('Sorry, currently you can drop in a single file. Directory support is coming!') + } + } else if (files.length > 1) { + showError('Sorry, currently you can drop in a single file. Multiple files support is coming!') + } +} diff --git a/package.json b/package.json index 51331e3..c86bae7 100644 --- a/package.json +++ b/package.json @@ -41,12 +41,14 @@ "dependencies": { "app-root-path": "^2.0.1", "child-process-promise": "^2.2.1", + "data.js": "^0.9.10", "ejs-electron": "^2.0.1", "electron-is-dev": "^0.3.0", "electron-updater": "^2.16.1", "fix-path": "^2.1.0", "fs-extra": "^4.0.2", "global-packages": "^1.0.2", + "human-readable-ids": "^1.0.3", "just-compare": "^1.2.1", "mkdirp": "^0.5.1", "ms": "^2.0.0",