Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: require node >= 8.0 & upgrade eslint-config-eslint #34

Merged
merged 7 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
env:
node: true
extends: "eslint-config-eslint"
rules:
no-console: 0
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "7"
- "8"
- "10"
- "12"
sudo: false
script: "npm test"
6 changes: 3 additions & 3 deletions bin/eslint-generate-prerelease.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Requirements
//------------------------------------------------------------------------------

var ReleaseOps = require("../lib/release-ops");
const ReleaseOps = require("../lib/release-ops");

//------------------------------------------------------------------------------
// Execution
Expand All @@ -23,13 +23,13 @@ var ReleaseOps = require("../lib/release-ops");
* Usage:
* $ eslint-generate-prerelease beta
*/
var args = process.argv.slice(2),
const args = process.argv.slice(2),
prereleaseId = (args.length ? args[0] : null);

// there must be a prerelease ID
if (!prereleaseId) {
console.log("Missing prerelease identifier (alpha, beta, rc, etc.).");
process.exit(1); // eslint-disable-line no-process-exit
process.exit(1); // eslint-disable-line no-process-exit
}

ReleaseOps.generateRelease(prereleaseId);
2 changes: 1 addition & 1 deletion bin/eslint-generate-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Requirements
//------------------------------------------------------------------------------

var ReleaseOps = require("../lib/release-ops");
const ReleaseOps = require("../lib/release-ops");

//------------------------------------------------------------------------------
// Execution
Expand Down
2 changes: 1 addition & 1 deletion bin/eslint-publish-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Requirements
//------------------------------------------------------------------------------

var ReleaseOps = require("../lib/release-ops");
const ReleaseOps = require("../lib/release-ops");

//------------------------------------------------------------------------------
// Execution
Expand Down
130 changes: 67 additions & 63 deletions lib/release-ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// Requirements
//------------------------------------------------------------------------------

var fs = require("fs"),
const fs = require("fs"),
path = require("path"),
shelljs = require("shelljs"),
semver = require("semver"),
Expand All @@ -33,7 +33,8 @@ var fs = require("fs"),
* @private
*/
function getPackageInfo() {
var filePath = path.join(process.cwd(), "package.json");
const filePath = path.join(process.cwd(), "package.json");

return JSON.parse(fs.readFileSync(filePath, "utf8"));
}

Expand All @@ -48,7 +49,8 @@ function validateSetup() {
ShellOps.exit(1);
}

var pkg = getPackageInfo();
const pkg = getPackageInfo();

