From ae2c1b5880061f3ac6b80769006e6e74e1962a5a Mon Sep 17 00:00:00 2001 From: Janghun Lee Date: Wed, 3 Jul 2019 22:50:34 +0900 Subject: [PATCH] Adding error callback in fs.appendFile() for fixing error --- extension.js | 166 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 98 insertions(+), 68 deletions(-) diff --git a/extension.js b/extension.js index 6cfc2a5..01ac1f0 100644 --- a/extension.js +++ b/extension.js @@ -1,40 +1,58 @@ -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}`); @@ -42,8 +60,10 @@ const generate = (componentName, inputPath, mode, enabledOptions) => { 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; @@ -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;