Skip to content

Commit

Permalink
refactoring and bug resolution. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
msamgan authored Jul 26, 2022
1 parent 6d166dc commit 52172ff
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 104 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
117 changes: 16 additions & 101 deletions utils/commands.js
Original file line number Diff line number Diff line change
@@ -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
)
}

Expand All @@ -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()
Expand Down
60 changes: 58 additions & 2 deletions utils/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -40,6 +41,61 @@ exports.copyPintJson = () => {
fs.copyFileSync(path.join(__dirname, "pint.json"), pintJson)
}

/**
* Installs the Laravel Pint package.
* @returns <Promise>
*/
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
Expand Down

0 comments on commit 52172ff

Please sign in to comment.