Skip to content

Instantly share code, notes, and snippets.

@laras126
Created September 2, 2020 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laras126/9500f99d9d67b9e67f80c915a267608e to your computer and use it in GitHub Desktop.
Save laras126/9500f99d9d67b9e67f80c915a267608e to your computer and use it in GitHub Desktop.
module scaffold script
#!/usr/bin/env node
'use strict';
const fs = require( 'fs' );
const chalk = require( 'chalk' );
const path = require( 'path' );
const getConfig = require( '@penskemediacorp/larva' ).getConfig;
const args = process.argv.slice( 2 );
const patternsDir = getConfig( 'patterns' ).projectPatternsDir;
switch ( args[0] ) {
case 'scaffold':
// TODO: the body of this case can be moved into the Larva binary in packages/larva/scripts
let name = args[1];
let fromLarva = args[2] == 'larva' ? true : false;
const snake = string => string.replace( /-/g, '_' );
const name_snake = snake( name );
const twigContents = `{# Update containing element with semantically appropriate element #}
\n<section class="${name} // {{ ${name_snake}_classes }}></section>"`;
let prototypeContents = `const clonedeep = require( 'lodash.clonedeep' );\n`
if ( fs.existsSync( path.join( patternsDir, `modules/${name}/` ) ) ) {
console.error( chalk.red( `Module ${name} already exists. Delete the directory and re-run the scaffold command.` ) );
return;
}
fs.mkdirSync( path.join( patternsDir, `modules/${name}/` ) );
if ( fromLarva ) {
prototypeContents += `const ${name_snake} = clonedeep( require( '@penskemediacorp/larva-patterns/modules/${name}/${name}.prototype' ) );\n`;
prototypeContents += `\n`;
prototypeContents += `module.exports = ${name_snake};`;
} else {
fs.writeFileSync( path.join( patternsDir, `modules/${name}/${name}.twig` ), twigContents );
}
fs.writeFileSync( path.join( patternsDir, `modules/${name}/${name}.prototype.js` ), prototypeContents );
console.log( chalk.green( `Created the module ${name}` ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment