diff --git a/addon/ng2/blueprints/class/files/__path__/__name__.spec.ts b/addon/ng2/blueprints/class/files/__path__/__name__.spec.ts new file mode 100644 index 000000000000..cd1ca0ec4512 --- /dev/null +++ b/addon/ng2/blueprints/class/files/__path__/__name__.spec.ts @@ -0,0 +1,14 @@ +import { + describe, + ddescribe, + expect, + iit, + it +} from 'angular2/testing'; +import {<%= classifiedModuleName %>} from './<%= fileName %>'; + +describe('<%= classifiedModuleName %>', () => { + it('should create an instance', () => { + expect(new <%= classifiedModuleName %>()).toBeTruthy(); + }) +}); diff --git a/addon/ng2/blueprints/class/files/__path__/__name__.ts b/addon/ng2/blueprints/class/files/__path__/__name__.ts new file mode 100644 index 000000000000..54c3f4c0b4cb --- /dev/null +++ b/addon/ng2/blueprints/class/files/__path__/__name__.ts @@ -0,0 +1,2 @@ +export class <%= classifiedModuleName %> { +} diff --git a/addon/ng2/blueprints/class/index.js b/addon/ng2/blueprints/class/index.js new file mode 100644 index 000000000000..a3c09e6d6010 --- /dev/null +++ b/addon/ng2/blueprints/class/index.js @@ -0,0 +1,51 @@ +const stringUtils = require('ember-cli-string-utils'); +var dynamicPathParser = require('../../utilities/dynamic-path-parser'); +var addBarrelRegistration = require('../../utilities/barrel-management'); + +module.exports = { + description: '', + + anonymousOptions: [ + '' + ], + + normalizeEntityName: function (entityName) { + var parsedPath = dynamicPathParser(this.project, entityName); + + this.dynamicPath = parsedPath; + return parsedPath.name; + }, + + locals: function (options) { + var classType = options.args [2] + this.fileName = stringUtils.dasherize(options.entity.name); + if (classType) { + this.fileName += '.' + classType; + } + return { + dynamicPath: this.dynamicPath.dir, + flat: options.flat, + fileName: this.fileName + }; + }, + + fileMapTokens: function () { + // Return custom template variables here. + return { + __path__: () => { + this.generatePath = this.dynamicPath.dir; + return this.generatePath; + }, + __name__: () => { + return this.fileName; + } + }; + }, + + afterInstall: function() { + return addBarrelRegistration( + this, + this.generatePath, + this.fileName); + } +}; diff --git a/addon/ng2/commands/generate.ts b/addon/ng2/commands/generate.ts index c7f5b3a5010f..3a5bbf177353 100644 --- a/addon/ng2/commands/generate.ts +++ b/addon/ng2/commands/generate.ts @@ -29,6 +29,7 @@ function mapBlueprintName(name) { } const aliasMap = { + 'cl': 'class', 'c': 'component', 'd': 'directive', 'p': 'pipe', diff --git a/tests/acceptance/generate-class.spec.js b/tests/acceptance/generate-class.spec.js new file mode 100644 index 000000000000..379c6bf6bf5e --- /dev/null +++ b/tests/acceptance/generate-class.spec.js @@ -0,0 +1,56 @@ +'use strict'; + +const ng = require('../helpers/ng'); +const tmp = require('../helpers/tmp'); + +const conf = require('ember-cli/tests/helpers/conf'); +const existsSync = require('exists-sync'); +const expect = require('chai').expect; +const path = require('path'); +const root = process.cwd(); + +const testPath = path.join(root, 'tmp', 'foo', 'src', 'client', 'app'); + +describe('Acceptance: ng generate class', function () { + before(conf.setup); + + after(conf.restore); + + beforeEach(function () { + return tmp.setup('./tmp').then(function () { + process.chdir('./tmp'); + }).then(function () { + return ng(['new', 'foo', '--skip-npm', '--skip-bower']); + }); + }); + + afterEach(function () { + this.timeout(10000); + + return tmp.teardown('./tmp'); + }); + + it('ng generate class my-class', function () { + return ng(['generate', 'class', 'my-class']).then(() => { + expect(existsSync(path.join(testPath, 'my-class.ts'))).to.equal(true); + }); + }); + + it('ng generate class my-class model', function () { + return ng(['generate', 'class', 'my-class', 'model']).then(() => { + expect(existsSync(path.join(testPath, 'my-class.model.ts'))).to.equal(true); + }); + }); + + it(`ng generate class shared${path.sep}my-class`, function () { + return ng(['generate', 'class', 'shared/my-class']).then(() => { + expect(existsSync(path.join(testPath, 'shared', 'my-class.ts'))).to.equal(true); + }); + }); + + it(`ng generate class shared${path.sep}my-class model`, function () { + return ng(['generate', 'class', 'shared/my-class', 'model']).then(() => { + expect(existsSync(path.join(testPath, 'shared', 'my-class.model.ts'))).to.equal(true); + }); + }); +});