add overwrite confirmation

This commit is contained in:
Ron Rennick 2021-12-08 15:12:27 -04:00
parent 59f1f3a6b5
commit 2698cb36ff
4 changed files with 44 additions and 6 deletions

View File

@ -2,6 +2,5 @@
* Internal dependencies
*/
const allSpecs = require( './specs' );
const fs = require( 'fs' );
module.exports = allSpecs;

View File

@ -5,6 +5,7 @@
*/
const fs = require( 'fs' );
const sprintf = require( 'sprintf-js' ).sprintf;
const readlineSync = require( 'readline-sync' );
/**
* Internal dependencies.
@ -18,6 +19,18 @@ const {
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
*/
@ -49,11 +62,11 @@ if ( command == 'install' ) {
console.log( `Created sample test container initialization script to 'tests/e2e/${defaultInitName}'.` );
}
// Write test files
if ( ! testSpecs ) {
return;
}
// Write test files
const testsSpecFile = resolvePackagePath( testSpecs, pkg );
const specs = fs.readFileSync( testsSpecFile );
const tests = JSON.parse( specs );
@ -82,6 +95,8 @@ if ( command == 'install' ) {
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 {
@ -100,6 +115,11 @@ if ( command == 'install' ) {
// 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;
@ -125,10 +145,21 @@ if ( command == 'install' ) {
const testFileName = `${specFolder}/${testFile.name}.${testExtension}`;
const testFilePath = resolveLocalE2ePath( testFileName );
// @todo Add check to see if file exists
// fs.existsSync
// @todo Confirm overwrite
// readline
// 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 ];

View File

@ -33,6 +33,7 @@
"jest-each": "25.5.0",
"jest-puppeteer": "^4.4.0",
"node-stream-zip": "^1.13.6",
"readline-sync": "^1.4.10",
"request": "^2.88.2",
"sprintf-js": "^1.1.2"
},

View File

@ -129,6 +129,7 @@ importers:
jest-puppeteer: ^4.4.0
ndb: ^1.1.5
node-stream-zip: ^1.13.6
readline-sync: ^1.4.10
request: ^2.88.2
semver: ^7.3.2
sprintf-js: ^1.1.2
@ -145,6 +146,7 @@ importers:
jest-each: 25.5.0
jest-puppeteer: 4.4.0
node-stream-zip: 1.15.0
readline-sync: 1.4.10
request: 2.88.2
sprintf-js: 1.1.2
devDependencies:
@ -18063,6 +18065,11 @@ packages:
dependencies:
picomatch: 2.3.0
/readline-sync/1.4.10:
resolution: {integrity: sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==}
engines: {node: '>= 0.8.0'}
dev: false
/realpath-native/1.1.0:
resolution: {integrity: sha512-wlgPA6cCIIg9gKz0fgAPjnzh4yR/LnXovwuo9hvyGvx3h8nX4+/iLZplfUWasXpqD8BdnGnP5njOFjkUwPzvjA==}
engines: {node: '>=4'}