diff --git a/public/template/assets/.keep b/public/template/assets/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/public/template/assets/previousInterview.json b/public/template/assets/previousInterview.json new file mode 100644 index 000000000..3af6b2124 --- /dev/null +++ b/public/template/assets/previousInterview.json @@ -0,0 +1,44 @@ +{ + "nodes": [ + { + "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", + "attributes": { + "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Anita", + "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Annie", + "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "21" + } + }, + { + "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", + "attributes": { + "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Barry", + "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Baz", + "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "28" + } + }, + { + "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", + "attributes": { + "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Carlito", + "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Carl", + "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "23" + } + }, + { + "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", + "attributes": { + "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Dee", + "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Dee", + "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "40" + } + }, + { + "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", + "attributes": { + "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Eugine", + "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Eu", + "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "18" + } + } + ] +} diff --git a/src/other/protocols/template.json b/public/template/protocol.json similarity index 66% rename from src/other/protocols/template.json rename to public/template/protocol.json index 3d1d82c2c..f80132ef6 100644 --- a/src/other/protocols/template.json +++ b/public/template/protocol.json @@ -81,50 +81,11 @@ } } }, - "externalData": { + "assetManifest": { "previousInterview": { - "nodes": [ - { - "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", - "attributes": { - "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Anita", - "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Annie", - "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "21" - } - }, - { - "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", - "attributes": { - "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Barry", - "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Baz", - "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "28" - } - }, - { - "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", - "attributes": { - "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Carlito", - "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Carl", - "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "23" - } - }, - { - "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", - "attributes": { - "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Dee", - "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Dee", - "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "40" - } - }, - { - "type": "d39a47507bbe27c2a7948861847f3607eda8e1be", - "attributes": { - "6ae999552a0d2dca14d62e2bc8b764d377b1dd6c": "Eugine", - "7428c4145a49b9d07e4f88033b7fc97aa98b6b99": "Eu", - "5dc56b9aab61867257a3c1bd7c786c9410d38cd2": "18" - } - } - ] + "type": "network", + "name": "Previous Interview", + "source": "previousInterview.json" } }, "forms": { diff --git a/src/other/protocols/createProtocol.js b/src/other/protocols/createProtocol.js index bbbc914a5..53ee62af1 100644 --- a/src/other/protocols/createProtocol.js +++ b/src/other/protocols/createProtocol.js @@ -1,9 +1,8 @@ import { remote } from 'electron'; -import fs from 'fs'; -import mkdirp from 'mkdirp'; +import fse from 'fs-extra'; import path from 'path'; import { getLocalDirectoryFromArchivePath } from './utils'; -import template from './template.json'; +// import template from './template/protocol.json'; const saveDialogOptions = { buttonLabel: 'Create', @@ -25,20 +24,36 @@ const saveDialog = () => /** * Creates an blank protocol directory at destinationPath, with correct directory structure. - * Expects a valid protocol object as input. * @param {string} destinationPath - destination for skeleton protocol. - * @param {object} protocol - protocol object, probably a template. */ -const createProtocolWorkingPath = (destinationPath, protocol) => +const createProtocolWorkingPath = destinationPath => new Promise((resolve) => { - const assetsPath = path.join(destinationPath, 'assets'); - const protocolPath = path.join(destinationPath, 'protocol.json'); - mkdirp.sync(destinationPath); - fs.mkdirSync(assetsPath); - fs.writeFileSync(protocolPath, JSON.stringify(protocol, null, 2)); - resolve(); + const appPath = remote.app.getAppPath(); + const templatePath = path.join(appPath, 'template'); + fse.copySync(templatePath, destinationPath); + resolve(destinationPath); }); +/** + * Updates protocol at protocolWorkingPath, merges with existing protocol. + * Expects a valid protocol object as input. + * @param {string} protocolWorkingPath - location of protocol + * @param {object} protocol - protocol object, probably a template. + */ +const updateProtocol = (protocolWorkingPath, protocol) => { + const protocolPath = path.join(protocolWorkingPath, 'protocol.json'); + + return fse.readJson(protocolPath) + .then((protocolTemplate) => { + const updatedProtocol = { + ...protocolTemplate, + ...protocol, + }; + + return fse.writeJson(protocolPath, updatedProtocol); + }); +}; + /** * Creates a blank protocol in a tempory path * Expects a valid protocol object as input. @@ -48,7 +63,8 @@ const createProtocolWorkingPath = (destinationPath, protocol) => export const createProtocolFiles = (destinationPath, protocol) => { const tempPath = getLocalDirectoryFromArchivePath(destinationPath); - return createProtocolWorkingPath(tempPath, protocol) + return createProtocolWorkingPath(tempPath) + .then(protocolWorkingPath => updateProtocol(protocolWorkingPath, protocol)) .then(() => tempPath); }; @@ -59,7 +75,6 @@ const createProtocol = () => saveDialog() .then((filePath) => { const protocol = { - ...template, name: path.basename(filePath, '.netcanvas'), };