Skip to content

Commit

Permalink
feat(wpUpdate): ✨ standardize composer.json settings - minimum-stabil…
Browse files Browse the repository at this point in the history
…ity, prefer-stable - skip php extensions (#73)

* set minimum-stability to dev
minimum-stability (root-only)#
This defines the default behavior for filtering packages by stability. This defaults to stable, so if you rely on a dev package, you should specify it in your file to avoid surprises.

All versions of each package are checked for stability, and those that are less stable than the minimum-stability setting will be ignored when resolving your project dependencies. (Note that you can also specify stability requirements on a per-package basis using stability flags in the version constraints that you specify in a require block (see package links for more details).

Available options (in order of stability) are dev, alpha, beta, RC, and stable.

set prefer-stable true
prefer-stable (root-only)#
When this is enabled, Composer will prefer more stable packages over unstable ones when finding compatible stable packages is possible. If you require a dev version or only alphas are available for a package, those will still be selected granted that the minimum-stability allows for it.

Use "prefer-stable": true to enable.

skip ext- dependencies

Note: It is important to list PHP extensions your project requires. Not all PHP installations are created equal: some may miss extensions you may consider as standard (such as ext-mysqli which is not installed by default in Fedora/CentOS minimal installation systems). Failure to list required PHP extensions may lead to a bad user experience: Composer will install your package without any errors but it will then fail at run-time. The composer show --platform command lists all PHP extensions available on your system. You may use it to help you compile the list of extensions you use and require. Alternatively you may use third party tools to analyze your project for the list of extensions used.

* Update commands/wordpressComposerUpdate.js

Co-authored-by: Axel Engström <axel.engstrom@triggerfish.se>

* Update commands/wordpressComposerUpdate.js

Co-authored-by: Axel Engström <axel.engstrom@triggerfish.se>

* Prettified Code!

* remove dependencies in functions (make it simpler)

---------

Co-authored-by: Axel Engström <axel.engstrom@triggerfish.se>
Co-authored-by: chrillep <chrillep@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 28, 2023
1 parent 15ee842 commit ba6c104
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 39 deletions.
153 changes: 115 additions & 38 deletions commands/wordpressComposerUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,129 @@ import {
writeWarning,
} from "../lib/write.js"

export async function wpUpdate() {
const asyncExecOptions = {
stdio: "inherit",
cwd: process.env.PWD,
/**
* @param {string} minimumStability
* @returns {void}
*/
function setMinimumStability(minimumStability) {
const composerJson = getComposerJson()

if (composerJson["minimum-stability"] !== minimumStability) {
const updatedComposerJson = composerJson
updatedComposerJson["minimum-stability"] = minimumStability
fs.writeFileSync(
`${process.env.PWD}/composer.json`,
JSON.stringify(updatedComposerJson, null, 2),
)
}
}

const parsedPhpVersion = semver.parse(versions.php, {})
const phpVersion = `>=${parsedPhpVersion.major}.${parsedPhpVersion.minor}`
/**
* @param {boolean} preferStable
* @returns {void}
*/
function setPreferredStability(preferStable) {
const composerJson = getComposerJson()

try {
const composerValidateOutput = await asyncExec(
"composer validate --no-check-all --strict --no-interaction --ansi",
asyncExecOptions,
if (composerJson["prefer-stable"] !== preferStable) {
const updatedComposerJson = composerJson
updatedComposerJson["prefer-stable"] = preferStable
fs.writeFileSync(
`${process.env.PWD}/composer.json`,
JSON.stringify(updatedComposerJson, null, 2),
)
console.log(composerValidateOutput.stdout)
}
}

const composerJson = JSON.parse(
fs.readFileSync(process.env.PWD + "/composer.json", "utf8"),
)
/**
* @param {string} phpVersion
* @returns {Promise<void>}
*/
async function updateRequirePackages(phpVersion) {
const composerJson = getComposerJson()

for (const [requiredComposerPackage] of Object.entries(
composerJson.require,
)) {
let requireCommand = ""
if (requiredComposerPackage === "php") {
requireCommand = `composer require "${requiredComposerPackage}:${phpVersion}" --with-all-dependencies --no-interaction --ansi`
}
if (requiredComposerPackage !== "php") {
requireCommand = `composer require ${requiredComposerPackage} --with-all-dependencies --no-interaction --no-progress --ansi`
}
let requiredOutput = await asyncExec(requireCommand, asyncExecOptions)
writeInfo("running: " + requireCommand)
console.log(requiredOutput.stdout || requiredOutput.stderr)
for (const requiredComposerPackage of Object.keys(composerJson.require)) {
let requireCommand = ""
if (requiredComposerPackage.startsWith("ext-")) {
continue
}

for (const [requiredDevComposerPackage] of Object.entries(
composerJson["require-dev"],
)) {
const requireCommand = `composer require ${requiredDevComposerPackage} --dev --with-all-dependencies --no-interaction --no-progress --ansi`
let requiredOutput = await asyncExec(
`composer require ${requiredDevComposerPackage} --dev --with-all-dependencies --no-interaction --no-progress --ansi`,
asyncExecOptions,
)
writeInfo("running: " + requireCommand)
console.log(requiredOutput.stdout || requiredOutput.stderr)
if (requiredComposerPackage === "php") {
requireCommand = `composer require "${requiredComposerPackage}:${phpVersion}" --with-all-dependencies --no-interaction --ansi`
}
if (requiredComposerPackage !== "php") {
requireCommand = `composer require ${requiredComposerPackage} --with-all-dependencies --no-interaction --no-progress --ansi`
}
const requiredOutput = await asyncExec(
requireCommand,
getAsyncExecOptionsForPWD(),
)
writeInfo(`running: ${requireCommand}`)
console.log(requiredOutput.stdout || requiredOutput.stderr)
}
}

/**
* @returns {Promise<void>}
*/
async function updateRequireDevPackages() {
const composerJson = getComposerJson()

for (const requiredDevComposerPackage of Object.keys(
composerJson["require-dev"],
)) {
const requireCommand = `composer require ${requiredDevComposerPackage} --dev --with-all-dependencies --no-interaction --no-progress --ansi`
const requiredOutput = await asyncExec(
`composer require ${requiredDevComposerPackage} --dev --with-all-dependencies --no-interaction --no-progress --ansi`,
getAsyncExecOptionsForPWD(),
)
writeInfo(`running: ${requireCommand}`)
console.log(requiredOutput.stdout || requiredOutput.stderr)
}
}

/**
* @return {Promise<void>}
*/
async function validateComposerFile() {
const composerValidateOutput = await asyncExec(
"composer validate --no-check-all --strict --no-interaction --ansi",
getAsyncExecOptionsForPWD(),
)
console.log(composerValidateOutput.stdout)
}

/**
* @returns {object}
*/
function getComposerJson() {
return JSON.parse(fs.readFileSync(`${process.env.PWD}/composer.json`, "utf8"))
}

/**
* @return {{cwd: string, stdio: string}}
*/
function getAsyncExecOptionsForPWD() {
return {
stdio: "inherit",
cwd: process.env.PWD,
}
}

/**
* @returns {Promise<void>}
*/
export default async function wpUpdate() {
const parsedPhpVersion = semver.parse(versions.php, {})
const phpVersion = `>=${parsedPhpVersion.major}.${parsedPhpVersion.minor}`
const minimumStability = "dev"
const preferStable = true

try {
await validateComposerFile()
setMinimumStability(minimumStability)
setPreferredStability(preferStable)
await updateRequirePackages(phpVersion)
await updateRequireDevPackages()

writeWarning("Don't forget to Check your Major Updates.")
writeSuccess("Composer update completed")
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { createPageComponent } from "./commands/pageComponent.js"
import setupPath from "./commands/path.js"
import { createSendGrid } from "./commands/sendgrid.js"
import writeLisaStatusSummary from "./commands/status.js"
import { wpUpdate } from "./commands/wordpressComposerUpdate.js"
import wpUpdate from "./commands/wordpressComposerUpdate.js"
import { resetConf } from "./lib/conf.js"
import { checkDependencies, checkNodeVersion } from "./lib/dependencies.js"
import exec from "./lib/exec.js"
Expand Down

0 comments on commit ba6c104

Please sign in to comment.