Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·184 lines (162 loc) · 5.49 KB
/
Copy pathindex.js
File metadata and controls
executable file
·184 lines (162 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env node
/**
* wp-deployer
*
* Deploys a WordPress plugin or theme to the WordPress.org SVN repo.
* Config is read from package.json under the "wpDeployer" key, a standalone
* wp-deployer.json (mutually exclusive with the package.json key), or an
* explicit --config path.
* Run from the project root (where package.json and wpDeployer config live).
*
* 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'
import { loadConfigSource } from './lib/config-source.js'
import { createPluginSteps, createThemeSteps } from './lib/steps.js'
import { runPreflightSync } from './lib/preflight.js'
import { DeployError } from './lib/deploy-error.js'
import { EXIT_SUCCESS, EXIT_CONFIG, EXIT_RUNTIME, EXIT_SIGINT } from './lib/exit-codes.js'
const exec = promisify(execCb)
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const argv = minimist(process.argv.slice(2), {
boolean: ['help', 'version', 'assets', 'dry-run'],
string: ['config'],
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 config
from package.json, wp-deployer.json, or --config.
Options:
--help, -h Show this message
--version, -v Print wp-deployer version
--assets Deploy only the assets directory (plugin only; skips trunk and tag)
--dry-run Prepare working copy and local SVN changes only; no commit or remote tag copy
--config <path> Use this config file instead of package.json / wp-deployer.json
`)
}
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 ' : ''
process.on('SIGINT', () => {
console.error(chalk.yellow('Interrupted.'))
process.exit(EXIT_SIGINT)
})
const wpDeployer = async () => {
const dryRun = Boolean(argv['dry-run'])
if (dryRun) {
console.log(chalk.yellow('Dry run: SVN checkout and local prep will run; commits and remote operations are skipped.'))
}
console.log(chalk.cyan('Processing...'))
const configPath = argv.config
const sourceResult = loadConfigSource(pkg, { fs, cwd: process.cwd(), configPath })
if (sourceResult.error) {
console.error(chalk.red(sourceResult.errorMessage))
return EXIT_CONFIG
}
let { settings, error, errorMessage } = resolveSettings(pkg, sourceResult.wpDeployerConfig)
if (error === 'invalid_slug') {
console.error(chalk.red(`Invalid slug: ${errorMessage}`))
return EXIT_CONFIG
}
if (error === 'username_required') {
console.error(chalk.red('Username is required.'))
return EXIT_CONFIG
}
if (error === 'invalid_username') {
console.error(chalk.red(errorMessage || 'Invalid username.'))
return EXIT_CONFIG
}
if (error === 'theme_earlier_version_required') {
console.error(chalk.red('For repoType theme, earlierVersion is required.'))
return EXIT_CONFIG
}
if (error === 'invalid_new_version') {
console.error(chalk.red(`Invalid package version for SVN (newVersion): ${errorMessage}`))
return EXIT_CONFIG
}
if (error === 'invalid_earlier_version') {
console.error(chalk.red(`Invalid earlierVersion for SVN: ${errorMessage}`))
return EXIT_CONFIG
}
if (error === 'invalid_svn_url') {
console.error(chalk.red(`Invalid SVN URL: ${errorMessage}`))
return EXIT_CONFIG
}
if (error === 'invalid_config') {
console.error(chalk.red(errorMessage || 'Invalid wpDeployer configuration.'))
return EXIT_CONFIG
}
if (argv.assets) {
if (settings.repoType !== 'plugin') {
console.error(chalk.red('--assets is only supported for plugins.'))
return EXIT_CONFIG
}
settings = {
...settings,
deployTrunk: false,
deployTag: false,
deployAssets: true
}
}
try {
runPreflightSync(settings, { fs, awk })
} catch (e) {
if (e instanceof DeployError) {
console.error(chalk.red(e.message))
return e.exitCode
}
throw e
}
const helpers = { exec, fs, awk, noRunIfEmpty, dryRun }
const steps = settings.repoType === 'plugin'
? createPluginSteps(settings, helpers)
: createThemeSteps(settings, helpers)
try {
let s = settings
for (const step of steps) {
s = await step(s)
}
if (dryRun) {
console.log(chalk.green('Dry run finished — working copy prepared; no changes were pushed to WordPress.org.'))
} else {
console.log(chalk.green('Finished successfully.'))
}
return EXIT_SUCCESS
} catch (err) {
console.error(chalk.red(err?.message || err))
return EXIT_RUNTIME
}
}
wpDeployer()
.then((code) => {
process.exit(code ?? EXIT_SUCCESS)
})
.catch((err) => {
if (err instanceof DeployError) {
console.error(chalk.red(err.message))
process.exit(err.exitCode)
}
console.error(chalk.red(err?.message || err))
process.exit(EXIT_RUNTIME)
})