From 6d166dc07c7f784864da32af4e9a5df0e9fa5778 Mon Sep 17 00:00:00 2001 From: Mohd Samgan Khan Date: Mon, 25 Jul 2022 16:54:19 +0400 Subject: [PATCH] V0.1.0 (#15) * Format on save (#14) * v0.1.0 --- README.md | 12 ++++ extension.js | 153 +++++++++------------------------------------- package.json | 6 +- utils/commands.js | 138 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 126 deletions(-) create mode 100644 utils/commands.js diff --git a/README.md b/README.md index e8ca759..bd8840f 100644 --- a/README.md +++ b/README.md @@ -38,4 +38,16 @@ This will **format the current open file** with the set configurations you provi PS: A system pop up will ask you to install pint if it's not installed. +## Setting (Format on Save) + +**This setting is entirely optional** + +if you would like to unable format on save. Add the following line to the `settings.json` + +```json +"editor.laravel.pint.enabled": true +``` + +This will unable pint format on save for the current file. + **Enjoy!** diff --git a/extension.js b/extension.js index ca98e65..70822d2 100644 --- a/extension.js +++ b/extension.js @@ -1,135 +1,42 @@ const vscode = require("vscode") -const cp = require("child_process") -const { - checkBinaryExist, - projectDirectory, - infoMessage, - errorMessage, - currentEditor, - copyPintJson -} = require("./utils/methods") -const { PINT_BINARY } = require("./utils/constants") +const { formatCurrentFile, formatProject, copyPintJsonToRoot } = require("./utils/commands") /** * @param {vscode.ExtensionContext} context */ function activate(context) { - let format = vscode.commands.registerCommand("laravel-pint-vscode.format", 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...") - } - } - ) + context.subscriptions.push( + vscode.commands.registerCommand("laravel-pint-vscode.format", async () => { + await formatProject() + }) + ) + + context.subscriptions.push( + vscode.commands.registerCommand("laravel-pint-vscode.format-file", async () => { + await formatCurrentFile() + }) + ) + + context.subscriptions.push( + vscode.commands.registerCommand("laravel-pint-vscode.publish-config", async () => { + copyPintJsonToRoot() + }) + ) + + context.subscriptions.push( + vscode.workspace.onWillSaveTextDocument(async (event) => { + if (event.document.fileName.endsWith(".php")) { + let allConfig = JSON.parse(JSON.stringify(await vscode.workspace.getConfiguration())) + try { + if (allConfig.editor.laravel.pint.enabled) { + await formatCurrentFile(false) } - }) - } - - 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.") - } - } - ) - }) - - context.subscriptions.push(format) - - let formatFile = vscode.commands.registerCommand("laravel-pint-vscode.format-file", 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...") - } - } - ) - } - }) - } - - cp.exec( - 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 { - return infoMessage("Formatting your current file with Laravel Pint.") + } catch (e) { + // console.log(e) } } - ) - }) - - context.subscriptions.push(formatFile) - - let publishConfig = vscode.commands.registerCommand("laravel-pint-vscode.publish-config", 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...") - } - } - ) - } - }) - } - - copyPintJson() - return infoMessage("pint.json copied to your project.") - }) - - context.subscriptions.push(publishConfig) + }) + ) } // this method is called when your extension is deactivated diff --git a/package.json b/package.json index f1bba33..14a0b25 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "laravel-pint-vscode", "displayName": "Laravel Pint Formatter", - "description": "vscode extension for laravel pint with zero config", + "description": "vscode extension for laravel pint with zero config and optional format on save option.", "publisher": "msamgan", - "version": "0.0.9", + "version": "0.1.0", "icon": "larapint-icon.png", "engines": { "vscode": "^1.68.0" @@ -46,7 +46,7 @@ "command": "laravel-pint-vscode.format-file", "alt": "laravel-pint-vscode.format-file", "group": "1_modification" - } + } ] }, "keybindings": [ diff --git a/utils/commands.js b/utils/commands.js new file mode 100644 index 0000000..4ba3578 --- /dev/null +++ b/utils/commands.js @@ -0,0 +1,138 @@ +const vscode = require("vscode") +const cp = require("child_process") +const { + checkBinaryExist, + infoMessage, + projectDirectory, + errorMessage, + currentEditor, + copyPintJson +} = 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 + */ +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...") + } + } + ) + } + }) + } + + cp.exec( + 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.") + } + } + } + ) +} + +/** + * Runs the Laravel Pint command to format the project. + * @returns None + */ +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...") + } + } + ) + } + }) + } + + 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.") + } + } + ) +} + +/** + * 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...") + } + } + ) + } + }) + } + + copyPintJson() + return infoMessage("pint.json copied to your project.") +}