#!/usr/bin/env node /** * External dependencies. */ const fs = require( 'fs' ); const sprintf = require( 'sprintf-js' ).sprintf; /** * Internal dependencies. */ const { resolveLocalE2ePath, resolvePackage, resolvePackagePath, } = require( '../utils' ); const args = process.argv.slice( 2 ); const [ command, packageName ] = args; /** * Install the test scripts and sample default.json configuration */ if ( command == 'install' ) { //** const package = resolvePackage( packageName ).name; if ( ! package.length ) { //@todo add error message return; } const packageSlug = package.replace( '@', '' ).replace( /\//g, '.' ); const { packageInstallFiles } = require( package ); const { testSpecs, defaultJson, initializeSh } = packageInstallFiles; // Write sample default.json if ( defaultJson ) { const defaultJsonName = `config/default-${packageSlug}.json`; const defaultJsonSample = resolveLocalE2ePath( defaultJsonName ); const packageJsonSample = resolvePackagePath( defaultJson, package ); fs.copyFileSync( packageJsonSample, defaultJsonSample ); console.log( `\nCreated sample test configuration to 'tests/e2e/${defaultJsonName}'.\n` ); } // Write sample initialize.sh if ( initializeSh ) { const defaultInitName = `docker/${packageSlug}.sh`; const defaultInitPath = resolveLocalE2ePath( defaultInitName ); const packageInitPath = resolvePackagePath( initializeSh, package ); fs.copyFileSync( packageInitPath, defaultInitPath ); console.log( `\nCreated sample test container initialization script to 'tests/e2e/${defaultInitName}'.\n` ); } // Write test files if ( ! testSpecs ) { return; } const testsSpecFile = resolvePackagePath( testSpecs, package ); 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 eol = "\n"; const autoGenerate = sprintf( '/* This file was auto-generated by the command `npx wc-e2e install %s. */', packageName ); let importLineFormat; if ( testFormat.toLowerCase() == 'cjs' ) { importLineFormat = sprintf( "const {%%s} = require( '%s' );", package ); } else { importLineFormat = sprintf( "import {%%s} from '%s';", package ); } // Loop through folders and files to write test scripts. for ( let f = 0; f < active.length; f++ ) { const testFolder = active[ f ]; const { testFiles } = testFolder; if ( ! testFiles || ! testFiles.length ) { continue; } const specFolder = testFolder.name.length ? `specs/${testFolder.name}` : 'specs'; const 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 ); // @todo Add check to see if file exists // fs.existsSync // @todo Confirm overwrite // readline 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; // '' adds a blank line buffer.push( sprintf( importLineFormat, importPrefix + testImport ), '' ); // Add test function calls and write the file let functionCalls = testFile.functions.map( functionName => functionName + '();' ); buffer.push( ...functionCalls ); fs.writeFileSync( testFilePath, buffer.join( eol ) ); } console.log( eol ); } } // @todo: deprecated files. }