Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,41 @@ Run from your project root (the directory that contains `package.json` with your
npx wp-deployer
```

Show CLI help or version:

```sh
npx wp-deployer --help
npx wp-deployer --version
```

Or add a script to `package.json` and run it:

```sh
npm run wpdeploy
```

## SVN authentication

wp-deployer is intended for **deploying from your own computer** to WordPress.org SVN. It does **not** store or read an SVN password.

* **`username`** in `wpDeployer` is passed to `svn` as `--username` only.
* **Passwords** are handled entirely by your **Subversion client** (prompt in the terminal and/or the **OS keychain** / SVN credential cache, depending on your setup).
* The first time you deploy (or after credentials expire), SVN may **ask for your password**. Later runs often **do not prompt**, because SVN reuses **cached** credentials—that is normal and secure on a personal machine.
* Follow [WordPress.org’s current documentation](https://developer.wordpress.org/plugins/wordpress-org/how-to-use-subversion/) for account access.

On a **shared** computer, review SVN’s credential storage and clear saved auth when you are done if needed.

## Settings

* **slug** : Plugin or theme slug; Default: `name` value in `package.json`
* **username** : WordPress repository username; This is required.
* **repoType**: Repo type; `plugin` or `theme`; Default: `plugin`
* **repoType**: Repo type; `plugin` or `theme`; Default: `plugin`.
* **buildDir**: The directory where your theme or plugin exists as you want it on the repo. Default: `dist`
* **deployTrunk**: Whether to deploy to trunk. This could be set to false to only commit the assets directory. Applies for `plugin` only; Default: `true`
* **deployTag**: Whether to create a tag for this version from trunk. Set to `false` to skip tagging. Applies for `plugin` only; Default: `true`
* **deployAssets**: Whether to deploy assets. Applies for `plugin` only; Default: `false`
* **assetsDir**: The directory where your plugins assets are kept; Default: `.wordpress-org`
* **tmpDir**: Parent directory for the SVN working copy (`slug` is appended). Default: system temp directory from `os.tmpdir()` (not hard-coded `/tmp`).
* **earlierVersion**: Last released version. Applies for `theme` only; This is required if `repoType` is `theme`.

## Example
Expand Down
39 changes: 38 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
* Exit codes: 0 success, 1 config/validation/preflight, 2 deploy failure, 130 SIGINT.
*/

import path from 'node:path'
import { fileURLToPath } from 'node:url'
import fs from 'fs-extra'
import chalk from 'chalk'
import minimist from 'minimist'
import { exec as execCb } from 'child_process'
import { promisify } from 'util'
import { resolveSettings } from './lib/config.js'
Expand All @@ -22,7 +25,41 @@ import { EXIT_SUCCESS, EXIT_CONFIG, EXIT_RUNTIME, EXIT_SIGINT } from './lib/exit

const exec = promisify(execCb)

const pkg = fs.readJsonSync('./package.json')
const __dirname = path.dirname(fileURLToPath(import.meta.url))

const argv = minimist(process.argv.slice(2), {
boolean: ['help', 'version'],
alias: { h: 'help', v: 'version' }
})

function printHelp () {
console.log(`Usage: wp-deployer [options]

Deploy a WordPress plugin or theme to WordPress.org SVN using wpDeployer in package.json.

Options:
--help, -h Show this message
--version, -v Print wp-deployer version
`)
}

function printVersion () {
const selfPkg = fs.readJsonSync(path.join(__dirname, 'package.json'))
console.log(selfPkg.version)
}

if (argv.help) {
printHelp()
process.exit(EXIT_SUCCESS)
}

if (argv.version) {
printVersion()
process.exit(EXIT_SUCCESS)
}

const pkgPath = path.join(process.cwd(), 'package.json')
const pkg = fs.readJsonSync(pkgPath)

const awk = process.platform === 'win32' ? 'gawk' : 'awk'
const noRunIfEmpty = process.platform !== 'darwin' ? '--no-run-if-empty ' : ''
Expand Down
37 changes: 21 additions & 16 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
/**
* Config resolution: defaults + wpDeployer merge, url/svnPath/buildDir normalization.
* Config resolution: defaults + wpDeployer merge, svnPath/buildDir normalization,
* and WordPress.org SVN base URL (derived from repoType + slug).
* Returns { settings, error }. error is set for validation failures (caller should exit).
*/

import merge from 'just-merge'
import path from 'node:path'
import os from 'node:os'
import { validateSettingsSchema } from './config-schema.js'
import { validateSvnSafeVersion, validateSvnUrl } from './validators/index.js'
import { hasPathUnsafeChars } from './validators/shared.js'

function wordpressOrgSvnBaseUrl (repoType, slug) {
if (repoType === 'theme') {
return `https://themes.svn.wordpress.org/${slug}/`
}
return `https://plugins.svn.wordpress.org/${slug}/`
}

