From 482aa74bac4cb35156ea079cdda5d6005edb96d0 Mon Sep 17 00:00:00 2001 From: Mike Brocchi Date: Wed, 18 May 2016 16:20:30 -0400 Subject: [PATCH] feat(blueprint): add blueprint for generating interfaces (#757) Fixes #729 --- .../interface/files/__path__/__name__.ts | 2 + addon/ng2/blueprints/interface/index.js | 58 +++++++++++++++++++ .../ng2/blueprints/ng2/files/angular-cli.json | 3 +- lib/config/schema.json | 3 + tests/e2e/e2e_workflow.spec.js | 17 ++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 addon/ng2/blueprints/interface/files/__path__/__name__.ts create mode 100644 addon/ng2/blueprints/interface/index.js diff --git a/addon/ng2/blueprints/interface/files/__path__/__name__.ts b/addon/ng2/blueprints/interface/files/__path__/__name__.ts new file mode 100644 index 000000000000..a329d9e8dd0b --- /dev/null +++ b/addon/ng2/blueprints/interface/files/__path__/__name__.ts @@ -0,0 +1,2 @@ +export interface <%= prefix %><%= classifiedModuleName %> { +} diff --git a/addon/ng2/blueprints/interface/index.js b/addon/ng2/blueprints/interface/index.js new file mode 100644 index 000000000000..0ed4979ed602 --- /dev/null +++ b/addon/ng2/blueprints/interface/index.js @@ -0,0 +1,58 @@ +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 interfaceType = options.args [2] + this.fileName = stringUtils.dasherize(options.entity.name); + if (interfaceType) { + this.fileName += '.' + interfaceType; + } + var prefix = ''; + if (this.project.ngConfig && + this.project.ngConfig.defaults && + this.project.ngConfig.defaults.prefixInterfaces) { + prefix = 'I'; + } + return { + dynamicPath: this.dynamicPath.dir, + flat: options.flat, + fileName: this.fileName, + prefix: prefix + }; + }, + + 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/blueprints/ng2/files/angular-cli.json b/addon/ng2/blueprints/ng2/files/angular-cli.json index 167af80f6014..93dacf9ce2fa 100644 --- a/addon/ng2/blueprints/ng2/files/angular-cli.json +++ b/addon/ng2/blueprints/ng2/files/angular-cli.json @@ -25,6 +25,7 @@ "defaults": { "prefix": "<%= prefix %>", "sourceDir": "<%= sourceDir %>", - "styleExt": "css" + "styleExt": "css", + "prefixInterfaces": false } } diff --git a/lib/config/schema.json b/lib/config/schema.json index 467844abff2a..08db591b3809 100644 --- a/lib/config/schema.json +++ b/lib/config/schema.json @@ -89,6 +89,9 @@ }, "styleExt": { "type": "string" + }, + "prefixInterfaces": { + "type": "boolean" } }, "additionalProperties": false diff --git a/tests/e2e/e2e_workflow.spec.js b/tests/e2e/e2e_workflow.spec.js index 24611307d9cc..a9be368dcb17 100644 --- a/tests/e2e/e2e_workflow.spec.js +++ b/tests/e2e/e2e_workflow.spec.js @@ -188,6 +188,23 @@ describe('Basic end-to-end Workflow', function () { }); }); + it('Can create a test interface using `ng generate interface test-interface model`', function () { + return ng(['generate', 'interface', 'test-interface', 'model']).then(function () { + var interfaceDir = path.join(process.cwd(), 'src', 'app'); + expect(existsSync(interfaceDir)).to.be.equal(true); + expect(existsSync(path.join(interfaceDir, 'test-interface.model.ts'))).to.be.equal(true); + }); + }); + + it('Perform `ng test` after adding a interface', function () { + this.timeout(420000); + + return ng(testArgs).then(function (result) { + const exitCode = typeof result === 'object' ? result.exitCode : result; + expect(exitCode).to.be.equal(0); + }); + }); + it('moves all files that live inside `public` into `dist`', function () { this.timeout(420000);