Skip to content

Commit

Permalink
Merge pull request #96 from fivetanley/remove-is-new-serializer-api
Browse files Browse the repository at this point in the history
add remove-ember-data-is-new-serializer-api command
  • Loading branch information
abuiles committed Apr 21, 2016
2 parents eb1aa83 + fdabd66 commit e61e5d7
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A young Ember Doctor.
* [find-overloaded-cps](#ember-watsonfind-overloaded-cps-path)
* [use-destroy-app-helper](#ember-watsonuse-destroy-app-helper-path)
* [replace-needs-with-injection](#ember-watsonreplace-needs-with-injection-path)
* [remove-ember-data-is-new-serializer-api](#ember-watson-remove-ember-data-is-new-serializer-api)

## Using as an ember CLI addon

Expand Down Expand Up @@ -113,6 +114,26 @@ Ember CLI 1.13.9.

Convert `needs` declarations the individual properties using the new `Ember.inject.controller()` feature. Also convert any uses of the `controllers` hash to use the newly defined properties.

### `ember watson:remove-ember-data-is-new-serializer-api`

Removes `isNewSerializerAPI` literals in your serializer code.
You should use this *after* you make sure all your serializers are
compatible with the new serializer API in 1.13. You can find more
information on how to upgrade serializers
[here](http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_transition-to-the-new-jsonserializer-and-restserializer-apis).

```javascript

// before
export default DS.RESTSerializer.extend({
isNewSerializerAPI: true
});

// after

export default DS.RESTSerializer.extend({});
```

### Specifying a file or path.

You can run any of the commands passing as argument the path, file or
Expand Down
10 changes: 10 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,22 @@ program
watson.replaceNeedsWithInjection(path);
});

program
.command('remove-ember-data-is-new-serializer-api [path]')
.description('Remove `isNewSerializerAPI` in Ember Data serializers.')
.action(function(path) {
path = path || 'app/serializers';
watson.removeEmberDataIsNewSerializerAPI(path);
});

program
.command('all')
.option('--tests-path [path]', 'Path to tests directory.', 'tests')
.option('--app-path [path]', 'Path to app directory.', 'app')
.option('--router-path [path]', 'Path to router file.', 'app/router.js')
.option('--acceptance-path [path]', 'Path to acceptance tests directory.', 'tests/acceptance')
.option('--controllers-path [path]', 'Path to controllers directory.', 'app/controllers')
.option('--serializers-path [path]', 'Path to serializers directory.', 'app/serializers')
.option('--output [format]', 'Output format: pretty or json.', 'pretty')
.description('Run all commands.')
.action(function(options) {
Expand All @@ -100,6 +109,7 @@ program
watson.findOverloadedCPs(options.appPath).outputSummary(options.output);
watson.transformTestToUseDestroyApp(options.acceptancePath);
watson.replaceNeedsWithInjection(options.controllersPath);
watson.removeEmberDataIsNewSerializerAPI(options.serializersPath);
});

module.exports = function init(args) {
Expand Down
15 changes: 15 additions & 0 deletions lib/commands/remove-ember-data-is-new-serializer-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

var Watson = require('../../index');

module.exports = {
name: 'watson:remove-ember-data-is-new-serializer-api',
description: 'Removes `isNewSerializerAPI` from serialiers',
works: ['insideProject', 'insideAddon'],
anonymousOptions: [
'<path>'
],
run: function(commandOptions, rawArgs) {
var path = rawArgs[0] || 'app';
}
};
35 changes: 35 additions & 0 deletions lib/formulas/remove-ember-data-is-new-serializer-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var parseAST = require('../helpers/parse-ast');
var recast = require('recast');

module.exports = function(source) {
var ast = parseAST(source);

recast.visit(ast, {

visitCallExpression: function(path) {
var callee = path.node.callee;
if (callee.type === 'MemberExpression' &&
callee.property &&
callee.property.name === 'extend' &&
callee.object &&
callee.object.property &&
/serializer/i.test(callee.object.property.name)) {
this.traverse(path);
return;
}

return false;
},

visitProperty: function(path) {
if (path.node.key.name === 'isNewSerializerAPI') {
path.prune();
}
return false;
}
});

return recast.print(ast, { tabWidth: 2, quote: 'single' }).code;
};
3 changes: 2 additions & 1 deletion lib/helpers/find-files.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var path = require('path');
var walkSync = require('walk-sync');
var fs = require('fs');

module.exports = function(rootPath, ext) {
var files = [];

if (path.extname(rootPath).length > 0) {
files.push(rootPath);
} else {
} else if (fs.existsSync(rootPath)) {
files = walkSync(rootPath).filter(function(file) {
return path.extname(file) === ext;
}).map(function(file) {
Expand Down
7 changes: 7 additions & 0 deletions lib/watson.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ var transformMethodify = require('./formulas/methodify');
var FindOverloadedCPs = require('./formulas/find-overloaded-cps');
var transformDestroyApp = require('./formulas/destroy-app-transform');
var replaceNeedsWithInjection = require('./formulas/replace-needs-with-injection');
var removeEmberDataIsNewSerializerAPI = require('./formulas/remove-ember-data-is-new-serializer-api');

module.exports = EmberWatson;

function EmberWatson() { }

EmberWatson.prototype._removeEmberDataIsNewSerializerAPI = removeEmberDataIsNewSerializerAPI;
EmberWatson.prototype.removeEmberDataIsNewSerializerAPI = function(path) {
var files = findFiles(path, '.js');
transform(files, this._removeEmberDataIsNewSerializerAPI);
};

EmberWatson.prototype._transformEmberDataAsyncFalseRelationships = transformEmberDataAsyncFalseRelationships;

EmberWatson.prototype.transformEmberDataAsyncFalseRelationships = function(rootPath) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import DS from 'ember-data';

export default DS.RESTSerializer.extend({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import DS from 'ember-data';

export default DS.RESTSerializer.extend({
isNewSerializerAPI: true
});
27 changes: 27 additions & 0 deletions tests/remove-ember-data-is-new-serializer-api-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

var Watson = require('../index.js');
var fs = require('fs');
var astEquality = require('./helpers/ast-equality');
var recast = require('recast');


describe('ember data isNewSerializerAPI', function(){
var baseDir = './tests/fixtures/remove-ember-data-is-new-serializer-api';
var watson;

beforeEach(function(){
watson = new Watson();
});

describe('removing isNewSerializerAPI literals', function() {

it('removes the isNewSerializerAPI directive', function() {
var source = fs.readFileSync(baseDir + '/old/serializer.js');
var newSource = watson._removeEmberDataIsNewSerializerAPI(source);
var expectedNewSource = fs.readFileSync(baseDir + '/new/serializer.js');

astEquality(newSource, expectedNewSource);
});
});
});

0 comments on commit e61e5d7

Please sign in to comment.