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

125 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-12-08 23:08:29 +00:00
/**
* External dependencies.
*/
const fs = require( 'fs' );
2021-12-08 23:38:45 +00:00
const path = require( 'path' );
2021-12-08 23:08:29 +00:00
const readlineSync = require( 'readline-sync' );
2021-12-08 23:38:45 +00:00
/**
* Internal dependencies.
*/
2021-12-08 23:08:29 +00:00
const { resolveLocalE2ePath, resolvePackagePath } = require( './test-config' );
/**
* Create a path relative to the local `tests/e2e` folder.
* @param relativePath
* @return {string}
*/
const createLocalE2ePath = ( relativePath ) => {
let specFolderPath = '';
2021-12-08 23:38:45 +00:00
const folders = [ `..${path.sep}..${path.sep}tests`, `..${path.sep}e2e`, relativePath ];
2021-12-08 23:08:29 +00:00
folders.forEach( ( folder ) => {
specFolderPath = resolveLocalE2ePath( folder );
if ( ! fs.existsSync( specFolderPath ) ) {
console.log( `Creating folder ${specFolderPath}` );
fs.mkdirSync( specFolderPath );
}
} );
return specFolderPath;
};
/**
* 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;
};
/**
*
* @param {string} localE2ePath Destination path
* @param {string} packageE2ePath Source path
* @param {string} packageName Source package. Default @woocommerce/e2e-environment package.
* @return {boolean}
*/
const confirmLocalCopy = ( localE2ePath, packageE2ePath, packageName = '' ) => {
const localPath = resolveLocalE2ePath( localE2ePath );
const packagePath = resolvePackagePath( packageE2ePath, packageName );
const confirmPrompt = `${localE2ePath} already exists. Overwrite? [Y]es/[n]o: `;
let overwriteFiles;
if ( fs.existsSync( localPath ) ) {
overwriteFiles = confirm( confirmPrompt, 'ny' );
overwriteFiles = overwriteFiles.toLowerCase();
} else {
overwriteFiles = 'y';
}
if ( overwriteFiles == 'y' ) {
fs.copyFileSync( packagePath, localPath );
return true;
}
return false;
};
/**
* Prompt for confirmation before deleting a local E2E file.
*
* @param {string} localE2ePath Relative path to local E2E file.
*/
const confirmLocalDelete = ( localE2ePath ) => {
const localPath = resolveLocalE2ePath( localE2ePath );
if ( ! fs.existsSync( localPath ) ) {
return;
}
2021-12-08 23:38:45 +00:00
const confirmPrompt = `${localE2ePath} exists. Delete? [y]es/[n]o: `;
2021-12-08 23:08:29 +00:00
const deleteFile = confirm( confirmPrompt, 'ny' );
if ( deleteFile == 'y' ) {
fs.unlinkSync( localPath );
}
};
/**
* Get the install data for a tests package.
*
* @param {string} packageName npm package name
* @return {string}
*/
const getPackageData = ( packageName ) => {
const packageSlug = packageName.replace( '@', '' ).replace( /\//g, '.' );
2021-12-08 23:38:45 +00:00
const installFiles = require( `${packageName}${path.sep}installFiles` );
2021-12-08 23:08:29 +00:00
return { packageSlug, ...installFiles };
};
/**
* Install test runner and test container defaults
*/
const installDefaults = () => {
createLocalE2ePath( 'docker' );
console.log( 'Writing tests/e2e/docker/initialize.sh' );
2021-12-08 23:38:45 +00:00
confirmLocalCopy( `docker${path.sep}initialize.sh`, `installFiles${path.sep}initialize.sh` );
2021-12-08 23:08:29 +00:00
createLocalE2ePath( 'config' );
console.log( 'Writing tests/e2e/config/jest.config.js' );
2021-12-08 23:38:45 +00:00
confirmLocalCopy( `config${path.sep}jest.config.js`, `installFiles${path.sep}jest.config.js` );
2021-12-08 23:08:29 +00:00
console.log( 'Writing tests/e2e/config/jest.setup.js' );
2021-12-08 23:38:45 +00:00
confirmLocalCopy( `config${path.sep}jest.setup.js`, `installFiles${path.sep}jest.setup.js` );
2021-12-08 23:08:29 +00:00
};
module.exports = {
createLocalE2ePath,
confirm,
confirmLocalCopy,
confirmLocalDelete,
getPackageData,
installDefaults,
};