From 816ba289e57a95e64c294badacedd378d44cd1b8 Mon Sep 17 00:00:00 2001 From: Roger Lam Date: Sat, 20 Aug 2016 00:46:54 -0700 Subject: [PATCH 1/6] Add failing test for name option in create --- lib/plugins/create/tests/create.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/plugins/create/tests/create.js b/lib/plugins/create/tests/create.js index 2c9e2bf7e..6130e10a2 100644 --- a/lib/plugins/create/tests/create.js +++ b/lib/plugins/create/tests/create.js @@ -42,6 +42,23 @@ describe('Create', () => { expect(() => create.create()).to.throw(Error); }); + it('should overwrite the name for the service if user passed name', () => { + const tmpDir = path.join(os.tmpdir(), (new Date()).getTime().toString()); + const cwd = process.cwd(); + fse.mkdirsSync(tmpDir); + process.chdir(tmpDir); + create.options.template = 'aws-nodejs'; + create.options.name = 'my_service'; + + return create.create().then(() => { + return create.serverless.yamlParser.parse(path.join(tmpDir, 'serverless.yml')).then((obj) => { + expect(obj.service).to.equal('my_service') + + process.chdir(cwd); + }); + }); + }); + it('should set servicePath based on cwd', () => { const tmpDir = path.join(os.tmpdir(), (new Date()).getTime().toString()); const cwd = process.cwd(); From e04226e922247c730e079b4f3eeeb4c88092443d Mon Sep 17 00:00:00 2001 From: Roger Lam Date: Mon, 19 Sep 2016 23:40:09 -0700 Subject: [PATCH 2/6] fix test --- lib/plugins/create/tests/create.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/plugins/create/tests/create.js b/lib/plugins/create/tests/create.js index 448f15324..b53ec2fc8 100644 --- a/lib/plugins/create/tests/create.js +++ b/lib/plugins/create/tests/create.js @@ -49,7 +49,6 @@ describe('Create', () => { }); it('should overwrite the name for the service if user passed name', () => { - const tmpDir = path.join(os.tmpdir(), (new Date()).getTime().toString()); const cwd = process.cwd(); fse.mkdirsSync(tmpDir); process.chdir(tmpDir); From 7ca12674a47897f21b9d51ba618eb6aa2bd86895 Mon Sep 17 00:00:00 2001 From: Roger Lam Date: Tue, 20 Sep 2016 00:28:58 -0700 Subject: [PATCH 3/6] add additional test for when path and name are provided --- lib/plugins/create/tests/create.js | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/plugins/create/tests/create.js b/lib/plugins/create/tests/create.js index b53ec2fc8..d1e69514c 100644 --- a/lib/plugins/create/tests/create.js +++ b/lib/plugins/create/tests/create.js @@ -181,10 +181,13 @@ describe('Create', () => { process.chdir(tmpDir); create.options.path = 'my-new-service'; + create.options.name = null; // using the nodejs template (this test is completely be independent from the template) create.options.template = 'aws-nodejs'; + console.log("name is: " + create.options.name); + return create.create().then(() => { const serviceDir = path.join(tmpDir, create.options.path); @@ -198,10 +201,44 @@ describe('Create', () => { const serverlessYmlfileContent = fse .readFileSync(path.join(serviceDir, 'serverless.yml')).toString(); + console.log(serverlessYmlfileContent); expect((/service: my-new-service/).test(serverlessYmlfileContent)).to.equal(true); process.chdir(cwd); }); }); + + it('should create a custom renamed service in the directory if using the "path" and "name" option', () => { + const cwd = process.cwd(); + fse.mkdirsSync(tmpDir); + process.chdir(tmpDir); + + create.options.path = 'my-new-service'; + create.options.name = 'my-custom-new-service'; + + // using the nodejs template (this test is completely be independent from the template) + create.options.template = 'aws-nodejs'; + + console.log("name is: " + create.options.name); + + return create.create().then(() => { + const serviceDir = path.join(tmpDir, create.options.path); + + // check if files are created in the correct directory + expect(create.serverless.utils.fileExistsSync( + path.join(serviceDir, 'serverless.yml'))).to.be.equal(true); + expect(create.serverless.utils.fileExistsSync( + path.join(serviceDir, 'handler.js'))).to.be.equal(true); + + // check if the service was renamed + const serverlessYmlfileContent = fse + .readFileSync(path.join(serviceDir, 'serverless.yml')).toString(); + + console.log(serverlessYmlfileContent); + expect((/service: my-custom-new-service/).test(serverlessYmlfileContent)).to.equal(true); + + process.chdir(cwd); + }); + }); }); }); From 65489ad34c0620bffb3114da48626e583b6e9d2b Mon Sep 17 00:00:00 2001 From: Roger Lam Date: Tue, 20 Sep 2016 00:30:20 -0700 Subject: [PATCH 4/6] add ability to pass custom name through options --- lib/plugins/create/create.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/plugins/create/create.js b/lib/plugins/create/create.js index 4d77a7c3c..50e9c06e0 100644 --- a/lib/plugins/create/create.js +++ b/lib/plugins/create/create.js @@ -36,6 +36,10 @@ class Create { usage: 'The path where the service should be created (e.g. --path my-service)', shortcut: 'p', }, + name: { + usage: 'Name for the service. Overwrites the default name of the created service.', + shortcut: 'n', + }, }, }, }; @@ -57,8 +61,9 @@ class Create { throw new this.serverless.classes.Error(errorMessage); } - // store the path option for the service if given + // store the custom options for the service if given const servicePath = this.options.path && this.options.path.length ? this.options.path : null; + const serviceName = this.options.name && this.options.name.length ? this.options.name : null; // create (if not yet present) and chdir into the directory for the service if (servicePath) { @@ -78,8 +83,8 @@ class Create { 'plugins', 'create', 'templates', this.options.template), this.serverless.config.servicePath); // rename the service if the user has provided a path via options - if (servicePath) { - const serviceName = servicePath.split(path.sep).pop(); + if (servicePath || serviceName) { + const newServiceName = serviceName || servicePath.split(path.sep).pop(); const serverlessYmlFilePath = path .join(this.serverless.config.servicePath, 'serverless.yml'); @@ -87,7 +92,7 @@ class Create { .readFileSync(serverlessYmlFilePath).toString(); serverlessYmlFileContent = serverlessYmlFileContent - .replace(/service: .+/, `service: ${serviceName}`); + .replace(/service: .+/, `service: ${newServiceName}`); fse.writeFileSync(serverlessYmlFilePath, serverlessYmlFileContent); } @@ -100,6 +105,11 @@ class Create { this.serverless.cli .log('NOTE: Please update the "service" property in serverless.yml with your service name'); } + // if (!this.options.name) { + // this.serverless.cli + // .log('NOTE: Please update the "service" property in serverless.yml with your service name'); + // } + return BbPromise.resolve(); } From 9a5c01815ea4bc596fab8f11c3684969744516f9 Mon Sep 17 00:00:00 2001 From: Roger Lam Date: Tue, 20 Sep 2016 10:56:27 -0700 Subject: [PATCH 5/6] remote comments and debug statements --- lib/plugins/create/create.js | 5 ----- lib/plugins/create/tests/create.js | 6 ------ 2 files changed, 11 deletions(-) diff --git a/lib/plugins/create/create.js b/lib/plugins/create/create.js index 50e9c06e0..84e8d0212 100644 --- a/lib/plugins/create/create.js +++ b/lib/plugins/create/create.js @@ -105,11 +105,6 @@ class Create { this.serverless.cli .log('NOTE: Please update the "service" property in serverless.yml with your service name'); } - // if (!this.options.name) { - // this.serverless.cli - // .log('NOTE: Please update the "service" property in serverless.yml with your service name'); - // } - return BbPromise.resolve(); } diff --git a/lib/plugins/create/tests/create.js b/lib/plugins/create/tests/create.js index d1e69514c..be846a50a 100644 --- a/lib/plugins/create/tests/create.js +++ b/lib/plugins/create/tests/create.js @@ -186,8 +186,6 @@ describe('Create', () => { // using the nodejs template (this test is completely be independent from the template) create.options.template = 'aws-nodejs'; - console.log("name is: " + create.options.name); - return create.create().then(() => { const serviceDir = path.join(tmpDir, create.options.path); @@ -201,7 +199,6 @@ describe('Create', () => { const serverlessYmlfileContent = fse .readFileSync(path.join(serviceDir, 'serverless.yml')).toString(); - console.log(serverlessYmlfileContent); expect((/service: my-new-service/).test(serverlessYmlfileContent)).to.equal(true); process.chdir(cwd); @@ -219,8 +216,6 @@ describe('Create', () => { // using the nodejs template (this test is completely be independent from the template) create.options.template = 'aws-nodejs'; - console.log("name is: " + create.options.name); - return create.create().then(() => { const serviceDir = path.join(tmpDir, create.options.path); @@ -234,7 +229,6 @@ describe('Create', () => { const serverlessYmlfileContent = fse .readFileSync(path.join(serviceDir, 'serverless.yml')).toString(); - console.log(serverlessYmlfileContent); expect((/service: my-custom-new-service/).test(serverlessYmlfileContent)).to.equal(true); process.chdir(cwd); From 5cb7dc1734c864beb99d97254d39fba9203095bc Mon Sep 17 00:00:00 2001 From: Roger Lam Date: Tue, 20 Sep 2016 11:15:35 -0700 Subject: [PATCH 6/6] fix linting errors --- lib/plugins/create/tests/create.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/plugins/create/tests/create.js b/lib/plugins/create/tests/create.js index be846a50a..11775a503 100644 --- a/lib/plugins/create/tests/create.js +++ b/lib/plugins/create/tests/create.js @@ -55,13 +55,14 @@ describe('Create', () => { create.options.template = 'aws-nodejs'; create.options.name = 'my_service'; - return create.create().then(() => { - return create.serverless.yamlParser.parse(path.join(tmpDir, 'serverless.yml')).then((obj) => { - expect(obj.service).to.equal('my_service') - + return create.create().then(() => + create.serverless.yamlParser.parse( + path.join(tmpDir, 'serverless.yml') + ).then((obj) => { + expect(obj.service).to.equal('my_service'); process.chdir(cwd); - }); - }); + }) + ); }); it('should set servicePath based on cwd', () => { @@ -205,7 +206,8 @@ describe('Create', () => { }); }); - it('should create a custom renamed service in the directory if using the "path" and "name" option', () => { + it('should create a custom renamed service in the directory if using ' + + 'the "path" and "name" option', () => { const cwd = process.cwd(); fse.mkdirsSync(tmpDir); process.chdir(tmpDir);