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 all 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 |
---|---|---|
@@ -1,26 +1,28 @@ | ||
{ | ||
"name": "react-wp-scripts", | ||
"description": "A wrapper for react-scripts to allow seamless usage of webpack-dev-server while developing a WordPress theme or plugin.", | ||
"version": "0.1.1", | ||
"contributors": [ | ||
"Ryan McCue", | ||
"K. Adam White", | ||
"Human Made" | ||
], | ||
"license": "GPL-2.0+", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/humanmade/react-wp-scripts.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/humanmade/react-wp-scripts/issues" | ||
}, | ||
"homepage": "https://github.com/humanmade/react-wp-scripts#readme", | ||
"bin": { | ||
"react-wp-scripts": "./bin/react-wp-scripts.js" | ||
}, | ||
"dependencies": { | ||
"react-scripts": "1.0.17", | ||
"signal-exit": "^3.0.2" | ||
} | ||
"name": "react-wp-scripts", | ||
"description": "A wrapper for react-scripts to allow seamless usage of webpack-dev-server while developing a WordPress theme or plugin.", | ||
"version": "0.1.1", | ||
"contributors": [ | ||
"Ryan McCue", | ||
"K. Adam White", | ||
"Human Made" | ||
], | ||
"license": "GPL-2.0+", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/humanmade/react-wp-scripts.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/humanmade/react-wp-scripts/issues" | ||
}, | ||
"homepage": "https://github.com/humanmade/react-wp-scripts#readme", | ||
"bin": { | ||
"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" | ||
} | ||
} |
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,80 @@ | ||
/** | ||
* Sets the start script for react-wp-scripts and moves | ||
* loader.php to the project root folder. | ||
*/ | ||
'use strict'; | ||
|
||
process.on( 'unhandledRejection', err => { | ||
throw err; | ||
} ); | ||
|
||
const fs = require( 'fs-extra' ); | ||
const path = require( 'path' ); | ||
const chalk = require( 'chalk' ); | ||
|
||
const argv = require( 'minimist' )( process.argv.slice( 2 ) ); | ||
|
||
module.exports = function( | ||
appPath, | ||
appName, | ||
verbose, | ||
originalDirectory, | ||
template | ||
) { | ||
|
||
// Parse a namespace based on the name of the package | ||
const namespace = argv['php-namespace'] || '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' ); | ||
|
||
const destinationFile = path.join( appPath, 'react-wp-scripts.php' ); | ||
fs.copy( loaderPath, destinationFile ) | ||
.then( () => new Promise( ( resolve, reject ) => { | ||
// Replace %%NAMESPACE%% for the specified namespace | ||
fs.readFile( destinationFile, 'utf8', function( err, data ) { | ||
if ( err ) { | ||
return reject( err ); | ||
} | ||
|
||
var result = data.replace( '%%NAMESPACE%%', namespace ); | ||
fs.writeFile( destinationFile, result, 'utf8', function( err ) { | ||
if ( 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' ) ); | ||
} ) | ||
.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 ) ); | ||
} ); | ||
}; |
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.
Actually I didn't realize initially but these docs have lagged, too -- the
--php-namespace
argument's been removed entirely in favor of parsing the project name. I kind of liked making that configurable, but at minimum we should explain that the namespace does NOT default to ReactWPScripts unless the packageJSON cannot be parsed, in which case the init is likely to fail anyway.I honestly don't know that I like auto-generating the namespace from the project; @rmccue why did you feel it was preferable to using an opinionated default like ReactWPScripts? If I have a project
H2
, I wouldn't want the namespace for my script-loader file to beH2
.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.
@kadamwhite see discussion on #21. I initially introduced a simple guard, because I see it pretty much like you. The functions are part of the
react-wp-script
package, and not of my app, whatever it is that I am usingreact-wp-scripts
with.@rmccue then mentioned the possibility to allow for multiple (potentially different) version of the functions being used. Not sure if this is to be aimed for at all, but that was where our ideas/approaches diverged.
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.
On the contrary, that's exactly what I want. After init, the loader file is part of my code, not a library being loaded in. It should match that.