From a634ebe37b5b8f281597c1cf87aea56212d77830 Mon Sep 17 00:00:00 2001 From: Matias Saggiorato Date: Thu, 13 May 2021 10:52:52 -0300 Subject: [PATCH] Add enquirer to ask for confirmation when replacing. --- package.json | 1 + scripts/plugin.js | 124 ++++++++++++++++++++++++++------------------- scripts/project.js | 24 +++++++-- utils/file.js | 25 ++++++--- 4 files changed, 112 insertions(+), 62 deletions(-) diff --git a/package.json b/package.json index a3666da..9f5ad77 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "dependencies": { "cross-spawn": "^5.1.0", "download-git-repo": "^3.0.2", + "enquirer": "^2.3.6", "minimist": "^1.2.5", "mkdirp": "^1.0.4", "read-pkg-up": "^1.0.1", diff --git a/scripts/plugin.js b/scripts/plugin.js index bf52e3d..9179bcd 100644 --- a/scripts/plugin.js +++ b/scripts/plugin.js @@ -7,6 +7,7 @@ const downloadGH = require( 'download-git-repo' ); const rimraf = require( 'rimraf' ); const replace = require( 'replace' ); const slugify = require( 'slugify' ); +const { prompt } = require('enquirer'); /** * Internal dependencies @@ -85,60 +86,77 @@ for( var i in paths ) { var targetPluginPath = path.join( basePath, path.basename( sourcePluginPath ) ); -if ( existsSync( targetPluginPath ) ) { - console.warn( "Plugin already exists." ); - process.exit( 1 ); -} - -rimraf.sync( sourcePath ); - -downloadGH( "saucal/WordPress-Plugin-Boilerplate#" + data.branch, sourcePath, function(err) { - if ( err ) { - throw err; +(async function(){ + if ( existsSync( targetPluginPath ) ) { + let relative = path.relative( process.cwd(), targetPluginPath ); + let response; + try { + response = await prompt({ + type: 'confirm', + name: 'question', + message: 'Folder ' + relative + ' exists. Replace?', + initial: false, + }); + } catch { + return process.exit( 1 ); + } + if ( ! response ) { + return process.exit( 1 ); + } else { + rimraf.sync( targetPluginPath ); + } } - - renameSync( path.join( sourcePath, 'plugin-name' ), sourcePluginPath ); - - walkDirectory( sourcePluginPath, function( f ) { - let pname = path.dirname( f ); - let fname = path.basename( f ); - let newName = fname - .replace(/plugin-name/gi, pluginSlug) - .replace(/pname/gi, pluginNameShortPackage.toLowerCase().replace(/_/gi, '-')); - newName = path.join( pname, newName ); - if( newName === f ) { - return f; + + rimraf.sync( sourcePath ); + + downloadGH( "saucal/WordPress-Plugin-Boilerplate#" + data.branch, sourcePath, async function(err) { + if ( err ) { + throw err; } - renameSync( f, newName ); - return newName; - } ); - - let replacements = [ - [ "http://example.com/plugin-name-uri/", pluginURI ], - [ "WordPress Plugin Boilerplate", pluginName ], - [ "Your Name or Your Company", pluginAuthor ], - [ "Your Name ", pluginAuthorFull ], - [ "Plugin_Name", pluginNamePackage ], - [ "plugin-name", pluginSlug ], - [ "plugin_name", pluginNameInstance ], - [ "PNameSingleton", pluginNameSingleton ], - [ "PName", pluginNameShortPackage ], - [ "pname", pluginNameShortPackage.toLowerCase().replace(/_/gi, '-') ], - [ "PNAME", pluginNameContantsPrefix ], - [ "http://example.com/?", pluginAuthorURI ], - ]; - - replacements.map( function( rule ) { - replace( { - regex: rule[0], - replacement: rule[1], - paths: [ sourcePluginPath ], - recursive: true, - silent: true, + + renameSync( path.join( sourcePath, 'plugin-name' ), sourcePluginPath ); + + await walkDirectory( sourcePluginPath, function( f ) { + let pname = path.dirname( f ); + let fname = path.basename( f ); + let newName = fname + .replace(/plugin-name/gi, pluginSlug) + .replace(/pname/gi, pluginNameShortPackage.toLowerCase().replace(/_/gi, '-')); + newName = path.join( pname, newName ); + if( newName === f ) { + return f; + } + renameSync( f, newName ); + return newName; + } ); + + let replacements = [ + [ "http://example.com/plugin-name-uri/", pluginURI ], + [ "WordPress Plugin Boilerplate", pluginName ], + [ "Your Name or Your Company", pluginAuthor ], + [ "Your Name ", pluginAuthorFull ], + [ "Plugin_Name", pluginNamePackage ], + [ "plugin-name", pluginSlug ], + [ "plugin_name", pluginNameInstance ], + [ "PNameSingleton", pluginNameSingleton ], + [ "PName", pluginNameShortPackage ], + [ "pname", pluginNameShortPackage.toLowerCase().replace(/_/gi, '-') ], + [ "PNAME", pluginNameContantsPrefix ], + [ "http://example.com/?", pluginAuthorURI ], + ]; + + replacements.map( function( rule ) { + replace( { + regex: rule[0], + replacement: rule[1], + paths: [ sourcePluginPath ], + recursive: true, + silent: true, + } ); } ); + + renameSync( sourcePluginPath, targetPluginPath ); + + rimraf.sync( sourcePath ); } ); - - renameSync( sourcePluginPath, targetPluginPath ); - - rimraf.sync( sourcePath ); -} ); +})() diff --git a/scripts/project.js b/scripts/project.js index 76312ce..7a0e189 100644 --- a/scripts/project.js +++ b/scripts/project.js @@ -8,6 +8,7 @@ const rimraf = require( 'rimraf' ); const replace = require( 'replace' ); const slugify = require( 'slugify' ); const mkdirp = require('mkdirp'); +const { prompt } = require('enquirer'); /** * Internal dependencies @@ -30,7 +31,7 @@ let targetPath = process.cwd(); rimraf.sync( sourcePath ); -downloadGH( "saucal/project-gulp-boilerplate#" + data.branch, sourcePath, function(err) { +downloadGH( "saucal/project-gulp-boilerplate#" + data.branch, sourcePath, async function(err) { if ( err ) { throw err; } @@ -50,12 +51,29 @@ downloadGH( "saucal/project-gulp-boilerplate#" + data.branch, sourcePath, functi } ); } ); - walkDirectory( sourcePath, function( f ) { + await walkDirectory( sourcePath, async function( f ) { let relative = path.relative( sourcePath, f ); let replacePath = path.join( targetPath, relative ); if ( existsSync( replacePath ) ) { - rimraf.sync( replacePath ); + let response; + try { + response = await prompt({ + type: 'confirm', + name: 'question', + message: 'File ' + relative + ' exists. Replace?', + initial: false, + }); + } catch { + rimraf.sync( sourcePath ); + return process.exit( 1 ); + } + + if ( response ) { + rimraf.sync( replacePath ); + } else { + return; + } } mkdirp.sync( path.dirname( replacePath ) ); renameSync( f, replacePath ); diff --git a/utils/file.js b/utils/file.js index 311dafa..0a6db81 100644 --- a/utils/file.js +++ b/utils/file.js @@ -29,12 +29,25 @@ const getScripts = () => .filter( ( f ) => path.extname( f ) === '.js' ) .map( ( f ) => path.basename( f, '.js' ) ); -const walkDirectory = (dir, cb) => - readdirSync(dir) - .map( ( f ) => path.join( dir, f ) ) - .map( ( f ) => statSync( f ).isDirectory() ? walkDirectory( f ) : f ) - .flat() - .map( cb || ( (f) => f ) ); +const mapAsync = async function( data, cb ) { + for( let i in data ) { + data[i] = await cb( data[i], i, data ); + } + return data; +} + +const walkDirectory = async (dir, cb) => { + cb = cb || ( (f) => f ); + let data = readdirSync(dir) + .map( ( f ) => path.join( dir, f ) ); + + data = await mapAsync( data, async ( f ) => statSync( f ).isDirectory() ? await walkDirectory( f ) : f ); + + data = data.flat(); + + data = await mapAsync( data, cb ); + return data; +} module.exports = { fromProjectRoot,