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
Created init.js script #15
Merged
Merged
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ccc8907
Added init.js script
igmoweb 3abbdbe
Debugging init.js
igmoweb ac3fcc1
Init script development
igmoweb 9ef580d
Code cleaning
igmoweb 6024803
Fixed syntax error
igmoweb 76e49e0
Setup rest of npm scripts
igmoweb 7fe027d
Updated README
igmoweb 6a12dfb
Script wasn't calling react-scripts' init.js
igmoweb d5a7a34
Merge branch 'master' into 12-scripts-version-compatibility
igmoweb 11771e7
Renamed loader file to a more meaningful name
igmoweb bdba5fa
README update
igmoweb 3665d8a
Added namespace placeholder
igmoweb 66b256f
Updated README
igmoweb 862834b
Updated file reference in README
igmoweb 0275150
Linting
igmoweb 7a9d0d8
Parse a namespace based on the name of the package
igmoweb f72727a
Added missing package
igmoweb a81b636
Reinstate plugin-specific instructions to README
kadamwhite 4cbb8e6
Use Promise wrapper for file system interactions
kadamwhite f98804c
Reinstate --php-namespace command-line argument
kadamwhite 11c9109
Remove no-longer-used uppercamelcase dependency
kadamwhite c997370
PR feedback: Change let to const
kadamwhite 2abe292
Merge pull request #25 from humanmade/12-init-proposed-changes
rmccue File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'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 commander = require('commander'); | ||
const packageJson = require('../package.json'); | ||
|
||
const program = new commander.Command( packageJson.name ) | ||
.version( packageJson.version ) | ||
.option( '--php-namespace [namespace]', 'Specify a namespace. Default: ReactWPScripts' ) | ||
.parse( process.argv ); | ||
|
||
|
||
module.exports = function( | ||
appPath, | ||
appName, | ||
verbose, | ||
originalDirectory, | ||
template | ||
) { | ||
const namespace = program.phpNamespace ? program.phpNamespace : 'ReactWPScripts'; | ||
const pkgName = require( path.join( __dirname, '..', 'package.json' ) ).name; | ||
const reactWPScriptsPath = path.join( appPath, 'node_modules', pkgName ); | ||
const appPackage = require( path.join( appPath, 'package.json' ) ); | ||
|
||
const scriptsPath = path.resolve( | ||
process.cwd(), | ||
'node_modules', | ||
'react-scripts', | ||
'scripts', | ||
'init.js' | ||
); | ||
const reactScriptsInit = require(scriptsPath); | ||
reactScriptsInit( appPath, appName, verbose, originalDirectory, template ); | ||
|
||
// Setup the custom start script | ||
appPackage.scripts.start = 'react-wp-scripts start'; | ||
|
||
fs.writeFileSync( | ||
path.join( appPath, 'package.json' ), | ||
JSON.stringify( appPackage, null, 2 ) | ||
); | ||
|
||
// 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( () => { | ||
// Replace %%NAMESPACE%% for the specified namespace | ||
fs.readFile(destinationFile, 'utf8', function(err, data) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
|
||
var result = data.replace('%%NAMESPACE%%',namespace); | ||
fs.writeFile(destinationFile, result, 'utf8', function(err) { | ||
if (err) { | ||
return console.log(err); | ||
}; | ||
}); | ||
}); | ||
}) | ||
.then( () => successMessage() ) | ||
.catch( err => { | ||
console.log( chalk.bgRed( 'React WP Scripts loader could not be copied to your root folder. Error details:' ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation is off here. |
||
console.log( chalk.red( err ) ); | ||
} ); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for a
;
here