Skip to content

Commit

Permalink
CLI component generator first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
allmarkedup committed Jan 11, 2016
1 parent 8653a43 commit 2c2b772
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 32 deletions.
4 changes: 3 additions & 1 deletion bin/fractal.js → bin/fractal
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#! /usr/bin/env node

'use strict';

/*
Expand Down Expand Up @@ -164,7 +166,7 @@ checkCommandCount(yargs, argv, 1);
function checkCommandCount(yargs, argv, numRequired) {
if (argv._.length < numRequired) {
yargs.showHelp();
process.exit(1);
process.exit(0);
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"generator": {
"config": {
"ext": ".js"
"name": "{{name}}.config.js"
}
},
"components": {
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@
"yargs": "^3.31.0"
},
"bin": {
"fractal": "./bin/fractal.js"
"fractal": "./bin/fractal"
},
"engines": {
"node": ">= 4.0.0"
},
"scripts": {
"start": "node ./bin/fractal.js start",
"build": "node ./bin/fractal.js build",
"init": "node ./bin/fractal.js init"
"start": "node ./bin/fractal start",
"build": "node ./bin/fractal build",
"init": "node ./bin/fractal init",
"create": "node ./bin/fractal create"
}
}
2 changes: 1 addition & 1 deletion src/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ app.run = function(argv){
this.runGeneratorService(argv);
break;
case 'init':
console.log(chalk.magenta('Fractal `init` command is not yet implemented.'));
console.log(chalk.magenta('The `init` command is not yet implemented.'));
process.exit(0);
break;
default:
Expand Down
25 changes: 25 additions & 0 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ module.exports = {
return _.defaultsDeep(data, defaults);
});

},

write: function(filePath, data) {
var pathInfo = path.parse(path.resolve(filePath));
var ext = pathInfo.ext.toLowerCase();
var content = null;

switch(ext) {
case ".js":
content = 'module.exports = ' + JSON.stringify(data, null, 4) + ';';
break;
case ".json":
content = JSON.stringify(data, null, 4);
break;
break;
case ".yml":
case ".yaml":
content = yaml.safeDump(data);
break;
default:
throw new Error('Unknown data file extension: ' + ext);
return;
}

return fs.writeFileAsync(filePath, content);
}

};
13 changes: 13 additions & 0 deletions src/errors/exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = ExistsError;

function ExistsError(message, previous) {
var error = Error.call(this, message);

this.name = 'ExistsError';
this.message = error.message;
this.stack = previous ? previous.stack : error.stack;
this.previous = previous;
}

ExistsError.prototype = Object.create(Error.prototype);
ExistsError.prototype.constructor = ExistsError;
45 changes: 20 additions & 25 deletions src/services/generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,33 @@
* Module dependencies.
*/

var logger = require('winston');
var Promise = require('bluebird');
var chalk = require('chalk');
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var mkdirp = Promise.promisify(require('mkdirp'));
var ncp = Promise.promisify(require('ncp'));
var rimraf = Promise.promisify(require('rimraf'));
var logger = require('winston');

/**
* Export the generator function
*/