export function getDefaults (pkg) {
return {
url: '',
slug: `${pkg.name}`,
// Seeded from pkg.name; resolveSettings aligns with slug when wpDeployer omits mainFile
mainFile: `${pkg.name}.php`,
Expand All @@ -22,7 +30,7 @@ export function getDefaults (pkg) {
deployTag: true,
deployAssets: false,
assetsDir: '.wordpress-org',
tmpDir: '/tmp/',
tmpDir: os.tmpdir(),
earlierVersion: '',
newVersion: pkg.version
}
Expand All @@ -36,7 +44,7 @@ export function getDefaults (pkg) {
export function resolveSettings (pkg) {
const defaults = getDefaults(pkg)
const wpDeployer = Object.prototype.hasOwnProperty.call(pkg, 'wpDeployer') ? pkg.wpDeployer : {}
let settings = merge(defaults, wpDeployer)
let settings = { ...defaults, ...wpDeployer }

const slug = settings.slug
if (typeof slug !== 'string' || slug.length === 0) {
Expand All @@ -60,20 +68,15 @@ export function resolveSettings (pkg) {
settings.mainFile = `${settings.slug}.php`
}

settings = merge(settings, {
svnPath: settings.tmpDir.replace(/\/$|$/, '/') + settings.slug
})
settings = {
...settings,
svnPath: path.join(settings.tmpDir, settings.slug)
}

settings.buildDir = settings.buildDir.replace(/\/$|$/, '/')

// Default WordPress.org SVN URLs follow `slug` (merged default: pkg.name), not pkg.name directly.
if (!settings.url) {
if (settings.repoType === 'plugin') {
settings.url = `https://plugins.svn.wordpress.org/${settings.slug}/`
} else if (settings.repoType === 'theme') {
settings.url = `https://themes.svn.wordpress.org/${settings.slug}/`
}
}
// Not configurable: only wordpress.org SVN is supported; ignore legacy wpDeployer.url.
delete settings.url

const schemaResult = validateSettingsSchema(settings)
if (!schemaResult.ok) {
Expand All @@ -84,6 +87,8 @@ export function resolveSettings (pkg) {
}
}

settings.url = wordpressOrgSvnBaseUrl(settings.repoType, settings.slug)

const urlResult = validateSvnUrl(settings.url)
if (!urlResult.ok) {
return {
Expand Down
2 changes: 0 additions & 2 deletions lib/schemas/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"description": "Schema for wp-deployer runtime configuration.",
"type": "object",
"required": [
"url",
"slug",
"mainFile",
"username",
Expand All @@ -22,7 +21,6 @@
"svnPath"
],
"properties": {
"url": { "type": "string" },
"slug": { "type": "string", "minLength": 1 },
"mainFile": { "type": "string", "minLength": 1 },
"username": { "type": "string", "minLength": 1 },
Expand Down
32 changes: 17 additions & 15 deletions lib/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Errors propagate — the runner should abort the pipeline on first failure.
*/

import path from 'node:path'

function copyDirectory (fs, srcDir, destDir) {
if (srcDir.slice(-1) !== '/') {
srcDir = `${srcDir}/`
Expand All @@ -16,15 +18,15 @@ export function createPluginSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
const clearTrunk = (s) => {
return async function (s) {
console.log('Clearing trunk.')
await exec(`rm -fr ${s.svnPath}/trunk/*`)
await exec(`rm -fr ${path.join(s.svnPath, 'trunk')}/*`)
return s
}
}

const clearAssets = (s) => {
return async function (s) {
console.log('Clearing assets.')
await exec(`rm -fr ${s.svnPath}/assets/*`)
await exec(`rm -fr ${path.join(s.svnPath, 'assets')}/*`)
return s
}
}
Expand All @@ -33,7 +35,7 @@ export function createPluginSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
return async function (s) {
console.log(`Checking out ${s.url}${dir}/...`)
const checkoutUrl = `${s.url}${dir}/`
const targetPath = `${s.svnPath}/${dir}`
const targetPath = path.join(s.svnPath, dir)
await exec(`svn co --force-interactive --username="${s.username}" ${checkoutUrl} ${targetPath}`, { maxBuffer: s.maxBuffer })
console.log('Check out complete.')
return s
Expand All @@ -43,7 +45,7 @@ export function createPluginSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
const copyBuild = (s) => {
return async function (s) {
console.log(`Copying build directory: ${s.buildDir}`)
copyDirectory(fs, s.buildDir, `${s.svnPath}/trunk/`)
copyDirectory(fs, s.buildDir, path.join(s.svnPath, 'trunk') + path.sep)
return s
}
}
Expand All @@ -53,7 +55,7 @@ export function createPluginSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
console.log('Adding files in trunk')
let cmd = 'svn resolve --accept working -R . && svn status |' + awk + " '/^[?]/{print $2}' | xargs " + noRunIfEmpty + 'svn add;'
cmd += 'svn status | ' + awk + " '/^[!]/{print $2}' | xargs " + noRunIfEmpty + 'svn delete;'
await exec(cmd, { cwd: `${s.svnPath}/trunk` })
await exec(cmd, { cwd: path.join(s.svnPath, 'trunk') })
return s
}
}
Expand All @@ -62,7 +64,7 @@ export function createPluginSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
return async function (s) {
const commitMsg = `Committing ${s.newVersion} to trunk`
const cmd = `svn commit --force-interactive --username="${s.username}" -m "${commitMsg}"`
await exec(cmd, { cwd: `${s.svnPath}/trunk` })
await exec(cmd, { cwd: path.join(s.svnPath, 'trunk') })
return s
}
}
Expand All @@ -80,7 +82,7 @@ export function createPluginSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
const copyAssets = (s) => {
return async function (s) {
console.log('Copying assets')
copyDirectory(fs, s.assetsDir, `${s.svnPath}/assets/`)
copyDirectory(fs, s.assetsDir, path.join(s.svnPath, 'assets') + path.sep)
return s
}
}
Expand All @@ -90,7 +92,7 @@ export function createPluginSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
console.log('Adding assets')
let cmd = 'svn resolve --accept working -R . && svn status |' + awk + " '/^[?]/{print $2}' | xargs " + noRunIfEmpty + 'svn add;'
cmd += 'svn status | ' + awk + " '/^[!]/{print $2}' | xargs " + noRunIfEmpty + 'svn delete;'
await exec(cmd, { cwd: `${s.svnPath}/assets` })
await exec(cmd, { cwd: path.join(s.svnPath, 'assets') })
return s
}
}
Expand All @@ -99,7 +101,7 @@ export function createPluginSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
return async function (s) {
const commitMsg = 'Committing assets'
const cmd = `svn commit --force-interactive --username="${s.username}" -m "${commitMsg}"`
await exec(cmd, { cwd: `${s.svnPath}/assets` })
await exec(cmd, { cwd: path.join(s.svnPath, 'assets') })
return s
}
}
Expand All @@ -125,7 +127,7 @@ export function createThemeSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
const prepareThemeTmp = (s) => {
return async function (s) {
console.log('Preparing temporary folder.')
await exec(`rm -fr ${s.svnPath}/`)
await exec(`rm -fr ${s.svnPath}`)
return s
}
}
Expand All @@ -134,7 +136,7 @@ export function createThemeSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
return async function (s) {
console.log(`Checking out ${s.url}...`)
const checkoutUrl = `${s.url}/`
const targetPath = `${s.svnPath}/`
const targetPath = s.svnPath
await exec(`svn co --force-interactive --username="${s.username}" ${checkoutUrl} ${targetPath}`, { maxBuffer: s.maxBuffer })
console.log('Check out complete.')
return s
Expand All @@ -153,15 +155,15 @@ export function createThemeSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
const clearTheme = (s) => {
return async function (s) {
console.log('Clearing theme.')
await exec(`rm -fr ${s.svnPath}/${s.newVersion}/*`)
await exec(`rm -fr ${path.join(s.svnPath, s.newVersion)}/*`)
return s
}
}

const copyTheme = (s) => {
return async function (s) {
console.log(`Copying build directory: ${s.buildDir}`)
copyDirectory(fs, s.buildDir, `${s.svnPath}/${s.newVersion}/`)
copyDirectory(fs, s.buildDir, path.join(s.svnPath, s.newVersion) + path.sep)
return s
}
}
Expand All @@ -171,7 +173,7 @@ export function createThemeSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
console.log('Adding theme files')
let cmd = 'svn resolve --accept working -R . && svn status |' + awk + " '/^[?]/{print $2}' | xargs " + noRunIfEmpty + 'svn add;'
cmd += 'svn status | ' + awk + " '/^[!]/{print $2}' | xargs " + noRunIfEmpty + 'svn delete;'
await exec(cmd, { cwd: `${s.svnPath}/${s.newVersion}` })
await exec(cmd, { cwd: path.join(s.svnPath, s.newVersion) })
return s
}
}
Expand All @@ -181,7 +183,7 @@ export function createThemeSteps (settings, { exec, fs, awk, noRunIfEmpty }) {
const commitMsg = `Committing theme ${s.newVersion}`
console.log(commitMsg)
const cmd = `svn commit --force-interactive --username="${s.username}" -m "${commitMsg}"`
await exec(cmd, { cwd: `${s.svnPath}/${s.newVersion}` })
await exec(cmd, { cwd: path.join(s.svnPath, s.newVersion) })
return s
}
}
Expand Down
13 changes: 3 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wp-deployer",
"version": "1.0.3",
"version": "2.0.0",
"description": "Deploy WordPress plugin and theme to the WordPress.org repo.",
"main": "index.js",
"type": "module",
Expand Down Expand Up @@ -33,7 +33,7 @@
"ajv": "^8.18.0",
"chalk": "^5.6.2",
"fs-extra": "^11.3.4",
"just-merge": "^3.2.0"
"minimist": "^1.2.8"
},
"devDependencies": {
"standard": "^17.1.2"
Expand Down
Loading