File size: 4,136 Bytes
1e92f2d |
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 |
#! /usr/env/bin node
const fs = require( 'fs' );
const chalk = require( 'chalk' );
async function getConfigFeatures( config ) {
const file = `${ __dirname }/../config/${ config }.json`;
const json = await fs.readFileSync( file, 'utf8' );
const data = JSON.parse( json );
return data.features;
}
function calcLongestConfig( result ) {
const configLengths = result.map( ( item ) => ( item.config ? item.config.length : 0 ) );
return Math.max( ...configLengths );
}
function getPadding( count ) {
let padding = '';
for ( let i = 0; i < count; i++ ) {
padding += ' ';
}
return padding;
}
function sortResult( result ) {
result.sort( ( a, b ) => {
if ( a.config < b.config ) {
return -1;
}
if ( a.config > b.config ) {
return 1;
}
return 0;
} );
}
function getFormattedFeatureString( set ) {
if ( set === true ) {
return chalk.green( set );
}
if ( set === false ) {
return chalk.red( set );
}
if ( set === null ) {
return chalk.yellow( '(not set)' );
}
return chalk.yellow( set );
}
function outputResults( results ) {
const resultKeys = Object.keys( results ).sort();
if ( resultKeys.length === 0 ) {
console.log( 'No matching features found.' );
return;
}
for ( const key of resultKeys ) {
const result = results[ key ];
const maxLength = calcLongestConfig( result );
sortResult( result );
console.log( chalk.white.bgBlue.bold( key ) );
for ( const item of result ) {
const { config, set } = item;
const configStr = chalk.cyan( `\t${ config }:` );
const setStr = getFormattedFeatureString( set );
console.log( `${ configStr }${ getPadding( maxLength - config.length + 5 ) }${ setStr }` );
}
console.log( '' );
}
}
const configs = [
'development',
'jetpack-cloud-development',
'jetpack-cloud-horizon',
'jetpack-cloud-production',
'jetpack-cloud-stage',
'a8c-for-agencies-development',
'a8c-for-agencies-horizon',
'a8c-for-agencies-stage',
'a8c-for-agencies-production',
'horizon',
'production',
'stage',
'test',
'wpcalypso',
];
if ( process.argv.length !== 3 ) {
const helpText = `
${ chalk.yellow.bold( 'Usage: yarn feature-search {flag-search}' ) }
${ chalk.cyan(
'\nThis script makes it easy to search for particular feature flags across config environments. The value of {flag-search} can be a simple string (letters and numbers, no special characters) or a valid regular expression.'
) }
${ chalk.cyan( 'Example: Searching by simple string' ) }
${ chalk.bgBlue( '\tyarn feature-search plugin' ) }
${ chalk.cyan( 'Example: Searching by regex' ) }
${ chalk.bgBlue( `\tyarn feature-search 'bundl(e|ing)'` ) }
${ chalk.cyan.italic( 'Note: Regular expression searches should be surrounded by quotes.' ) }
`;
console.log( helpText );
process.exit( 1 );
}
let searchRe = null;
try {
searchRe = new RegExp( process.argv[ 2 ], 'g' );
} catch ( e ) {
console.log( chalk.red( `Error processing search term '${ process.argv[ 2 ] }'` ) );
console.log(
chalk.red( `
Please make sure your search is a valid regular expression or does not contain any special characters (for simple string searches).
[${ e.name }] ${ e.message }
` )
);
process.exit( 1 );
}
const main = async () => {
const results = {};
// Find all of the matching flags in each config file.
for ( const config of configs ) {
const features = await getConfigFeatures( config );
for ( const flag in features ) {
if ( flag.match( searchRe ) ) {
if ( ! results.hasOwnProperty( flag ) ) {
results[ flag ] = [];
}
results[ flag ].push( {
config,
set: features[ flag ],
} );
}
}
}
// Add any configs that aren't part of the result set because they didn't have a flag match.
// This ensures that our output is the same for each flag.
for ( const key in results ) {
const knownConfigs = results[ key ].map( ( item ) => item.config );
const missingConfigs = configs.filter( ( config ) => ! knownConfigs.includes( config ) );
missingConfigs.forEach( ( missingConfig ) => {
results[ key ].push( {
config: missingConfig,
set: null,
} );
} );
}
outputResults( results );
};
main();
|