module.exports = function(type, relPath, opts, app){
var opts = opts || {};
switch (type) {
case 'component':
var rootPath = app.get('components:path');
break;
case 'page':
var rootPath = app.get('pages:path');
break;
default:
logger.error("Entity type '%s' is not recognised. Try 'page' or 'component' instead.", type);
process.exit(1);
return;
}
module.exports = function(argv, app){

var entityPath = path.join(rootPath, relPath);
var type = argv._[1];
var relPath = argv._[2].trim('/');

try {
var Handler = require('./handlers/' + type);
} catch(e) {
logger.debug(e.message);
logger.error("Entity type '%s' is not recognised. Try 'page' or 'component' instead.", type);
process.exit(1);
return;
}

console.log(entityPath);
var generator = new Handler(app);
generator.generate(relPath, argv).catch(function(e){
logger.error(e.message);
process.exit(1);
}).finally(function(){
process.exit(0);
});

process.exit(0);
};
100 changes: 100 additions & 0 deletions src/services/generator/handlers/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Module dependencies.
*/

var logger = require('winston');
var Promise = require('bluebird');
var path = require('path');
var _ = require('lodash');
var fs = Promise.promisifyAll(require('fs'));
var mkdirp = Promise.promisify(require('mkdirp'));
var ncp = Promise.promisify(require('ncp'));
var rimraf = Promise.promisify(require('rimraf'));

var ExistsError = require('../../../errors/exists');
var utils = require('../../../utils');


module.exports = ComponentGenerator;

/*
* ComponentGenerator constructor.
*
* @api private
*/

function ComponentGenerator(app){
this.app = app;
};


/*
* Run the generator.
*
* @api public
*/

ComponentGenerator.prototype.generate = function(relPath, opts){
var self = this;
var fullPath = path.join(self.app.get('components:path'), relPath);
return this.app.getComponents().then(function(components){
if (components.exists(relPath)) {
throw new ExistsError('The component at ' + relPath +' already exists.');
}
return mkdirp(fullPath).then(function(){
return components
});
}).then(function(components){
return components.create(relPath)
// var pathParts = path.parse(fullPath);
// var title = utils.titlize(pathParts.name);
//
// var config = {
// handle: pathParts.name,
// label: utils.titlize(pathParts.name)
// context: {}
// };
//
// var templatePath = path.join(fullPath, pathParts.name + self.app.getComponentViewEngine().ext);
// var configPath = path.join(fullPath, self.app.get('generator:config:name').replace('{{name}}', pathParts.name));
//
// var writes = [
// fs.writeFileAsync(templatePath, '<p>' + + ' component</p>'),
// fs.writeFileAsync(configPath, JSON.stringify(config, null, 4))
// ];
//
// return Promise.all(writes);
});
};

// /**
// * Export the generator function
// */
//
// module.exports = function(argv, app){
//
// var type = argv._[1];
// var relPath = argv._[2];
//
// switch (type) {
// case 'component':
// var rootPath = app.get('components:path');
// break;
// case 'page':
// var rootPath = app.get('pages:path');
// break;
// default:
// logger.error("Entity type '%s' is not recognised. Try 'page' or 'component' instead.", type);
// process.exit(1);
// return;
// }
//
// var components = app.getComponents();
// .findByPath(relPath.trim('/'));
//
// // var entityPath = path.join(rootPath, relPath);
//
// console.log(entityPath);
//
// process.exit(0);
// };
47 changes: 47 additions & 0 deletions src/sources/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
var Promise = require('bluebird');
var _ = require('lodash');
var logger = require('winston');
var path = require('path');
var fs = Promise.promisifyAll(require('fs'));
var mkdirp = Promise.promisify(require('mkdirp'));

var Directory = require('../filesystem/directory');
var Component = require('./entities/component');
var Group = require('./entities/group');
var mixin = require('./source');
var data = require('../data');
var NotFoundError = require('../errors/notfound')
var utils = require('../utils');

/*
* Export the component source.
Expand Down Expand Up @@ -196,6 +200,49 @@ ComponentSource.prototype.filter = function(key, value){
return new ComponentSource(filter(this.components), this.app).init();
};

/*
* Checks if a component exists
*
* @api public
*/

ComponentSource.prototype.exists = function(str){
try {
var component = this.resolve(str);
return component;
} catch(e){
return false;
}
};

/*
* Creates a new component
*
* @api public
*/

ComponentSource.prototype.create = function(relPath, opts){

var fullPath = path.join(this.app.get('components:path'), relPath);
var pathParts = path.parse(fullPath);
var title = utils.titlize(pathParts.name);

var config = {
handle: pathParts.name,
label: title
};

var templatePath = pathParts.name + this.app.getComponentViewEngine().ext;
var configPath = this.app.get('generator:config:name').replace('{{name}}', pathParts.name);

var writes = [
fs.writeFileAsync(path.join(fullPath, templatePath), '<p>' + title + ' component</p>'),
data.write(path.join(fullPath, configPath), config)
];

return Promise.all(writes);
};

/*
* Returns a JSON representation of all the components
*
Expand Down

0 comments on commit 2c2b772

Please sign in to comment.