Skip to content

Commit

Permalink
V0.1.0 (#15)
Browse files Browse the repository at this point in the history
* Format on save (#14)

* v0.1.0
  • Loading branch information
msamgan authored Jul 25, 2022
1 parent d0ddff4 commit 6d166dc
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 126 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!**
153 changes: 30 additions & 123 deletions extension.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -46,7 +46,7 @@
"command": "laravel-pint-vscode.format-file",
"alt": "laravel-pint-vscode.format-file",
"group": "1_modification"
}
}
]
},
"keybindings": [
Expand Down
138 changes: 138 additions & 0 deletions utils/commands.js
Original file line number Diff line number Diff line change
@@ -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.")
}

0 comments on commit 6d166dc

Please sign in to comment.