woocommerce/packages/js/e2e-environment/bin/scaffold.js

191 lines
5.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* External dependencies.
*/
const fs = require( 'fs' );
const sprintf = require( 'sprintf-js' ).sprintf;
const readlineSync = require( 'readline-sync' );
/**
* Internal dependencies.
*/
const {
resolveLocalE2ePath,
resolvePackage,
resolvePackagePath,
} = require( '../utils' );
const args = process.argv.slice( 2 );
const [ command, packageName ] = args;
/**
* Prompt the console for confirmation.
*
* @param {string} prompt Prompt for the user.
* @param {string} choices valid responses.
* @return {string}
*/
const confirm = ( prompt, choices ) => {
const answer = readlineSync.keyIn( prompt, choices );
return answer;
};
/**
* Install the test scripts and sample default.json configuration
*/
if ( command == 'install' ) {
// `package` is a reserved word
const pkg = resolvePackage( packageName ).name;
if ( ! pkg.length ) {
//@todo add error message
return;
}
const packageSlug = pkg.replace( '@', '' ).replace( /\//g, '.' );
const { testSpecs, defaultJson, initializeSh } = require( `${pkg}/installFiles` );
// Write sample default.json
if ( defaultJson ) {
const defaultJsonName = `config/default-${packageSlug}.json`;
const defaultJsonSample = resolveLocalE2ePath( defaultJsonName );
const packageJsonSample = resolvePackagePath( defaultJson, pkg );
fs.copyFileSync( packageJsonSample, defaultJsonSample );
console.log( `Created sample test configuration to 'tests/e2e/${defaultJsonName}'.` );
}
// Write sample initialize.sh
if ( initializeSh ) {
const defaultInitName = `docker/${packageSlug}.sh`;
const defaultInitPath = resolveLocalE2ePath( defaultInitName );
const packageInitPath = resolvePackagePath( initializeSh, pkg );
fs.copyFileSync( packageInitPath, defaultInitPath );
console.log( `Created sample test container initialization script to 'tests/e2e/${defaultInitName}'.` );
}
if ( ! testSpecs ) {
return;
}
// Write test files
const testsSpecFile = resolvePackagePath( testSpecs, pkg );
const specs = fs.readFileSync( testsSpecFile );
const tests = JSON.parse( specs );
const { active, deprecated } = tests;
// Allow multiple spec file extensions and formats.
let testExtension = 'test.js';
let testFormat = '';
for ( let a = 2; a < args.length; a++ ) {
const nextArg = a + 1;
if ( nextArg >= args.length ) {
break;
}
switch ( args[ a ] ) {
case '--format':
testFormat = args[ nextArg ];
break;
case '--ext':
testExtension = args[ nextArg ];
break;
}
}
if ( active && active.length ) {
const blankLine = '';
const eol = "\n";
const autoGenerate = sprintf( '/* This file was auto-generated by the command `npx wc-e2e install %s. */', packageName );
let importLineFormat;
let overwriteFiles;
let confirmPrompt;
if ( testFormat.toLowerCase() == 'cjs' ) {
importLineFormat = sprintf( "const {%%s} = require( '%s' );", pkg );
} else {
importLineFormat = sprintf( "import {%%s} from '%s';", pkg );
}
// Create the entire folder structure if not present
let specFolderPath;
const rootFolders = [ '../../tests', '../e2e', 'specs' ];
rootFolders.forEach( ( folder ) => {
specFolderPath = resolveLocalE2ePath( folder );
if ( ! fs.existsSync( specFolderPath ) ) {
fs.mkdirSync( specFolderPath );
}
} );
// Loop through folders and files to write test scripts.
for ( let f = 0; f < active.length; f++ ) {
if ( overwriteFiles == 'q' ) {
overwriteFiles = '';
break;
}
const testFolder = active[ f ];
const { testFiles } = testFolder;
if ( ! testFiles || ! testFiles.length ) {
continue;
}
const specFolder = testFolder.name.length ? `specs/${testFolder.name}` : 'specs';
specFolderPath = resolveLocalE2ePath( specFolder );
// Create the test folder if it doesn't exist.
if ( ! fs.existsSync( specFolderPath ) ) {
fs.mkdirSync( specFolderPath );
}
// Create the test files.
for ( let t = 0; t < testFiles.length; t++ ) {
const testFile = testFiles[ t ];
if ( ! testFile.functions.length ) {
continue;
}
const testFileName = `${specFolder}/${testFile.name}.${testExtension}`;
const testFilePath = resolveLocalE2ePath( testFileName );
// Check to see if file exists.
if ( fs.existsSync( testFilePath ) ) {
if ( overwriteFiles != 'a' ) {
confirmPrompt = `${testFileName} already exists. Overwrite? [Y]es/[n]o/[a]ll/[q]uit: `;
overwriteFiles = confirm( confirmPrompt, 'anqy' );
overwriteFiles = overwriteFiles.toLowerCase();
}
if ( overwriteFiles == 'q' ) {
break;
}
if ( overwriteFiles != 'a' && overwriteFiles != 'y' ) {
continue;
}
}
console.log( 'Writing tests/e2e/' + testFileName );
let buffer = [ autoGenerate ];
let testSeparator, testTerminator, importPrefix;
// Add the import line.
if ( testFile.functions.length > 3 ) {
testSeparator = ',' + eol;
testTerminator = eol;
importPrefix = eol;
} else {
testSeparator = ', ';
testTerminator = ' ';
importPrefix = ' ';
}
const testImport = testFile.functions.join( testSeparator ) + testTerminator;
buffer.push( sprintf( importLineFormat, importPrefix + testImport ), blankLine );
// Add test function calls and write the file
let functionCalls = testFile.functions.map( functionName => functionName + '();' );
buffer.push( ...functionCalls, blankLine );
fs.writeFileSync( testFilePath, buffer.join( eol ) );
}
}
}
// @todo: deprecated files.
}