Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
working on #3, added checkboxes
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Wilkowski <Hi@Dominik-Wilkowski.com>
  • Loading branch information
dominikwilkowski committed Jan 30, 2017
1 parent a96d697 commit 8b4e5a0
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ encounters a conflict.

> Can’t eat no pancake without Syrup.
**Cream** will return a radio list of all modules that can be selected and installed automatically.
**Cream** will return a checkbox list of all modules that can be selected and installed automatically.

> And if you don’t want to deal with any of it: Use the cream on top straight up.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"cfonts": "^1.0.2",
"chalk": "^1.1.3",
"commander": "^2.9.0",
"inquirer": "^3.0.1",
"node-sass": "^4.3.0",
"play-sound": "^1.1.1",
"postcss": "^5.2.11",
Expand Down
4 changes: 2 additions & 2 deletions pancake-batter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const Fs = require(`fs`);
let pkgPath = Path.normalize(`${ process.cwd() }/`); //default value of the pkgPath path

Program
.arguments('<pkgPath>')
.usage( `[command] <input> <option>` )
.arguments('<pkgPath>')
.action( pkgPathArgument => {
pkgPath = pkgPathArgument; //overwriting default value with user input
})
Expand All @@ -52,7 +52,7 @@ const Log = pancakes.Log;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// PREPARE, Check for dependencies conflicts
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
Log.info(`PANCAKE CHECKING DEPENDENCIES`);
Log.info(`PANCAKE BAKING THE BATTER`);

const dependencies = new Map(); //a map we populate with the dependencies of our modules we found
const modules = new Map(); //a map for all installed modules and their versions
Expand Down
165 changes: 162 additions & 3 deletions pancake-cream.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@
// Dependencies
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const Program = require('commander');
const Inquirer = require('inquirer');
const Request = require('request');
const Semver = require('semver');
const Chalk = require('chalk');
const Path = require(`path`);


//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// CLI program
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
let pkgPath = Path.normalize(`${ process.cwd() }/`); //default value of the pkgPath path

Program
.description('cream')
.usage( `[command] <input> <option>` )
.arguments('<pkgPath>')
.action( pkgPathArgument => {
pkgPath = pkgPathArgument; //overwriting default value with user input
})
.option( `-v, --verbose`, `Run the program in verbose mode` )
.parse( process.argv );

Expand All @@ -33,12 +44,160 @@ Program
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const pancakes = require(`./pancake-utilities.js`)( Program.verbose );
const Log = pancakes.Log;
const UIKITurl = `https://raw.githubusercontent.com/govau/uikit/master/uikit.json`;


//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Reusable functions
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
* Get remote json file and return it's data
*
* @param {string} url - The URL of the remote json file
*
* @return {object} - The parsed object of the json content
*/
const GetRemoteJson = url => {
return new Promise( ( resolve, reject ) => {
Request.get(
{
url: url,
json: true,
headers: {
'User-Agent': 'pancake',
},
}, ( error, result, data ) => {
if( error ) {
Log.error( error );

reject( error );
}
else if( result.statusCode !== 200 ) {
Log.error(`Status code of request to ${ Chalk.yellow( url ) } returned: ${ Chalk.yellow( result.statusCode ) } `);

reject( result.statusCode );
}
else {
resolve( data );
}
}
);
});
};


//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Cream
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
Log.info(`PANCAKE PUTTING THE CREAM ON TOP`);

let allPromises = []; //collect both promises

//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Get uikit.json
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
Log.verbose(`Getting uikit.json from: ${ Chalk.yellow( UIKITurl ) }`);

const gettingUikit = GetRemoteJson( UIKITurl ); //get the uikig json
let UIKIT = {}; //for uikit data in a larger scope

gettingUikit.then( data => {
UIKIT = data; //adding the data of uikit.json into our scoped variable
});

allPromises.push( gettingUikit ); //keep track of all promises


//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Get uikit modules
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
let allModules = {}; //for all modules in a larger scope

const allPackages = pancakes.GetPackages( pkgPath ); //read all packages and return an object per module

allPackages.then( data => {
allModules = data; //adding all uikit modules into our scoped variable
});

allPromises.push( allPackages ); //keep track of all promises


//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// TODO
// Start compiling what we have vs what we could have
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
Log.verbose(`cream!!`);
Promise.all( allPromises )
.catch( error => {
Log.error(`An error occurred getting the basics: ${ error }`);
})
.then( () => {
let choices = []; //to be filled with all choices we have
let installed = new Map(); //to be filled with installed modules

//convert installed modules array into map for better querying
for( const modulePackage of allModules ) {
installed.set( modulePackage.name, modulePackage.version );
}

Log.verbose(
`Got all data from uikit.json and installed modules:\n` +
`Installed: ${ Chalk.yellow( JSON.stringify( [ ... installed ] ) ) }\n` +
`UIKIT: ${ Chalk.yellow( JSON.stringify( UIKIT ) ) }`
);

//iterate over all uikit modules
for( const module of Object.keys( UIKIT ) ) {
const thisChoice = {}; //let's build this choice out
const installedVersion = installed.get( module ); //the installed version of this module

thisChoice.name = `${ module } v${ UIKIT[ module ] }`; //we add each module of the uikit in here
thisChoice.value = {
[ module ]: UIKIT[ module ], //let's make sure we can parse the answer
};

if( installedVersion.length ) { //in case we have this module already installed, let's check if you can upgrade
if(
Semver.gte( UIKIT[ module ], installedVersion ) && //if this version is newer than the installed one
!Semver.eq( UIKIT[ module ], installedVersion ) //and not equal
) {
thisChoice.name = `${ thisChoice.name } - ${ Chalk.green('*NEWER VERSION AVAILABLE*') }`; //this is actually an upgrade

choices.push({ //this is the module version you got installed
name: `${ module } v${ installedVersion }`,
value: {
[ module ]: installedVersion,
},
checked: true,
});
}

if( Semver.eq( UIKIT[ module ], installedVersion ) ) { //if this version is the same as what's installed
thisChoice.checked = true; //you got the latest
}
}

choices.push( thisChoice ); //adding to all checkboxes
}

Log.space(); //prettiness

Inquirer.prompt([
{
type: 'checkbox',
message: 'Select your UI-Kit modules',
name: 'modules',
choices: choices,
validate: ( answer ) => {
if( answer.length < 1 ) {
return 'You must choose at least one module to install.';
}

return true;
}
}
]).then(( modules ) => {
console.log(JSON.stringify(modules, null, ' ')); //let's work from here :)
});
});


//--------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pancake-syrup.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const Fs = require(`fs`);
let pkgPath = Path.normalize(`${ process.cwd() }/`); //default value of the pkgPath path

Program
.description('syrup')
.usage( `[command] <input> <option>` )
.arguments('<pkgPath>')
.action( pkgPathArgument => {
pkgPath = pkgPathArgument; //overwriting default value with user input
})
Expand Down Expand Up @@ -315,7 +315,7 @@ const MinifyAllJS = ( allJS, settings ) => {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Reading and merging settings
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
Log.info(`PANCAKE COMPILING MODULES`);
Log.info(`PANCAKE ADDING SYRUP`);

//reading local settings
const PackagePath = Path.normalize(`${ pkgPath }/package.json`);
Expand Down

0 comments on commit 8b4e5a0

Please sign in to comment.