-
-
Save aulisius/d3bc3e7221d6613e34b093cf64c22da9 to your computer and use it in GitHub Desktop.
php version manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! node | |
// Simple node.js based PHP Version Manager | |
// Tested with v16.17.1 | |
let process = require("process"); | |
let [command = "help", version = ""] = process.argv.slice(2); | |
let fs = require("fs"); | |
let { execSync } = require("child_process"); | |
if (command === "help") { | |
console.log(` | |
PHP Version Manager | |
Switch between installed PHP versions. | |
The PHP versions themselves need to installed using Homebrew. | |
Usage | |
# Switch to [email protected] | |
~ $(pvm use 8.0) | |
# View all installed versions | |
~ pvm list | |
`); | |
return; | |
} | |
const currentPath = execSync("echo $PATH").toString().trim(); | |
const BREW_PREFIX = execSync("brew --prefix").toString().trim(); | |
let installedVersions = fs | |
.readdirSync(`${BREW_PREFIX}/opt/`, { encoding: "utf-8" }) | |
.filter((content) => content.startsWith("php")) | |
.map((version) => version.replace("php@", "")) | |
.map((version) => (version === "php" ? "default" : version)); | |
if (["list", "ls"].includes(command)) { | |
console.log(`${installedVersions.join("\n")}`); | |
} | |
if (command === "use" && installedVersions.includes(version)) { | |
let phpPathPrefix = `${BREW_PREFIX}/opt/php`; | |
let PATH = currentPath | |
.split(":") | |
.filter((path) => !path.startsWith(phpPathPrefix)) | |
.join(":"); | |
let versionPath = version === "default" ? "" : `@${version}`; | |
let PHP = `${phpPathPrefix}${versionPath}/`; | |
console.log(`export PATH=${PHP}bin:${PHP}sbin:${PATH}`); | |
} else { | |
process.exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment