Skip to content

Commit

Permalink
#986: Added export as json only.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattRedBox committed May 25, 2020
1 parent eb2105b commit 55c5c41
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
6 changes: 6 additions & 0 deletions src/main/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ class AppMenu {
click () {
webContents().send('triggerMenuButton', 'Export')
}
},
{
label: 'Export Package Properties...',
click () {
webContents().send('createJsonPackage')
}
}
]
}, {
Expand Down
27 changes: 21 additions & 6 deletions src/renderer/components/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ import {
validateActiveDataAgainstSchema
} from '../frictionless.js'
import {
createDataPackage
createDataPackageAsZippedResources,
createDataPackageAsJson
} from '@/frictionlessDataPackage.js'
import HomeTooltip from '../mixins/HomeTooltip'
import ErrorsTooltip from '../mixins/ErrorsTooltip'
Expand Down Expand Up @@ -596,6 +597,9 @@ export default {
self.removeTab(targetTabId)
self.cleanUpTabDependencies(targetTabId)
})
ipc.on('createJsonPackage', function (event, arg) {
self.createJsonPackage()
})
this.$subscribeTo(errorFeedback$, function (nextError) {
if (!self.messages) {
self.messages = []
Expand Down Expand Up @@ -814,7 +818,12 @@ export default {
},
exportPackageFeedback: function () {
this.messagesTitle = 'Export package success'
this.messages = 'Data package created.'
this.messages = 'Data package exported.'
this.messagesType = 'feedback'
},
exportPackageJsonFeedback: function () {
this.messagesTitle = 'Export package success'
this.messages = 'Data package JSON exported.'
this.messagesType = 'feedback'
},
exportPackageErrors: function (errorMessages) {
Expand All @@ -823,17 +832,23 @@ export default {
this.messagesType = 'error'
this.updateHotComments()
},
createPackage: async function () {
createZipPackage: async function () {
await this.createPackage(createDataPackageAsZippedResources, this.exportPackageFeedback)
},
createJsonPackage: async function () {
await this.createPackage(createDataPackageAsJson, this.exportPackageJsonFeedback)
},
createPackage: async function (exportFunc, exportFeedbackFunc) {
try {
let messages = await createDataPackage()
let messages = await exportFunc()
if (messages.length > 0) {
this.exportPackageErrors(messages.map(x => {
return {
message: x
}
}))
} else {
this.exportPackageFeedback()
exportFeedbackFunc()
}
} catch (err) {
console.error('There was an error creating a data package.', err)
Expand Down Expand Up @@ -1111,7 +1126,7 @@ export default {
this.validateTable()
break
case 'Export':
this.createPackage()
this.createZipPackage()
break
case 'Guess':
this.inferColumnProperties()
Expand Down
20 changes: 18 additions & 2 deletions src/renderer/exportPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@ import { compileAndStringifyProvenance } from '@/provenance.js'
// import os from 'os'
const Dialog = remote.dialog

export function createZipFile (text) {
let json = JSON.stringify(text, null, 4)
export function createJsonFile (jsonObj) {
Dialog.showSaveDialog({
filters: [
{
name: '*',
extensions: ['json']
}
]
}, function (filename) {
if (filename === undefined) {
return
}
fs.writeJsonSync(filename, jsonObj, { spaces: 4 })
})
}

export function createZipFile (jsonObj) {
let json = JSON.stringify(jsonObj, null, 4)
Dialog.showSaveDialog({
filters: [
{
Expand Down
27 changes: 17 additions & 10 deletions src/renderer/frictionlessDataPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@ import { HotRegister } from '@/hot.js'
import tabStore from '@/store/modules/tabs.js'
import hotStore from '@/store/modules/hots.js'
import path from 'path'
import { createZipFile } from '@/exportPackage.js'
import { createJsonFile, createZipFile } from '@/exportPackage.js'
import { getValidNames, hasAllColumnNames } from '@/frictionlessUtilities.js'
import _ from 'lodash'

export async function createDataPackage () {
export async function createDataPackageAsZippedResources () {
const errorMessages = await createDataPackage(createZipFile)
return errorMessages
}

export async function createDataPackageAsJson () {
const errorMessages = await createDataPackage(createJsonFile)
return errorMessages
}

export async function createDataPackage (postCreateFunc) {
const errorMessages = []
if (!haveAllTabsGotFilenames()) {
errorMessages.push('All tabs must be saved before exporting.')
}
try {
let dataPackage = await buildDataPackage(errorMessages)
if (!_.isEmpty(errorMessages)) {
return errorMessages
}
if (dataPackage) {
if (_.isEmpty(errorMessages) && dataPackage) {
dataPackage.commit()
if (!dataPackage.valid) {
if (dataPackage.valid) {
postCreateFunc(dataPackage.descriptor)
} else {
errorMessages.push('There is a problem with at least 1 package property. Please check and try again.')
return errorMessages
}
createZipFile(dataPackage.descriptor)
}
} catch (err) {
if (err) {
Expand Down Expand Up @@ -92,7 +99,7 @@ async function buildAllResourcesForDataPackage (dataPackage, errorMessages) {
} catch (err) {
if (err) {
console.error('There was an error creating a resource.', err)
return false
return
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/specs/Home.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Home.vue', function () {
'loadDataIntoLatestHot': sandbox.stub().withArgs().returns(hot.guid),
'initHotTablePropertiesFromDescriptor': sandbox.stub(),
'removePreviousHotComments': sandbox.stub(),
'createPackage': sandbox.stub().callsFake(function fakeFn () {
'createZipPackage': sandbox.stub().callsFake(function fakeFn () {
wrapper.setData({ messagesTitle: 'Export success' })
}),
'validateTable': sandbox.stub().callsFake(function fakeFn () {
Expand Down

0 comments on commit 55c5c41

Please sign in to comment.