Skip to content

Commit

Permalink
Initial Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
hngrylobster committed Jan 19, 2019
1 parent d111f05 commit dfcf692
Show file tree
Hide file tree
Showing 11 changed files with 1,896 additions and 63 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
63 changes: 2 additions & 61 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,61 +1,2 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
/node_modules/
npm-debug.log
21 changes: 21 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"white": true
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- '0.8'
- '0.10'
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# shiny
Shiny new things.
#shiny

## Description

Creates shiny objects.

## Usage

To install shiny from npm, run:

```
$ npm install -g shiny
```

```node ./bin/shiny --help```

## License

Copyright (c) 2019 Paul Legan

[MIT License](http://en.wikipedia.org/wiki/MIT_License)

## Acknowledgments

Built using [generator-commader](https://github.com/Hypercubed/generator-commander).
22 changes: 22 additions & 0 deletions bin/shiny
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node
'use strict';

var program = require('commander');

require('autocmdr/lib/logger')(program);
require('autocmdr/lib/loader')(program);
require('autocmdr/lib/completion')(program);
require('autocmdr/lib/package')(program);
require('autocmdr/lib/config')(program);
require('autocmdr/lib/help')(program);



program
.parse(process.argv);

if (program.args.length < 1 ) {
console.log('No command specified. See \'shiny --help\':');
program.outputHelp();
process.exit(1);
}
115 changes: 115 additions & 0 deletions cmds/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* new commander component
* To use add require('../cmds/new.js')(program) to your commander.js based node executable before program.parse
*/
'use strict';

const homedir = require('os').homedir();
const path = require('path');
const fs = require('fs-extra');
const inquirer = require('inquirer');
const fg = require('fast-glob');
const mustache = require('mustache');
const sanitize = require("sanitize-filename");

const util = require('../lib/util.js');

module.exports = function(program) {

program
.command('new <what> [params...]')
.version('0.0.1')
.description('Something old, something new.')
.action(function (what) {

if (what) {

// try to folder in the user's home path with that name
let templateDir = path.join(homedir, "shiny/templates/" + what);
let currentDir = process.cwd();

// if it finds the folder, find a config inside it and load the missing variables as prompts
if (fs.pathExistsSync(templateDir)) {

let configPath = path.join(templateDir, "shiny.json");

if (fs.pathExistsSync(configPath)) {

// load the configuration file
let config = require(configPath);

// load the passed in parameters
let passedInValues = process.argv.slice(4);
let shinyParameters = [];
let questions = [];
let preAnswers = {};

passedInValues.forEach(function(value) {
let option = {};
option.name = value.split("=")[0];
option.value = value.split("=")[1];
shinyParameters.push(option);
});

config.variables.forEach(function(variable) {

// for every possible variable, check if a user has already passed in an answer/value
let found = false;
shinyParameters.forEach(function(param) {
if (variable.name == param.name) {
// we have an answer, lets' add it to the answers array
preAnswers[variable.name] = param.value;
found = true;
}
});

// if we didn't find it, add it to the list of questions to ask the user
if (!found) {
questions.push({
type: variable.type,
name: variable.name,
message: variable.message
})
}
});

// prompt for all the variables in the questions array
inquirer.prompt(questions).then(answers => {

// merge the answers with the parameters to form a final name=value array
let finalAnswers = Object.assign({}, answers, preAnswers);

// for each file in the directory, run through the rules engine to see if this file is OK to copy
let files = fg.sync([templateDir + '/**', '!**/shiny.json']);

console.log(finalAnswers);

files.forEach(function(file){
let relativeFile = path.relative(templateDir, file);
let destFileName = mustache.render(path.join(currentDir, relativeFile), finalAnswers);

if (util.shouldWriteFile(relativeFile, config.rules, finalAnswers)) {
util.writeFile(destFileName, file, finalAnswers);
}
});

// now we can exit
process.exit();
});

} else {
console.error("Oops. This template is misconfigured and needs a shiny.json file.");
}

} else {
console.error("Oops. shiny doesn't see a template for that one yet.");
// do we prompt to create and navigate to that folder?
}

} else {
// if no parameter is passed, throw an error
console.error("Oops. shiny doesn't know what to create. Please pass in an object name.");
}

});

};
32 changes: 32 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const mustache = require('mustache');
const fs = require('fs-extra');

module.exports = {
shouldWriteFile: function(file, rules, answers) {
let pass = true;
rules.forEach(rule => {
if (rule.file == file) {
console.log("Testing rule for: " + file);
// now we need to see how we test the rule (based on the condition object)
if (rule.condition.operation == 'equals') {
pass = (answers[rule.condition.variable] == rule.condition.value);
} else if (rule.condition.operation == 'does not equal') {
pass = (answers[rule.condition.variable] != rule.condition.value);
} else if (rule.condition.operation == 'contains') {
let variable = answers[rule.condition.variable];
pass = variable.includes(rule.condition.value);
}
}
});
return pass;
},
writeFile: function(destFile, sourceFile, data) {
try {
let sourceData = fs.readFileSync(sourceFile, 'utf8');
let destData = mustache.render(sourceData, data);
fs.outputFileSync(destFile, destData);
} catch (err) {
console.error(err)
}
},
}
Loading

0 comments on commit dfcf692

Please sign in to comment.