wp-advads 1.0.36 → 1.0.38

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "wp-advads",
4
- "version": "1.0.36",
4
+ "version": "1.0.38",
5
5
  "description": "Create an Advanced Ads WordPress plugin ecosystem.",
6
6
  "author": {
7
7
  "name": "Shakeeb Ahmed",
@@ -64,17 +64,33 @@ function run( commitMessages ) {
64
64
  }
65
65
 
66
66
  process.stdout.write('Building plugin resources...')
67
- let buildScript
67
+ let pkg
68
68
  try {
69
- buildScript = JSON.parse( fs.readFileSync( 'package.json', 'utf8' ) ).scripts?.build
69
+ pkg = JSON.parse( fs.readFileSync( 'package.json', 'utf8' ) )
70
70
  } catch {
71
- buildScript = null
71
+ pkg = {}
72
72
  }
73
+ const buildScript = pkg.scripts?.build
74
+ const usesWpScripts = Object.values( pkg.scripts || {} ).some( ( script ) => /\bwp-scripts\b/.test( script ) )
73
75
 
74
76
  if ( ! buildScript ) {
75
77
  onSameLine(`${logSymbols.warning} No package.json build script, skipping`)
78
+ } else if ( /\bwp-advads\b/.test( buildScript ) || process.env.WP_ADVADS_BUILDING ) {
79
+ if ( usesWpScripts ) {
80
+ await runExec( 'npx wp-scripts build', {
81
+ env: { ...process.env, WP_ADVADS_BUILDING: '1' },
82
+ } )
83
+ onSameLine(`${logSymbols.success} Built plugin resources`);
84
+ if ( Array.isArray( commitMessages ) ) {
85
+ commitMessages.push( 'Build plugin resources' )
86
+ }
87
+ } else {
88
+ onSameLine(`${logSymbols.warning} Skipping recursive npm run build`)
89
+ }
76
90
  } else {
77
- await runExec( 'npm run build' )
91
+ await runExec( 'npm run build', {
92
+ env: { ...process.env, WP_ADVADS_BUILDING: '1' },
93
+ } )
78
94
  onSameLine(`${logSymbols.success} Built plugin resources`);
79
95
  if ( Array.isArray( commitMessages ) ) {
80
96
  commitMessages.push( 'Build plugin resources' )
@@ -3,6 +3,48 @@
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
+ function spawnArgv( file, args, spawnOpts ) {
40
+ if ( process.platform !== 'win32' ) {
41
+ return spawn( file, args, { ...spawnOpts, shell: false } )
42
+ }
43
+
44
+ const spec = winArgvSpawnSpec( file, args )
45
+ return spawn( spec.file, spec.args, { ...spawnOpts, ...spec.options } )
46
+ }
47
+
6
48
  /**
7
49
  * Run a command in the terminal.
8
50
  *
@@ -38,7 +80,7 @@ export function execCommand( command, callback, options = {} ) {
38
80
  }
39
81
 
40
82
  const child = Array.isArray( command )
41
- ? spawn( command[0], command.slice( 1 ), { ...spawnOpts, shell: false } )
83
+ ? spawnArgv( command[0], command.slice( 1 ), spawnOpts )
42
84
  : spawn( command, { ...spawnOpts, shell: true } )
43
85
 
44
86
  let stdout = ''
@@ -136,6 +136,39 @@ test( 'resource build is skipped when package.json has no build script', async (
136
136
  } )
137
137
  } )
138
138
 
139
+ test( 'resource build is skipped when scripts.build invokes wp-advads', async () => {
140
+ await withPackage( { scripts: { build: 'rm -rf assets/dist && npx wp-advads build' } }, async () => {
141
+ await runBuild()
142
+
143
+ assert.equal(
144
+ execCalls.some( ( call ) => call.cmd === 'npm run build' ),
145
+ false
146
+ )
147
+ assert.equal( potCalled, true )
148
+ } )
149
+ } )
150
+
151
+ test( 'resource build runs wp-scripts when scripts.build invokes wp-advads', async () => {
152
+ await withPackage( {
153
+ scripts: {
154
+ build: 'rm -rf assets/dist && npx wp-advads build',
155
+ start: 'wp-scripts start',
156
+ },
157
+ }, async () => {
158
+ await runBuild()
159
+
160
+ assert.equal(
161
+ execCalls.some( ( call ) => call.cmd === 'npm run build' ),
162
+ false
163
+ )
164
+ assert.equal(
165
+ execCalls.some( ( call ) => call.cmd === 'npx wp-scripts build' ),
166
+ true
167
+ )
168
+ assert.equal( potCalled, true )
169
+ } )
170
+ } )
171
+
139
172
  test( 'composer dump receives COMPOSER_VENDOR_DIR packages', async () => {
140
173
  await runBuild()
141
174
 
@@ -7,7 +7,7 @@ import { existsSync, 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 } from '../src/utilities/shell.js'
10
+ import { execCommand, runCommand, winArgvSpawnSpec } from '../src/utilities/shell.js'
11
11
 
12
12
  function execOnce( command ) {
13
13
  return new Promise( ( resolve ) => {
@@ -130,3 +130,24 @@ test( 'execCommand does not ENOBUFS on stdout larger than 1MB', async () => {
130
130
  console.log = originalLog
131
131
  }
132
132
  } )
133
+
134
+ test( 'Windows argv spawn uses cmd.exe so PATHEXT commands like wp.cmd resolve', () => {
135
+ const spec = winArgvSpawnSpec( 'wp', [ 'i18n', 'make-pot' ], 'C:\\Windows\\system32\\cmd.exe' )
136
+ assert.equal( spec.file, 'C:\\Windows\\system32\\cmd.exe' )
137
+ assert.deepEqual( spec.args.slice( 0, 3 ), [ '/d', '/s', '/c' ] )
138
+ assert.match( spec.args[3], /wp/ )
139
+ assert.equal( spec.options.windowsVerbatimArguments, true )
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, /\^&/ )
153
+ } )