From 2e34f8dce5587e01b285d942a28a5ae016e8447b Mon Sep 17 00:00:00 2001 From: Richie Mackay Date: Wed, 18 May 2016 23:35:35 +0100 Subject: [PATCH] Wip (#6) * adding red on major revision change * add dependency status * add coverage for dokuwiki --- README.md | 1 + lib/index.js | 2 +- lib/tableGenerator.js | 131 +++++++++++++++++---------------- package.json | 5 +- spec/tableGenerator.spec.js | 142 +++++++++++++++++++++++------------- 5 files changed, 168 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index 841fbe3..b52e207 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ [![npm stable version](https://img.shields.io/npm/v/ncu-report.svg?label=npm)](https://npmjs.org/package/ncu-report) [![Build Status](https://travis-ci.org/cizer/ncu-report.svg?branch=master)](https://travis-ci.org/cizer/ncu-report) +[![Dependency Status](https://david-dm.org/cizer/ncu-report.svg)](https://david-dm.org/cizer/ncu-report) # ncu-report A report generator that produces a table showing which packages require updating. The report is generated in markdown (and optionally dokuwiki) format and can be integrated into your build. diff --git a/lib/index.js b/lib/index.js index 84d4b31..ee039d2 100755 --- a/lib/index.js +++ b/lib/index.js @@ -8,7 +8,7 @@ var reportService = require('./reportService'); var vm = require('npm-check-updates/lib/versionmanager'); program - .version('0.0.8') + .version('0.1.0') .option('-m, --packageManager ', 'npm (default) | bower', 'npm') .option('-o, --outputFile ', '.\/ncu-report\/ncu-report.md (default)') .option('-f, --format ', 'markdown (default) | dokuwiki', 'markdown') diff --git a/lib/tableGenerator.js b/lib/tableGenerator.js index 22f4117..ea6e8c4 100644 --- a/lib/tableGenerator.js +++ b/lib/tableGenerator.js @@ -2,82 +2,91 @@ var dateformat = require('dateformat'); var format = function(packageList, format) { - var table = []; - if (!packageList || packageList.length === 0) { - table.push("No data."); - return table; - } - - switch (format) { - case 'dokuwiki': - return formatDokuWiki(packageList); - default: //markdown - return formatMarkdown(packageList); - } + var table = []; + if (!packageList || packageList.length === 0) { + table.push("No data."); + return table; + } + switch (format) { + case 'dokuwiki': + return formatDokuWiki(packageList); + default: //markdown + return formatMarkdown(packageList); + } } function formatMarkdown(packageList) { - var table = []; - table.push("| Package | Current | Installed | Latest | Status |"); - table.push("| :------ | ------: | --------: | -----: | :----: |"); - - packageList.forEach(function(package) { - package.status = "OUTDATED"; - if (isLatestPackage(package.version.installed, package.version.new)) { - package.status = "OK"; - package.version.new = package.version.current; - } - - table.push("| " + package.name + " | " + - package.version.current + " | " + - package.version.installed + " | " + - package.version.new + " | " + - package.status + " |"); - - }); - table.push('Report generated on: ' + Date() + ''); - return table; + var table = []; + table.push("| Package | Current | Installed | Latest | Status |"); + table.push("| :------ | ------: | --------: | -----: | :----: |"); + + packageList.forEach(function(package) { + package.status = "OUTDATED"; + if (isLatestPackage(package.version.installed, package.version.new)) { + package.status = "OK"; + package.version.new = package.version.current; + } + + table.push("| " + package.name + " | " + + package.version.current + " | " + + package.version.installed + " | " + + package.version.new + " | " + + package.status + " |"); + + }); + table.push('Report generated on: ' + Date() + ''); + return table; } function formatDokuWiki(packageList) { + var table = []; + table.push("| Package ^ Current ^ Installed ^ Latest ^ Status ^"); + + packageList.forEach(function(package) { + var installedColor; + package.status = "OUTDATED"; + if (isLatestPackage(package.version.installed, package.version.new)) { + package.status = "OK"; + installedColor = '@green:'; + package.version.new = package.version.current; + } else if (isMajorUpdate(package.version.installed, package.version.new)) { + installedColor = '@red:'; + } else { + installedColor = '@orange:'; + } + + table.push("^ " + escapeCarat(package.name + " | " + + package.version.current + " | " + + installedColor + package.version.installed + " | " + + package.version.new + " | " + package.status + " |")); + + }); + + table.push('^ Report generated on: ' + Date() + '|||||'); + return table; - var table = []; - table.push("| Package ^ Current ^ Installed ^ Latest ^ Status ^"); - - packageList.forEach(function(package) { - var installedColor; - if (isLatestPackage(package.version.installed, package.version.new)) { - package.status = "OK"; - installedColor = '@green:'; - package.version.new = package.version.current; - } else { - package.status = "OUTDATED"; - installedColor = '@orange:'; - } - - table.push("^ " + escapeCarat(package.name + " | " + - package.version.current + " | " + - installedColor + package.version.installed + " | " + - package.version.new + " | " + package.status + " |")); - - }); +}; - table.push('^ Report generated on: ' + Date() + '|||||'); +function isLatestPackage(installedVersion, newVersion) { + return !newVersion || newVersion === installedVersion; +} - return table; +function escapeCarat(version) { + return version.toString().replace(/\^/g, "%%^%%"); +} -}; +function isMajorUpdate(installedVersion, newVersion) { + var installedMajorVersion = installedVersion.split('.')[0] + .toString().replace('^', ''); -function isLatestPackage(installedVersion, newVersion){ - return !newVersion || newVersion === installedVersion; -} + var newMajorVersion = newVersion.split('.')[0] + .toString().replace('^', ''); -function escapeCarat(version){ - return version.toString().replace(/\^/g, "%%^%%"); + return (newMajorVersion > installedMajorVersion); } module.exports = { - format: format + format: format }; diff --git a/package.json b/package.json index 8531551..9c720d5 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "ncu-report", - "version": "0.0.11", + "version": "0.1.0", "description": "A utility that formats the output of ncu in markdown format", "main": "./lib/index.js", "scripts": { - "test": "./node_modules/jasmine-node/bin/jasmine-node spec" + "test": "istanbul cover --include-all-sources jasmine-node spec" }, "bin": { "ncu-report": "./lib/index.js" @@ -18,6 +18,7 @@ "path": "^0.12.7" }, "devDependencies": { + "istanbul": "^0.4.3", "jasmine-node": "^1.14.5", "proxyquire": "^1.7.4" } diff --git a/spec/tableGenerator.spec.js b/spec/tableGenerator.spec.js index 620424a..b7c20bf 100644 --- a/spec/tableGenerator.spec.js +++ b/spec/tableGenerator.spec.js @@ -2,65 +2,109 @@ describe("format", function() { - var generator = require("../lib/tableGenerator"), - result; + var service = require('../lib/tableGenerator'), + result, testPackageList; - describe("when called with invalid data", function() { + describe("when called with invalid data", function() { - it("no params", function() { - result = generator.format(); - expect(result[0]).toBe("No data."); - }); + it("no params", function() { + result = service.format(); + expect(result[0]).toBe("No data."); + }); - it("empty array", function() { - result = generator.format([]); - expect(result[0]).toBe("No data."); + it("empty array", function() { + result = service.format([]); + expect(result[0]).toBe("No data."); + }); }); - }); - - describe("when called with valid list", function() { - - beforeEach(function() { - - result = generator.format([{ - name: "outdated1", - version: { - current: 1, - installed: 1.5, - new: 2 - } - }, { - name: "outdated2", - version: { - current: 2, - installed: 2.5, - new: 3 - } - }, { - name: "ok1", - version: { - current: '^4', - installed: 4 - } - }]); - }); + describe("when called with valid list", function() { - it("return table header", function() { - expect(result[0]).toBe("| Package | Current | Installed | Latest | Status |"); - }); + beforeEach(function() { + testPackageList = [{ + name: "outdated1", + version: { + current: '1.0', + installed: '1.5', + new: '2.0' + } + }, { + name: "outdated2", + version: { + current: '2.0', + installed: '2.5', + new: '3.0' + } + }, { + name: "ok1", + version: { + current: '^4.0', + installed: '4.0' + } + }]; + result = service.format(testPackageList); + }); - it("returns valid formatted table item (line1)", function() { - expect(result[2]).toBe("| outdated1 | 1 | 1.5 | 2 | OUTDATED |"); - }); + it("return table header", function() { + expect(result[0]).toBe("| Package | Current | Installed | Latest | Status |"); + }); + + it("returns valid formatted table item (line1)", function() { + expect(result[2]).toBe("| outdated1 | 1.0 | 1.5 | 2.0 | OUTDATED |"); + }); + + it("returns valid formatted table item (line2)", function() { + expect(result[3]).toBe("| outdated2 | 2.0 | 2.5 | 3.0 | OUTDATED |"); + }); - it("returns valid formatted table item (line2)", function() { - expect(result[3]).toBe("| outdated2 | 2 | 2.5 | 3 | OUTDATED |"); + it("returns valid formatted table item (line3)", function() { + expect(result[4]).toBe("| ok1 | ^4.0 | 4.0 | ^4.0 | OK |"); + }); }); - it("returns valid formatted table item (line3)", function() { - expect(result[4]).toBe("| ok1 | ^4 | 4 | ^4 | OK |"); + + describe("when called with valid list and docuwiki format", function() { + + beforeEach(function() { + testPackageList = [{ + name: "outdated1", + version: { + current: '1.0', + installed: '1.5', + new: '2.0' + } + }, { + name: "outdated2", + version: { + current: '2.0', + installed: '2.5', + new: '3.0' + } + }, { + name: "ok1", + version: { + current: '^4.0', + installed: '4.0' + } + }]; + result = service.format(testPackageList, 'dokuwiki'); + }); + + it("return table header", function() { + expect(result[0]).toBe("| Package ^ Current ^ Installed ^ Latest ^ Status ^"); + }); + + it("returns valid formatted table item (line1)", function() { + expect(result[1]).toBe("^ outdated1 | 1.0 | @red:1.5 | 2.0 | OUTDATED |"); + }); + + it("returns valid formatted table item (line2)", function() { + expect(result[2]).toBe("^ outdated2 | 2.0 | @red:2.5 | 3.0 | OUTDATED |"); + }); + + it("returns valid formatted table item (line3)", function() { + expect(result[3]).toBe("^ ok1 | %%^%%4.0 | @green:4.0 | %%^%%4.0 | OK |"); + }); }); - }); });