Move plugin module to nplugm

This commit is contained in:
Juan Cruz Viotti 2015-01-23 10:23:47 -04:00
parent 67cf0a1b9a
commit af9ae6579c
4 changed files with 4 additions and 197 deletions

View File

@ -2,9 +2,9 @@ _ = require('lodash')
path = require('path')
capitano = require('capitano')
resin = require('resin-sdk')
nplugm = require('nplugm')
actions = require('./actions')
errors = require('./errors/errors')
plugin = require('./plugin/plugin')
capitano.permission 'user', (done) ->
resin.auth.isLoggedIn (isLoggedIn) ->
@ -96,9 +96,9 @@ capitano.command(actions.examples.list)
capitano.command(actions.examples.clone)
capitano.command(actions.examples.info)
for pluginPath in plugin.getPluginsPathsByGlob('resin-plugin-*')
for pluginPath in nplugm.getPluginsPathsByGlob('resin-plugin-*')
try
pluginMeta = plugin.getPluginMeta(pluginPath)
pluginMeta = nplugm.getPluginMeta(pluginPath)
pluginMain = path.join(pluginPath, pluginMeta.main)
pluginCommands = require(pluginMain)

View File

@ -1,44 +0,0 @@
_ = require('lodash')
fs = require('fs')
path = require('path')
yeoman = require('yeoman-environment')
glob = require('glob')
exports.getNpmPaths = ->
return yeoman.createEnv().getNpmPaths()
exports.getPluginsPathsByGlob = (nameGlob) ->
if not nameGlob?
throw new Error('Missing glob')
if not _.isString(nameGlob)
throw new Error('Invalid glob')
npmPaths = exports.getNpmPaths()
result = []
for npmPath in npmPaths
foundModules = glob.sync(nameGlob, cwd: npmPath)
foundModules = _.map foundModules, (foundModule) ->
return path.join(npmPath, foundModule)
result = result.concat(foundModules)
return result
exports.getPluginMeta = (pluginPath) ->
pluginPackageJSONPath = path.join(pluginPath, 'package.json')
if not fs.existsSync(pluginPackageJSONPath)
throw new Error("Missing or invalid plugin: #{pluginPath}")
pluginPackageJSON = fs.readFileSync pluginPackageJSONPath,
encoding: 'utf8'
try
meta = JSON.parse(pluginPackageJSON)
catch
throw new Error("Invalid package.json: #{pluginPackageJSONPath}")
return meta

View File

@ -1,150 +0,0 @@
os = require('os')
_ = require('lodash')
path = require('path')
sinon = require('sinon')
chai = require('chai')
chai.use(require('sinon-chai'))
glob = require('glob')
fs = require('fs')
fsPlus = require('fs-plus')
mockFs = require('mock-fs')
expect = chai.expect
plugin = require('./plugin')
describe 'Plugin:', ->
describe '#getPluginsPathsByGlob()', ->
describe 'given no glob', ->
it 'should throw an error', ->
expect ->
plugin.getPluginsPathsByGlob()
.to.throw('Missing glob')
describe 'given an invalid glob', ->
it 'should throw an error', ->
expect ->
plugin.getPluginsPathsByGlob([ 'glob' ])
.to.throw('Invalid glob')
describe 'given a glob that does not matches anything', ->
beforeEach ->
@globSyncStub = sinon.stub(glob, 'sync')
@globSyncStub.returns []
@plugins = plugin.getPluginsPathsByGlob('myGlob*')
afterEach ->
@globSyncStub.restore()
it 'should return an empty array', ->
expect(@plugins).to.deep.equal([])
describe 'given a glob that matches packages', ->
beforeEach ->
@getNpmPathsStub = sinon.stub(plugin, 'getNpmPaths')
if os.platform() is 'win32'
@getNpmPathsStub.returns([ 'C:\\node_modules' ])
else
@getNpmPathsStub.returns([ '/usr/lib/node_modules' ])
@globSyncStub = sinon.stub(glob, 'sync')
@globSyncStub.returns [
'one'
'two'
'three'
]
@plugins = plugin.getPluginsPathsByGlob('myGlob*')
afterEach ->
@getNpmPathsStub.restore()
@globSyncStub.restore()
it 'should return an array', ->
expect(@plugins).to.be.an.instanceof(Array)
it 'should have the proper length', ->
expect(@plugins).to.have.length(3)
it 'should contain absolute paths', ->
for pluginPath in @plugins
expect(fsPlus.isAbsolute(pluginPath)).to.be.true
it 'should return the appropriate paths', ->
if os.platform() is 'win32'
expect(@plugins[0]).to.equal('C:\\node_modules\\one')
expect(@plugins[1]).to.equal('C:\\node_modules\\two')
expect(@plugins[2]).to.equal('C:\\node_modules\\three')
else
expect(@plugins[0]).to.equal('/usr/lib/node_modules/one')
expect(@plugins[1]).to.equal('/usr/lib/node_modules/two')
expect(@plugins[2]).to.equal('/usr/lib/node_modules/three')
describe '#getNpmPaths()', ->
beforeEach ->
@npmPaths = plugin.getNpmPaths()
it 'should return an array', ->
expect(@npmPaths).to.be.an.instanceof(Array)
it 'should return at least one path', ->
expect(@npmPaths.length > 1).to.be.true
it 'should contain absolute paths', ->
for npmPath in @npmPaths
expect(fsPlus.isAbsolute(npmPath)).to.be.true
describe '#getPluginMeta()', ->
describe 'given an invalid plugin', ->
beforeEach ->
mockFs
'/hello/world':
'package.json': 'Invalid package.json'
afterEach ->
mockFs.restore()
it 'should throw an error', ->
pluginPath = path.join('/', 'hello', 'world')
pluginPathPackageJSON = path.join(pluginPath, 'package.json')
expect ->
plugin.getPluginMeta(pluginPath)
.to.throw("Invalid package.json: #{pluginPathPackageJSON}")
describe 'given a plugin that exists', ->
beforeEach ->
mockFs
'/hello/world':
'package.json': JSON.stringify({ name: 'myPlugin' })
afterEach ->
mockFs.restore()
it 'should return the parsed object', ->
result = plugin.getPluginMeta('/hello/world')
expect(result).to.deep.equal
name: 'myPlugin'
describe 'given a plugin that does not exist', ->
beforeEach ->
@fsExistsSyncStub = sinon.stub(fs, 'existsSync')
@fsExistsSyncStub.returns(false)
afterEach ->
@fsExistsSyncStub.restore()
it 'should throw an error', ->
expect ->
plugin.getPluginMeta('/hello/world')
.to.throw('Missing or invalid plugin: /hello/world')

View File

@ -60,6 +60,7 @@
"progress-stream": "^0.5.0",
"resin-cli-visuals": "0.0.1",
"resin-sdk": "git+https://git@github.com/resin-io/resin-sdk.git",
"nplugm": "git+https://git@github.com/resin-io/nplugm.git",
"underscore.string": "~2.4.0",
"yeoman-environment": "^1.2.0"
}