Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a Promise wrapper for fs actions and minimist for argv parsing #25

Merged
merged 4 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"react-wp-scripts": "./bin/react-wp-scripts.js"
},
"dependencies": {
"chalk": "^2.3.2",
"minimist": "^1.2.0",
"react-scripts": "1.0.17",
"signal-exit": "^3.0.2",
"uppercamelcase": "^3.0.0"
"signal-exit": "^3.0.2"
}
}
35 changes: 16 additions & 19 deletions scripts/init.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/**
* Sets the start script for react-wp-scripts and moves
* loader.php to the project root folder.
*/
'use strict';

// Sets the start script for react-wp-scripts
// And move the loader.php to the project root folder
process.on( 'unhandledRejection', err => {
throw err;
} );

const fs = require( 'fs-extra' );
const path = require( 'path' );
const chalk = require( 'chalk' );
const upperCamelCase = require( 'uppercamelcase' );

const argv = require( 'minimist' )( process.argv.slice( 2 ) );

module.exports = function(
appPath,
Expand All @@ -20,12 +23,7 @@ module.exports = function(
) {

// Parse a namespace based on the name of the package
let namespace = 'ReactWPScripts';
try {
const projectPackageJSON = require( path.join( process.cwd(), 'package.json' ) );
namespace = upperCamelCase( projectPackageJSON.name );
}
catch ( err ) {}
const namespace = argv['php-namespace'] || 'ReactWPScripts';

const pkgName = require( path.join( __dirname, '..', 'package.json' ) ).name;
const reactWPScriptsPath = path.join( appPath, 'node_modules', pkgName );
Expand All @@ -52,30 +50,29 @@ module.exports = function(
// Copy the loader.php
const loaderPath = path.join( reactWPScriptsPath, 'loader.php' );

function successMessage() {
console.log( chalk.green( 'React WP Scripts Loader copied to your project root folder.' ) );
console.log( chalk.green( 'Please, follow instructions to add PHP to enqueue your assets:' ) );
console.log( chalk.blue( 'https://github.com/humanmade/react-wp-scripts#react-wp-scripts' ) );
}

const destinationFile = path.join( appPath, 'react-wp-scripts.php' );
fs.copy( loaderPath, destinationFile )
.then( () => {
.then( () => new Promise( ( resolve, reject ) => {
// Replace %%NAMESPACE%% for the specified namespace
fs.readFile( destinationFile, 'utf8', function( err, data ) {
if ( err ) {
console.log( err );
return reject( err );
}

var result = data.replace( '%%NAMESPACE%%', namespace );
fs.writeFile( destinationFile, result, 'utf8', function( err ) {
if ( err ) {
return console.log( err );
return reject( err );
}
resolve();
} );
} );
} ) )
.then( () => {
console.log( chalk.green( 'React WP Scripts Loader copied to your project root folder.' ) );
console.log( chalk.green( 'Please follow these instructions to enqueue your assets in PHP:' ) );
console.log( chalk.blue( 'https://github.com/humanmade/react-wp-scripts#react-wp-scripts' ) );
} )
.then( () => successMessage() )
.catch( err => {
console.log( chalk.bgRed( 'React WP Scripts loader could not be copied to your root folder. Error details:' ) );
console.log( chalk.red( err ) );
Expand Down