if (!pkg.files || pkg.files.length === 0) {
console.error("Missing 'files' property in package.json");
ShellOps.exit(1);
Expand All @@ -58,7 +60,7 @@ function validateSetup() {
if (!pkg.repository) {
console.error("Missing 'repository' in package.json");
ShellOps.exit(1);
} else if (!/^[\w\-]+\/[\w\-]+$/.test(pkg.repository)) {
} else if (!/^[\w-]+\/[\w-]+$/.test(pkg.repository)) {
console.error("The 'repository' field must be in the format 'foo/bar' in package.json");
ShellOps.exit(1);
}
Expand Down Expand Up @@ -89,14 +91,14 @@ function validateEnvironment() {
* @private
*/
function getPrereleaseVersion(currentVersion, prereleaseId, releaseType) {
var ver = new semver.SemVer(currentVersion);
const ver = new semver.SemVer(currentVersion);

// if it's already a prerelease version
if (ver.prerelease.length) {
return ver.inc("prerelease", prereleaseId).version;
} else {
return ver.inc("pre" + releaseType, prereleaseId).version;
}
return ver.inc(`pre${releaseType}`, prereleaseId).version;

}

/**
Expand All @@ -105,9 +107,9 @@ function getPrereleaseVersion(currentVersion, prereleaseId, releaseType) {
* @private
*/
function getVersionTags() {
var tags = ShellOps.execSilent("git tag").trim().split("\n");
const tags = ShellOps.execSilent("git tag").trim().split("\n");

return tags.reduce(function(list, tag) {
return tags.reduce((list, tag) => {
if (semver.valid(tag)) {
list.push(tag);
}
Expand All @@ -122,11 +124,11 @@ function getVersionTags() {
* @private
*/
function parseLogs(logs) {
var regexp = /^\* ([0-9a-f]{40}) ((?:([a-z]+): ?)?.*) \((.*)\)/i,
const regexp = /^\* ([0-9a-f]{40}) ((?:([a-z]+): ?)?.*) \((.*)\)/i,
parsed = [];

logs.forEach(function(log) {
var match = log.match(regexp);
logs.forEach(log => {
const match = log.match(regexp);

if (match) {
parsed.push({
Expand All @@ -138,7 +140,7 @@ function parseLogs(logs) {
body: ""
});
} else if (parsed.length) {
parsed[parsed.length - 1].body += log + "\n";
parsed[parsed.length - 1].body += `${log}\n`;
}
});

Expand All @@ -152,32 +154,32 @@ function parseLogs(logs) {
* @returns {Object[]} An array of parsed commit log messages.
*/
function excludeReverts(logs) {
logs = logs.slice();
const newLogs = logs.slice();

var revertRegex = /This reverts commit ([0-9a-f]{40})/,
shaIndexMap = Object.create(null), // Map of commit shas to indices
i, log, match, sha;
const revertRegex = /This reverts commit ([0-9a-f]{40})/,
shaIndexMap = Object.create(null); // Map of commit shas to indices
let i, log, match, sha;

// iterate in reverse because revert commits have lower indices than the
// commits they revert
for (i = logs.length - 1; i >= 0; i--) {
log = logs[i];
for (i = newLogs.length - 1; i >= 0; i--) {
log = newLogs[i];
match = log.body.match(revertRegex);

if (match) {
sha = match[1];

// only exclude this revert if we can find the commit it reverts
if (typeof shaIndexMap[sha] !== "undefined") {
logs[shaIndexMap[sha]] = null;
logs[i] = null;
newLogs[shaIndexMap[sha]] = null;
newLogs[i] = null;
}
} else {
shaIndexMap[log.sha] = i;
}
}

return logs.filter(Boolean);
return newLogs.filter(Boolean);
}

/**
Expand All @@ -191,9 +193,9 @@ function excludeReverts(logs) {
*/
function calculateReleaseFromGitLogs(currentVersion, logs, prereleaseId) {

logs = excludeReverts(parseLogs(logs));
const excludedLogs = excludeReverts(parseLogs(logs));

var changelog = {},
const changelog = {},
repository = getPackageInfo().repository;

/**
Expand All @@ -202,19 +204,20 @@ function calculateReleaseFromGitLogs(currentVersion, logs, prereleaseId) {
* @returns {string} A line for a changelog with a link to the GitHub commit
*/
function generateChangelogLine(log) {
return log.raw.replace(/^\* ([0-9a-f]{40})/, function(_, hash) {
return "* [`" + hash.slice(0, 7) + "`](https://github.com/" + repository + "/commit/" + hash + ")";
});
return log.raw.replace(
/^\* ([0-9a-f]{40})/,
(_, hash) => `* [\`${hash.slice(0, 7)}\`](https://github.com/${repository}/commit/${hash})`
);
}
var releaseInfo = {
const releaseInfo = {
version: currentVersion,
type: "",
changelog: changelog,
rawChangelog: logs.map(generateChangelogLine).join("\n")
changelog,
rawChangelog: excludedLogs.map(generateChangelogLine).join("\n")
};

// arrange change types into categories
logs.forEach(function(log) {
excludedLogs.forEach(log => {

// exclude untagged (e.g. revert) commits from version calculation
if (!log.flag) {
Expand All @@ -238,9 +241,9 @@ function calculateReleaseFromGitLogs(currentVersion, logs, prereleaseId) {

// increment version from current version
releaseInfo.version = (
prereleaseId ?
getPrereleaseVersion(currentVersion, prereleaseId, releaseInfo.type) :
semver.inc(currentVersion, releaseInfo.type)
prereleaseId
? getPrereleaseVersion(currentVersion, prereleaseId, releaseInfo.type)
: semver.inc(currentVersion, releaseInfo.type)
);

return releaseInfo;
Expand All @@ -255,14 +258,15 @@ function calculateReleaseFromGitLogs(currentVersion, logs, prereleaseId) {
function calculateReleaseInfo(prereleaseId) {

// get most recent tag
var pkg = getPackageInfo(),
const pkg = getPackageInfo(),
tags = getVersionTags(),
lastTag = tags[tags.length - 1],
commitRange = lastTag ? lastTag + "..HEAD" : "";
commitRange = lastTag ? `${lastTag}..HEAD` : "";

// get log statements
var logs = ShellOps.execSilent("git log --no-merges --pretty=format:\"* %H %s (%an)%n%b\" " + commitRange).split(/\n/g);
var releaseInfo = calculateReleaseFromGitLogs(pkg.version, logs, prereleaseId);
const logs = ShellOps.execSilent(`git log --no-merges --pretty=format:"* %H %s (%an)%n%b" ${commitRange}`).split(/\n/g);
const releaseInfo = calculateReleaseFromGitLogs(pkg.version, logs, prereleaseId);

releaseInfo.repository = pkg.repository;
return releaseInfo;
}
Expand All @@ -277,14 +281,14 @@ function calculateReleaseInfo(prereleaseId) {
function writeChangelog(releaseInfo) {

// get most recent two tags
var now = new Date(),
const now = new Date(),
timestamp = dateformat(now, "mmmm d, yyyy");

// output header
("v" + releaseInfo.version + " - " + timestamp + "\n").to("CHANGELOG.tmp");
(`v${releaseInfo.version} - ${timestamp}\n`).to("CHANGELOG.tmp");

// output changelog
("\n" + releaseInfo.rawChangelog + "\n").toEnd("CHANGELOG.tmp");
(`\n${releaseInfo.rawChangelog}\n`).toEnd("CHANGELOG.tmp");

// ensure there's a CHANGELOG.md file
if (!shelljs.test("-f", "CHANGELOG.md")) {
Expand Down Expand Up @@ -312,7 +316,7 @@ function generateRelease(prereleaseId) {
ShellOps.execSilent("npm test");

console.log("Calculating changes for release");
var releaseInfo = calculateReleaseInfo(prereleaseId);
const releaseInfo = calculateReleaseInfo(prereleaseId);

console.log("Release is %s", releaseInfo.version);

Expand All @@ -321,17 +325,15 @@ function generateRelease(prereleaseId) {

console.log("Committing to git");
ShellOps.exec("git add CHANGELOG.md");
ShellOps.exec("git commit -m \"Build: changelog update for " + releaseInfo.version + "\"");
ShellOps.exec(`git commit -m "Build: changelog update for ${releaseInfo.version}"`);

console.log("Generating %s", releaseInfo.version);
ShellOps.execSilent("npm version " + releaseInfo.version);
ShellOps.execSilent(`npm version ${releaseInfo.version}`);

console.log("Fixing line endings");
getPackageInfo().files.filter(function(dirPath) {
return fs.lstatSync(dirPath).isDirectory();
}).forEach(function(filePath) {
getPackageInfo().files.filter(dirPath => fs.lstatSync(dirPath).isDirectory()).forEach(filePath => {
if (fs.existsSync(filePath)) {
ShellOps.execSilent("linefix " + filePath);
ShellOps.execSilent(`linefix ${filePath}`);
}
});

Expand All @@ -346,18 +348,18 @@ function generateRelease(prereleaseId) {
*/
function publishReleaseToGitHub(releaseInfo) {

var repoParts = releaseInfo.repository.split("/"),
const repoParts = releaseInfo.repository.split("/"),
gh = new GitHub({ token: process.env.ESLINT_GITHUB_TOKEN }),
repo = gh.getRepo(repoParts[0], repoParts[1]),
tag = "v" + releaseInfo.version;
tag = `v${releaseInfo.version}`;

return repo.createRelease({
tag_name: tag,
tag_name: tag, // eslint-disable-line camelcase
body: releaseInfo.rawChangelog,
prerelease: !!semver.prerelease(releaseInfo.version)
}).then(function() {
}).then(() => {
console.log("Posted release notes to GitHub");
}).catch(function(ex) {
}).catch(ex => {
console.error("Could not post release notes to GitHub");
if (ex.message) {
console.error(ex.message);
Expand All @@ -373,9 +375,10 @@ function publishReleaseToGitHub(releaseInfo) {
function publishRelease() {
validateSetup();
validateEnvironment();
var releaseInfo = JSON.parse(fs.readFileSync(".eslint-release-info.json", "utf8"));
const releaseInfo = JSON.parse(fs.readFileSync(".eslint-release-info.json", "utf8"));

let oldNpmrcContents;

var oldNpmrcContents;
if (fs.existsSync(".npmrc")) {
oldNpmrcContents = fs.readFileSync(".npmrc", "utf8");
} else {
Expand All @@ -389,13 +392,14 @@ function publishRelease() {
// if there's a prerelease ID, publish under "next" tag
console.log("Publishing to npm");

var command = "npm publish";
let command = "npm publish";

if (semver.prerelease(releaseInfo.version)) {
command += " --tag next";
}

if (process.env.NPM_OTP && /^\d+$/.test(process.env.NPM_OTP)) {
command += " --otp=" + process.env.NPM_OTP;
command += ` --otp=${process.env.NPM_OTP}`;
}

ShellOps.exec(command);
Expand All @@ -419,10 +423,10 @@ function publishRelease() {
//------------------------------------------------------------------------------

module.exports = {
getPrereleaseVersion: getPrereleaseVersion,
generateRelease: generateRelease,
publishRelease: publishRelease,
calculateReleaseInfo: calculateReleaseInfo,
calculateReleaseFromGitLogs: calculateReleaseFromGitLogs,
writeChangelog: writeChangelog
getPrereleaseVersion,
generateRelease,
publishRelease,
calculateReleaseInfo,
calculateReleaseFromGitLogs,
writeChangelog
};
Loading