Skip to content

Commit

Permalink
feat(2492): Update to eslint-config-screwdriver v5 (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi authored Jul 9, 2021
1 parent b54ae96 commit 7fbf9ac
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 250 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ artifacts/
npm-debug.log
.DS_STORE
.*.swp
package-lock.json
.nyc_output
7 changes: 4 additions & 3 deletions bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
// (similar to git with 'git branch', 'git status', etc)
//
// The other commands are kept around for backwards compatibility.
const operations = require('../commands').operations;
const nomnom = require('nomnom');
const { operations } = require('../commands');

Object.keys(operations).forEach((key) => {
nomnom.command(key)
Object.keys(operations).forEach(key => {
nomnom
.command(key)
.options(operations[key].opts)
.callback(operations[key].exec)
.help(operations[key].help);
Expand Down
89 changes: 49 additions & 40 deletions commands.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
'use strict';

const nomnom = require('nomnom');
const index = require('./index');
const path = process.env.SD_TEMPLATE_PATH || './sd-template.yaml';
const nomnom = require('nomnom');

const operations = {

/* Publish template */
publish: {
opts: {
json: { abbr: 'j', flag: true, help: 'Output result as json' },
tag: { abbr: 't', default: 'latest', help: 'Add template tag' }
},
exec(opts) {
return index.loadYaml(path)
return index
.loadYaml(path)
.then(config => index.publishTemplate(config))
.then(publishResult => index.tagTemplate({
name: publishResult.name,
tag: opts.tag,
version: publishResult.version
}))
.then((result) => {
.then(publishResult =>
index.tagTemplate({
name: publishResult.name,
tag: opts.tag,
version: publishResult.version
})
)
.then(result => {
if (!opts.json) {
console.log(`Template ${result.name}@${result.version} was `
+ `successfully published and tagged as ${result.tag}`);
console.log(
`Template ${result.name}@${result.version} was ` +
`successfully published and tagged as ${result.tag}`
);
} else {
console.log(JSON.stringify(result));
}
})
.catch((err) => {
.catch(err => {
console.error(err);
process.exit(1);
});
Expand All @@ -42,16 +46,17 @@ const operations = {
json: { abbr: 'j', flag: true, help: 'Output result as json' }
},
exec(opts) {
return index.loadYaml(path)
return index
.loadYaml(path)
.then(config => index.validateTemplate(config))
.then((result) => {
.then(result => {
if (!opts.json) {
console.log('Template is valid');
} else {
console.log(JSON.stringify(result));
}
})
.catch((err) => {
.catch(err => {
console.error(err);
process.exit(1);
});
Expand All @@ -68,22 +73,22 @@ const operations = {
json: { abbr: 'j', flag: true, help: 'Output result as json' }
},
exec(opts) {
return index.tagTemplate({
name: opts.name,
tag: opts.tag,
version: opts.version
})
.then((result) => {
return index
.tagTemplate({
name: opts.name,
tag: opts.tag,
version: opts.version
})
.then(result => {
if (!opts.json) {
console.log(
`Template ${result.name}@${result.version} was successfully ` +
`tagged as ${result.tag}`
`Template ${result.name}@${result.version} was successfully tagged as ${result.tag}`
);
} else {
console.log(JSON.stringify(result));
}
})
.catch((err) => {
.catch(err => {
console.error(err);
process.exit(1);
});
Expand All @@ -100,18 +105,19 @@ const operations = {
},

exec(opts) {
return index.removeTag({
name: opts.name,
tag: opts.tag
})
.then((result) => {
return index
.removeTag({
name: opts.name,
tag: opts.tag
})
.then(result => {
if (!opts.json) {
console.log(`Tag ${opts.tag} was successfully removed from ${opts.name}`);
} else {
console.log(JSON.stringify(result));
}
})
.catch((err) => {
.catch(err => {
console.error(err);
process.exit(1);
});
Expand All @@ -127,15 +133,16 @@ const operations = {
},

exec(opts) {
return index.removeTemplate(opts.name)
.then((result) => {
return index
.removeTemplate(opts.name)
.then(result => {
if (!opts.json) {
console.log(`Template ${result.name} was successfully removed`);
} else {
console.log(JSON.stringify(result));
}
})
.catch((err) => {
.catch(err => {
console.error(err);
process.exit(1);
});
Expand All @@ -151,14 +158,15 @@ const operations = {
},

exec(opts) {
return index.getVersionFromTag({
name: opts.name,
tag: opts.tag
})
.then((result) => {
return index
.getVersionFromTag({
name: opts.name,
tag: opts.tag
})
.then(result => {
console.log(`${result}`);
})
.catch((err) => {
.catch(err => {
console.error(err);
process.exit(1);
});
Expand All @@ -174,7 +182,8 @@ const operations = {
* @return {Object} result of command, if any
*/
function run(name) {
const opts = nomnom.options(operations[name].opts)
const opts = nomnom
.options(operations[name].opts)
.help(operations[name].help)
.parse();

Expand Down
42 changes: 17 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const Yaml = require('js-yaml');
* @return {Promise} Promise that resolves to the template as a config object
*/
function loadYaml(path) {
return new Promise(resolve =>
resolve(Yaml.safeLoad(fs.readFileSync(path, 'utf8'))));
return new Promise(resolve => resolve(Yaml.safeLoad(fs.readFileSync(path, 'utf8'))));
}

/**
Expand All @@ -36,11 +35,11 @@ function validateTemplate(config) {
body: {
yaml: JSON.stringify(config)
}
}).then((response) => {
}).then(response => {
if (response.errors.length > 0) {
let errorMessage = 'Template is not valid for the following reasons:';

response.errors.forEach((err) => {
response.errors.forEach(err => {
/* eslint-disable prefer-template */
errorMessage += `\n${JSON.stringify(err, null, 4)},`;
/* eslint-enable prefer-template */
Expand Down Expand Up @@ -77,12 +76,11 @@ function publishTemplate(config) {
},
resolveWithFullResponse: true,
simple: false
}).then((response) => {
const body = response.body;
}).then(response => {
const { body } = response;

if (response.statusCode !== 201) {
throw new Error('Error publishing template. ' +
`${response.statusCode} (${body.error}): ${body.message}`);
throw new Error(`Error publishing template. ${response.statusCode} (${body.error}): ${body.message}`);
}

let fullTemplateName = body.name;
Expand Down Expand Up @@ -119,12 +117,11 @@ function removeTemplate(name) {
json: true,
resolveWithFullResponse: true,
simple: false
}).then((response) => {
}).then(response => {
const { body } = response;

if (response.statusCode !== 204) {
throw new Error(`Error removing template ${name}. ` +
`${response.statusCode} (${body.error}): ${body.message}`);
throw new Error(`Error removing template ${name}. ${response.statusCode} (${body.error}): ${body.message}`);
}

return { name };
Expand All @@ -151,12 +148,11 @@ function getLatestVersion(name) {
json: true,
resolveWithFullResponse: true,
simple: false
}).then((response) => {
}).then(response => {
const { body, statusCode } = response;

if (statusCode !== 200) {
throw new Error('Error getting latest template version. ' +
`${statusCode} (${body.error}): ${body.message}`);
throw new Error(`Error getting latest template version. ${statusCode} (${body.error}): ${body.message}`);
}

return body[0].version;
Expand Down Expand Up @@ -185,12 +181,11 @@ function getVersionFromTag({ name, tag }) {
json: true,
resolveWithFullResponse: true,
simple: false
}).then((response) => {
}).then(response => {
const { body, statusCode } = response;

if (statusCode !== 200) {
throw new Error('Error getting version from tag. ' +
`${statusCode} (${body.error}): ${body.message}`);
throw new Error(`Error getting version from tag. ${statusCode} (${body.error}): ${body.message}`);
}

return body.version;
Expand All @@ -213,8 +208,7 @@ function tagTemplate({ name, tag, version }) {
const url = URL.resolve(hostname, `templates/${templateName}/tags/${templateTag}`);

if (!version) {
return getLatestVersion(name)
.then(latest => tagTemplate({ name, tag, version: latest }));
return getLatestVersion(name).then(latest => tagTemplate({ name, tag, version: latest }));
}

return request({
Expand All @@ -229,12 +223,11 @@ function tagTemplate({ name, tag, version }) {
},
resolveWithFullResponse: true,
simple: false
}).then((response) => {
}).then(response => {
const { body, statusCode } = response;

if (statusCode !== 201 && statusCode !== 200) {
throw new Error('Error tagging template. ' +
`${statusCode} (${body.error}): ${body.message}`);
throw new Error(`Error tagging template. ${statusCode} (${body.error}): ${body.message}`);
}

return {
Expand Down Expand Up @@ -268,12 +261,11 @@ function removeTag({ name, tag }) {
json: true,
resolveWithFullResponse: true,
simple: false
}).then((response) => {
}).then(response => {
const { body, statusCode } = response;

if (statusCode !== 204) {
throw new Error('Error removing template tag. ' +
`${statusCode} (${body.error}): ${body.message}`);
throw new Error(`Error removing template tag. ${statusCode} (${body.error}): ${body.message}`);
}

return {
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@
"Tiffany Kyi <[email protected]>"
],
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^4.19.0",
"eslint-config-screwdriver": "^3.0.1",
"chai": "^4.3.4",
"eslint": "^7.5.0",
"eslint-config-screwdriver": "^5.0.1",
"mocha": "^8.2.1",
"mocha-multi-reporters": "^1.5.1",
"mocha-sonarqube-reporter": "^1.0.2",
"nyc": "^15.0.0",
"mockery": "^2.0.0",
"sinon": "^7.1.0"
"nyc": "^15.0.0",
"sinon": "^9.0.0"
},
"dependencies": {
"js-yaml": "^3.12.1",
"js-yaml": "^3.14.1",
"nomnom": "^1.8.1",
"request": "^2.88.0",
"request-promise-native": "^1.0.5"
"request": "^2.88.2",
"request-promise-native": "^1.0.9"
},
"release": {
"debug": false,
Expand Down
Loading

0 comments on commit 7fbf9ac

Please sign in to comment.