Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIxes: #111 - File open from application main menu
- Loading branch information
Showing
11 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const {dialog} = require('electron'); | ||
const log = require('./logger'); | ||
|
||
class AssetImport { | ||
/** | ||
* Opens a dialog to open a file and notifies the window about new content. | ||
* | ||
* @param {BrowserWindow} bWindow | ||
*/ | ||
static openAssetDialog(bWindow) { | ||
log.debug('Opening file import dialog.'); | ||
dialog.showOpenDialog(bWindow, { | ||
title: 'Select file to import', | ||
buttonLabel: 'Import', | ||
properties: ['openFile'], | ||
filters: [ | ||
{name: 'All supported files', extensions: ['arc', 'json', 'raml', 'yaml', 'zip']} | ||
] | ||
}, (filePaths) => { | ||
if (!filePaths || !filePaths[0]) { | ||
log.debug('Import dialog was canceled.'); | ||
return; | ||
} | ||
log.debug('Sending file path to open in UI thread: ' + filePaths[0]); | ||
bWindow.webContents.send('command', 'process-external-file', filePaths[0]); | ||
}); | ||
} | ||
} | ||
module.exports.AssetImport = AssetImport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
class ImportFilePrePprocessor { | ||
/** | ||
* Recognizes file type and sends to appropriate parser. | ||
* @param {String} filePath Location of the file. | ||
* @return {Promise} | ||
*/ | ||
processFile(filePath) { | ||
if (!filePath) { | ||
throw new Error('Argument not set'); | ||
} | ||
return fs.readFile(filePath) | ||
.then((buffer) => { | ||
const ext = path.extname(filePath); | ||
if (this._isApiFile(ext)) { | ||
return this._notifyApiParser(buffer); | ||
} | ||
// Only JSON files left. It can be either ARC, Postam or OAS | ||
return this._discoverFile(buffer); | ||
}); | ||
} | ||
|
||
_isApiFile(ext) { | ||
const apiTypes = [ | ||
'.zip', '.yaml', '.raml' | ||
]; | ||
return apiTypes.indexOf(ext) !== -1; | ||
} | ||
|
||
_dispatch(type, detail) { | ||
const e = new CustomEvent(type, { | ||
bubbles: true, | ||
cancelable: true, | ||
detail | ||
}); | ||
document.body.dispatchEvent(e); | ||
return e; | ||
} | ||
|
||
/** | ||
* Dispatches `api-process-file` to parse API data usingseparate module. | ||
* In ARC electron it is `@advanced-rest-client/electron-amf-service` | ||
* node module. In other it might be other component. | ||
* @param {File} file User file. | ||
* @return {Promise} | ||
*/ | ||
_notifyApiParser(file) { | ||
const e = this._dispatch('api-process-file', { | ||
file | ||
}); | ||
if (!e.defaultPrevented) { | ||
return Promise.reject(new Error('API processor not available')); | ||
} | ||
return e.detail.result | ||
.then((api) => { | ||
this._dispatch('api-data-ready', { | ||
model: api.model, | ||
type: api.type | ||
}); | ||
}); | ||
} | ||
|
||
_discoverFile(buffer) { | ||
const content = buffer.toString().trim(); | ||
if (content[0] !== '{') { | ||
return Promise.reject(new Error('Unsupported file.')); | ||
} | ||
let data; | ||
try { | ||
data = JSON.parse(content); | ||
} catch (_) { | ||
return Promise.reject(new Error('Unknown file format.')); | ||
} | ||
if (data.swagger) { | ||
return this._notifyApiParser(buffer); | ||
} | ||
|
||
const e = this._dispatch('import-process-data', { | ||
data | ||
}); | ||
return e.detail.result; | ||
} | ||
} | ||
module.exports.ImportFilePrePprocessor = ImportFilePrePprocessor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters