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

Check versions when npm.license is missing #53

Closed
wants to merge 9 commits into from
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"devDependencies": {
"@types/jest": "^19.2.4",
"@types/node": "^7.0.29",
"@types/semver": "^5.3.33",
"commitizen": "^2.9.6",
"cz-conventional-changelog": "^2.0.0",
"danger": "*",
Expand Down
135 changes: 135 additions & 0 deletions src/fixtures/pinpoint-npm-info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"_attachments": {},
"_id": "pinpoint",
"_rev": "5-25a718426e3c3e3763b7a8f85b9fe65b",
"author": {
"email": "[email protected]",
"name": "Glen Huang"
},
"description": "Display an arrow in a string of code to point to a location",
"dist-tags": {
"latest": "1.1.0"
},
"maintainers": [
{
"email": "[email protected]",
"name": "curvedmark"
}
],
"name": "pinpoint",
"readme": "# pinpoint\n\nAdd line numbers and an arrow to a string of code that points to a specific location.\n\n## Installation\n\n\tnpm install pinpoint\n\n## Example\n\nWith this code:\n\n```javascript\nfunction add(left, right) {\n return left + right;\n}\n\nconsole.log(add(1 + 1));\n```\n\nWhen being asked to point to line 2 and column 2, generates this:\n\n```javascript\n1| function add(left, right) {\n2| return left + right;\n-----^\n3| }\n4|\n5| console.log(add(1 + 1));\n```\n## API\n\n```javascript\nvar pinpoint = require('pinpoint');\n\nvar str = pinpoint(code, options);\n```\n\n* `code` - a string of code\n* `options` - an object literal supports these options:\n\t* `line` - the line number to point to, 1-based.\n\t* `column` - the column number to point to, 1-based.\n\t* `showLines` - the number of lines to show. The pointed line will try to stay in the middle of the displays lines.\n\t* `indent` - indent string prepend to each line, before the line number.",
"repository": {
"type": "git",
"url": "git://github.com/curvedmark/pinpoint.git"
},
"time": {
"1.0.0": "2013-08-19T09:30:51.336Z",
"1.1.0": "2013-09-21T09:56:46.195Z",
"created": "2013-08-19T09:30:46.479Z",
"modified": "2013-09-21T09:56:46.195Z"
},
"versions": {
"1.0.0": {
"_from": ".",
"_id": "[email protected]",
"_npmUser": {
"email": "[email protected]",
"name": "curvedmark"
},
"_npmVersion": "1.3.5",
"author": {
"email": "[email protected]",
"name": "Glen Huang"
},
"bugs": {
"url": "https://github.com/curvedmark/pinpoint/issues"
},
"description": "Display an arrow in a string of code to point to a location",
"devDependencies": {
"coffee-script": "1.x",
"mocha": "1.x"
},
"directories": {
"lib": "lib",
"test": "test"
},
"dist": {
"shasum": "11469494e0b14816e2fbebdd88ae189ac1a97288",
"tarball": "https://registry.npmjs.org/pinpoint/-/pinpoint-1.0.0.tgz"
},
"keywords": [
"editor"
],
"license": "MIT",
"main": "lib/pinpoint.js",
"maintainers": [
{
"email": "[email protected]",
"name": "curvedmark"
}
],
"name": "pinpoint",
"readme": "# pinpoint\n\nAdd line numbers and an arrow to a string of code that points to a specific location.\n\n## Installation\n\n\tnpm install pinpoint\n\n## Example\n\nWith this code:\n\n```javascript\nfunction add(left, right) {\n return left + right;\n}\n\nconsole.log(add(1 + 1));\n```\n\nWhen being asked to point to line 2 and column 2, generates this:\n\n```javascript\n1| function add(left, right) {\n2| return left + right;\n-----^\n3| }\n4|\n5| console.log(add(1 + 1));\n```\n## API\n\n```javascript\nvar pinpoint = require('pinpoint');\n\nvar str = pinpoint(code, options);\n```\n\n* `code` - a string of code\n* `options` - an object literal supports these options:\n\t* `line` - the line number to point to, 1-based.\n\t* `column` - the column number to point to, 1-based.\n\t* `showLines` - the number of lines to show. The pointed line will try to stay in the middle of the displays lines.\n\t* `indent` - indent string prepend to each line, before the line number.",
"readmeFilename": "README.md",
"repository": {
"type": "git",
"url": "git://github.com/curvedmark/pinpoint.git"
},
"scripts": {
"test": "mocha --bail --compilers coffee:coffee-script --ui qunit"
},
"version": "1.0.0"
},
"1.1.0": {
"_from": ".",
"_id": "[email protected]",
"_npmUser": {
"email": "[email protected]",
"name": "curvedmark"
},
"_npmVersion": "1.3.8",
"author": {
"email": "[email protected]",
"name": "Glen Huang"
},
"bugs": {
"url": "https://github.com/curvedmark/pinpoint/issues"
},
"description": "Display an arrow in a string of code to point to a location",
"devDependencies": {
"coffee-script": "1.x",
"mocha": "1.x"
},
"directories": {
"lib": "lib",
"test": "test"
},
"dist": {
"shasum": "0cf7757a6977f1bf7f6a32207b709e377388e874",
"tarball": "https://registry.npmjs.org/pinpoint/-/pinpoint-1.1.0.tgz"
},
"keywords": [
"editor"
],
"license": "MIT",
"main": "lib/pinpoint.js",
"maintainers": [
{
"email": "[email protected]",
"name": "curvedmark"
}
],
"name": "pinpoint",
"readme": "# pinpoint\n\nAdd line numbers and an arrow to a string of code that points to a specific location.\n\n## Installation\n\n\tnpm install pinpoint\n\n## Example\n\nWith this code:\n\n```javascript\nfunction add(left, right) {\n return left + right;\n}\n\nconsole.log(add(1 + 1));\n```\n\nWhen being asked to point to line 2 and column 2, generates this:\n\n```javascript\n1| function add(left, right) {\n2| return left + right;\n-----^\n3| }\n4|\n5| console.log(add(1 + 1));\n```\n## API\n\n```javascript\nvar pinpoint = require('pinpoint');\n\nvar str = pinpoint(code, options);\n```\n\n* `code` - a string of code\n* `options` - an object literal supports these options:\n\t* `line` - the line number to point to, 1-based.\n\t* `column` - the column number to point to, 1-based.\n\t* `showLines` - the number of lines to show. The pointed line will try to stay in the middle of the displays lines.\n\t* `indent` - indent string prepend to each line, before the line number.\n\t* `tabSize` - width of a tab character.",
"readmeFilename": "README.md",
"repository": {
"type": "git",
"url": "git://github.com/curvedmark/pinpoint.git"
},
"scripts": {
"test": "mocha --bail --compilers coffee:coffee-script --ui qunit"
},
"version": "1.1.0"
}
}
}
50 changes: 43 additions & 7 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import * as mockfs from "fs"
jest.mock("node-fetch", () => () =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(JSON.parse(mockfs.readFileSync("src/fixtures/danger-npm-info.json", "utf8"))),
})
)

