woocommerce/plugins/woocommerce-admin/bin/starter-pack/starter-pack.js

88 lines
1.9 KiB
JavaScript

const fs = require( 'fs-extra' );
const path = require( 'path' );
const promptly = require( 'promptly' );
const chalk = require( 'chalk' );
const files = [
'._gitignore',
'_README.md',
'_webpack.config.js',
'_main.php',
'_package.json',
];
const maybeThrowError = ( error ) => {
if ( error ) throw error;
};
( async () => {
console.log( '\n' );
console.log(
chalk.yellow(
'🎉 Welcome to WooCommerce Admin Extension Starter Pack 🎉'
)
);
console.log( '\n' );
const extensionName = await promptly.prompt(
chalk.yellow( 'What is the name of your extension?' )
);
const extensionSlug = extensionName.replace( / /g, '-' ).toLowerCase();
const folder = path.join( __dirname, extensionSlug );
fs.mkdir( folder, maybeThrowError );
files.forEach( ( file ) => {
const from = path.join( __dirname, file );
const to = path.join(
folder,
'_main.php' === file
? `${ extensionSlug }.php`
: file.replace( '_', '' )
);
fs.readFile( from, 'utf8', ( error, data ) => {
maybeThrowError( error );
const addSlugs = data.replace(
/{{extension_slug}}/g,
extensionSlug
);
const result = addSlugs.replace(
/{{extension_name}}/g,
extensionName
);
fs.writeFile( to, result, 'utf8', maybeThrowError );
} );
} );
fs.copy(
path.join( __dirname, 'src' ),
path.join( folder, 'src' ),
maybeThrowError
);
fs.copy( folder, path.join( '../', extensionSlug ), ( error ) => {
maybeThrowError( error );
fs.remove( folder, maybeThrowError );
} );
process.stdout.write( '\n' );
console.log(
chalk.green(
'Wonderful, your extension has been scaffolded and placed as a sibling directory to this one.'
)
);
process.stdout.write( '\n' );
console.log(
chalk.green(
'Run the following commands from the root of the extension and activate the plugin.'
)
);
process.stdout.write( '\n' );
console.log( 'npm install' );
console.log( 'npm start' );
process.stdout.write( '\n' );
} )();