Skip to content

Commit

Permalink
Initial attempt to convert to auto-pods (ember-cli style)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Radchenko committed Jun 30, 2014
1 parent 50101af commit 4a6820d
Show file tree
Hide file tree
Showing 29 changed files with 93 additions and 97 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,30 @@ Emberate [![Build Status][travis-img]][travis-url] [![Code Climate][coverage-img
This generator set is used to create an CJS `require` hierarchy for an EmberJS project structure.
The main use-case, is for use with Browserify.

**Now supporting [PODS][pods] project structure.**

For example, given the following structure:

```no-highlight
app
|_controllers/
application.js
router.js
controllers/
|_user.js
|_user/
|_new.js
|_views/
views/
|_user.js
|_routes/
routes/
|_user.js
|_user/
|_new.js
|_...
pods/
|_post/
|_route.js
|_index/
|_template.hbs
|_controller.js
|_edit/
|_template.hbs
|_route.js
```

This generator set can be used to generate a file, along the lines of `.index.js`, with the following contents:
Expand Down
16 changes: 9 additions & 7 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
var cli = require('ltcdr');
var pkg = require('../package');
var app = require('./index');
var emberate = require('./index');
var fs = require('fs');

cli.version(pkg.version)
.option('-o, --output-path [path]', 'Output path of generated file', './client/.index.js')
.option('-p, --pods', 'Enable PODS support')
.option('-i, --input-directory [dir]', 'Directory to start crawling file tree', './client')
.option('-i, --app-directory [dir]', 'Directory to start crawling file tree', './client')
.option('-n, --app-name [app-name]', 'App Name, where your app resides globally')
.parse(process.argv);

app(cli.inputDirectory, {
emberate({
appDirectory: cli.appDirectory.
appName: cli.appName,
pods: cli.pods,
outPath: cli.outputPath
}, function () {
}).then(function () {
process.exit();
})
}, function (err) {
console.error(err);
process.exit(1);
});

module.exports = cli;
15 changes: 6 additions & 9 deletions lib/defaultTemplate.hbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// this file is auto-generated, do not edit
require('ember'); // get Ember global around for the templates
require('./.templates');
var Ember = require('ember'); // get Ember global around for the templates

var {{appName}} = require('./config/application');
{{appName}}.Router.map(require('./config/routes'));
var {{appName}} = require('./app');
{{appName}}.Router = require('./router');

{{#each helpers}}
require('{{path}}');{{/each}}

{{#each modules}}
{{../appName}}.{{name}} = require('{{path}}');{{/each}}
{{#each modules}}{{#if isTemplate}}
Ember.TEMPLATES['{{name}}'] = require('{{path}}');{{else}}
{{../../appName}}.{{name}} = require('{{path}}');{{/if}}{{/each}}

module.exports = {{appName}};
10 changes: 2 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ module.exports = function (rootPath, appName, templatePath) {
var defaultPath = path.join(__dirname, './defaultTemplate.hbs');
var defaultOptions = {
appName: 'App',
templatePath: defaultPath,
pods: false
templatePath: defaultPath
};
var options;
var callback;
Expand All @@ -34,17 +33,12 @@ module.exports = function (rootPath, appName, templatePath) {
}
}

// Use pods template if pods: true
if (options.pods) {
options.templatePath = path.join(__dirname, './podsTemplate.hbs');
}

rootPath = rootPath || process.cwd();

var out = spider(rootPath)
.pipe(baseClean(rootPath))
.pipe(inflector(options))
.pipe(orderStream(options.pods))
.pipe(orderStream())
.pipe(templateStream(options));

if (callback) {
Expand Down
2 changes: 1 addition & 1 deletion lib/inflectStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var path = require('path');

module.exports = function (options) {
return through.obj(function(chunk, _, next) {
var inf = inflector(chunk, options.pods);
var inf = inflector(chunk);

if (!inf) {
return next();
Expand Down
18 changes: 7 additions & 11 deletions lib/inflector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ module.exports = function (str, pods) {
return;
}

if (pods) {
return byPod(parts);
}
else {
return byType(parts)
}
return byPod(parts);
};

function byType(parts) {
var cat = fleck.singularize(parts.shift());
var name =
var name;

parts.push(cat);
name = buildName(cat, parts);
Expand All @@ -35,10 +30,11 @@ function byPod(parts) {
var cat, section, name;

if (parts.length > 1) {
cat = parts.slice(-1)[0];
section = parts.shift();

if (section === 'app') {
if (parts[0] === 'pods') {
cat = parts.slice(-1)[0];
section = parts.shift();
}
else {
return byType(parts);
}
}
Expand Down
33 changes: 27 additions & 6 deletions lib/orderStream.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var through = require('through2');

module.exports = function (pods) {
module.exports = function () {
var out = {};
var order = [
'template',
'initializer',
'helper',
'mixin',
Expand All @@ -16,10 +17,6 @@ module.exports = function (pods) {
'view'
];

if (pods) {
order.unshift('template');
}

return through.obj(function (chunk, _, next) {
if (!(chunk.cat in out)) {
out[chunk.cat] = [];
Expand All @@ -28,11 +25,35 @@ module.exports = function (pods) {
next();
}, function (next) {
this.push(true);

order.forEach(function (item) {
var prioritized;

if (item in out && Array.isArray(out[item])) {
this.push(out[item]);
prioritized = prioritize(out[item]);
this.push(prioritized);
}
}, this);
next();
});
};

function prioritize(category) {
var podKeys = [];
var notPods = [];

var pods = category.filter(function (item) {
if (item && item.path.indexOf('pods') > -1) {
podKeys.push(item.name);
return true;
}
else {
notPods.push(item);
return false;
}
});

return notPods.filter(function (item) {
return podKeys.indexOf(item.name) > -1 ? false : true;
}).concat(pods);
}
14 changes: 0 additions & 14 deletions lib/podsTemplate.hbs

This file was deleted.

1 change: 1 addition & 0 deletions lib/templateStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = function (options) {
}
return 1;
});

if (chunk[0].cat === 'helper') {
things.helpers = chunk;
} else {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"scripts": {
"test": "node ./test/index.js | tnyan",
"test-debug": "node debug ./test/index.js",
"tspec": "node ./test/index.js | tspec",
"coverage": "istanbul cover test/index.js"
},
Expand Down
14 changes: 7 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var esg = require('../lib');
var emberate = require('../lib');
var fs = require('fs');
var path = require('path');
var test = require('tape');
Expand All @@ -10,7 +10,7 @@ var inflector = require('../lib/inflector');
test('creates expected output', function (t) {
var generatedFile = path.join(__dirname, 'structures', 'all', '.index.js');
var expectedFile = path.join(__dirname, 'structures', 'all-structures.js');
var instance = esg(allStructure).pipe(fs.createWriteStream(generatedFile));
var instance = emberate(allStructure).pipe(fs.createWriteStream(generatedFile));

instance.on('finish', function () {
fs.readFile(generatedFile, function (err, data) {
Expand All @@ -26,7 +26,7 @@ test('creates expected output', function (t) {
test('creates expected output with callback', function (t) {
var generatedFile = path.join(__dirname, 'structures', 'all', '.index.js');
var expectedFile = path.join(__dirname, 'structures', 'all-structures.js');
var instance = esg(allStructure, {
var instance = emberate(allStructure, {
outPath: generatedFile
}, function () {
fs.readFile(generatedFile, function (err, data) {
Expand All @@ -42,7 +42,7 @@ test('creates expected output with callback', function (t) {
test('works with required dirs/files only', function (t) {
var expectedMinFile = path.join(__dirname, 'structures', 'min-structures.js');
var generatedMinFile = path.join(__dirname, 'structures', 'min', '.index.js');
var instance = esg(minStructure).pipe(fs.createWriteStream(generatedMinFile));
var instance = emberate(minStructure).pipe(fs.createWriteStream(generatedMinFile));

instance.on('finish', function () {
fs.readFile(generatedMinFile, function (err, data) {
Expand All @@ -58,7 +58,7 @@ test('works with required dirs/files only', function (t) {
test('pods: all available items', function (t) {
var expectedPodsAllFile = path.join(__dirname, 'structures', 'pods-all-structures.js');
var generatedPodsAllFile = path.join(__dirname, 'structures', 'pods-all', '.index.js');
var instance = esg(podsAllStructure, { pods: true }).pipe(fs.createWriteStream(generatedPodsAllFile));
var instance = emberate(podsAllStructure).pipe(fs.createWriteStream(generatedPodsAllFile));

instance.on('finish', function () {
fs.readFile(generatedPodsAllFile, function (err, data) {
Expand All @@ -83,7 +83,7 @@ test('inflector: pods', function (t) {
t.deepEqual(inflector('pods/application/controller.js', true), { cat: 'controller', name: 'ApplicationController' }, 'inflects one level deep');
t.deepEqual(inflector('pods/user/index/route.js', true), { cat: 'route', name: 'UserIndexRoute' }, 'inflects two levels deep');
t.deepEqual(inflector('pods/user/index/template.hbs', true), { cat: 'template', name: 'user/index' }, 'inflects two levels deep - template');
t.deepEqual(inflector('app/templates/sidebar/header.hbs', true), { cat: 'template', name: 'sidebar/header' }, 'inflects two levels deep - template, by type');
t.deepEqual(inflector('app/mixins/ajax.js', true), { cat: 'mixin', name: 'AjaxMixin' }, 'inflects app level by type');
t.deepEqual(inflector('templates/sidebar/header.hbs', true), { cat: 'template', name: 'sidebar/header' }, 'inflects two levels deep - template, by type');
t.deepEqual(inflector('mixins/ajax.js', true), { cat: 'mixin', name: 'AjaxMixin' }, 'inflects app level by type');
t.end();
});
11 changes: 4 additions & 7 deletions test/structures/all-structures.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// this file is auto-generated, do not edit
require('ember'); // get Ember global around for the templates
require('./.templates');
var Ember = require('ember'); // get Ember global around for the templates

var App = require('./config/application');
App.Router.map(require('./config/routes'));


require('./helpers/ajax');
var App = require('./app');
App.Router = require('./router');


Ember.TEMPLATES['application'] = require('./pods/application/template.hbs');
App.TestInitializer = require('./initializers/test');
App.TestMixin = require('./mixins/test');
App.ObjectTransform = require('./transforms/object');
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 3 additions & 6 deletions test/structures/min-structures.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// this file is auto-generated, do not edit
require('ember'); // get Ember global around for the templates
require('./.templates');

var App = require('./config/application');
App.Router.map(require('./config/routes'));

var Ember = require('ember'); // get Ember global around for the templates

var App = require('./app');
App.Router = require('./router');



Expand Down
25 changes: 11 additions & 14 deletions test/structures/pods-all-structures.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
// this file is auto-generated, do not edit
require('ember'); // get Ember global around for the templates
var Ember = require('ember'); // get Ember global around for the templates

var App = require('./app/config/application');
App.Router.map(require('./app/config/routes'));


require('./app/helpers/ajax');
var App = require('./app');
App.Router = require('./router');


Ember.TEMPLATES['application'] = require('./pods/application/template.hbs');
Ember.TEMPLATES['sidebar'] = require('./app/templates/sidebar.hbs');
Ember.TEMPLATES['sidebar'] = require('./templates/sidebar.hbs');
Ember.TEMPLATES['user/index'] = require('./pods/user/index/template.hbs');
App.TestInitializer = require('./app/initializers/test');
App.TestMixin = require('./app/mixins/test');
App.ObjectTransform = require('./app/transforms/object');
App.UserSerializer = require('./app/serializers/user');
App.UserAdapter = require('./app/adapters/user');
App.UserModel = require('./app/models/user');
App.XPlayerComponent = require('./components/x-player/component');
App.TestInitializer = require('./initializers/test');
App.TestMixin = require('./mixins/test');
App.ObjectTransform = require('./transforms/object');
App.UserSerializer = require('./serializers/user');
App.UserAdapter = require('./adapters/user');
App.UserModel = require('./models/user');
App.XPlayerComponent = require('./components/x-player');
App.ApplicationController = require('./pods/application/controller');
App.UserIndexController = require('./pods/user/index/controller');
App.ApplicationRoute = require('./pods/application/route');
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Empty file.

0 comments on commit 4a6820d

Please sign in to comment.