import yarn, {
_operateOnSingleDiff,
_renderNPMTable,
checkForLockfileDiff,
checkForNewDependencies,
checkForRelease,
checkForTypesInDeps,
getNPMMetadataForDep,
} from "./index"

const provideFixture = (fixture: string) => {
return () => () => Promise.resolve({
ok: true,
json: () => Promise.resolve(JSON.parse(mockfs.readFileSync(`src/fixtures/${fixture}.json`, "utf8"))),
})
};

const fixtureDangerNpmInfo = provideFixture("danger-npm-info");
const fixturePinpointNpmInfo = provideFixture("pinpoint-npm-info");

declare const global: any
beforeEach(() => {
global.warn = jest.fn()
Expand Down Expand Up @@ -121,7 +126,32 @@ describe("checkForLockfileDiff", () => {
})

describe("npm metadata", () => {

jest.mock("node-fetch", () => () =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(JSON.parse(mockfs.readFileSync("src/fixtures/danger-npm-info.json", "utf8"))),
}),
)
it("Shows a bunch of useful text for a new dep", async () => {

// const provideFixture2 = (fixture: string) => {
// return () => () => Promise.resolve({
// ok: true,
// json: () => Promise.resolve(JSON.parse(mockfs.readFileSync(`src/fixtures/${fixture}.json`, "utf8"))),
// })
// };
//
// const fixtureDangerNpmInfo2 = provideFixture2("danger-npm-info");
// jest.mock("node-fetch", fixtureDangerNpmInfo2)

jest.mock("node-fetch", () => () =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(JSON.parse(mockfs.readFileSync("src/fixtures/danger-npm-info.json", "utf8"))),
}),
)

expect.assertions(1)
const npmData = await getNPMMetadataForDep("danger")
expect(_renderNPMTable({ usedInPackageJSONPaths: ["package.json"], npmData: npmData! })).toMatchSnapshot()
Expand Down Expand Up @@ -188,5 +218,11 @@ describe("Feature Flags", () => {
expect(global.message).toHaveBeenCalledTimes(0)
expect(global.warn).toHaveBeenCalledTimes(2) // Called with "Changes were made to package.json, but not […]"
expect(global.fail).toHaveBeenCalledTimes(0)
})
//
// it("looks through versions if license is missing", async () => {
// // jest.mock("node-fetch", fixturePinpointNpmInfo)
// const { getNPMMetadataForDep } = require("./")
// const data = await getNPMMetadataForDep("pinpoint")
// expect(data).toMatchSnapshot()
// })
})
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,14 @@ export const getNPMMetadataForDep = async (
tableDeets.push({ name: "License", message: license })
} else {
// License is important, so always show info
const { versions = {} } = npm
const licenses = Object.keys(versions)
.sort((a, b) => (semver.gte(b, a) ? 1 : 0)) // sort latest versions first
.map(version => versions[version].license) // get the license
.filter(Boolean) // remove falsy values
tableDeets.push({
name: "License",
message: "<b>NO LICENSE FOUND</b>",
message: `<b>${licenses[0] || "NO LICENSE FOUND"}</b>`,
})
}
// Right
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
version "7.0.29"
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.29.tgz#ccfcec5b7135c7caf6c4ffb8c7f33102340d99df"

"@types/semver@^5.3.33":
version "5.3.33"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-5.3.33.tgz#58ebb6c8c48e161e24f8901915e7184900d341f7"

abab@^1.0.0, abab@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/abab/-/abab-1.0.3.tgz#b81de5f7274ec4e756d797cd834f303642724e5d"
Expand Down