Skip to content

Commit

Permalink
Add enquirer to ask for confirmation when replacing.
Browse files Browse the repository at this point in the history
  • Loading branch information
msaggiorato committed Jun 25, 2021
1 parent 650054a commit a634ebe
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 62 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
124 changes: 71 additions & 53 deletions scripts/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <[email protected]>", 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 <[email protected]>", 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 );
} );
})()
24 changes: 21 additions & 3 deletions scripts/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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 );
Expand Down
25 changes: 19 additions & 6 deletions utils/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a634ebe

Please sign in to comment.