Skip to content

Commit

Permalink
Merge pull request #374 from codaco/fix/protocol-template
Browse files Browse the repository at this point in the history
Update new protocol template to include a network asset
  • Loading branch information
jthrilly authored Jan 21, 2019
2 parents e2a1b13 + f0274ff commit b221e45
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 57 deletions.
Empty file added public/template/assets/.keep
Empty file.
44 changes: 44 additions & 0 deletions public/template/assets/previousInterview.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
43 changes: 29 additions & 14 deletions src/other/protocols/createProtocol.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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.
Expand All @@ -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);
};

Expand All @@ -59,7 +75,6 @@ const createProtocol = () =>
saveDialog()
.then((filePath) => {
const protocol = {
...template,
name: path.basename(filePath, '.netcanvas'),
};

Expand Down

0 comments on commit b221e45

Please sign in to comment.