From 52172fffe95e099dd1d3d4466938d65a8114cd05 Mon Sep 17 00:00:00 2001 From: Mohd Samgan Khan Date: Wed, 27 Jul 2022 00:13:52 +0400 Subject: [PATCH] refactoring and bug resolution. (#17) --- package.json | 2 +- utils/commands.js | 117 +++++++--------------------------------------- utils/methods.js | 60 +++++++++++++++++++++++- 3 files changed, 75 insertions(+), 104 deletions(-) diff --git a/package.json b/package.json index 14a0b25..7e02d93 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "Laravel Pint Formatter", "description": "vscode extension for laravel pint with zero config and optional format on save option.", "publisher": "msamgan", - "version": "0.1.0", + "version": "0.1.1", "icon": "larapint-icon.png", "engines": { "vscode": "^1.68.0" diff --git a/utils/commands.js b/utils/commands.js index 4ba3578..cecc4f7 100644 --- a/utils/commands.js +++ b/utils/commands.js @@ -1,61 +1,29 @@ -const vscode = require("vscode") -const cp = require("child_process") const { checkBinaryExist, infoMessage, projectDirectory, - errorMessage, currentEditor, - copyPintJson + copyPintJson, + installPint, + runCommand } = require("./methods") const { PINT_BINARY } = require("./constants") - /** - * Format the current file with Laravel Pint. - * @param {boolean} [showNotification=true] - whether or not to show a notification when formatting. - * @returns None + * Format the current file with Laravel Pint. + * @param {boolean} [showNotification=true] - whether or not to show a notification when formatting. + * @returns None */ exports.formatCurrentFile = async (showNotification = true) => { if (!checkBinaryExist()) { - return await vscode.window - .showInformationMessage("Pint binary not found. Do you want in install?", "Yes", "No") - .then((answer) => { - infoMessage("Initiating installation...") - if (answer === "Yes") { - cp.exec( - "composer require laravel/pint", - { - cwd: projectDirectory() - }, - // eslint-disable-next-line no-unused-vars - (err, stdout, stderr) => { - if (err) { - return errorMessage("Something went wrong while running Laravel Pint.") - } else { - return infoMessage("Initiating complete...") - } - } - ) - } - }) + return installPint() } - cp.exec( + await runCommand( PINT_BINARY + " " + currentEditor(), - { - cwd: projectDirectory() - }, - // eslint-disable-next-line no-unused-vars - (err, stdout, stderr) => { - if (err) { - return errorMessage("Something went wrong while running Laravel Pint.") - } else { - if (showNotification) { - return infoMessage("Formatting your current file with Laravel Pint.") - } - } - } + projectDirectory(), + "Formatting your current file with Laravel Pint.", + showNotification ) } @@ -65,72 +33,19 @@ exports.formatCurrentFile = async (showNotification = true) => { */ exports.formatProject = async () => { if (!checkBinaryExist()) { - return await vscode.window - .showInformationMessage("Pint binary not found. Do you want in install?", "Yes", "No") - .then((answer) => { - infoMessage("Initiating installation...") - if (answer === "Yes") { - cp.exec( - "composer require laravel/pint", - { - cwd: projectDirectory() - }, - // eslint-disable-next-line no-unused-vars - (err, stdout, stderr) => { - if (err) { - return errorMessage("Something went wrong while running Laravel Pint.") - } else { - return infoMessage("Initiating complete...") - } - } - ) - } - }) + return installPint() } - cp.exec( - PINT_BINARY, - { - cwd: projectDirectory() - }, - // eslint-disable-next-line no-unused-vars - (err, stdout, stderr) => { - if (err) { - return errorMessage("Something went wrong while running Laravel Pint.") - } else { - return infoMessage("Formatting your project with Laravel Pint.") - } - } - ) + await runCommand(PINT_BINARY, projectDirectory(), "Formatting your project with Laravel Pint.") } /** - * Copies the pint.json file to the root of your project. - * @returns None + * Copies the pint.json file to the root of your project. + * @returns None */ exports.copyPintJsonToRoot = async () => { if (!checkBinaryExist()) { - return await vscode.window - .showInformationMessage("Pint binary not found. Do you want in install?", "Yes", "No") - .then((answer) => { - infoMessage("Initiating installation...") - if (answer === "Yes") { - cp.exec( - "composer require laravel/pint", - { - cwd: projectDirectory() - }, - // eslint-disable-next-line no-unused-vars - (err, stdout, stderr) => { - if (err) { - return errorMessage("Something went wrong while running Laravel Pint.") - } else { - return infoMessage("Initiating complete...") - } - } - ) - } - }) + return installPint() } copyPintJson() diff --git a/utils/methods.js b/utils/methods.js index 8e0c924..a75bc15 100644 --- a/utils/methods.js +++ b/utils/methods.js @@ -2,6 +2,7 @@ const vscode = require("vscode") const { PINT_BINARY } = require("./constants") const fs = require("fs") const path = require("path") +const cp = require("child_process") /** * Returns the path to the project directory. @@ -28,8 +29,8 @@ exports.checkBinaryExist = () => { } /** - * Copies the pint.json file to the project directory. - * @returns None + * Copies the pint.json file to the project directory. + * @returns None */ exports.copyPintJson = () => { const pintJson = path.join(this.projectDirectory(), "pint.json") @@ -40,6 +41,61 @@ exports.copyPintJson = () => { fs.copyFileSync(path.join(__dirname, "pint.json"), pintJson) } +/** + * Installs the Laravel Pint package. + * @returns + */ +exports.installPint = async () => { + return await vscode.window + .showInformationMessage("Pint binary not found. Do you want in install?", "Yes", "No") + .then((answer) => { + if (answer === "Yes") { + this.infoMessage("Initiating installation...") + cp.exec( + "composer require laravel/pint", + { + cwd: this.projectDirectory() + }, + // eslint-disable-next-line no-unused-vars + (err, stdout, stderr) => { + if (err) { + return this.errorMessage("Something went wrong while running Laravel Pint.") + } else { + return this.infoMessage("Installation complete...") + } + } + ) + } + }) +} + +/** + * Runs a command in the given directory. + * @param {string} command - the command to run + * @param {string} directory - the directory to run the command in + * @param {string} successMessage - the message to show when the command is successful + * @param {boolean} [showNotification=true] - whether or not to show a notification when the command is successful + * @returns None + */ +exports.runCommand = async (command, directory, successMessage, showNotification = true) => { + cp.exec( + command, + { + cwd: directory + }, + // eslint-disable-next-line no-unused-vars + (err, stdout, stderr) => { + if (err) { + return this.errorMessage("Something went wrong while running Laravel Pint.") + } else { + if (showNotification) { + return this.infoMessage(successMessage) + } + } + } + ) +} + /** * Displays a message to the user. * @param {string} message - the message to display