diff --git a/lib/cli.js b/lib/cli.js index 96df694..2446b72 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -81,6 +81,14 @@ program watson.replaceNeedsWithInjection(path); }); +program + .command('replace-needs-with-injection [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: [ + '' + ], + 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) { 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); + }); + }); +});