Repository: abuiles/ember-watson
Pull request #96 (2016-04-21 15:07:17)

add remove-ember-data-is-new-serializer-api command
This adds a handy command for removing all the `isNewSerializerAPI`
properties in serializers after you've finished upgrading.

abuiles: @fivetanley thanks! Looks good, can you fix the 🍝  issue and then I'll merge and release :)

Patch:
diff --git a/README.md b/README.md
index 348f23f..91d727a 100644
--- a/README.md
+++ b/README.md
@@ -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
 
@@ -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
diff --git a/lib/cli.js b/lib/cli.js
index 96df694..19ce027 100644
--- a/lib/cli.js
+++ b/lib/cli.js
@@ -81,6 +81,14 @@ 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')
@@ -88,6 +96,7 @@ program
   .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) {
@@ -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) {
diff --git a/lib/commands/remove-ember-data-is-new-serializer-api.js b/lib/commands/remove-ember-data-is-new-serializer-api.js
new file mode 100644
index 0000000..990816f
--- /dev/null
+++ b/lib/commands/remove-ember-data-is-new-serializer-api.js
@@ -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';
+  }
+};
diff --git a/lib/formulas/remove-ember-data-is-new-serializer-api.js b/lib/formulas/remove-ember-data-is-new-serializer-api.js
new file mode 100644
index 0000000..8c96844
--- /dev/null
+++ b/lib/formulas/remove-ember-data-is-new-serializer-api.js
@@ -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;
+};
diff --git a/lib/helpers/find-files.js b/lib/helpers/find-files.js
index e47a90f..165805a 100644
--- a/lib/helpers/find-files.js
+++ b/lib/helpers/find-files.js
@@ -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) {
diff --git a/lib/watson.js b/lib/watson.js
index 6a69eff..e58f355 100644
--- a/lib/watson.js
+++ b/lib/watson.js
@@ -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) {


Test patch:
diff --git a/tests/fixtures/remove-ember-data-is-new-serializer-api/new/serializer.js b/tests/fixtures/remove-ember-data-is-new-serializer-api/new/serializer.js
new file mode 100644
index 0000000..e101f11
--- /dev/null
+++ b/tests/fixtures/remove-ember-data-is-new-serializer-api/new/serializer.js
@@ -0,0 +1,3 @@
+import DS from 'ember-data';
+
+export default DS.RESTSerializer.extend({});
diff --git a/tests/fixtures/remove-ember-data-is-new-serializer-api/old/serializer.js b/tests/fixtures/remove-ember-data-is-new-serializer-api/old/serializer.js
new file mode 100644
index 0000000..d19891c
--- /dev/null
+++ b/tests/fixtures/remove-ember-data-is-new-serializer-api/old/serializer.js
@@ -0,0 +1,5 @@
+import DS from 'ember-data';
+
+export default DS.RESTSerializer.extend({
+  isNewSerializerAPI: true
+});
diff --git a/tests/remove-ember-data-is-new-serializer-api-test.js b/tests/remove-ember-data-is-new-serializer-api-test.js
new file mode 100644
index 0000000..230dd4a
--- /dev/null
+++ b/tests/remove-ember-data-is-new-serializer-api-test.js
@@ -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);
+    });
+  });
+});