Skip to content
This repository was archived by the owner on Aug 23, 2021. It is now read-only.
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
22 changes: 14 additions & 8 deletions lib/generate-react-component-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<input class='input-checkbox' type='checkbox'> ${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 = `<input class='input-checkbox' type='checkbox'> ${name}`;
div.appendChild(checkbox);
this.element.appendChild(div);
}

// Returns an object that can be retrieved when package is activated
serialize() {}

Expand Down
76 changes: 51 additions & 25 deletions lib/generate-react-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export default {
order: 3,
type: 'string',
default: ''
},
createDirectory: {
description: '',
order: 4,
type: 'boolean',
default: true
}
},

Expand All @@ -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();
Expand Down Expand Up @@ -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;
});
});
Expand All @@ -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';
}
Expand Down