Skip to content
This repository has been archived by the owner on Nov 16, 2019. It is now read-only.

Commit

Permalink
Merge 9615089 into e14bbc6
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Aug 25, 2016
2 parents e14bbc6 + 9615089 commit 98987e6
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 28 deletions.
32 changes: 9 additions & 23 deletions bin/npm-explicit-installs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env node

var chalk = require('chalk')
var fs = require('fs')
var path = require('path')
var inquirer = require('inquirer')

require('yargs')
Expand All @@ -24,27 +22,19 @@ require('yargs')
})
})
.command('delete', 'delete packages from the home page', function () {
var packages = require('../packages')
var logos = require('../logos')

var npmExplicitInstalls = require('../')
inquirer.prompt({
name: 'package',
message: 'remove package from homepage',
type: 'list',
choices: packages
}, function (answer) {
packages.splice(packages.indexOf(answer.package), 1)
fs.writeFileSync(path.resolve(__dirname, '../packages.json'), JSON.stringify(packages, null, 2), 'utf-8')
if (logos[answer.package]) {
delete logos[answer.package]
fs.writeFileSync(path.resolve(__dirname, '../logos.json'), JSON.stringify(logos, null, 2), 'utf-8')
}
choices: npmExplicitInstalls.getPackagesSync()
}).then(function (answer) {
npmExplicitInstalls.delete(answer.package)
npmExplicitInstalls.client.end(true)
})
})
.command('add', 'add a new package to the home page', function () {
var packages = require('../packages')
var logos = require('../logos')

var npmExplicitInstalls = require('../')
inquirer.prompt([
{
name: 'package',
Expand All @@ -58,13 +48,9 @@ require('yargs')
name: 'logo',
message: 'url of icon to use for package (optional)'
}
], function (answer) {
if (answer.logo) {
logos[answer.package] = answer.logo
fs.writeFileSync(path.resolve(__dirname, '../logos.json'), JSON.stringify(logos, null, 2), 'utf-8')
}
packages.push(answer.package)
fs.writeFileSync(path.resolve(__dirname, '../packages.json'), JSON.stringify(packages, null, 2), 'utf-8')
]).then(function (answer) {
npmExplicitInstalls.add(answer.package, answer.logo)
npmExplicitInstalls.client.end(true)
})
})
.command('bust-cache', 'clear the cache of home page packages', function () {
Expand Down
58 changes: 56 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
var fs = require('fs')
var path = require('path')
var Promise = require('bluebird')
var redis = require('redis')
var map = require('async').map
var xor = require('lodash.xor')
var logger = require('bole')('npm-explicit-installs')

var configDirectory // from whence should we load packages.json and logos.json?

function ExplicitInstalls (cb) {
configDirectory = process.env.NEI_CONFIG_DIRECTORY || __dirname

return checkCache()
.then(function (pkgs) {
return ExplicitInstalls.getPackages()
Expand All @@ -31,7 +36,7 @@ function ExplicitInstalls (cb) {

ExplicitInstalls.getPackages = function () {
return new Promise(function (resolve, reject) {
ExplicitInstalls.fs.readFile(path.resolve(__dirname, './packages.json'), 'utf-8', function (err, packages) {
ExplicitInstalls.fs.readFile(path.resolve(configDirectory, './packages.json'), 'utf-8', function (err, packages) {
// error occurred fetching packages from disk.
if (err) {
logger.error('failed to read packages from disk:', err.message)
Expand All @@ -53,7 +58,7 @@ ExplicitInstalls.getPackages = function () {

ExplicitInstalls.getLogos = function () {
return new Promise(function (resolve, reject) {
ExplicitInstalls.fs.readFile(path.resolve(__dirname, './logos.json'), 'utf-8', function (err, logos) {
ExplicitInstalls.fs.readFile(path.resolve(configDirectory, './logos.json'), 'utf-8', function (err, logos) {
// error occurred fetching logos from disk.
if (err) {
logger.error('failed to read logos from disk:', err.message)
Expand Down Expand Up @@ -186,6 +191,55 @@ ExplicitInstalls.bustCache = function (cb) {
.nodeify(cb)
}

ExplicitInstalls.add = function (pkg, logo) {
configDirectory = process.env.NEI_CONFIG_DIRECTORY || __dirname
var logoPath = path.resolve(configDirectory, './logos.json')
var logos = tryLoadJson(logoPath, {})
var pkgPath = path.resolve(configDirectory, './packages.json')
var packages = tryLoadJson(pkgPath, [])

if (logo) {
logos[pkg] = logo
fs.writeFileSync(logoPath, JSON.stringify(logos, null, 2), 'utf-8')
}
packages.push(pkg)
fs.writeFileSync(pkgPath, JSON.stringify(packages, null, 2), 'utf-8')
}

ExplicitInstalls.delete = function (pkg) {
configDirectory = process.env.NEI_CONFIG_DIRECTORY || __dirname
var logoPath = path.resolve(configDirectory, './logos.json')
var logos = tryLoadJson(logoPath, {})
var pkgPath = path.resolve(configDirectory, './packages.json')
var packages = tryLoadJson(pkgPath, [])

packages.splice(packages.indexOf(pkg), 1)
fs.writeFileSync(pkgPath, JSON.stringify(packages, null, 2), 'utf-8')
if (logos[pkg]) {
delete logos[pkg]
fs.writeFileSync(logoPath, JSON.stringify(logos, null, 2), 'utf-8')
}
}

ExplicitInstalls.getPackagesSync = function () {
configDirectory = process.env.NEI_CONFIG_DIRECTORY || __dirname
var pkgPath = path.resolve(configDirectory, './packages.json')
var packages = tryLoadJson(pkgPath, [])
return packages
}

function tryLoadJson (path, defaultValue) {
var value
try {
value = JSON.parse(
fs.readFileSync(path)
)
} catch (_) {
value = defaultValue
}
return value
}

/*
Make pkgs match the format expected by newww:
{{name}}
Expand Down
2 changes: 1 addition & 1 deletion logos.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"pm2": "https://cldup.com/PKpktytKH9.png",
"statsd": "https://cldup.com/3s3hGntQAy.svg",
"yo": "https://cldup.com/P3MQgWdDyG.png"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"coveralls": "^2.11.12",
"mocha": "^3.0.2",
"nyc": "^8.1.0",
"rimraf": "^2.5.4",
"standard": "^8.0.0",
"standard-version": "^2.4.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
"coffee-script",
"statsd",
"yo"
]
]
Empty file added test/fixtures/add-test/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/logos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"browserify": "https://logo.example.com"
}
4 changes: 4 additions & 0 deletions test/fixtures/packages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"browserify",
"grunt-cli"
]
93 changes: 92 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* global describe it after beforeEach, before */
/* global describe it after, beforeEach, before */

var npmExplicitInstalls = require('../')
var clearRequire = require('clear-require')
var expect = require('chai').expect
var fs = require('fs')
var redis = require('redis')
var rimraf = require('rimraf')

require('chai').should()
console.error = function () {}
Expand Down Expand Up @@ -210,6 +211,78 @@ describe('npm-explicit-installs', function () {
})
})

describe('add', function () {
var logosPath = './test/fixtures/add-test/logos.json'
var pkgsPath = './test/fixtures/add-test/packages.json'
beforeEach(function () {
rimraf.sync(logosPath)
rimraf.sync(pkgsPath)
})

it('adds a new package and logo', function () {
process.env.NEI_CONFIG_DIRECTORY = './test/fixtures/add-test'
npmExplicitInstalls.add('foo', 'foo.logo')
npmExplicitInstalls.add('bar', 'bar.logo')
delete process.env.NEI_CONFIG_DIRECTORY

expect(
JSON.parse(fs.readFileSync(logosPath))
).to.deep.equal({
foo: 'foo.logo',
bar: 'bar.logo'
})

expect(
JSON.parse(fs.readFileSync(pkgsPath))
).to.deep.equal(['foo', 'bar'])
})

after(function () {
rimraf.sync(logosPath)
rimraf.sync(pkgsPath)
})
})

describe('delete', function () {
var logosPath = './test/fixtures/add-test/logos.json'
var pkgsPath = './test/fixtures/add-test/packages.json'
beforeEach(function () {
rimraf.sync(logosPath)
rimraf.sync(pkgsPath)
})

it('deletes from packages.json and logos.json', function () {
process.env.NEI_CONFIG_DIRECTORY = './test/fixtures/add-test'
npmExplicitInstalls.add('foo', 'foo.logo')
npmExplicitInstalls.add('bar', 'bar.logo')
npmExplicitInstalls.delete('foo')
delete process.env.NEI_CONFIG_DIRECTORY

expect(
JSON.parse(fs.readFileSync(logosPath))
).to.deep.equal({
bar: 'bar.logo'
})

expect(
JSON.parse(fs.readFileSync(pkgsPath))
).to.deep.equal(['bar'])
})

after(function () {
rimraf.sync(logosPath)
rimraf.sync(pkgsPath)
})
})

describe('getPackagesSync', function () {
it('deletes from packages.json and logos.json', function () {
var packages = npmExplicitInstalls.getPackagesSync()
packages.should.include('browserify')
packages.should.include('grunt-cli')
})
})

describe('logos', function () {
it('should not use logo from package.json, unless we are npmo', function (done) {
npmExplicitInstalls(function (err, pkgs) {
Expand Down Expand Up @@ -254,6 +327,24 @@ describe('npm-explicit-installs', function () {
})
})

it('allows an alternate config location to be specified', function (done) {
process.env.NEI_CONFIG_DIRECTORY = './test/fixtures'
npmExplicitInstalls(function (err, pkgs) {
delete process.env.NEI_CONFIG_DIRECTORY

expect(err).to.equal(null)
var browserify = pkgs[0]
var gruntCli = pkgs[1]

pkgs.length.should.equal(2)
gruntCli.name.should.equal('grunt-cli')
gruntCli.version.should.equal('0.1.13')
browserify.logo.should.equal('https://logo.example.com')
expect(gruntCli.logo).to.equal(undefined)
return done()
})
})

after(function () { npmExplicitInstalls.client.end(true) })
})

Expand Down

0 comments on commit 98987e6

Please sign in to comment.