From 66fd3f87ad97d4f209364e8a01e33b239602c322 Mon Sep 17 00:00:00 2001 From: Merlin Patterson Date: Mon, 5 Feb 2018 09:42:05 -0500 Subject: [PATCH 1/2] Add createDirectory option --- lib/generate-react-component.js | 76 ++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/lib/generate-react-component.js b/lib/generate-react-component.js index 7f04e27..c293819 100644 --- a/lib/generate-react-component.js +++ b/lib/generate-react-component.js @@ -50,6 +50,12 @@ export default { order: 3, type: 'string', default: '' + }, + createDirectory: { + description: '', + order: 4, + type: 'boolean', + default: true } }, @@ -66,6 +72,8 @@ export default { return prev; }, {}); + this.options.createDirectory = atom.config.get('generate-react-component.createDirectory'); + // Re-render whenever conditionals list is changed atom.config.onDidChange('generate-react-component.conditionals', () => { this.deactivate(); @@ -113,6 +121,13 @@ export default { const panelChildren = this.modalPanel.getItem().children; Object.keys(panelChildren).slice(3).forEach(i => { panelChildren[i].addEventListener('change', () => { + console.log( + panelChildren[i].id, + 'changing from', + this.options[panelChildren[i].id], + 'to', + panelChildren[i].children[0].children[0].checked + ); this.options[panelChildren[i].id] = panelChildren[i].children[0].children[0].checked; }); }); @@ -134,39 +149,50 @@ export default { this.modalPanel.hide(); }, + createFiles(newPath) { + // use included templates if user-defined path is empty + const templatePath = + atom.config.get(`generate-react-component.${this.mode}TemplatePath`).trim() + || path.resolve(__dirname, `${this.mode}_template`); + fs.readdir(templatePath, (err, files) => { + if (err) throw err; + files.map(filename => { + const newFilename = replacePlaceholders(filename, this.componentName); + const filePath = path.resolve(templatePath, filename); + fs.readFile(filePath, (err, data) => { + if (err) throw err; + const newFilePath = path.join(newPath, newFilename); + fs.appendFile(newFilePath, replacePlaceholders( + computeConditionals(data.toString(), this.options), + this.componentName + )); + }); + }); + }); + }, + generate() { this.componentName = this.modalPanel.getItem().children[1].getModel().getText().trim(); + const {createDirectory} = this.options; if (!!this.componentName) { - const newPath = path.join(this.basePath, this.componentName); - if (!fs.existsSync(newPath)) { - console.log(`Generating ${newPath}`); - fs.mkdir(newPath, err => { - if (err) throw err; - // use included templates if user-defined path is empty - const templatePath = - atom.config.get(`generate-react-component.${this.mode}TemplatePath`).trim() - || path.resolve(__dirname, `${this.mode}_template`); - fs.readdir(templatePath, (err, files) => { + const newPath = createDirectory ? path.join(this.basePath, this.componentName) : this.basePath; + + if (createDirectory) { + if (!fs.existsSync(newPath)) { + console.log(`Generating ${newPath}`); + fs.mkdir(newPath, err => { if (err) throw err; - files.map(filename => { - const newFilename = replacePlaceholders(filename, this.componentName); - const filePath = path.resolve(templatePath, filename); - fs.readFile(filePath, (err, data) => { - if (err) throw err; - const newFilePath = path.join(newPath, newFilename); - fs.appendFile(newFilePath, replacePlaceholders( - computeConditionals(data.toString(), this.options), - this.componentName - )); - }); - }); + this.createFiles(newPath); }); - }); - return this.close(); + } else { + this.modalPanel.getItem().children[2].textContent = 'Path already exists'; + } } else { - this.modalPanel.getItem().children[2].textContent = 'Path already exists'; + console.log(`Generating component`); + this.createFiles(newPath); } + return this.close(); } else { this.modalPanel.getItem().children[2].textContent = 'Invalid name'; } From f00ff6839a3c7553a30814f0b560feecc41bcbb7 Mon Sep 17 00:00:00 2001 From: Merlin Patterson Date: Mon, 5 Feb 2018 09:51:58 -0500 Subject: [PATCH 2/2] Add createDirectory option to modal --- lib/generate-react-component-view.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/generate-react-component-view.js b/lib/generate-react-component-view.js index 68363a9..c13a6a8 100644 --- a/lib/generate-react-component-view.js +++ b/lib/generate-react-component-view.js @@ -26,19 +26,25 @@ export default class GenerateReactComponentView { errorMessage.classList.add('text-error'); this.element.appendChild(errorMessage); + this.generateCheckbox('createDirectory'); + // Create checkbox for each conditional atom.config.get('generate-react-component.conditionals').forEach(cnd => { - const div = document.createElement('div'); - div.setAttribute('style', 'margin-top: 6px'); - div.setAttribute('id', cnd); - const checkbox = document.createElement('label'); - checkbox.classList.add('input-label'); - checkbox.innerHTML = ` ${cnd}`; - div.appendChild(checkbox); - this.element.appendChild(div); + this.generateCheckbox(cnd); }); } + generateCheckbox(name) { + const div = document.createElement('div'); + div.setAttribute('style', 'margin-top: 6px'); + div.setAttribute('id', name); + const checkbox = document.createElement('label'); + checkbox.classList.add('input-label'); + checkbox.innerHTML = ` ${name}`; + div.appendChild(checkbox); + this.element.appendChild(div); + } + // Returns an object that can be retrieved when package is activated serialize() {}