From ca1ed841983ee84dd8063454603d67723ab5699b Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:44:29 +0100 Subject: [PATCH 1/8] add adapter-core --- mystrom-wifi-switch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mystrom-wifi-switch.js b/mystrom-wifi-switch.js index 6cfc8d8..51ca593 100644 --- a/mystrom-wifi-switch.js +++ b/mystrom-wifi-switch.js @@ -68,7 +68,7 @@ var lastValue = 0; // you have to require the utils module and call adapter function -var utils = require(__dirname + '/lib/utils'); // Get common adapter utils +var utils = require('@iobroker/adapter-core'); // Get common adapter utils // you have to call the adapter function and pass a options object // name has to be set and has to be equal to adapters folder name and main file name excluding extension From 6a95dba5e5b083074ad3e8ddd4461306f5595d6a Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:44:29 +0100 Subject: [PATCH 2/8] remove utils.js --- lib/utils.js | 83 ---------------------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 lib/utils.js diff --git a/lib/utils.js b/lib/utils.js deleted file mode 100644 index c8a0eb7..0000000 --- a/lib/utils.js +++ /dev/null @@ -1,83 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); - -let controllerDir; -let appName; - -/** - * returns application name - * - * The name of the application can be different and this function finds it out. - * - * @returns {string} - */ - function getAppName() { - const parts = __dirname.replace(/\\/g, '/').split('/'); - return parts[parts.length - 2].split('.')[0]; -} - -/** - * looks for js-controller home folder - * - * @param {boolean} isInstall - * @returns {string} - */ -function getControllerDir(isInstall) { - // Find the js-controller location - const possibilities = [ - 'iobroker.js-controller', - 'ioBroker.js-controller', - ]; - /** @type {string} */ - let controllerPath; - for (const pkg of possibilities) { - try { - const possiblePath = require.resolve(pkg); - if (fs.existsSync(possiblePath)) { - controllerPath = possiblePath; - break; - } - } catch (e) { /* not found */ } - } - if (controllerPath == null) { - if (!isInstall) { - console.log('Cannot find js-controller'); - process.exit(10); - } else { - process.exit(); - } - } - // we found the controller - return path.dirname(controllerPath); -} - -/** - * reads controller base settings - * - * @alias getConfig - * @returns {object} - */ - function getConfig() { - let configPath; - if (fs.existsSync( - configPath = path.join(controllerDir, 'conf', appName + '.json') - )) { - return JSON.parse(fs.readFileSync(configPath, 'utf8')); - } else if (fs.existsSync( - configPath = path.join(controllerDir, 'conf', + appName.toLowerCase() + '.json') - )) { - return JSON.parse(fs.readFileSync(configPath, 'utf8')); - } else { - throw new Error('Cannot find ' + controllerDir + '/conf/' + appName + '.json'); - } -} -appName = getAppName(); -controllerDir = getControllerDir(typeof process !== 'undefined' && process.argv && process.argv.indexOf('--install') !== -1); -const adapter = require(path.join(controllerDir, 'lib/adapter.js')); - -exports.controllerDir = controllerDir; -exports.getConfig = getConfig; -exports.Adapter = adapter; -exports.appName = appName; From c3755fa9a5578966227db154b5c86fa994811b20 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:47:26 +0100 Subject: [PATCH 3/8] update package.json --- package.json | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 2bc6071..26905ae 100644 --- a/package.json +++ b/package.json @@ -28,20 +28,21 @@ "request": "^2.81.0" }, "devDependencies": { - "grunt": "^0.4.5", - "grunt-replace": "^0.9.3", - "grunt-contrib-jshint": "^0.11.2", - "grunt-jscs": "^2.0.0", - "grunt-http": "^1.6.0", - "mocha": "^2.3.4", - "chai": "^3.4.1" + "grunt": "^1.0.1", + "grunt-replace": "^1.0.1", + "grunt-contrib-jshint": "^1.1.0", + "grunt-jscs": "^3.0.1", + "grunt-http": "^2.2.0", + "mocha": "^6.0.2", + "chai": "^4.2.0", + "@iobroker/adapter-core": "^1.0.3" }, "main": "mystrom-wifi-switch.js", "scripts": { - "test": "node node_modules/mocha/bin/mocha" + "test": "node node_modules/mocha/bin/mocha --exit" }, "bugs": { "url": "https://github.com/rgwch/ioBroker.mystrom-wifi-switch/issues" }, "readmeFilename": "README.md" -} +} \ No newline at end of file From bd218e0a37f77271640f9ed919355184f888628d Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:55:22 +0100 Subject: [PATCH 4/8] update travis.yml --- .travis.yml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index d4de452..0008cbf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,23 @@ os: - linux - osx + - windows language: node_js node_js: - - '4' - '6' - '8' + - '10' before_script: - - npm install winston@2.3.0 + - export NPMVERSION=$(echo "$($(which npm) -v)"|cut -c1) + - 'if [[ $NPMVERSION == 5 ]]; then npm install -g npm@5; fi' + - npm -v + - npm install winston@3.2.1 - 'npm install https://github.com/ioBroker/ioBroker.js-controller/tarball/master --production' env: - - CXX=g++-4.8 + - CXX=g++-4.9 addons: apt: sources: - ubuntu-toolchain-r-test packages: - - g++-4.8 + - g++-4.9 From 4d8fe72ac4165cb40b2b382757195400ce77cd0b Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:55:22 +0100 Subject: [PATCH 5/8] update appveyor.yml --- appveyor.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 293bd2e..98ae8e8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,17 +1,20 @@ version: 'test-{build}' environment: matrix: - - nodejs_version: '4' - nodejs_version: '6' - nodejs_version: '8' + - nodejs_version: '10' platform: - x86 - x64 clone_folder: 'c:\projects\%APPVEYOR_PROJECT_NAME%' install: - ps: 'Install-Product node $env:nodejs_version $env:platform' + - ps: '$NpmVersion = (npm -v).Substring(0,1)' + - ps: 'if($NpmVersion -eq 5) { npm install -g npm@5 }' + - ps: npm --version - npm install - - npm install winston@2.3.0 + - npm install winston@3.2.1 - 'npm install https://github.com/ioBroker/ioBroker.js-controller/tarball/master --production' test_script: - echo %cd% From d4899202dd61f9c2f36cc0602e1c3344aba751b4 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 22:59:07 +0100 Subject: [PATCH 6/8] update testing setup.js --- test/lib/setup.js | 1419 +++++++++++++++++++++++---------------------- 1 file changed, 729 insertions(+), 690 deletions(-) diff --git a/test/lib/setup.js b/test/lib/setup.js index 55f0d86..28e5e8b 100644 --- a/test/lib/setup.js +++ b/test/lib/setup.js @@ -1,690 +1,729 @@ -/* jshint -W097 */// jshint strict:false -/*jslint node: true */ -// check if tmp directory exists -var fs = require('fs'); -var path = require('path'); -var child_process = require('child_process'); -var rootDir = path.normalize(__dirname + '/../../'); -var pkg = require(rootDir + 'package.json'); -var debug = typeof v8debug === 'object'; - -var adapterName = path.normalize(rootDir).replace(/\\/g, '/').split('/'); -adapterName = adapterName[adapterName.length - 2]; - -function getAppName() { - var parts = __dirname.replace(/\\/g, '/').split('/'); - return parts[parts.length - 3].split('.')[0]; -} - -var appName = getAppName().toLowerCase(); - -var objects; -var states; - -var pid = null; - -function copyFileSync(source, target) { - - var targetFile = target; - - //if target is a directory a new file with the same name will be created - if (fs.existsSync(target)) { - if ( fs.lstatSync( target ).isDirectory() ) { - targetFile = path.join(target, path.basename(source)); - } - } - - try { - fs.writeFileSync(targetFile, fs.readFileSync(source)); - } - catch (err) { - console.log("file copy error: " +source +" -> " + targetFile + " (error ignored)"); - } -} - -function copyFolderRecursiveSync(source, target, ignore) { - var files = []; - - var base = path.basename(source); - if (base === adapterName) { - base = pkg.name; - } - //check if folder needs to be created or integrated - var targetFolder = path.join(target, base); - if (!fs.existsSync(targetFolder)) { - fs.mkdirSync(targetFolder); - } - - //copy - if (fs.lstatSync(source).isDirectory()) { - files = fs.readdirSync(source); - files.forEach(function (file) { - if (ignore && ignore.indexOf(file) !== -1) { - return; - } - - var curSource = path.join(source, file); - if (fs.lstatSync(curSource).isDirectory()) { - // ignore grunt files - if (file.indexOf('grunt') !== -1) return; - if (file === 'chai') return; - if (file === 'mocha') return; - copyFolderRecursiveSync(curSource, targetFolder, ignore); - } else { - copyFileSync(curSource, targetFolder); - } - }); - } -} - -if (!fs.existsSync(rootDir + 'tmp')) { - fs.mkdirSync(rootDir + 'tmp'); -} - -function storeOriginalFiles() { - console.log('Store original files...'); - var dataDir = rootDir + 'tmp/' + appName + '-data/'; - - var f = fs.readFileSync(dataDir + 'objects.json'); - var objects = JSON.parse(f.toString()); - if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) { - objects['system.adapter.admin.0'].common.enabled = false; - } - if (objects['system.adapter.admin.1'] && objects['system.adapter.admin.1'].common) { - objects['system.adapter.admin.1'].common.enabled = false; - } - - fs.writeFileSync(dataDir + 'objects.json.original', JSON.stringify(objects)); - try { - f = fs.readFileSync(dataDir + 'states.json'); - fs.writeFileSync(dataDir + 'states.json.original', f); - } - catch (err) { - console.log('no states.json found - ignore'); - } -} - -function restoreOriginalFiles() { - console.log('restoreOriginalFiles...'); - var dataDir = rootDir + 'tmp/' + appName + '-data/'; - - var f = fs.readFileSync(dataDir + 'objects.json.original'); - fs.writeFileSync(dataDir + 'objects.json', f); - try { - f = fs.readFileSync(dataDir + 'states.json.original'); - fs.writeFileSync(dataDir + 'states.json', f); - } - catch (err) { - console.log('no states.json.original found - ignore'); - } - -} - -function checkIsAdapterInstalled(cb, counter, customName) { - customName = customName || pkg.name.split('.').pop(); - counter = counter || 0; - var dataDir = rootDir + 'tmp/' + appName + '-data/'; - console.log('checkIsAdapterInstalled...'); - - try { - var f = fs.readFileSync(dataDir + 'objects.json'); - var objects = JSON.parse(f.toString()); - if (objects['system.adapter.' + customName + '.0']) { - console.log('checkIsAdapterInstalled: ready!'); - setTimeout(function () { - if (cb) cb(); - }, 100); - return; - } else { - console.warn('checkIsAdapterInstalled: still not ready'); - } - } catch (err) { - - } - - if (counter > 20) { - console.error('checkIsAdapterInstalled: Cannot install!'); - if (cb) cb('Cannot install'); - } else { - console.log('checkIsAdapterInstalled: wait...'); - setTimeout(function() { - checkIsAdapterInstalled(cb, counter + 1); - }, 1000); - } -} - -function checkIsControllerInstalled(cb, counter) { - counter = counter || 0; - var dataDir = rootDir + 'tmp/' + appName + '-data/'; - - console.log('checkIsControllerInstalled...'); - try { - var f = fs.readFileSync(dataDir + 'objects.json'); - var objects = JSON.parse(f.toString()); - if (objects['system.adapter.admin.0']) { - console.log('checkIsControllerInstalled: installed!'); - setTimeout(function () { - if (cb) cb(); - }, 100); - return; - } - } catch (err) { - - } - - if (counter > 20) { - console.log('checkIsControllerInstalled: Cannot install!'); - if (cb) cb('Cannot install'); - } else { - console.log('checkIsControllerInstalled: wait...'); - setTimeout(function() { - checkIsControllerInstalled(cb, counter + 1); - }, 1000); - } -} - -function installAdapter(customName, cb) { - if (typeof customName === 'function') { - cb = customName; - customName = null; - } - customName = customName || pkg.name.split('.').pop(); - console.log('Install adapter...'); - var startFile = 'node_modules/' + appName + '.js-controller/' + appName + '.js'; - // make first install - if (debug) { - child_process.execSync('node ' + startFile + ' add ' + customName + ' --enabled false', { - cwd: rootDir + 'tmp', - stdio: [0, 1, 2] - }); - checkIsAdapterInstalled(function (error) { - if (error) console.error(error); - console.log('Adapter installed.'); - if (cb) cb(); - }); - } else { - // add controller - var _pid = child_process.fork(startFile, ['add', customName, '--enabled', 'false'], { - cwd: rootDir + 'tmp', - stdio: [0, 1, 2, 'ipc'] - }); - - waitForEnd(_pid, function () { - checkIsAdapterInstalled(function (error) { - if (error) console.error(error); - console.log('Adapter installed.'); - if (cb) cb(); - }); - }); - } -} - -function waitForEnd(_pid, cb) { - if (!_pid) { - cb(-1, -1); - return; - } - _pid.on('exit', function (code, signal) { - if (_pid) { - _pid = null; - cb(code, signal); - } - }); - _pid.on('close', function (code, signal) { - if (_pid) { - _pid = null; - cb(code, signal); - } - }); -} - -function installJsController(cb) { - console.log('installJsController...'); - if (!fs.existsSync(rootDir + 'tmp/node_modules/' + appName + '.js-controller') || - !fs.existsSync(rootDir + 'tmp/' + appName + '-data')) { - // try to detect appName.js-controller in node_modules/appName.js-controller - // travis CI installs js-controller into node_modules - if (fs.existsSync(rootDir + 'node_modules/' + appName + '.js-controller')) { - console.log('installJsController: no js-controller => copy it from "' + rootDir + 'node_modules/' + appName + '.js-controller"'); - // copy all - // stop controller - console.log('Stop controller if running...'); - var _pid; - if (debug) { - // start controller - _pid = child_process.exec('node ' + appName + '.js stop', { - cwd: rootDir + 'node_modules/' + appName + '.js-controller', - stdio: [0, 1, 2] - }); - } else { - _pid = child_process.fork(appName + '.js', ['stop'], { - cwd: rootDir + 'node_modules/' + appName + '.js-controller', - stdio: [0, 1, 2, 'ipc'] - }); - } - - waitForEnd(_pid, function () { - // copy all files into - if (!fs.existsSync(rootDir + 'tmp')) fs.mkdirSync(rootDir + 'tmp'); - if (!fs.existsSync(rootDir + 'tmp/node_modules')) fs.mkdirSync(rootDir + 'tmp/node_modules'); - - if (!fs.existsSync(rootDir + 'tmp/node_modules/' + appName + '.js-controller')){ - console.log('Copy js-controller...'); - copyFolderRecursiveSync(rootDir + 'node_modules/' + appName + '.js-controller', rootDir + 'tmp/node_modules/'); - } - - console.log('Setup js-controller...'); - var __pid; - if (debug) { - // start controller - _pid = child_process.exec('node ' + appName + '.js setup first --console', { - cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller', - stdio: [0, 1, 2] - }); - } else { - __pid = child_process.fork(appName + '.js', ['setup', 'first', '--console'], { - cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller', - stdio: [0, 1, 2, 'ipc'] - }); - } - waitForEnd(__pid, function () { - checkIsControllerInstalled(function () { - // change ports for object and state DBs - var config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json'); - config.objects.port = 19001; - config.states.port = 19000; - fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2)); - console.log('Setup finished.'); - - copyAdapterToController(); - - installAdapter(function () { - storeOriginalFiles(); - if (cb) cb(true); - }); - }); - }); - }); - } else { - // check if port 9000 is free, else admin adapter will be added to running instance - var client = new require('net').Socket(); - client.connect(9000, '127.0.0.1', function() { - console.error('Cannot initiate fisrt run of test, because one instance of application is running on this PC. Stop it and repeat.'); - process.exit(0); - }); - - setTimeout(function () { - client.destroy(); - if (!fs.existsSync(rootDir + 'tmp/node_modules/' + appName + '.js-controller')) { - console.log('installJsController: no js-controller => install from git'); - - child_process.execSync('npm install https://github.com/' + appName + '/' + appName + '.js-controller/tarball/master --prefix ./ --production', { - cwd: rootDir + 'tmp/', - stdio: [0, 1, 2] - }); - } else { - console.log('Setup js-controller...'); - var __pid; - if (debug) { - // start controller - child_process.exec('node ' + appName + '.js setup first', { - cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller', - stdio: [0, 1, 2] - }); - } else { - child_process.fork(appName + '.js', ['setup', 'first'], { - cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller', - stdio: [0, 1, 2, 'ipc'] - }); - } - } - - // let npm install admin and run setup - checkIsControllerInstalled(function () { - var _pid; - - if (fs.existsSync(rootDir + 'node_modules/' + appName + '.js-controller/' + appName + '.js')) { - _pid = child_process.fork(appName + '.js', ['stop'], { - cwd: rootDir + 'node_modules/' + appName + '.js-controller', - stdio: [0, 1, 2, 'ipc'] - }); - } - - waitForEnd(_pid, function () { - // change ports for object and state DBs - var config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json'); - config.objects.port = 19001; - config.states.port = 19000; - fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2)); - - copyAdapterToController(); - - installAdapter(function () { - storeOriginalFiles(); - if (cb) cb(true); - }); - }); - }); - }, 1000); - } - } else { - setTimeout(function () { - console.log('installJsController: js-controller installed'); - if (cb) cb(false); - }, 0); - } -} - -function copyAdapterToController() { - console.log('Copy adapter...'); - // Copy adapter to tmp/node_modules/appName.adapter - copyFolderRecursiveSync(rootDir, rootDir + 'tmp/node_modules/', ['.idea', 'test', 'tmp', '.git', appName + '.js-controller']); - console.log('Adapter copied.'); -} - -function clearControllerLog() { - var dirPath = rootDir + 'tmp/log'; - var files; - try { - if (fs.existsSync(dirPath)) { - console.log('Clear controller log...'); - files = fs.readdirSync(dirPath); - } else { - console.log('Create controller log directory...'); - files = []; - fs.mkdirSync(dirPath); - } - } catch(e) { - console.error('Cannot read "' + dirPath + '"'); - return; - } - if (files.length > 0) { - try { - for (var i = 0; i < files.length; i++) { - var filePath = dirPath + '/' + files[i]; - fs.unlinkSync(filePath); - } - console.log('Controller log cleared'); - } catch (err) { - console.error('cannot clear log: ' + err); - } - } -} - -function clearDB() { - var dirPath = rootDir + 'tmp/iobroker-data/sqlite'; - var files; - try { - if (fs.existsSync(dirPath)) { - console.log('Clear sqlite DB...'); - files = fs.readdirSync(dirPath); - } else { - console.log('Create controller log directory...'); - files = []; - fs.mkdirSync(dirPath); - } - } catch(e) { - console.error('Cannot read "' + dirPath + '"'); - return; - } - if (files.length > 0) { - try { - for (var i = 0; i < files.length; i++) { - var filePath = dirPath + '/' + files[i]; - fs.unlinkSync(filePath); - } - console.log('Clear sqlite DB'); - } catch (err) { - console.error('cannot clear DB: ' + err); - } - } -} - -function setupController(cb) { - installJsController(function (isInited) { - clearControllerLog(); - clearDB(); - - if (!isInited) { - restoreOriginalFiles(); - copyAdapterToController(); - } - if (cb) cb(); - }); -} - -function startAdapter(objects, states, callback) { - console.log('startAdapter...'); - if (fs.existsSync(rootDir + 'tmp/node_modules/' + pkg.name + '/' + pkg.main)) { - try { - if (debug) { - // start controller - pid = child_process.exec('node node_modules/' + pkg.name + '/' + pkg.main + ' --console debug', { - cwd: rootDir + 'tmp', - stdio: [0, 1, 2] - }); - } else { - // start controller - pid = child_process.fork('node_modules/' + pkg.name + '/' + pkg.main, ['--console', 'debug'], { - cwd: rootDir + 'tmp', - stdio: [0, 1, 2, 'ipc'] - }); - } - } catch (error) { - console.error(JSON.stringify(error)); - } - } else { - console.error('Cannot find: ' + rootDir + 'tmp/node_modules/' + pkg.name + '/' + pkg.main); - } - if (callback) callback(objects, states); -} - -function startController(isStartAdapter, onObjectChange, onStateChange, callback) { - if (typeof isStartAdapter === 'function') { - onObjectChange = isStartAdapter; - isStartAdapter = true; - } - - if (onStateChange === undefined) { - callback = onObjectChange; - onObjectChange = undefined; - } - - if (pid) { - console.error('Controller is already started!'); - } else { - console.log('startController...'); - var isObjectConnected; - var isStatesConnected; - - var Objects = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/objects/objectsInMemServer'); - objects = new Objects({ - connection: { - "type" : "file", - "host" : "127.0.0.1", - "port" : 19001, - "user" : "", - "pass" : "", - "noFileCache": false, - "connectTimeout": 2000 - }, - logger: { - debug: function (msg) { - console.log(msg); - }, - info: function (msg) { - console.log(msg); - }, - warn: function (msg) { - console.warn(msg); - }, - error: function (msg) { - console.error(msg); - } - }, - connected: function () { - isObjectConnected = true; - if (isStatesConnected) { - console.log('startController: started!'); - if (isStartAdapter) { - startAdapter(objects, states, callback); - } else { - if (callback) { - callback(objects, states); - callback = null; - } - } - } - }, - change: onObjectChange - }); - - // Just open in memory DB itself - var States = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/states/statesInMemServer'); - states = new States({ - connection: { - type: 'file', - host: '127.0.0.1', - port: 19000, - options: { - auth_pass: null, - retry_max_delay: 15000 - } - }, - logger: { - debug: function (msg) { - }, - info: function (msg) { - }, - warn: function (msg) { - console.log(msg); - }, - error: function (msg) { - console.log(msg); - } - }, - connected: function () { - isStatesConnected = true; - if (isObjectConnected) { - console.log('startController: started!!'); - if (isStartAdapter) { - startAdapter(objects, states, callback); - } else { - if (callback) { - callback(objects, states); - callback = null; - } - } - } - }, - change: onStateChange - }); - } -} - -function stopAdapter(cb) { - if (!pid) { - console.error('Controller is not running!'); - if (cb) { - setTimeout(function () { - cb(false); - }, 0); - } - } else { - pid.on('exit', function (code, signal) { - if (pid) { - console.log('child process terminated due to receipt of signal ' + signal); - if (cb) cb(); - pid = null; - } - }); - - pid.on('close', function (code, signal) { - if (pid) { - if (cb) cb(); - pid = null; - } - }); - - pid.kill('SIGTERM'); - } -} - -function _stopController() { - if (objects) { - objects.destroy(); - objects = null; - } - if (states) { - states.destroy(); - states = null; - } -} - -function stopController(cb) { - var timeout; - if (objects) { - console.log('Set system.adapter.' + pkg.name + '.0'); - objects.setObject('system.adapter.' + pkg.name + '.0', { - common:{ - enabled: false - } - }); - } - - stopAdapter(function () { - if (timeout) { - clearTimeout(timeout); - timeout = null; - } - - _stopController(); - - if (cb) { - cb(true); - cb = null; - } - }); - - timeout = setTimeout(function () { - timeout = null; - console.log('child process NOT terminated'); - - _stopController(); - - if (cb) { - cb(false); - cb = null; - } - pid = null; - }, 5000); -} - -// Setup the adapter -function setAdapterConfig(common, native, instance) { - var objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString()); - var id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0); - if (common) objects[id].common = common; - if (native) objects[id].native = native; - fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/objects.json', JSON.stringify(objects)); -} - -// Read config of the adapter -function getAdapterConfig(instance) { - var objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString()); - var id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0); - return objects[id]; -} - -if (typeof module !== undefined && module.parent) { - module.exports.getAdapterConfig = getAdapterConfig; - module.exports.setAdapterConfig = setAdapterConfig; - module.exports.startController = startController; - module.exports.stopController = stopController; - module.exports.setupController = setupController; - module.exports.stopAdapter = stopAdapter; - module.exports.startAdapter = startAdapter; - module.exports.installAdapter = installAdapter; - module.exports.appName = appName; - module.exports.adapterName = adapterName; -} +/* jshint -W097 */// jshint strict:false +/*jslint node: true */ +// check if tmp directory exists +var fs = require('fs'); +var path = require('path'); +var child_process = require('child_process'); +var rootDir = path.normalize(__dirname + '/../../'); +var pkg = require(rootDir + 'package.json'); +var debug = typeof v8debug === 'object'; +pkg.main = pkg.main || 'main.js'; + +var adapterName = path.normalize(rootDir).replace(/\\/g, '/').split('/'); +adapterName = adapterName[adapterName.length - 2]; +var adapterStarted = false; + +function getAppName() { + var parts = __dirname.replace(/\\/g, '/').split('/'); + return parts[parts.length - 3].split('.')[0]; +} + +var appName = getAppName().toLowerCase(); + +var objects; +var states; + +var pid = null; + +function copyFileSync(source, target) { + + var targetFile = target; + + //if target is a directory a new file with the same name will be created + if (fs.existsSync(target)) { + if ( fs.lstatSync( target ).isDirectory() ) { + targetFile = path.join(target, path.basename(source)); + } + } + + try { + fs.writeFileSync(targetFile, fs.readFileSync(source)); + } + catch (err) { + console.log("file copy error: " +source +" -> " + targetFile + " (error ignored)"); + } +} + +function copyFolderRecursiveSync(source, target, ignore) { + var files = []; + + var base = path.basename(source); + if (base === adapterName) { + base = pkg.name; + } + //check if folder needs to be created or integrated + var targetFolder = path.join(target, base); + if (!fs.existsSync(targetFolder)) { + fs.mkdirSync(targetFolder); + } + + //copy + if (fs.lstatSync(source).isDirectory()) { + files = fs.readdirSync(source); + files.forEach(function (file) { + if (ignore && ignore.indexOf(file) !== -1) { + return; + } + + var curSource = path.join(source, file); + var curTarget = path.join(targetFolder, file); + if (fs.lstatSync(curSource).isDirectory()) { + // ignore grunt files + if (file.indexOf('grunt') !== -1) return; + if (file === 'chai') return; + if (file === 'mocha') return; + copyFolderRecursiveSync(curSource, targetFolder, ignore); + } else { + copyFileSync(curSource, curTarget); + } + }); + } +} + +if (!fs.existsSync(rootDir + 'tmp')) { + fs.mkdirSync(rootDir + 'tmp'); +} + +function storeOriginalFiles() { + console.log('Store original files...'); + var dataDir = rootDir + 'tmp/' + appName + '-data/'; + + var f = fs.readFileSync(dataDir + 'objects.json'); + var objects = JSON.parse(f.toString()); + if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) { + objects['system.adapter.admin.0'].common.enabled = false; + } + if (objects['system.adapter.admin.1'] && objects['system.adapter.admin.1'].common) { + objects['system.adapter.admin.1'].common.enabled = false; + } + + fs.writeFileSync(dataDir + 'objects.json.original', JSON.stringify(objects)); + try { + f = fs.readFileSync(dataDir + 'states.json'); + fs.writeFileSync(dataDir + 'states.json.original', f); + } + catch (err) { + console.log('no states.json found - ignore'); + } +} + +function restoreOriginalFiles() { + console.log('restoreOriginalFiles...'); + var dataDir = rootDir + 'tmp/' + appName + '-data/'; + + var f = fs.readFileSync(dataDir + 'objects.json.original'); + fs.writeFileSync(dataDir + 'objects.json', f); + try { + f = fs.readFileSync(dataDir + 'states.json.original'); + fs.writeFileSync(dataDir + 'states.json', f); + } + catch (err) { + console.log('no states.json.original found - ignore'); + } + +} + +function checkIsAdapterInstalled(cb, counter, customName) { + customName = customName || pkg.name.split('.').pop(); + counter = counter || 0; + var dataDir = rootDir + 'tmp/' + appName + '-data/'; + console.log('checkIsAdapterInstalled...'); + + try { + var f = fs.readFileSync(dataDir + 'objects.json'); + var objects = JSON.parse(f.toString()); + if (objects['system.adapter.' + customName + '.0']) { + console.log('checkIsAdapterInstalled: ready!'); + setTimeout(function () { + if (cb) cb(); + }, 100); + return; + } else { + console.warn('checkIsAdapterInstalled: still not ready'); + } + } catch (err) { + + } + + if (counter > 20) { + console.error('checkIsAdapterInstalled: Cannot install!'); + if (cb) cb('Cannot install'); + } else { + console.log('checkIsAdapterInstalled: wait...'); + setTimeout(function() { + checkIsAdapterInstalled(cb, counter + 1); + }, 1000); + } +} + +function checkIsControllerInstalled(cb, counter) { + counter = counter || 0; + var dataDir = rootDir + 'tmp/' + appName + '-data/'; + + console.log('checkIsControllerInstalled...'); + try { + var f = fs.readFileSync(dataDir + 'objects.json'); + var objects = JSON.parse(f.toString()); + if (objects['system.adapter.admin.0']) { + console.log('checkIsControllerInstalled: installed!'); + setTimeout(function () { + if (cb) cb(); + }, 100); + return; + } + } catch (err) { + + } + + if (counter > 20) { + console.log('checkIsControllerInstalled: Cannot install!'); + if (cb) cb('Cannot install'); + } else { + console.log('checkIsControllerInstalled: wait...'); + setTimeout(function() { + checkIsControllerInstalled(cb, counter + 1); + }, 1000); + } +} + +function installAdapter(customName, cb) { + if (typeof customName === 'function') { + cb = customName; + customName = null; + } + customName = customName || pkg.name.split('.').pop(); + console.log('Install adapter...'); + var startFile = 'node_modules/' + appName + '.js-controller/' + appName + '.js'; + // make first install + if (debug) { + child_process.execSync('node ' + startFile + ' add ' + customName + ' --enabled false', { + cwd: rootDir + 'tmp', + stdio: [0, 1, 2] + }); + checkIsAdapterInstalled(function (error) { + if (error) console.error(error); + console.log('Adapter installed.'); + if (cb) cb(); + }); + } else { + // add controller + var _pid = child_process.fork(startFile, ['add', customName, '--enabled', 'false'], { + cwd: rootDir + 'tmp', + stdio: [0, 1, 2, 'ipc'] + }); + + waitForEnd(_pid, function () { + checkIsAdapterInstalled(function (error) { + if (error) console.error(error); + console.log('Adapter installed.'); + if (cb) cb(); + }); + }); + } +} + +function waitForEnd(_pid, cb) { + if (!_pid) { + cb(-1, -1); + return; + } + _pid.on('exit', function (code, signal) { + if (_pid) { + _pid = null; + cb(code, signal); + } + }); + _pid.on('close', function (code, signal) { + if (_pid) { + _pid = null; + cb(code, signal); + } + }); +} + +function installJsController(cb) { + console.log('installJsController...'); + if (!fs.existsSync(rootDir + 'tmp/node_modules/' + appName + '.js-controller') || + !fs.existsSync(rootDir + 'tmp/' + appName + '-data')) { + // try to detect appName.js-controller in node_modules/appName.js-controller + // travis CI installs js-controller into node_modules + if (fs.existsSync(rootDir + 'node_modules/' + appName + '.js-controller')) { + console.log('installJsController: no js-controller => copy it from "' + rootDir + 'node_modules/' + appName + '.js-controller"'); + // copy all + // stop controller + console.log('Stop controller if running...'); + var _pid; + if (debug) { + // start controller + _pid = child_process.exec('node ' + appName + '.js stop', { + cwd: rootDir + 'node_modules/' + appName + '.js-controller', + stdio: [0, 1, 2] + }); + } else { + _pid = child_process.fork(appName + '.js', ['stop'], { + cwd: rootDir + 'node_modules/' + appName + '.js-controller', + stdio: [0, 1, 2, 'ipc'] + }); + } + + waitForEnd(_pid, function () { + // copy all files into + if (!fs.existsSync(rootDir + 'tmp')) fs.mkdirSync(rootDir + 'tmp'); + if (!fs.existsSync(rootDir + 'tmp/node_modules')) fs.mkdirSync(rootDir + 'tmp/node_modules'); + + if (!fs.existsSync(rootDir + 'tmp/node_modules/' + appName + '.js-controller')){ + console.log('Copy js-controller...'); + copyFolderRecursiveSync(rootDir + 'node_modules/' + appName + '.js-controller', rootDir + 'tmp/node_modules/'); + } + + console.log('Setup js-controller...'); + var __pid; + if (debug) { + // start controller + _pid = child_process.exec('node ' + appName + '.js setup first --console', { + cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller', + stdio: [0, 1, 2] + }); + } else { + __pid = child_process.fork(appName + '.js', ['setup', 'first', '--console'], { + cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller', + stdio: [0, 1, 2, 'ipc'] + }); + } + waitForEnd(__pid, function () { + checkIsControllerInstalled(function () { + // change ports for object and state DBs + var config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json'); + config.objects.port = 19001; + config.states.port = 19000; + fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2)); + console.log('Setup finished.'); + + copyAdapterToController(); + + installAdapter(function () { + storeOriginalFiles(); + if (cb) cb(true); + }); + }); + }); + }); + } else { + // check if port 9000 is free, else admin adapter will be added to running instance + var client = new require('net').Socket(); + client.on('error', () => {}); + client.connect(9000, '127.0.0.1', function() { + console.error('Cannot initiate fisrt run of test, because one instance of application is running on this PC. Stop it and repeat.'); + process.exit(0); + }); + + setTimeout(function () { + client.destroy(); + if (!fs.existsSync(rootDir + 'tmp/node_modules/' + appName + '.js-controller')) { + console.log('installJsController: no js-controller => install from git'); + + child_process.execSync('npm install https://github.com/' + appName + '/' + appName + '.js-controller/tarball/master --prefix ./ --production', { + cwd: rootDir + 'tmp/', + stdio: [0, 1, 2] + }); + } else { + console.log('Setup js-controller...'); + var __pid; + if (debug) { + // start controller + child_process.exec('node ' + appName + '.js setup first', { + cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller', + stdio: [0, 1, 2] + }); + } else { + child_process.fork(appName + '.js', ['setup', 'first'], { + cwd: rootDir + 'tmp/node_modules/' + appName + '.js-controller', + stdio: [0, 1, 2, 'ipc'] + }); + } + } + + // let npm install admin and run setup + checkIsControllerInstalled(function () { + var _pid; + + if (fs.existsSync(rootDir + 'node_modules/' + appName + '.js-controller/' + appName + '.js')) { + _pid = child_process.fork(appName + '.js', ['stop'], { + cwd: rootDir + 'node_modules/' + appName + '.js-controller', + stdio: [0, 1, 2, 'ipc'] + }); + } + + waitForEnd(_pid, function () { + // change ports for object and state DBs + var config = require(rootDir + 'tmp/' + appName + '-data/' + appName + '.json'); + config.objects.port = 19001; + config.states.port = 19000; + fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/' + appName + '.json', JSON.stringify(config, null, 2)); + + copyAdapterToController(); + + installAdapter(function () { + storeOriginalFiles(); + if (cb) cb(true); + }); + }); + }); + }, 1000); + } + } else { + setTimeout(function () { + console.log('installJsController: js-controller installed'); + if (cb) cb(false); + }, 0); + } +} + +function copyAdapterToController() { + console.log('Copy adapter...'); + // Copy adapter to tmp/node_modules/appName.adapter + copyFolderRecursiveSync(rootDir, rootDir + 'tmp/node_modules/', ['.idea', 'test', 'tmp', '.git', appName + '.js-controller']); + console.log('Adapter copied.'); +} + +function clearControllerLog() { + var dirPath = rootDir + 'tmp/log'; + var files; + try { + if (fs.existsSync(dirPath)) { + console.log('Clear controller log...'); + files = fs.readdirSync(dirPath); + } else { + console.log('Create controller log directory...'); + files = []; + fs.mkdirSync(dirPath); + } + } catch(e) { + console.error('Cannot read "' + dirPath + '"'); + return; + } + if (files.length > 0) { + try { + for (var i = 0; i < files.length; i++) { + var filePath = dirPath + '/' + files[i]; + fs.unlinkSync(filePath); + } + console.log('Controller log cleared'); + } catch (err) { + console.error('cannot clear log: ' + err); + } + } +} + +function clearDB() { + var dirPath = rootDir + 'tmp/iobroker-data/sqlite'; + var files; + try { + if (fs.existsSync(dirPath)) { + console.log('Clear sqlite DB...'); + files = fs.readdirSync(dirPath); + } else { + console.log('Create controller log directory...'); + files = []; + fs.mkdirSync(dirPath); + } + } catch(e) { + console.error('Cannot read "' + dirPath + '"'); + return; + } + if (files.length > 0) { + try { + for (var i = 0; i < files.length; i++) { + var filePath = dirPath + '/' + files[i]; + fs.unlinkSync(filePath); + } + console.log('Clear sqlite DB'); + } catch (err) { + console.error('cannot clear DB: ' + err); + } + } +} + +function setupController(cb) { + installJsController(function (isInited) { + clearControllerLog(); + clearDB(); + + if (!isInited) { + restoreOriginalFiles(); + copyAdapterToController(); + } + // read system.config object + var dataDir = rootDir + 'tmp/' + appName + '-data/'; + + var objs; + try { + objs = fs.readFileSync(dataDir + 'objects.json'); + objs = JSON.parse(objs); + } + catch (e) { + console.log('ERROR reading/parsing system configuration. Ignore'); + objs = {'system.config': {}}; + } + if (!objs || !objs['system.config']) { + objs = {'system.config': {}}; + } + + if (cb) cb(objs['system.config']); + }); +} + +function startAdapter(objects, states, callback) { + if (adapterStarted) { + console.log('Adapter already started ...'); + if (callback) callback(objects, states); + return; + } + adapterStarted = true; + console.log('startAdapter...'); + if (fs.existsSync(rootDir + 'tmp/node_modules/' + pkg.name + '/' + pkg.main)) { + try { + if (debug) { + // start controller + pid = child_process.exec('node node_modules/' + pkg.name + '/' + pkg.main + ' --console silly', { + cwd: rootDir + 'tmp', + stdio: [0, 1, 2] + }); + } else { + // start controller + pid = child_process.fork('node_modules/' + pkg.name + '/' + pkg.main, ['--console', 'silly'], { + cwd: rootDir + 'tmp', + stdio: [0, 1, 2, 'ipc'] + }); + } + } catch (error) { + console.error(JSON.stringify(error)); + } + } else { + console.error('Cannot find: ' + rootDir + 'tmp/node_modules/' + pkg.name + '/' + pkg.main); + } + if (callback) callback(objects, states); +} + +function startController(isStartAdapter, onObjectChange, onStateChange, callback) { + if (typeof isStartAdapter === 'function') { + callback = onStateChange; + onStateChange = onObjectChange; + onObjectChange = isStartAdapter; + isStartAdapter = true; + } + + if (onStateChange === undefined) { + callback = onObjectChange; + onObjectChange = undefined; + } + + if (pid) { + console.error('Controller is already started!'); + } else { + console.log('startController...'); + adapterStarted = false; + var isObjectConnected; + var isStatesConnected; + + var Objects = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/objects/objectsInMemServer'); + objects = new Objects({ + connection: { + "type" : "file", + "host" : "127.0.0.1", + "port" : 19001, + "user" : "", + "pass" : "", + "noFileCache": false, + "connectTimeout": 2000 + }, + logger: { + silly: function (msg) { + console.log(msg); + }, + debug: function (msg) { + console.log(msg); + }, + info: function (msg) { + console.log(msg); + }, + warn: function (msg) { + console.warn(msg); + }, + error: function (msg) { + console.error(msg); + } + }, + connected: function () { + isObjectConnected = true; + if (isStatesConnected) { + console.log('startController: started!'); + if (isStartAdapter) { + startAdapter(objects, states, callback); + } else { + if (callback) { + callback(objects, states); + callback = null; + } + } + } + }, + change: onObjectChange + }); + + // Just open in memory DB itself + var States = require(rootDir + 'tmp/node_modules/' + appName + '.js-controller/lib/states/statesInMemServer'); + states = new States({ + connection: { + type: 'file', + host: '127.0.0.1', + port: 19000, + options: { + auth_pass: null, + retry_max_delay: 15000 + } + }, + logger: { + silly: function (msg) { + console.log(msg); + }, + debug: function (msg) { + console.log(msg); + }, + info: function (msg) { + console.log(msg); + }, + warn: function (msg) { + console.log(msg); + }, + error: function (msg) { + console.log(msg); + } + }, + connected: function () { + isStatesConnected = true; + if (isObjectConnected) { + console.log('startController: started!!'); + if (isStartAdapter) { + startAdapter(objects, states, callback); + } else { + if (callback) { + callback(objects, states); + callback = null; + } + } + } + }, + change: onStateChange + }); + } +} + +function stopAdapter(cb) { + if (!pid) { + console.error('Controller is not running!'); + if (cb) { + setTimeout(function () { + cb(false); + }, 0); + } + } else { + adapterStarted = false; + pid.on('exit', function (code, signal) { + if (pid) { + console.log('child process terminated due to receipt of signal ' + signal); + if (cb) cb(); + pid = null; + } + }); + + pid.on('close', function (code, signal) { + if (pid) { + if (cb) cb(); + pid = null; + } + }); + + pid.kill('SIGTERM'); + } +} + +function _stopController() { + if (objects) { + objects.destroy(); + objects = null; + } + if (states) { + states.destroy(); + states = null; + } +} + +function stopController(cb) { + var timeout; + if (objects) { + console.log('Set system.adapter.' + pkg.name + '.0'); + objects.setObject('system.adapter.' + pkg.name + '.0', { + common:{ + enabled: false + } + }); + } + + stopAdapter(function () { + if (timeout) { + clearTimeout(timeout); + timeout = null; + } + + _stopController(); + + if (cb) { + cb(true); + cb = null; + } + }); + + timeout = setTimeout(function () { + timeout = null; + console.log('child process NOT terminated'); + + _stopController(); + + if (cb) { + cb(false); + cb = null; + } + pid = null; + }, 5000); +} + +// Setup the adapter +function setAdapterConfig(common, native, instance) { + var objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString()); + var id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0); + if (common) objects[id].common = common; + if (native) objects[id].native = native; + fs.writeFileSync(rootDir + 'tmp/' + appName + '-data/objects.json', JSON.stringify(objects)); +} + +// Read config of the adapter +function getAdapterConfig(instance) { + var objects = JSON.parse(fs.readFileSync(rootDir + 'tmp/' + appName + '-data/objects.json').toString()); + var id = 'system.adapter.' + adapterName.split('.').pop() + '.' + (instance || 0); + return objects[id]; +} + +if (typeof module !== undefined && module.parent) { + module.exports.getAdapterConfig = getAdapterConfig; + module.exports.setAdapterConfig = setAdapterConfig; + module.exports.startController = startController; + module.exports.stopController = stopController; + module.exports.setupController = setupController; + module.exports.stopAdapter = stopAdapter; + module.exports.startAdapter = startAdapter; + module.exports.installAdapter = installAdapter; + module.exports.appName = appName; + module.exports.adapterName = adapterName; + module.exports.adapterStarted = adapterStarted; +} From dcf2ce15494d781f17a04aacb305bce435a0711e Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 3 Mar 2019 23:23:46 +0100 Subject: [PATCH 7/8] update basic package-file testing --- test/testPackageFiles.js | 95 +++++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 15 deletions(-) diff --git a/test/testPackageFiles.js b/test/testPackageFiles.js index 6640c90..a63e6bc 100644 --- a/test/testPackageFiles.js +++ b/test/testPackageFiles.js @@ -1,30 +1,95 @@ -/* jshint -W097 */// jshint strict:false -/*jslint node: true */ -var expect = require('chai').expect; -var fs = require('fs'); +/* jshint -W097 */ +/* jshint strict:false */ +/* jslint node: true */ +/* jshint expr: true */ +'use strict'; -describe('Test package.json and io-package.json', function() { - it('Test package files', function (done) { - var fileContentIOPackage = fs.readFileSync(__dirname + '/../io-package.json'); - var ioPackage = JSON.parse(fileContentIOPackage); +const expect = require('chai').expect; +const fs = require('fs'); - var fileContentNPMPackage = fs.readFileSync(__dirname + '/../package.json'); - var npmPackage = JSON.parse(fileContentNPMPackage); +describe('Test package.json and io-package.json', () => { + it('Test package files', done => { + console.log(); + + const fileContentIOPackage = fs.readFileSync(__dirname + '/../io-package.json', 'utf8'); + const ioPackage = JSON.parse(fileContentIOPackage); + + const fileContentNPMPackage = fs.readFileSync(__dirname + '/../package.json', 'utf8'); + const npmPackage = JSON.parse(fileContentNPMPackage); expect(ioPackage).to.be.an('object'); expect(npmPackage).to.be.an('object'); - expect(ioPackage.common.version).to.exist; - expect(npmPackage.version).to.exist; + expect(ioPackage.common.version, 'ERROR: Version number in io-package.json needs to exist').to.exist; + expect(npmPackage.version, 'ERROR: Version number in package.json needs to exist').to.exist; - if (!expect(ioPackage.common.version).to.be.equal(npmPackage.version)) { - console.log('ERROR: Version numbers in package.json and io-package.json differ!!'); - } + expect(ioPackage.common.version, 'ERROR: Version numbers in package.json and io-package.json needs to match').to.be.equal(npmPackage.version); if (!ioPackage.common.news || !ioPackage.common.news[ioPackage.common.version]) { console.log('WARNING: No news entry for current version exists in io-package.json, no rollback in Admin possible!'); + console.log(); } + expect(npmPackage.author, 'ERROR: Author in package.json needs to exist').to.exist; + expect(ioPackage.common.authors, 'ERROR: Authors in io-package.json needs to exist').to.exist; + + expect(ioPackage.common.license, 'ERROR: License missing in io-package in common.license').to.exist; + + if (ioPackage.common.name.indexOf('template') !== 0) { + if (Array.isArray(ioPackage.common.authors)) { + expect(ioPackage.common.authors.length, 'ERROR: Author in io-package.json needs to be set').to.not.be.equal(0); + if (ioPackage.common.authors.length === 1) { + expect(ioPackage.common.authors[0], 'ERROR: Author in io-package.json needs to be a real name').to.not.be.equal('my Name '); + } + } + else { + expect(ioPackage.common.authors, 'ERROR: Author in io-package.json needs to be a real name').to.not.be.equal('my Name '); + } + } + else { + console.log('WARNING: Testing for set authors field in io-package skipped because template adapter'); + console.log(); + } + expect(fs.existsSync(__dirname + '/../README.md'), 'ERROR: README.md needs to exist! Please create one with description, detail information and changelog. English is mandatory.').to.be.true; + if (!ioPackage.common.titleLang || typeof ioPackage.common.titleLang !== 'object') { + console.log('WARNING: titleLang is not existing in io-package.json. Please add'); + console.log(); + } + if ( + ioPackage.common.title.indexOf('iobroker') !== -1 || + ioPackage.common.title.indexOf('ioBroker') !== -1 || + ioPackage.common.title.indexOf('adapter') !== -1 || + ioPackage.common.title.indexOf('Adapter') !== -1 + ) { + console.log('WARNING: title contains Adapter or ioBroker. It is clear anyway, that it is adapter for ioBroker.'); + console.log(); + } + + if (!ioPackage.common.controller && !ioPackage.common.onlyWWW && !ioPackage.common.noConfig) { + if (!ioPackage.common.materialize || !fs.existsSync(__dirname + '/../admin/index_m.html') || !fs.existsSync(__dirname + '/../gulpfile.js')) { + console.log('WARNING: Admin3 support is missing! Please add it'); + console.log(); + } + if (ioPackage.common.materialize) { + expect(fs.existsSync(__dirname + '/../admin/index_m.html'), 'Admin3 support is enabled in io-package.json, but index_m.html is missing!').to.be.true; + } + } + + const licenseFileExists = fs.existsSync(__dirname + '/../LICENSE'); + const fileContentReadme = fs.readFileSync(__dirname + '/../README.md', 'utf8'); + if (fileContentReadme.indexOf('## Changelog') === -1) { + console.log('Warning: The README.md should have a section ## Changelog'); + console.log(); + } + expect((licenseFileExists || fileContentReadme.indexOf('## License') !== -1), 'A LICENSE must exist as LICENSE file or as part of the README.md').to.be.true; + if (!licenseFileExists) { + console.log('Warning: The License should also exist as LICENSE file'); + console.log(); + } + if (fileContentReadme.indexOf('## License') === -1) { + console.log('Warning: The README.md should also have a section ## License to be shown in Admin3'); + console.log(); + } done(); }); }); From 4e0f970ca2c015cdd885dfcfd21e157257155bfd Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Mon, 4 Mar 2019 22:40:42 +0100 Subject: [PATCH 8/8] update package.json --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 26905ae..0ff9b1d 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "url": "https://github.com/rgwch/ioBroker.mystrom-wifi-switch" }, "dependencies": { - "request": "^2.81.0" + "request": "^2.81.0", + "@iobroker/adapter-core": "^1.0.3" }, "devDependencies": { "grunt": "^1.0.1", @@ -34,8 +35,7 @@ "grunt-jscs": "^3.0.1", "grunt-http": "^2.2.0", "mocha": "^6.0.2", - "chai": "^4.2.0", - "@iobroker/adapter-core": "^1.0.3" + "chai": "^4.2.0" }, "main": "mystrom-wifi-switch.js", "scripts": {