Skip to content

Commit e419afd

Browse files
authored
chore: remove dependency shelljs (#61)
* chore: remove dependency shelljs * delete temporary test directories
1 parent c55858f commit e419afd

File tree

3 files changed

+96
-22
lines changed

3 files changed

+96
-22
lines changed

lib/release-ops.js

+30-20
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55
* MIT License
66
*/
77

8-
/* global test, cat, rm, mv */
9-
108
"use strict";
119

1210
//------------------------------------------------------------------------------
1311
// Requirements
1412
//------------------------------------------------------------------------------
1513

16-
// TODO: Update to use non-global module to avoid prototype pollution.
17-
require("shelljs/global");
18-
1914
const fs = require("fs"),
2015
path = require("path"),
2116
semver = require("semver"),
@@ -49,6 +44,21 @@ const commitTagMap = new Map([
4944
// /MIT/, /BSD/, /Apache/, /ISC/, /WTF/, /Public Domain/
5045
// ];
5146

47+
/**
48+
* Tests if a file exists.
49+
* @param {string} file The path of the file to be tested.
50+
* @returns {boolean} `true` if the specified path denotes an existing file, otherwise `false`.
51+
*/
52+
function fileExists(file) {
53+
54+
// We can't use the `throwIfNoEntry` option with `fs.statSync`, because it's not supported in Node.js 10,
55+
// so we check if the file exists in advance.
56+
if (!fs.existsSync(file)) {
57+
return false;
58+
}
59+
return fs.statSync(file).isFile();
60+
}
61+
5262
/**
5363
* Loads the package.json file from the current directory.
5464
* @returns {void}
@@ -66,7 +76,7 @@ function getPackageInfo() {
6676
* @private
6777
*/
6878
function validateSetup() {
69-
if (!test("-f", "package.json")) {
79+
if (!fileExists("package.json")) {
7080
console.error("Missing package.json file");
7181
ShellOps.exit(1);
7282
}
@@ -333,27 +343,27 @@ function calculateReleaseInfo(prereleaseId) {
333343
*/
334344
function writeChangelog(releaseInfo) {
335345

336-
// get most recent two tags
346+
// get today's date in "mmmm d, yyyy" format
337347
const now = new Date(),
338-
timestamp = dateformat(now, "mmmm d, yyyy");
339-
340-
// output header
341-
(`v${releaseInfo.version} - ${timestamp}\n`).to("CHANGELOG.tmp");
348+
today = dateformat(now, "mmmm d, yyyy");
342349

343-
// output changelog
344-
(`\n${releaseInfo.rawChangelog}\n\n`).toEnd("CHANGELOG.tmp");
350+
// output header and changelog
351+
fs.writeFileSync(
352+
"CHANGELOG.tmp",
353+
`v${releaseInfo.version} - ${today}\n\n${releaseInfo.rawChangelog}\n\n`
354+
);
345355

346356
// ensure there's a CHANGELOG.md file
347-
if (!test("-f", "CHANGELOG.md")) {
357+
if (!fileExists("CHANGELOG.md")) {
348358
fs.writeFileSync("CHANGELOG.md", "");
349359
}
350360

351-
// switch-o change-o
352-
// `cat` returns a ShellString and `fs.writeFileSync` is throwing an error saying that this must be a String.
353-
fs.writeFileSync("CHANGELOG.md.tmp", cat("CHANGELOG.tmp", "CHANGELOG.md").toString());
354-
rm("CHANGELOG.tmp");
355-
rm("CHANGELOG.md");
356-
mv("CHANGELOG.md.tmp", "CHANGELOG.md");
361+
const data = `${fs.readFileSync("CHANGELOG.tmp", "utf-8")}${fs.readFileSync("CHANGELOG.md", "utf-8")}`;
362+
363+
fs.writeFileSync("CHANGELOG.md.tmp", data);
364+
fs.unlinkSync("CHANGELOG.tmp");
365+
fs.unlinkSync("CHANGELOG.md");
366+
fs.renameSync("CHANGELOG.md.tmp", "CHANGELOG.md");
357367
}
358368

359369
/**

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"dateformat": "^3.0.3",
5353
"github-api": "^3.2.2",
5454
"linefix": "^0.1.1",
55-
"semver": "^6.1.1",
56-
"shelljs": "^0.8.3"
55+
"semver": "^6.1.1"
5756
}
5857
}

tests/lib/release-ops.js

+65
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
//------------------------------------------------------------------------------
1313

1414
const assert = require("chai").assert,
15+
fs = require("fs"),
1516
leche = require("leche"),
17+
os = require("os"),
18+
path = require("path"),
19+
sinon = require("sinon"),
1620
ReleaseOps = require("../../lib/release-ops");
1721

1822
//------------------------------------------------------------------------------
@@ -479,4 +483,65 @@ describe("ReleaseOps", () => {
479483
});
480484
});
481485

486+
describe("writeChangelog", () => {
487+
488+
const cwd = process.cwd();
489+
let sandbox = null;
490+
let tmpDir = null;
491+
492+
beforeEach(() => {
493+
sandbox = sinon.sandbox.create();
494+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "writeChangelog-"));
495+
process.chdir(tmpDir);
496+
});
497+
498+
afterEach(() => {
499+
sandbox.restore();
500+
sandbox = null;
501+
process.chdir(cwd);
502+
fs.readdirSync(tmpDir).forEach(filename => fs.unlinkSync(path.join(tmpDir, filename))); // delete files in tmpDir
503+
fs.rmdirSync(tmpDir);
504+
tmpDir = null;
505+
});
506+
507+
it("creates a changelog", () => {
508+
const rawChangelog =
509+
"* [`bfb7759`](https://github.com/eslint/eeslint-release/commit/bfb7759a67daeb65410490b4d98bb9da7d1ea2ce) feat: First alpha (Firstname Lastname)";
510+
const releaseInfo = { version: "1.0.0-alpha.0", rawChangelog };
511+
const date = new Date(2024, 1, 15);
512+
513+
sandbox.stub(global, "Date").returns(date);
514+
515+
ReleaseOps.writeChangelog(releaseInfo);
516+
517+
assert.deepStrictEqual(fs.readdirSync("."), ["CHANGELOG.md"]);
518+
const newChangelog = fs.readFileSync("CHANGELOG.md", "utf-8");
519+
520+
assert.strictEqual(newChangelog, `v1.0.0-alpha.0 - February 15, 2024\n\n${rawChangelog}\n\n`);
521+
});
522+
523+
it("extends a changelog", () => {
524+
const oldChangelog =
525+
"v9.0.0 - December 31, 2023\n" +
526+
"\n" +
527+
"* [`0beec7b`](https://github.com/eslint/eeslint-release/commit/0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33) chore: Remove a dependency (𐌕𐌄𐌔𐌕)\n" +
528+
"\n";
529+
const rawChangelog =
530+
"* [`62cdb70`](https://github.com/eslint/eeslint-release/commit/62cdb7020ff920e5aa642c3d4066950dd1f01f4d) fix: Fix something (Abc D. Efg)\n" +
531+
"* [`bbe960a`](https://github.com/eslint/eeslint-release/commit/bbe960a25ea311d21d40669e93df2003ba9b90a2) test: Make sure it's broken (Francesco Trotta)";
532+
const releaseInfo = { version: "10.0.0", rawChangelog };
533+
const date = new Date(2024, 1, 2);
534+
535+
sandbox.stub(global, "Date").returns(date);
536+
fs.writeFileSync("CHANGELOG.md", oldChangelog);
537+
538+
ReleaseOps.writeChangelog(releaseInfo);
539+
540+
assert.deepStrictEqual(fs.readdirSync("."), ["CHANGELOG.md"]);
541+
const newChangelog = fs.readFileSync("CHANGELOG.md", "utf-8");
542+
543+
assert.strictEqual(newChangelog, `v10.0.0 - February 2, 2024\n\n${rawChangelog}\n\n${oldChangelog}`);
544+
});
545+
});
546+
482547
});

0 commit comments

Comments
 (0)