2019-12-15 22:59:35 +00:00
|
|
|
const fs = require( 'fs-extra' );
|
|
|
|
const path = require( 'path' );
|
|
|
|
const promptly = require( 'promptly' );
|
|
|
|
const chalk = require( 'chalk' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const files = [
|
|
|
|
'._gitignore',
|
|
|
|
'_README.md',
|
|
|
|
'_webpack.config.js',
|
|
|
|
'_main.php',
|
|
|
|
'_package.json',
|
2020-11-12 09:31:18 +00:00
|
|
|
'._eslintrc.js',
|
|
|
|
'._prettierrc.json',
|
2022-04-25 20:51:35 +00:00
|
|
|
'._wp-env.json',
|
2020-02-14 02:23:21 +00:00
|
|
|
];
|
|
|
|
const maybeThrowError = ( error ) => {
|
2019-12-15 22:59:35 +00:00
|
|
|
if ( error ) throw error;
|
|
|
|
};
|
|
|
|
|
|
|
|
( async () => {
|
|
|
|
console.log( '\n' );
|
2020-02-14 02:23:21 +00:00
|
|
|
console.log(
|
|
|
|
chalk.yellow(
|
|
|
|
'🎉 Welcome to WooCommerce Admin Extension Starter Pack 🎉'
|
|
|
|
)
|
|
|
|
);
|
2019-12-15 22:59:35 +00:00
|
|
|
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 );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
files.forEach( ( file ) => {
|
2019-12-15 22:59:35 +00:00
|
|
|
const from = path.join( __dirname, file );
|
|
|
|
const to = path.join(
|
|
|
|
folder,
|
2022-04-25 20:51:35 +00:00
|
|
|
file === '_main.php'
|
2020-02-14 02:23:21 +00:00
|
|
|
? `${ extensionSlug }.php`
|
|
|
|
: file.replace( '_', '' )
|
2019-12-15 22:59:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fs.readFile( from, 'utf8', ( error, data ) => {
|
|
|
|
maybeThrowError( error );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const addSlugs = data.replace(
|
|
|
|
/{{extension_slug}}/g,
|
|
|
|
extensionSlug
|
|
|
|
);
|
|
|
|
const result = addSlugs.replace(
|
|
|
|
/{{extension_name}}/g,
|
|
|
|
extensionName
|
|
|
|
);
|
2019-12-15 22:59:35 +00:00
|
|
|
|
|
|
|
fs.writeFile( to, result, 'utf8', maybeThrowError );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
fs.copy(
|
|
|
|
path.join( __dirname, 'src' ),
|
|
|
|
path.join( folder, 'src' ),
|
|
|
|
maybeThrowError
|
|
|
|
);
|
2019-12-15 22:59:35 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
fs.copy( folder, path.join( '../', extensionSlug ), ( error ) => {
|
2019-12-15 22:59:35 +00:00
|
|
|
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' );
|
2020-02-14 02:23:21 +00:00
|
|
|
console.log(
|
|
|
|
chalk.green(
|
2022-04-25 20:51:35 +00:00
|
|
|
'Run the following commands from the root of the extension to scaffold a dev environment.'
|
2020-02-14 02:23:21 +00:00
|
|
|
)
|
|
|
|
);
|
2019-12-15 22:59:35 +00:00
|
|
|
process.stdout.write( '\n' );
|
2022-04-25 20:51:35 +00:00
|
|
|
console.log( 'wp-env start' );
|
2022-03-04 04:01:16 +00:00
|
|
|
console.log( 'pnpm install' );
|
|
|
|
console.log( 'pnpm start' );
|
2019-12-15 22:59:35 +00:00
|
|
|
process.stdout.write( '\n' );
|
|
|
|
} )();
|