-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Env: Add support for running in themes. (#17732)
* Env: Add support for running in themes. * Env: Optimize context detection filter. * Env: Update test directory structure to match convention.
- Loading branch information
Showing
5 changed files
with
75 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
/** | ||
* External dependencies | ||
*/ | ||
const util = require( 'util' ); | ||
const fs = require( 'fs' ); | ||
const stream = require( 'stream' ); | ||
const path = require( 'path' ); | ||
|
||
/** | ||
* Promisified dependencies | ||
*/ | ||
const readDir = util.promisify( fs.readdir ); | ||
const finished = util.promisify( stream.finished ); | ||
|
||
module.exports = async function detectContext() { | ||
const context = {}; | ||
|
||
// Race multiple file read streams against each other until | ||
// a plugin or theme header is found. | ||
const files = ( await readDir( './' ) ).filter( | ||
( file ) => path.extname( file ) === '.php' || path.basename( file ) === 'style.css' | ||
); | ||
const streams = []; | ||
for ( const file of files ) { | ||
const fileStream = fs.createReadStream( file, 'utf8' ); | ||
fileStream.on( 'data', ( text ) => { | ||
const [ , type ] = text.match( /(Plugin|Theme) Name: .*[\r\n]/ ) || []; | ||
if ( type ) { | ||
context.type = type.toLowerCase(); | ||
|
||
// Stop the creation of new streams by mutating the iterated array. We can't `break`, because we are inside a function. | ||
files.splice( 0 ); | ||
fileStream.destroy(); | ||
streams.forEach( ( otherFileStream ) => otherFileStream.destroy() ); | ||
} | ||
} ); | ||
streams.push( fileStream ); | ||
} | ||
await Promise.all( | ||
streams.map( ( fileStream ) => finished( fileStream ).catch( () => {} ) ) | ||
); | ||
|
||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.