wp-advads 1.0.38 → 1.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/package.json +1 -1
- package/src/app.js +6 -0
- package/src/commands/changelog.js +94 -0
- package/src/commands/index.js +1 -0
- package/src/utilities/shell.js +1 -35
- package/test/changelog-preview.test.js +51 -0
- package/test/shell.test.js +8 -21
package/README.md
CHANGED
|
@@ -51,6 +51,14 @@ Bumps the version, changelog, translations, build, and git (explicit file list,
|
|
|
51
51
|
npx wp-advads release
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
#### `changelog`
|
|
55
|
+
|
|
56
|
+
Prints pending `.changelog` entries to the console. Does not write `changelog.txt` or `readme.txt`.
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npx wp-advads changelog
|
|
60
|
+
```
|
|
61
|
+
|
|
54
62
|
#### `class <classname> [options]`
|
|
55
63
|
|
|
56
64
|
Writes a PHP class under `paths.php`. Combine `--heading` with a template flag (`-i` / `-g` / `-r` / `-s`).
|
package/package.json
CHANGED
package/src/app.js
CHANGED
|
@@ -68,6 +68,12 @@ const app = async () => {
|
|
|
68
68
|
.description('Create a new release for the plugin')
|
|
69
69
|
.action(commands.release);
|
|
70
70
|
|
|
71
|
+
// Command: changelog
|
|
72
|
+
program
|
|
73
|
+
.command('changelog')
|
|
74
|
+
.description('Print pending .changelog entries to the console')
|
|
75
|
+
.action(commands.changelog);
|
|
76
|
+
|
|
71
77
|
// Command: class
|
|
72
78
|
program
|
|
73
79
|
.command('class')
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External Dependencies
|
|
3
|
+
*/
|
|
4
|
+
import fs from 'fs'
|
|
5
|
+
import logSymbols from 'log-symbols'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Internal Dependencies
|
|
9
|
+
*/
|
|
10
|
+
import { heading, onSameLine, write } from '../utilities/index.js'
|
|
11
|
+
|
|
12
|
+
const LABELS = {
|
|
13
|
+
fix: 'Fix',
|
|
14
|
+
feature: 'Feature',
|
|
15
|
+
improvement: 'Improvement',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Collect formatted changelog lines from `.changelog` (read-only).
|
|
20
|
+
*
|
|
21
|
+
* @param {string} dir Directory with pending changelog entry files
|
|
22
|
+
* @returns {string[]|null} Formatted lines, empty array if none, null if missing
|
|
23
|
+
*/
|
|
24
|
+
export function collectChangelogLines( dir = './.changelog' ) {
|
|
25
|
+
if ( ! fs.existsSync( dir ) ) {
|
|
26
|
+
return null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const changes = {
|
|
30
|
+
fix: [],
|
|
31
|
+
feature: [],
|
|
32
|
+
improvement: [],
|
|
33
|
+
}
|
|
34
|
+
const allowed = Object.keys( LABELS )
|
|
35
|
+
|
|
36
|
+
for ( const fileName of fs.readdirSync( dir ).sort() ) {
|
|
37
|
+
const type = fileName.split( '-' )[ 0 ]
|
|
38
|
+
|
|
39
|
+
if ( ! allowed.includes( type ) ) {
|
|
40
|
+
continue
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const content = fs.readFileSync( `${ dir }/${ fileName }`, 'utf8' ).trim()
|
|
44
|
+
if ( content ) {
|
|
45
|
+
changes[ type ].push( `- ${ LABELS[ type ] }: ${ content }` )
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const entries = [
|
|
50
|
+
...changes.feature,
|
|
51
|
+
...changes.improvement,
|
|
52
|
+
...changes.fix,
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
if ( entries.length === 0 ) {
|
|
56
|
+
return []
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const date = new Date().toLocaleDateString( 'en', {
|
|
60
|
+
year: 'numeric',
|
|
61
|
+
month: 'long',
|
|
62
|
+
day: 'numeric',
|
|
63
|
+
} )
|
|
64
|
+
|
|
65
|
+
return [
|
|
66
|
+
`= Unreleased (${ date }) =`,
|
|
67
|
+
'',
|
|
68
|
+
...entries,
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Print pending changelog entries to the console. Writes no files.
|
|
74
|
+
*/
|
|
75
|
+
export default function changelog() {
|
|
76
|
+
heading( 'Changelog preview' )
|
|
77
|
+
write( 'Reading .changelog...' )
|
|
78
|
+
|
|
79
|
+
const lines = collectChangelogLines()
|
|
80
|
+
|
|
81
|
+
if ( null === lines ) {
|
|
82
|
+
onSameLine( `${ logSymbols.error } Unable to find .changelog directory` )
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if ( 0 === lines.length ) {
|
|
87
|
+
onSameLine( `${ logSymbols.warning } No pending changelog entries` )
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
onSameLine( `${ logSymbols.success } Pending changelog entries` )
|
|
92
|
+
console.log( '' )
|
|
93
|
+
console.log( lines.join( '\n' ) )
|
|
94
|
+
}
|
package/src/commands/index.js
CHANGED
package/src/utilities/shell.js
CHANGED
|
@@ -3,46 +3,12 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { spawn } from 'node:child_process'
|
|
5
5
|
|
|
6
|
-
// cmd.exe metacharacters. See https://github.com/moxystudio/node-cross-spawn/blob/v7.0.6/lib/util/escape.js
|
|
7
|
-
const WIN_META = /([()\][%!^"`<>&|;, *?])/g
|
|
8
|
-
|
|
9
|
-
function escapeWinCommand( arg ) {
|
|
10
|
-
return String( arg ).replace( WIN_META, '^$1' )
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function escapeWinArgument( arg ) {
|
|
14
|
-
let value = String( arg )
|
|
15
|
-
value = value.replace( /(?=(\\+?)?)\1"/g, '$1$1\\"' )
|
|
16
|
-
value = value.replace( /(?=(\\+?)?)\1$/, '$1$1' )
|
|
17
|
-
value = `"${value}"`
|
|
18
|
-
return value.replace( WIN_META, '^$1' )
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Windows argv spawn must go through cmd.exe so PATHEXT (.cmd/.bat) resolves.
|
|
23
|
-
* Direct spawn('wp') looks for wp.exe only and throws ENOENT.
|
|
24
|
-
*
|
|
25
|
-
* @param {string} file Command name
|
|
26
|
-
* @param {string[]} args Arguments
|
|
27
|
-
* @param {string} [comspec] cmd.exe path
|
|
28
|
-
* @returns {{ file: string, args: string[], options: Object }}
|
|
29
|
-
*/
|
|
30
|
-
export function winArgvSpawnSpec( file, args, comspec = process.env.ComSpec || process.env.comspec || 'cmd.exe' ) {
|
|
31
|
-
const shellCommand = [ escapeWinCommand( file ), ...args.map( escapeWinArgument ) ].join( ' ' )
|
|
32
|
-
return {
|
|
33
|
-
file: comspec,
|
|
34
|
-
args: [ '/d', '/s', '/c', `"${shellCommand}"` ],
|
|
35
|
-
options: { shell: false, windowsVerbatimArguments: true },
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
6
|
function spawnArgv( file, args, spawnOpts ) {
|
|
40
7
|
if ( process.platform !== 'win32' ) {
|
|
41
8
|
return spawn( file, args, { ...spawnOpts, shell: false } )
|
|
42
9
|
}
|
|
43
10
|
|
|
44
|
-
|
|
45
|
-
return spawn( spec.file, spec.args, { ...spawnOpts, ...spec.options } )
|
|
11
|
+
return spawn( file, args, { ...spawnOpts, shell: true } )
|
|
46
12
|
}
|
|
47
13
|
|
|
48
14
|
/**
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standalone changelog command: print pending entries to console, write nothing.
|
|
3
|
+
*/
|
|
4
|
+
import assert from 'node:assert/strict'
|
|
5
|
+
import { existsSync } from 'node:fs'
|
|
6
|
+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
7
|
+
import { tmpdir } from 'node:os'
|
|
8
|
+
import { join } from 'node:path'
|
|
9
|
+
import { test } from 'node:test'
|
|
10
|
+
import { collectChangelogLines } from '../src/commands/changelog.js'
|
|
11
|
+
|
|
12
|
+
test( 'collectChangelogLines formats .changelog entries and does not write files', async () => {
|
|
13
|
+
const dir = await mkdtemp( join( tmpdir(), 'wp-advads-changelog-preview-' ) )
|
|
14
|
+
const cwd = process.cwd()
|
|
15
|
+
process.chdir( dir )
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
await mkdir( join( dir, '.changelog' ) )
|
|
19
|
+
await writeFile( join( dir, '.changelog', 'feature-1' ), 'add widgets\n' )
|
|
20
|
+
await writeFile( join( dir, '.changelog', 'fix-2' ), 'crash on save\n' )
|
|
21
|
+
await writeFile( join( dir, '.changelog', 'improvement-3' ), 'faster load\n' )
|
|
22
|
+
await writeFile( join( dir, '.changelog', 'notes' ), 'ignored\n' )
|
|
23
|
+
|
|
24
|
+
const lines = collectChangelogLines()
|
|
25
|
+
|
|
26
|
+
assert.ok( lines.some( ( line ) => line.startsWith( '= Unreleased (' ) ) )
|
|
27
|
+
assert.ok( lines.includes( '- Feature: add widgets' ) )
|
|
28
|
+
assert.ok( lines.includes( '- Improvement: faster load' ) )
|
|
29
|
+
assert.ok( lines.includes( '- Fix: crash on save' ) )
|
|
30
|
+
assert.equal( lines.indexOf( '- Feature: add widgets' ) < lines.indexOf( '- Improvement: faster load' ), true )
|
|
31
|
+
assert.equal( lines.indexOf( '- Improvement: faster load' ) < lines.indexOf( '- Fix: crash on save' ), true )
|
|
32
|
+
assert.equal( existsSync( join( dir, 'changelog.txt' ) ), false )
|
|
33
|
+
assert.equal( existsSync( join( dir, 'readme.txt' ) ), false )
|
|
34
|
+
} finally {
|
|
35
|
+
process.chdir( cwd )
|
|
36
|
+
await rm( dir, { recursive: true, force: true } )
|
|
37
|
+
}
|
|
38
|
+
} )
|
|
39
|
+
|
|
40
|
+
test( 'collectChangelogLines returns null when .changelog is missing', async () => {
|
|
41
|
+
const dir = await mkdtemp( join( tmpdir(), 'wp-advads-changelog-missing-' ) )
|
|
42
|
+
const cwd = process.cwd()
|
|
43
|
+
process.chdir( dir )
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
assert.equal( collectChangelogLines(), null )
|
|
47
|
+
} finally {
|
|
48
|
+
process.chdir( cwd )
|
|
49
|
+
await rm( dir, { recursive: true, force: true } )
|
|
50
|
+
}
|
|
51
|
+
} )
|
package/test/shell.test.js
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
* TASK-005: argv is not interpolated through a shell.
|
|
4
4
|
*/
|
|
5
5
|
import assert from 'node:assert/strict'
|
|
6
|
-
import { existsSync, rmSync } from 'node:fs'
|
|
6
|
+
import { existsSync, readFileSync, rmSync } from 'node:fs'
|
|
7
7
|
import { tmpdir } from 'node:os'
|
|
8
8
|
import { join } from 'node:path'
|
|
9
9
|
import { test } from 'node:test'
|
|
10
|
-
import { execCommand, runCommand
|
|
10
|
+
import { execCommand, runCommand } from '../src/utilities/shell.js'
|
|
11
11
|
|
|
12
12
|
function execOnce( command ) {
|
|
13
13
|
return new Promise( ( resolve ) => {
|
|
@@ -131,23 +131,10 @@ test( 'execCommand does not ENOBUFS on stdout larger than 1MB', async () => {
|
|
|
131
131
|
}
|
|
132
132
|
} )
|
|
133
133
|
|
|
134
|
-
test( 'Windows argv spawn uses
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
assert.
|
|
138
|
-
assert.match(
|
|
139
|
-
assert.
|
|
140
|
-
assert.equal( spec.options.shell, false )
|
|
141
|
-
} )
|
|
142
|
-
|
|
143
|
-
test( 'Windows argv spawn escapes cmd metacharacters so headers and injection stay literal', () => {
|
|
144
|
-
const spec = winArgvSpawnSpec(
|
|
145
|
-
'wp',
|
|
146
|
-
[ '--headers={"Last-Translator":"A <b@c.com>"}', '& echo pwned' ],
|
|
147
|
-
'cmd.exe'
|
|
148
|
-
)
|
|
149
|
-
const line = spec.args[3]
|
|
150
|
-
assert.match( line, /\^</ )
|
|
151
|
-
assert.match( line, /\^>/ )
|
|
152
|
-
assert.match( line, /\^&/ )
|
|
134
|
+
test( 'Windows argv spawn uses a shell so PATHEXT commands like wp.cmd resolve', () => {
|
|
135
|
+
const src = readFileSync( new URL( '../src/utilities/shell.js', import.meta.url ), 'utf8' )
|
|
136
|
+
const spawnArgv = src.split( 'function spawnArgv' )[1].split( 'export function' )[0]
|
|
137
|
+
assert.match( spawnArgv, /process\.platform !== 'win32'/ )
|
|
138
|
+
assert.match( spawnArgv, /shell:\s*false/ )
|
|
139
|
+
assert.match( spawnArgv, /shell:\s*true/ )
|
|
153
140
|
} )
|