-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf76152
commit 8fb30b7
Showing
12 changed files
with
692 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
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,5 @@ | ||
# SAU/CAL Scaffold | ||
|
||
This is meant to be a command to scaffold plugins, themes, projects with the command line. | ||
|
||
_This package is inspired by [@wordpress/scripts](https://www.npmjs.com/package/@wordpress/scripts)._ |
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 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { getNodeArgsFromCLI, spawnScript } = require( '../utils' ); | ||
|
||
const { scriptName, scriptArgs, nodeArgs } = getNodeArgsFromCLI(); | ||
|
||
spawnScript( scriptName, scriptArgs, nodeArgs ); |
This file was deleted.
Oops, something went wrong.
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,99 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const minimist = require( 'minimist' ); | ||
const spawn = require( 'cross-spawn' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { fromScriptsRoot, hasScriptFile, getScripts } = require( './file' ); | ||
const { exit, getArgsFromCLI } = require( './process' ); | ||
|
||
const getArgFromCLI = ( arg ) => { | ||
for ( const cliArg of getArgsFromCLI() ) { | ||
const [ name, value ] = cliArg.split( '=' ); | ||
if ( name === arg ) { | ||
return value || null; | ||
} | ||
} | ||
}; | ||
|
||
const hasArgInCLI = ( arg ) => getArgFromCLI( arg ) !== undefined; | ||
|
||
const getFileArgsFromCLI = () => minimist( getArgsFromCLI() )._; | ||
|
||
const getNodeArgsFromCLI = () => { | ||
const args = getArgsFromCLI(); | ||
const scripts = getScripts(); | ||
const scriptIndex = args.findIndex( ( arg ) => scripts.includes( arg ) ); | ||
return { | ||
nodeArgs: args.slice( 0, scriptIndex ), | ||
scriptName: args[ scriptIndex ], | ||
scriptArgs: args.slice( scriptIndex + 1 ), | ||
}; | ||
}; | ||
|
||
const hasFileArgInCLI = () => getFileArgsFromCLI().length > 0; | ||
|
||
const handleSignal = ( signal ) => { | ||
if ( signal === 'SIGKILL' ) { | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
'The script failed because the process exited too early. ' + | ||
'This probably means the system ran out of memory or someone called ' + | ||
'`kill -9` on the process.' | ||
); | ||
} else if ( signal === 'SIGTERM' ) { | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
'The script failed because the process exited too early. ' + | ||
'Someone might have called `kill` or `killall`, or the system could ' + | ||
'be shutting down.' | ||
); | ||
} | ||
exit( 1 ); | ||
}; | ||
|
||
const spawnScript = ( scriptName, args = [], nodeArgs = [] ) => { | ||
if ( ! scriptName ) { | ||
// eslint-disable-next-line no-console | ||
console.log( 'Script name is missing.' ); | ||
exit( 1 ); | ||
} | ||
|
||
if ( ! hasScriptFile( scriptName ) ) { | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
'Unknown script "' + | ||
scriptName + | ||
'". ' + | ||
'Perhaps you need to update the package?' | ||
); | ||
exit( 1 ); | ||
} | ||
|
||
const { signal, status } = spawn.sync( | ||
'node', | ||
[ ...nodeArgs, fromScriptsRoot( scriptName ), ...args ], | ||
{ | ||
stdio: 'inherit', | ||
} | ||
); | ||
|
||
if ( signal ) { | ||
handleSignal( signal ); | ||
} | ||
|
||
exit( status ); | ||
}; | ||
|
||
module.exports = { | ||
getArgFromCLI, | ||
getArgsFromCLI, | ||
getFileArgsFromCLI, | ||
getNodeArgsFromCLI, | ||
hasArgInCLI, | ||
hasFileArgInCLI, | ||
spawnScript, | ||
}; |
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,147 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { basename } = require( 'path' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { | ||
getArgsFromCLI, | ||
getFileArgsFromCLI, | ||
hasArgInCLI, | ||
hasFileArgInCLI, | ||
} = require( './cli' ); | ||
const { fromConfigRoot, fromProjectRoot, hasProjectFile } = require( './file' ); | ||
const { hasPackageProp } = require( './package' ); | ||
|
||
// See https://babeljs.io/docs/en/config-files#configuration-file-types | ||
const hasBabelConfig = () => | ||
hasProjectFile( '.babelrc.js' ) || | ||
hasProjectFile( '.babelrc.json' ) || | ||
hasProjectFile( 'babel.config.js' ) || | ||
hasProjectFile( 'babel.config.json' ) || | ||
hasProjectFile( '.babelrc' ) || | ||
hasPackageProp( 'babel' ); | ||
|
||
/** | ||
* Returns path to a Jest configuration which should be provided as the explicit | ||
* configuration when there is none available for discovery by Jest in the | ||
* project environment. Returns undefined if Jest should be allowed to discover | ||
* an available configuration. | ||
* | ||
* This can be used in cases where multiple possible configurations are | ||
* supported. Since Jest will only discover `jest.config.js`, or `jest` package | ||
* directive, such custom configurations must be specified explicitly. | ||
* | ||
* @param {"e2e"|"unit"} suffix Suffix of configuration file to accept. | ||
* | ||
* @return {string=} Override or fallback configuration file path. | ||
*/ | ||
function getJestOverrideConfigFile( suffix ) { | ||
if ( hasArgInCLI( '-c' ) || hasArgInCLI( '--config' ) ) { | ||
return; | ||
} | ||
|
||
if ( hasProjectFile( `jest-${ suffix }.config.js` ) ) { | ||
return fromProjectRoot( `jest-${ suffix }.config.js` ); | ||
} | ||
|
||
if ( ! hasJestConfig() ) { | ||
return fromConfigRoot( `jest-${ suffix }.config.js` ); | ||
} | ||
} | ||
|
||
const hasJestConfig = () => | ||
hasProjectFile( 'jest.config.js' ) || | ||
hasProjectFile( 'jest.config.json' ) || | ||
hasPackageProp( 'jest' ); | ||
|
||
// See https://prettier.io/docs/en/configuration.html. | ||
const hasPrettierConfig = () => | ||
hasProjectFile( '.prettierrc.js' ) || | ||
hasProjectFile( '.prettierrc.json' ) || | ||
hasProjectFile( '.prettierrc.toml' ) || | ||
hasProjectFile( '.prettierrc.yaml' ) || | ||
hasProjectFile( '.prettierrc.yml' ) || | ||
hasProjectFile( 'prettier.config.js' ) || | ||
hasProjectFile( '.prettierrc' ) || | ||
hasPackageProp( 'prettier' ); | ||
|
||
const hasWebpackConfig = () => | ||
hasArgInCLI( '--config' ) || | ||
hasProjectFile( 'webpack.config.js' ) || | ||
hasProjectFile( 'webpack.config.babel.js' ); | ||
|
||
// See https://github.com/michael-ciniawsky/postcss-load-config#usage (used by postcss-loader). | ||
const hasPostCSSConfig = () => | ||
hasProjectFile( 'postcss.config.js' ) || | ||
hasProjectFile( '.postcssrc' ) || | ||
hasProjectFile( '.postcssrc.json' ) || | ||
hasProjectFile( '.postcssrc.yaml' ) || | ||
hasProjectFile( '.postcssrc.yml' ) || | ||
hasProjectFile( '.postcssrc.js' ) || | ||
hasPackageProp( 'postcss' ); | ||
|
||
/** | ||
* Converts CLI arguments to the format which webpack understands. | ||
* | ||
* @see https://webpack.js.org/api/cli/#usage-with-config-file | ||
* | ||
* @return {Array} The list of CLI arguments to pass to webpack CLI. | ||
*/ | ||
const getWebpackArgs = () => { | ||
// Gets all args from CLI without those prefixed with `--webpack`. | ||
let webpackArgs = getArgsFromCLI( [ '--webpack' ] ); | ||
|
||
const hasWebpackOutputOption = | ||
hasArgInCLI( '-o' ) || hasArgInCLI( '--output' ); | ||
if ( hasFileArgInCLI() && ! hasWebpackOutputOption ) { | ||
/** | ||
* Converts a path to the entry format supported by webpack, e.g.: | ||
* `./entry-one.js` -> `entry-one=./entry-one.js` | ||
* `entry-two.js` -> `entry-two=./entry-two.js` | ||
* | ||
* @param {string} path The path provided. | ||
* | ||
* @return {string} The entry format supported by webpack. | ||
*/ | ||
const pathToEntry = ( path ) => { | ||
const entry = basename( path, '.js' ); | ||
|
||
if ( ! path.startsWith( './' ) ) { | ||
path = './' + path; | ||
} | ||
|
||
return [ entry, path ].join( '=' ); | ||
}; | ||
|
||
// The following handles the support for multiple entry points in webpack, e.g.: | ||
// `wp-scripts build one.js custom=./two.js` -> `webpack one=./one.js custom=./two.js` | ||
webpackArgs = webpackArgs.map( ( cliArg ) => { | ||
if ( | ||
getFileArgsFromCLI().includes( cliArg ) && | ||
! cliArg.includes( '=' ) | ||
) { | ||
return pathToEntry( cliArg ); | ||
} | ||
|
||
return cliArg; | ||
} ); | ||
} | ||
|
||
if ( ! hasWebpackConfig() ) { | ||
webpackArgs.push( '--config', fromConfigRoot( 'webpack.config.js' ) ); | ||
} | ||
|
||
return webpackArgs; | ||
}; | ||
|
||
module.exports = { | ||
getWebpackArgs, | ||
hasBabelConfig, | ||
getJestOverrideConfigFile, | ||
hasJestConfig, | ||
hasPrettierConfig, | ||
hasPostCSSConfig, | ||
}; |
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,39 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { existsSync, readdirSync } = require( 'fs' ); | ||
const path = require( 'path' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { getPackagePath } = require( './package' ); | ||
|
||
const fromProjectRoot = ( fileName ) => | ||
path.join( path.dirname( getPackagePath() ), fileName ); | ||
|
||
const hasProjectFile = ( fileName ) => | ||
existsSync( fromProjectRoot( fileName ) ); | ||
|
||
const fromConfigRoot = ( fileName ) => | ||
path.join( path.dirname( __dirname ), 'config', fileName ); | ||
|
||
const fromScriptsRoot = ( scriptName ) => | ||
path.join( path.dirname( __dirname ), 'scripts', `${ scriptName }.js` ); | ||
|
||
const hasScriptFile = ( scriptName ) => | ||
existsSync( fromScriptsRoot( scriptName ) ); | ||
|
||
const getScripts = () => | ||
readdirSync( path.join( path.dirname( __dirname ), 'scripts' ) ) | ||
.filter( ( f ) => path.extname( f ) === '.js' ) | ||
.map( ( f ) => path.basename( f, '.js' ) ); | ||
|
||
module.exports = { | ||
fromProjectRoot, | ||
fromConfigRoot, | ||
fromScriptsRoot, | ||
getScripts, | ||
hasProjectFile, | ||
hasScriptFile, | ||
}; |
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,43 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
const { | ||
getArgFromCLI, | ||
getArgsFromCLI, | ||
getFileArgsFromCLI, | ||
getNodeArgsFromCLI, | ||
hasArgInCLI, | ||
hasFileArgInCLI, | ||
spawnScript, | ||
} = require( './cli' ); | ||
const { | ||
getWebpackArgs, | ||
hasBabelConfig, | ||
getJestOverrideConfigFile, | ||
hasJestConfig, | ||
hasPrettierConfig, | ||
hasPostCSSConfig, | ||
} = require( './config' ); | ||
const { fromProjectRoot, fromConfigRoot, hasProjectFile } = require( './file' ); | ||
const { getPackageProp, hasPackageProp } = require( './package' ); | ||
|
||
module.exports = { | ||
fromProjectRoot, | ||
fromConfigRoot, | ||
getArgFromCLI, | ||
getArgsFromCLI, | ||
getFileArgsFromCLI, | ||
getJestOverrideConfigFile, | ||
getNodeArgsFromCLI, | ||
getPackageProp, | ||
getWebpackArgs, | ||
hasArgInCLI, | ||
hasBabelConfig, | ||
hasFileArgInCLI, | ||
hasJestConfig, | ||
hasPackageProp, | ||
hasPostCSSConfig, | ||
hasPrettierConfig, | ||
hasProjectFile, | ||
spawnScript, | ||
}; |
Oops, something went wrong.