Merge pull request #6408 from serverless/improve-service-rename

Strip trailing comment when renaming a service
This commit is contained in:
Mariusz Nowak 2019-07-19 17:03:07 +02:00 committed by GitHub
commit b7d413ba08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 13 deletions

View File

@ -19,16 +19,11 @@ function renameService(name, servicePath) {
const serverlessYml = fse
.readFileSync(serviceFile, 'utf-8')
.replace(/service\s*:.+/gi, match => {
const fractions = match.split('#');
fractions[0] = `service: ${name}`;
return fractions.join(' #');
})
.replace(/service\s*:\n {2}name:.+/gi, match => {
const fractions = match.split('#');
fractions[0] = `service:\n name: ${name}`;
return fractions.join(' #');
});
.replace(/service\s*:.+/gi, () => `service: ${name}`)
.replace(
/service\s*:\s*\n(\s+)name:.+/gi,
(match, indent) => `service:\n${indent}name: ${name}`
);
fse.writeFileSync(serviceFile, serverlessYml);

View File

@ -56,7 +56,7 @@ describe('renameService', () => {
const defaultServiceYml =
'# comment\nservice: service-name #comment\n\nprovider:\n name: aws\n# comment';
const newServiceYml =
'# comment\nservice: new-service-name #comment\n\nprovider:\n name: aws\n# comment';
'# comment\nservice: new-service-name\n\nprovider:\n name: aws\n# comment';
const defaultServiceName = 'service-name';
const newServiceName = 'new-service-name';
@ -78,7 +78,7 @@ describe('renameService', () => {
const defaultServiceYml =
'# comment\nservice: service-name #comment\n\nprovider:\n name: aws\n# comment';
const newServiceYml =
'# comment\nservice: new-service-name #comment\n\nprovider:\n name: aws\n# comment';
'# comment\nservice: new-service-name\n\nprovider:\n name: aws\n# comment';
const serviceFile = path.join(servicePath, 'serverless.yml');
@ -114,7 +114,7 @@ describe('renameService', () => {
const defaultServiceYml =
'# comment\nservice:\n name: service-name #comment\n\nprovider:\n name: aws\n# comment';
const newServiceYml =
'# comment\nservice:\n name: new-service-name #comment\n\nprovider:\n name: aws\n# comment';
'# comment\nservice:\n name: new-service-name\n\nprovider:\n name: aws\n# comment';
const defaultServiceName = 'service-name';
const newServiceName = 'new-service-name';