Skip to content
This repository has been archived by the owner on Aug 23, 2021. It is now read-only.

Adding error callback in fs.appendFile() for fixing error #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
166 changes: 98 additions & 68 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,69 @@
const vscode = require('vscode');
const path = require('path');
const fs = require('fs-plus');
const vscode = require("vscode");
const path = require("path");
const fs = require("fs-plus");

const replacePlaceholders = (templateString, componentName) => (
templateString.replace(/__ComponentName__/g, componentName)
);
const replacePlaceholders = (templateString, componentName) =>
templateString.replace(/__ComponentName__/g, componentName);

const computeConditionals = (templateString, options) => (
Object.keys(options).reduce((prev, curr) => (
options[curr]
? prev.replace(new RegExp('\\/\\* ?IF ?!'+curr+'[\\s\\S]*?ENDIF ?\\*\\/', 'g'), '')
: prev.replace(new RegExp(`\\/\\* ?IF ?`+curr+'[\\s\\S]*?ENDIF ?\\*\\/', 'g'), '')
), templateString).replace(/\/\* ?(END)?IF.*?\*\/\n?/g, '')
);
const computeConditionals = (templateString, options) =>
Object.keys(options)
.reduce(
(prev, curr) =>
options[curr]
? prev.replace(
new RegExp(
"\\/\\* ?IF ?!" + curr + "[\\s\\S]*?ENDIF ?\\*\\/",
"g"
),
""
)
: prev.replace(
new RegExp(
`\\/\\* ?IF ?` + curr + "[\\s\\S]*?ENDIF ?\\*\\/",
"g"
),
""
),
templateString
)
.replace(/\/\* ?(END)?IF.*?\*\/\n?/g, "");

const validateName = (value, basePath) => {
if (value === undefined || value.trim() === '') {
return 'Invalid name';
if (value === undefined || value.trim() === "") {
return "Invalid name";
}
if (fs.existsSync(path.join(basePath, value.trim()))) {
return 'Path already exists';
return "Path already exists";
}
return null;
}
};

const validateYN = (value) => {
if (value !== undefined && value.toLowerCase() !== 'y' && value.toLowerCase() !== 'n') {
const validateYN = value => {
if (
value !== undefined &&
value.toLowerCase() !== "y" &&
value.toLowerCase() !== "n"
) {
return 'Type "y" or "n"';
}
return null;
}
};

const generate = (componentName, inputPath, mode, enabledOptions) => {
const basePath = fs.isDirectorySync(inputPath)
? inputPath
: path.join(inputPath, '..');
: path.join(inputPath, "..");
const newPath = path.join(basePath, componentName);

console.log(`Generating ${newPath}`);
fs.mkdir(newPath, err => {
if (err) throw err;
// use included templates if user-defined path is empty
const templatePath =
vscode.workspace.getConfiguration('generate-react-component').get(`${mode}TemplatePath`).trim()
|| path.resolve(__dirname, `${mode}_template`);
vscode.workspace
.getConfiguration("generate-react-component")
.get(`${mode}TemplatePath`)
.trim() || path.resolve(__dirname, `${mode}_template`);

fs.readdir(templatePath, (err, files) => {
if (err) throw err;
Expand All @@ -53,62 +73,72 @@ const generate = (componentName, inputPath, mode, enabledOptions) => {
fs.readFile(filePath, (err, data) => {
if (err) throw err;
const newFilePath = path.join(newPath, newFilename);
fs.appendFile(newFilePath, replacePlaceholders(
computeConditionals(data.toString(), enabledOptions),
componentName
));
fs.appendFile(
newFilePath,
replacePlaceholders(
computeConditionals(data.toString(), enabledOptions),
componentName
),
error => {
if (err) throw err;
}
);
});
});
});
});
}
};

const createDisposable = (type) => (
vscode.commands.registerCommand(`extension.gen_${type}`, (target) => {
const createDisposable = type =>
vscode.commands.registerCommand(`extension.gen_${type}`, target => {
// Display input box prompting for component name
vscode.window.showInputBox({
prompt: `Enter ${type} name`,
validateInput: name => validateName(name, target.path),
}).then((name) => {
if (name === undefined) return undefined;
vscode.window
.showInputBox({
prompt: `Enter ${type} name`,
validateInput: name => validateName(name, target.path)
})
.then(name => {
if (name === undefined) return undefined;

const options = vscode.workspace
.getConfiguration('generate-react-component')
.get('conditionals');
const options = vscode.workspace
.getConfiguration("generate-react-component")
.get("conditionals");

// Display a new input box for every conditional, resolve all in series
let p = Promise.resolve({ name, enabledOptions: {} });
return options.reduce((pc, opt) => {
return pc = pc.then((prev) => (vscode.window.showInputBox({
prompt: `Enable ${opt}? y/N`,
validateInput: value => validateYN(value),
}).then(value => {
if (value === undefined || prev === undefined) return undefined;
// Display a new input box for every conditional, resolve all in series
let p = Promise.resolve({ name, enabledOptions: {} });
return options.reduce((pc, opt) => {
return (pc = pc.then(prev =>
vscode.window
.showInputBox({
prompt: `Enable ${opt}? y/N`,
validateInput: value => validateYN(value)
})
.then(value => {
if (value === undefined || prev === undefined) return undefined;

return {
name,
enabledOptions: Object.assign(
{},
prev.enabledOptions,
{ [opt]: (value.toLowerCase() === 'y') }
),
};
})));
}, p);
}).then((params) => {
if (params === undefined) return;
return {
name,
enabledOptions: Object.assign({}, prev.enabledOptions, {
[opt]: value.toLowerCase() === "y"
})
};
})
));
}, p);
})
.then(params => {
if (params === undefined) return;

const { name, enabledOptions } = params;
generate(name.trim(), target.fsPath, type, enabledOptions);
});
})
)
const { name, enabledOptions } = params;
generate(name.trim(), target.fsPath, type, enabledOptions);
});
});

const activate = (context) => {
const activate = context => {
context.subscriptions.push(
createDisposable('component'),
createDisposable('container')
);
}
createDisposable("component"),
createDisposable("container")
);
};

exports.activate = activate;