Skip to content

Commit

Permalink
Merge pull request #135 from Turbo87/async
Browse files Browse the repository at this point in the history
Use async/await instead of Promise chains
  • Loading branch information
Turbo87 authored Dec 22, 2020
2 parents 2097cb8 + 26f2565 commit d64f165
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions src/auto-dist-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,28 @@ const pkgUp = require('pkg-up');
const getDistTags = require('./fetch-dist-tags');
const calcDistTag = require('./calc-dist-tag');

module.exports = function autoDistTag(cwd, options) {
let pkgPath, pkg, tags, tag;
module.exports = async function autoDistTag(cwd, options) {
let pkgPath = await pkgUp(cwd);
let pkg = await fs.readJson(pkgPath);
let tags = await getDistTags(pkg.name);
let tag = await calcDistTag(pkg.version, tags);

return pkgUp(cwd)
.then(_pkgPath => (pkgPath = _pkgPath))
.then(() => fs.readJson(pkgPath))
.then(_pkg => (pkg = _pkg))
.then(() => getDistTags(pkg.name))
.then(_tags => (tags = _tags))
.then(() => calcDistTag(pkg.version, tags))
.then(_tag => (tag = _tag))
.then(() => {
if (options && options.write) {
// skip writing to `package.json if an explicit publishConfig.tag is set
if ('publishConfig' in pkg && 'tag' in pkg.publishConfig) {
return;
}
if (options && options.write) {
// skip writing to `package.json if an explicit publishConfig.tag is set
if ('publishConfig' in pkg && 'tag' in pkg.publishConfig) {
return;
}

// skip writing to `package.json if the calculated tag is "latest" because it's the default anyway
if (tag === 'latest') {
return;
}
// skip writing to `package.json if the calculated tag is "latest" because it's the default anyway
if (tag === 'latest') {
return;
}

pkg.publishConfig = pkg.publishConfig || {};
pkg.publishConfig.tag = tag;
pkg.publishConfig = pkg.publishConfig || {};
pkg.publishConfig.tag = tag;

return fs.writeJson(pkgPath, pkg, { spaces: 2 });
}
})
.then(() => tag);
fs.writeJson(pkgPath, pkg, { spaces: 2 });
}

return tag;
};

0 comments on commit d64f165

Please sign in to comment.