Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Templates #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions scripts/build-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ var fs = require('fs')

var argv = process.argv.slice(2);

if(argv.length < 2){
function usage() {
console.log('USAGE: npm run build-modules ../synthea/src/main/resources/modules ../synthea/src/main/resources/modules/lookup_tables ../synthea/src/main/resources/templates/modules/\n\n\n')
}

if(argv.length < 3){
console.log('\x1b[1m')
console.log('\n\nPlease specify directory where synthea modules and lookup tables are located.')
console.log('USAGE: npm run build-modules ../synthea/src/main/resources/modules ../synthea/src/test/resources/generic/lookup_tables\n\n\n')
usage()
console.log('Please point to the exact top level generic module directory and lookup table directory')
console.log('\x1b[0m')
process.exit()
Expand All @@ -21,12 +25,17 @@ if(argv[1].endsWith('/')){
tableDirectory = argv[1].slice(0,-1)
}

var templatesDirectory = argv[2]
if(argv[2].endsWith('/')){
templatesDirectory = argv[2].slice(0,-1)
}

try {
fs.lstatSync(directory).isDirectory()
} catch(e){
console.log('\x1b[1m')
console.log('\n\nNo such directory ' + directory)
console.log('EXAMPLE USAGE: npm run build-modules npm run build-modules ../synthea/src/main/resources/modules ../synthea/src/test/resources/generic/lookup_tables\n\n\n')
usage()
console.log('\x1b[0m')
process.exit()
}
Expand All @@ -36,7 +45,17 @@ try {
} catch(e){
console.log('\x1b[1m')
console.log('\n\nNo such directory ' + tableDirectory)
console.log('EXAMPLE USAGE: npm run build-modules npm run build-modules ../synthea/src/main/resources/modules ../synthea/src/test/resources/generic/lookup_tables\n\n\n')
usage()
console.log('\x1b[0m')
process.exit()
}

try {
fs.lstatSync(templatesDirectory).isDirectory()
} catch(e){
console.log('\x1b[1m')
console.log('\n\nNo such directory ' + templatesDirectory)
usage()
console.log('\x1b[0m')
process.exit()
}
Expand All @@ -63,7 +82,7 @@ var files = walkSync(directory,'.json');
if(files.length == 0){
console.log('\x1b[1m')
console.log('\n\nNo json files located at ' + directory)
console.log('EXAMPLE USAGE: npm run build-modules npm run build-modules ../synthea/src/main/resources/modules ../synthea/src/test/resources/generic/lookup_tables\n\n\n')
usage()
console.log('\x1b[0m')
process.exit()
}
Expand Down Expand Up @@ -150,9 +169,23 @@ try {

fs.writeFileSync(outputFile, output)

var templates = walkSync(templatesDirectory, '.json');
let output = 'export default {';

for(var i = 0; i< templates.length; i++){
var contents = fs.readFileSync(templates[i], 'utf8');
var filename = templates[i].replace(templatesDirectory + '/','').replace('.json','')
templateOutput += '"' + filename + '":' + contents + "\n,\n"
}
templateOutput += '};'

let templatesFile = './src/data/templates.js'

fs.writeFileSync(templatesFile, templateOutput)

console.log('\x1b[1m')
console.log('\n\nCompleted importing ' + count + ' modules from ' + directory + '\n\n')
console.log('Overwrite ' + outputFile + ' with new data')
console.log('Overwrite ' + outputFile + ' with new modules')
console.log('Wrote ' + templatesFile + ' with templates')
console.log('Run tests before committing (and no other files should have been changed)')
console.log('\n\n\x1b[0m')
44 changes: 36 additions & 8 deletions src/components/menu/LoadModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ class LoadModule extends Component {
</ul>
)

case 'templates':
return (
<ul className='LoadModule-list'>
{ Object.entries(this.state.templates || {}).map(([key, template]) => {
return (
<li key={key}>
<button className='btn btn-link' onClick={() => {
this.loadModule(JSON.stringify(template))
}}>
{template.name}
</button>
</li>
)
})}
</ul>
)

case 'my':
return (
<ul className='LoadModule-list'>
Expand Down Expand Up @@ -243,6 +260,16 @@ class LoadModule extends Component {
});
}

loadTemplates() {
if (!this.state.templates) {
import("../../data/templates").then(templates => {
this.setState({
templates: templates.default,
});
});
}
}

updateLocalStorageModules() {
const existingModules = getLocalStorageModules();

Expand Down Expand Up @@ -341,16 +368,16 @@ class LoadModule extends Component {
}

componentDidUpdate(prevProps, prevState) {
if (
this.state.selectedOption === "git" &&
prevState.selectedOption !== "git"
) {
const changedTo = (option) =>
(this.state.selectedOption === option &&
prevState.selectedOption !== option);

if (changedTo("git")) {
this.fetchBranchList();
} else if (
this.state.selectedOption === "local-storage" &&
prevState.selectedOption !== "local-storage"
) {
} else if (changedTo("local-storage")) {
this.updateLocalStorageModules();
} else if (changedTo("templates")) {
this.loadTemplates();
}
}

Expand Down Expand Up @@ -391,6 +418,7 @@ class LoadModule extends Component {
<ul className='LoadModule-options'>
<li className={(this.state.selectedOption === 'core') ? 'selected' : ''}><button className='btn btn-link' onClick={this.onOptionClick('core')}>Core Modules</button></li>
<li className={(this.state.selectedOption === 'submodules') ? 'selected' : ''}><button className='btn btn-link' onClick={this.onOptionClick('submodules')}>Submodules</button></li>
<li className={(this.state.selectedOption === 'templates') ? 'selected' : ''}><button className='btn btn-link' onClick={this.onOptionClick('templates')}>Templates</button></li>
{Object.keys(this.props.modules).length > 0 ? <li className={(this.state.selectedOption === 'my') ? 'selected' : ''}><button className='btn btn-link' onClick={this.onOptionClick('my')}>My Modules</button></li> : ''}
<li className={(this.state.selectedOption === 'json') ? 'selected' : ''}><button className='btn btn-link' onClick={this.onOptionClick('json')}>Paste JSON</button></li>
<li className={(this.state.selectedOption === 'git') ? 'selected' : ''}><button className='btn btn-link' onClick={this.onOptionClick('git')}>GitHub Modules</button></li>
Expand Down
Loading
Loading