diff --git a/index.js b/index.js index 9ccb713..7eb538e 100644 --- a/index.js +++ b/index.js @@ -3,44 +3,42 @@ const path = require('path'); const platformNames = { - win32 : 'win', - linux : 'lin', + win32 : 'windows', + linux : 'linux', darwin : 'osx', }; const platformName = platformNames[process.platform]; +const isWindows = process.platform === 'win32'; if ( ! platformName ) { console.log(`Error: UNKNOWN PLATFORM "${process.platform}"`); } -const isWindows = process.platform === 'win32'; - const rootPath = __dirname.replace(/\\/g, '/'); + const napiInclude = require('node-addon-api').include.replace(/\\/g, '/'); const thisInclude = `${rootPath}/include`; +const includePath = `${napiInclude} ${thisInclude}`; + const paths = dir => { dir = dir.replace(/\\/g, '/'); - const binPath = `${dir}/${platformName}`; + const bin = `${dir}/bin-${platformName}`; + const include = `${dir}/include`; if (isWindows) { - process.env.path = `${binPath};${process.env.path ? `${process.env.path}` : ''}`; + process.env.path = `${bin};${process.env.path ? `${process.env.path}` : ''}`; } - const includePath = `${dir}/include`; - return { bin, include }; }; -const includePath = `${napiInclude} ${thisInclude}`; -const binPath = currentDir; - const mkdirPath = isWindows ? `${rootPath}/bat/mkdir.bat` : 'mkdir'; const rmPath = isWindows ? `${rootPath}/bat/rm.bat` : 'rm'; const cpPath = isWindows ? `${rootPath}/bat/cp.bat` : 'cp'; @@ -50,11 +48,12 @@ module.exports = { paths, - platform: platformName, - include: includePath, + bin : `bin-${platformName}`, + platform : platformName, + include : includePath, - mkdir: mkdirPath, - rm: rmPath, - cp: cpPath, + mkdir : mkdirPath, + rm : rmPath, + cp : cpPath, }; diff --git a/test/test.js b/test/test.js index 84545a5..48f1fe0 100644 --- a/test/test.js +++ b/test/test.js @@ -1,19 +1,12 @@ 'use strict'; -const path = require('path'); -const fs = require('fs'); - const { expect } = require('chai'); const { stub, spy } = require('sinon'); const tools = require('addon-tools-raub'); -const toolsDir = path.dirname(require.resolve('addon-tools-raub')).replace(/\\/g, '/'); -const allMethods = ['paths', 'bin', 'root', 'include', 'mkdir', 'rm', 'cp']; -const ownMethods = allMethods.slice(1); -const cmdMethods = allMethods.slice(3); -const pathsMethods = ['bin', 'rem', 'include']; +const PROPS = ['bin', 'platform', 'include', 'mkdir', 'rm', 'cp']; describe('Tools', () => { @@ -27,101 +20,33 @@ describe('Tools', () => { afterEach(() => stubbed.restore()); - describe('Own Methods', () => { + describe('Properties', () => { - ownMethods.forEach( - m => it(`#${m}() is available`, () => { - expect(tools).to.respondTo(m); - }) - ); - - - ownMethods.forEach( - m => it(`#${m}() writes stdout`, () => { - tools[m](); - expect(log.getCall(0), 'called').to.exist; - expect(log.getCall(0).args[0], 'has args').to.exist; - expect(log.getCall(0).args[0], 'writes string').to.be.a('string'); - }) - ); - - - it('#bin() is correct', () => { - tools.bin(); - expect(log.getCall(0).args[0]).to.equal(path.basename(tools.paths(__dirname).binPath)); - }); - - - it('#root() is correct', () => { - tools.root(); - expect(log.getCall(0).args[0]).to.equal(toolsDir); - }); - - - it('#include() is correct', async () => { - - tools.include(); - const dirs = log.getCall(0).args[0].split(' '); - - const stats = await Promise.all(dirs.map(dir => new Promise( - res => fs.stat( - dir, - (err, stat) => err ? res(false) : res(stat.isDirectory()) - ) - ))); - - dirs.forEach((dir, i) => expect(stats[i], dir).to.be.true); - - }); - - }); - - - describe('Cmd Methods', () => { - - cmdMethods.forEach( - m => it(`#${m}() is available`, () => { - expect(tools).to.respondTo(m); - }) - ); - - - cmdMethods.forEach( - m => it(`#${m}() writes stdout`, () => { - tools[m](); - expect(log.getCall(0), 'called').to.exist; - expect(log.getCall(0).args[0], 'has args').to.exist; - expect(log.getCall(0).args[0], 'writes string').to.be.a('string'); + PROPS.forEach( + m => it(`#${m} is a string`, () => { + expect(tools[m]).to.be.a('string'); }) ); }); - - describe('Paths', () => { + describe('#paths()', () => { - it('#paths() returns an object', () => { + it('is a function', () => { + expect(tools.paths).to.be.a('function'); + }); + + it('returns an object', () => { expect(tools.paths(__dirname)).to.be.an('object'); }); + it('has "include" string', () => { + expect(tools.paths(__dirname).include).to.be.a('string'); + }); - pathsMethods.forEach( - m => it(`#${m}() is available`, () => { - const paths = tools.paths(__dirname); - expect(paths).to.respondTo(m); - }) - ); - - - pathsMethods.forEach( - m => it(`#${m}() writes stdout`, () => { - const paths = tools.paths(__dirname); - paths[m](); - expect(log.getCall(0), 'called').to.exist; - expect(log.getCall(0).args[0], 'has args').to.exist; - expect(log.getCall(0).args[0], 'writes string').to.be.a('string'); - }) - ); + it('has "bin" string', () => { + expect(tools.paths(__dirname).include).to.be.a('string'); + }); });