From f25594e74c06ad8c74d8b2697b4bf1e354364ffd Mon Sep 17 00:00:00 2001 From: "dominic.griesel" Date: Fri, 14 Dec 2018 16:38:30 +0100 Subject: [PATCH] Fix ESLint errors in test files Fixes #29 --- build/templates/index.js | 1 + build/templates/test/lib/setup.js.js | 1302 +++++++++---------- build/templates/test/testPackageFiles.js.js | 153 ++- build/templates/test/testStartup.js.js | 225 ++-- build/templates/test/tsconfig.json.js | 18 + src/templates/index.ts | 1 + src/templates/test/lib/setup.js.ts | 1302 +++++++++---------- src/templates/test/testPackageFiles.js.ts | 153 ++- src/templates/test/testStartup.js.ts | 225 ++-- src/templates/test/tsconfig.json.ts | 20 + 10 files changed, 1706 insertions(+), 1694 deletions(-) create mode 100644 build/templates/test/tsconfig.json.js create mode 100644 src/templates/test/tsconfig.json.ts diff --git a/build/templates/index.js b/build/templates/index.js index 3ac51b1e..0be99506 100644 --- a/build/templates/index.js +++ b/build/templates/index.js @@ -24,6 +24,7 @@ const templates = [ { name: "test/mocha.setup.js.ts", templateFunction: require("./test/mocha.setup.js") }, { name: "test/testPackageFiles.js.ts", templateFunction: require("./test/testPackageFiles.js") }, { name: "test/testStartup.js.ts", templateFunction: require("./test/testStartup.js") }, + { name: "test/tsconfig.json.ts", templateFunction: require("./test/tsconfig.json") }, { name: "tsconfig.build.json.ts", templateFunction: require("./tsconfig.build.json") }, { name: "tsconfig.json.ts", templateFunction: require("./tsconfig.json") }, { name: "tslint.json.ts", templateFunction: require("./tslint.json") }, diff --git a/build/templates/test/lib/setup.js.js b/build/templates/test/lib/setup.js.js index c387f539..5a055f2e 100644 --- a/build/templates/test/lib/setup.js.js +++ b/build/templates/test/lib/setup.js.js @@ -1,733 +1,729 @@ "use strict"; module.exports = (answers => { const template = ` -/* 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; +const fs = require("fs"); +const path = require("path"); +const child_process = require("child_process"); +const rootDir = path.normalize(__dirname + "/../../"); +const adapterName = path.basename(rootDir); +const pkg = require(rootDir + "package.json"); +const debug = typeof v8debug === "object"; +pkg.main = pkg.main || "main.js"; + +let adapterStarted = false; function getAppName() { - var parts = __dirname.replace(/\\\\/g, '/').split('/'); - return parts[parts.length - 3].split('.')[0]; + const parts = __dirname.replace(/\\\\/g, "/").split("/"); + return parts[parts.length - 3].split(".")[0]; } -var appName = getAppName().toLowerCase(); +const appName = getAppName().toLowerCase(); -var objects; -var states; +let objects; +let states; -var pid = null; +/** @type {undefined | ReturnType} */ +let pid; 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)"); - } + let 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); - } - }); - } + let files = []; + + let base = path.basename(source); + if (base === adapterName) { + base = pkg.name; + } + //check if folder needs to be created or integrated + const 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; + } + + const curSource = path.join(source, file); + const 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'); +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'); - } + console.log("Store original files..."); + const dataDir = rootDir + "tmp/" + appName + "-data/"; + + let f = fs.readFileSync(dataDir + "objects.json"); + const 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'); - } + console.log("restoreOriginalFiles..."); + const dataDir = rootDir + "tmp/" + appName + "-data/"; + + let 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); - } + customName = customName || pkg.name.split(".").pop(); + counter = counter || 0; + const dataDir = rootDir + "tmp/" + appName + "-data/"; + console.log("checkIsAdapterInstalled..."); + + try { + const f = fs.readFileSync(dataDir + "objects.json"); + const 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) { + // Don't care + } + + 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); - } + counter = counter || 0; + const dataDir = rootDir + "tmp/" + appName + "-data/"; + + console.log("checkIsControllerInstalled..."); + try { + const f = fs.readFileSync(dataDir + "objects.json"); + const objects = JSON.parse(f.toString()); + if (objects["system.adapter.admin.0"]) { + console.log("checkIsControllerInstalled: installed!"); + setTimeout(function () { + if (cb) cb(); + }, 100); + return; + } + } catch (err) { + // Don't care + } + + 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(); - }); - }); - } + if (typeof customName === "function") { + cb = customName; + customName = null; + } + customName = customName || pkg.name.split(".").pop(); + console.log("Install adapter..."); + const 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 + const _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); - } - }); + 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); - } + 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..."); + let _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..."); + let __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 + const 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 + const Socket = require("net").Socket; + const client = new 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..."); + 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 () { + let _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 + const 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.'); + 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); - } - } + const dirPath = rootDir + "tmp/log"; + let 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 (let i = 0; i < files.length; i++) { + const 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); - } - } + const dirPath = rootDir + "tmp/iobroker-data/sqlite"; + let 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 (let i = 0; i < files.length; i++) { + const 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']); - }); + installJsController(function (isInited) { + clearControllerLog(); + clearDB(); + + if (!isInited) { + restoreOriginalFiles(); + copyAdapterToController(); + } + // read system.config object + const dataDir = rootDir + "tmp/" + appName + "-data/"; + + let objs; + try { + objs = JSON.parse(fs.readFileSync(dataDir + "objects.json", "utf8")); + } + 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); + 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 - }); - } + 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; + let isObjectConnected; + let isStatesConnected; + + const 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 + const 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'); - } + 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 = undefined; + } + }); + + pid.on("close", () => { + if (pid) { + if (cb) cb(); + pid = undefined; + } + }); + + pid.kill("SIGTERM"); + } } function _stopController() { - if (objects) { - objects.destroy(); - objects = null; - } - if (states) { - states.destroy(); - states = null; - } + 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); + let 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 = undefined; + }, 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)); + const objects = JSON.parse(fs.readFileSync(rootDir + "tmp/" + appName + "-data/objects.json").toString()); + const 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]; + const objects = JSON.parse(fs.readFileSync(rootDir + "tmp/" + appName + "-data/objects.json").toString()); + const 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; + 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; } `; return template.trim(); diff --git a/build/templates/test/testPackageFiles.js.js b/build/templates/test/testPackageFiles.js.js index 44a6ebd5..63bebc2e 100644 --- a/build/templates/test/testPackageFiles.js.js +++ b/build/templates/test/testPackageFiles.js.js @@ -1,96 +1,89 @@ "use strict"; module.exports = (answers => { const template = ` -/* jshint -W097 */ -/* jshint strict:false */ -/* jslint node: true */ -/* jshint expr: true */ -var expect = require('chai').expect; -var fs = require('fs'); +const expect = require("chai").expect; +const fs = require("fs"); -describe('Test package.json and io-package.json', function() { - it('Test package files', function (done) { - console.log(); +describe("Test package.json and io-package.json", function() { + it("Test package files", function (done) { + console.log(); - var fileContentIOPackage = fs.readFileSync(__dirname + '/../io-package.json', 'utf8'); - var ioPackage = JSON.parse(fileContentIOPackage); + const ioPackage = require("../io-package.json"); + const npmPackage = require("../package.json"); - var fileContentNPMPackage = fs.readFileSync(__dirname + '/../package.json', 'utf8'); - var npmPackage = JSON.parse(fileContentNPMPackage); + expect(ioPackage).to.be.an("object"); + expect(npmPackage).to.be.an("object"); - expect(ioPackage).to.be.an('object'); - expect(npmPackage).to.be.an('object'); + 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; - 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; + expect(ioPackage.common.version, "ERROR: Version numbers in package.json and io-package.json needs to match").to.be.equal(npmPackage.version); - 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(); + } - 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(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; + 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.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.name.indexOf("vis-") !== 0) { + 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; + } + } - if (ioPackage.common.name.indexOf('vis-') !== 0) { - 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; - } - } - - var licenseFileExists = fs.existsSync(__dirname + '/../LICENSE'); - var 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(); - }); + 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(); + }); }); `; return template.trim(); diff --git a/build/templates/test/testStartup.js.js b/build/templates/test/testStartup.js.js index a43ddb77..d7fc0f2e 100644 --- a/build/templates/test/testStartup.js.js +++ b/build/templates/test/testStartup.js.js @@ -1,145 +1,142 @@ "use strict"; module.exports = (answers => { const template = ` -/* jshint -W097 */// jshint strict:false -/*jslint node: true */ -var expect = require('chai').expect; -var setup = require('./lib/setup'); +const expect = require("chai").expect; +const setup = require("./lib/setup"); -var objects = null; -var states = null; -var onStateChanged = null; -var onObjectChanged = null; -var sendToID = 1; +let objects = null; +let states = null; +let onStateChanged = null; +// let onObjectChanged = null; +let sendToID = 1; -var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1); +const adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf(".")+1); function checkConnectionOfAdapter(cb, counter) { - counter = counter || 0; - console.log('Try check #' + counter); - if (counter > 30) { - if (cb) cb('Cannot check connection'); - return; - } - - states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) { - if (err) console.error(err); - if (state && state.val) { - if (cb) cb(); - } else { - setTimeout(function () { - checkConnectionOfAdapter(cb, counter + 1); - }, 1000); - } - }); + counter = counter || 0; + console.log("Try check #" + counter); + if (counter > 30) { + if (cb) cb("Cannot check connection"); + return; + } + + states.getState("system.adapter." + adapterShortName + ".0.alive", function (err, state) { + if (err) console.error(err); + if (state && state.val) { + if (cb) cb(); + } else { + setTimeout(function () { + checkConnectionOfAdapter(cb, counter + 1); + }, 1000); + } + }); } function checkValueOfState(id, value, cb, counter) { - counter = counter || 0; - if (counter > 20) { - if (cb) cb('Cannot check value Of State ' + id); - return; - } - - states.getState(id, function (err, state) { - if (err) console.error(err); - if (value === null && !state) { - if (cb) cb(); - } else - if (state && (value === undefined || state.val === value)) { - if (cb) cb(); - } else { - setTimeout(function () { - checkValueOfState(id, value, cb, counter + 1); - }, 500); - } - }); + counter = counter || 0; + if (counter > 20) { + if (cb) cb("Cannot check value Of State " + id); + return; + } + + states.getState(id, function (err, state) { + if (err) console.error(err); + if (value === null && !state) { + if (cb) cb(); + } else + if (state && (value === undefined || state.val === value)) { + if (cb) cb(); + } else { + setTimeout(function () { + checkValueOfState(id, value, cb, counter + 1); + }, 500); + } + }); } function sendTo(target, command, message, callback) { - onStateChanged = function (id, state) { - if (id === 'messagebox.system.adapter.test.0') { - callback(state.message); - } - }; - - states.pushMessage('system.adapter.' + target, { - command: command, - message: message, - from: 'system.adapter.test.0', - callback: { - message: message, - id: sendToID++, - ack: false, - time: (new Date()).getTime() - } - }); + onStateChanged = function (id, state) { + if (id === "messagebox.system.adapter.test.0") { + callback(state.message); + } + }; + + states.pushMessage("system.adapter." + target, { + command: command, + message: message, + from: "system.adapter.test.0", + callback: { + message: message, + id: sendToID++, + ack: false, + time: (new Date()).getTime() + } + }); } -describe('Test ' + adapterShortName + ' adapter', function() { - before('Test ' + adapterShortName + ' adapter: Start js-controller', function (_done) { - this.timeout(600000); // because of first install from npm +describe("Test " + adapterShortName + " adapter", function() { + before("Test " + adapterShortName + " adapter: Start js-controller", function (_done) { + this.timeout(600000); // because of first install from npm - setup.setupController(function () { - var config = setup.getAdapterConfig(); - // enable adapter - config.common.enabled = true; - config.common.loglevel = 'debug'; + setup.setupController(function () { + const config = setup.getAdapterConfig(); + // enable adapter + config.common.enabled = true; + config.common.loglevel = "debug"; - //config.native.dbtype = 'sqlite'; + //config.native.dbtype = 'sqlite'; - setup.setAdapterConfig(config.common, config.native); + setup.setAdapterConfig(config.common, config.native); - setup.startController(true, function(id, obj) {}, function (id, state) { - if (onStateChanged) onStateChanged(id, state); - }, - function (_objects, _states) { - objects = _objects; - states = _states; - _done(); - }); - }); - }); + setup.startController(true, (id, obj) => { }, function (id, state) { + if (onStateChanged) onStateChanged(id, state); + }, + function (_objects, _states) { + objects = _objects; + states = _states; + _done(); + }); + }); + }); /* - ENABLE THIS WHEN ADAPTER RUNS IN DEAMON MODE TO CHECK THAT IT HAS STARTED SUCCESSFULLY + ENABLE THIS WHEN ADAPTER RUNS IN DEAMON MODE TO CHECK THAT IT HAS STARTED SUCCESSFULLY */ - it('Test ' + adapterShortName + ' adapter: Check if adapter started', function (done) { - this.timeout(60000); - checkConnectionOfAdapter(function (res) { - if (res) console.log(res); - expect(res).not.to.be.equal('Cannot check connection'); - objects.setObject('system.adapter.test.0', { - common: { - - }, - type: 'instance' - }, - function () { - states.subscribeMessage('system.adapter.test.0'); - done(); - }); - }); - }); -/**/ + it("Test " + adapterShortName + " adapter: Check if adapter started", function (done) { + this.timeout(60000); + checkConnectionOfAdapter(function (res) { + if (res) console.log(res); + expect(res).not.to.be.equal("Cannot check connection"); + objects.setObject("system.adapter.test.0", { + common: { + + }, + type: "instance" + }, + function () { + states.subscribeMessage("system.adapter.test.0"); + done(); + }); + }); + }); /* - PUT YOUR OWN TESTS HERE USING - it('Testname', function ( done) { - ... - }); + PUT YOUR OWN TESTS HERE USING + it('Testname', function ( done) { + ... + }); - You can also use "sendTo" method to send messages to the started adapter + You can also use "sendTo" method to send messages to the started adapter */ - after('Test ' + adapterShortName + ' adapter: Stop js-controller', function (done) { - this.timeout(10000); + after("Test " + adapterShortName + " adapter: Stop js-controller", function (done) { + this.timeout(10000); - setup.stopController(function (normalTerminated) { - console.log('Adapter normal terminated: ' + normalTerminated); - done(); - }); - }); + setup.stopController(function (normalTerminated) { + console.log("Adapter normal terminated: " + normalTerminated); + done(); + }); + }); }); `; return template.trim(); diff --git a/build/templates/test/tsconfig.json.js b/build/templates/test/tsconfig.json.js new file mode 100644 index 00000000..ff462600 --- /dev/null +++ b/build/templates/test/tsconfig.json.js @@ -0,0 +1,18 @@ +"use strict"; +module.exports = (answers => { + const useTypeScript = answers.language === "TypeScript"; + const useTypeChecking = answers.tools && answers.tools.indexOf("type checking") > -1; + if (!useTypeScript && !useTypeChecking) + return; + const template = ` +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "noImplicitAny": false + }, + "include": [ + "./**/*.js" + ] +}`; + return template.trim(); +}); diff --git a/src/templates/index.ts b/src/templates/index.ts index 02da7c3f..9bd11c73 100644 --- a/src/templates/index.ts +++ b/src/templates/index.ts @@ -26,6 +26,7 @@ const templates: { name: string, templateFunction: TemplateFunction }[] = [ { name: "test/mocha.setup.js.ts", templateFunction: require("./test/mocha.setup.js") }, { name: "test/testPackageFiles.js.ts", templateFunction: require("./test/testPackageFiles.js") }, { name: "test/testStartup.js.ts", templateFunction: require("./test/testStartup.js") }, + { name: "test/tsconfig.json.ts", templateFunction: require("./test/tsconfig.json") }, { name: "tsconfig.build.json.ts", templateFunction: require("./tsconfig.build.json") }, { name: "tsconfig.json.ts", templateFunction: require("./tsconfig.json") }, { name: "tslint.json.ts", templateFunction: require("./tslint.json") }, diff --git a/src/templates/test/lib/setup.js.ts b/src/templates/test/lib/setup.js.ts index 75fc56c5..dc8cd35c 100644 --- a/src/templates/test/lib/setup.js.ts +++ b/src/templates/test/lib/setup.js.ts @@ -4,733 +4,729 @@ import { Answers } from "../../../lib/questions"; export = (answers => { const template = ` -/* 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; +const fs = require("fs"); +const path = require("path"); +const child_process = require("child_process"); +const rootDir = path.normalize(__dirname + "/../../"); +const adapterName = path.basename(rootDir); +const pkg = require(rootDir + "package.json"); +const debug = typeof v8debug === "object"; +pkg.main = pkg.main || "main.js"; + +let adapterStarted = false; function getAppName() { - var parts = __dirname.replace(/\\\\/g, '/').split('/'); - return parts[parts.length - 3].split('.')[0]; + const parts = __dirname.replace(/\\\\/g, "/").split("/"); + return parts[parts.length - 3].split(".")[0]; } -var appName = getAppName().toLowerCase(); +const appName = getAppName().toLowerCase(); -var objects; -var states; +let objects; +let states; -var pid = null; +/** @type {undefined | ReturnType} */ +let pid; 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)"); - } + let 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); - } - }); - } + let files = []; + + let base = path.basename(source); + if (base === adapterName) { + base = pkg.name; + } + //check if folder needs to be created or integrated + const 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; + } + + const curSource = path.join(source, file); + const 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'); +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'); - } + console.log("Store original files..."); + const dataDir = rootDir + "tmp/" + appName + "-data/"; + + let f = fs.readFileSync(dataDir + "objects.json"); + const 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'); - } + console.log("restoreOriginalFiles..."); + const dataDir = rootDir + "tmp/" + appName + "-data/"; + + let 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); - } + customName = customName || pkg.name.split(".").pop(); + counter = counter || 0; + const dataDir = rootDir + "tmp/" + appName + "-data/"; + console.log("checkIsAdapterInstalled..."); + + try { + const f = fs.readFileSync(dataDir + "objects.json"); + const 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) { + // Don't care + } + + 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); - } + counter = counter || 0; + const dataDir = rootDir + "tmp/" + appName + "-data/"; + + console.log("checkIsControllerInstalled..."); + try { + const f = fs.readFileSync(dataDir + "objects.json"); + const objects = JSON.parse(f.toString()); + if (objects["system.adapter.admin.0"]) { + console.log("checkIsControllerInstalled: installed!"); + setTimeout(function () { + if (cb) cb(); + }, 100); + return; + } + } catch (err) { + // Don't care + } + + 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(); - }); - }); - } + if (typeof customName === "function") { + cb = customName; + customName = null; + } + customName = customName || pkg.name.split(".").pop(); + console.log("Install adapter..."); + const 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 + const _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); - } - }); + 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); - } + 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..."); + let _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..."); + let __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 + const 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 + const Socket = require("net").Socket; + const client = new 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..."); + 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 () { + let _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 + const 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.'); + 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); - } - } + const dirPath = rootDir + "tmp/log"; + let 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 (let i = 0; i < files.length; i++) { + const 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); - } - } + const dirPath = rootDir + "tmp/iobroker-data/sqlite"; + let 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 (let i = 0; i < files.length; i++) { + const 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']); - }); + installJsController(function (isInited) { + clearControllerLog(); + clearDB(); + + if (!isInited) { + restoreOriginalFiles(); + copyAdapterToController(); + } + // read system.config object + const dataDir = rootDir + "tmp/" + appName + "-data/"; + + let objs; + try { + objs = JSON.parse(fs.readFileSync(dataDir + "objects.json", "utf8")); + } + 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); + 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 - }); - } + 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; + let isObjectConnected; + let isStatesConnected; + + const 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 + const 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'); - } + 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 = undefined; + } + }); + + pid.on("close", () => { + if (pid) { + if (cb) cb(); + pid = undefined; + } + }); + + pid.kill("SIGTERM"); + } } function _stopController() { - if (objects) { - objects.destroy(); - objects = null; - } - if (states) { - states.destroy(); - states = null; - } + 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); + let 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 = undefined; + }, 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)); + const objects = JSON.parse(fs.readFileSync(rootDir + "tmp/" + appName + "-data/objects.json").toString()); + const 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]; + const objects = JSON.parse(fs.readFileSync(rootDir + "tmp/" + appName + "-data/objects.json").toString()); + const 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; + 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; } `; return template.trim(); diff --git a/src/templates/test/testPackageFiles.js.ts b/src/templates/test/testPackageFiles.js.ts index 4eee5860..b6ec3e49 100644 --- a/src/templates/test/testPackageFiles.js.ts +++ b/src/templates/test/testPackageFiles.js.ts @@ -3,96 +3,89 @@ import { TemplateFunction } from "../../lib/createAdapter"; export = (answers => { const template = ` -/* jshint -W097 */ -/* jshint strict:false */ -/* jslint node: true */ -/* jshint expr: true */ -var expect = require('chai').expect; -var fs = require('fs'); +const expect = require("chai").expect; +const fs = require("fs"); -describe('Test package.json and io-package.json', function() { - it('Test package files', function (done) { - console.log(); +describe("Test package.json and io-package.json", function() { + it("Test package files", function (done) { + console.log(); - var fileContentIOPackage = fs.readFileSync(__dirname + '/../io-package.json', 'utf8'); - var ioPackage = JSON.parse(fileContentIOPackage); + const ioPackage = require("../io-package.json"); + const npmPackage = require("../package.json"); - var fileContentNPMPackage = fs.readFileSync(__dirname + '/../package.json', 'utf8'); - var npmPackage = JSON.parse(fileContentNPMPackage); + expect(ioPackage).to.be.an("object"); + expect(npmPackage).to.be.an("object"); - expect(ioPackage).to.be.an('object'); - expect(npmPackage).to.be.an('object'); + 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; - 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; + expect(ioPackage.common.version, "ERROR: Version numbers in package.json and io-package.json needs to match").to.be.equal(npmPackage.version); - 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(); + } - 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(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; + 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.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.name.indexOf("vis-") !== 0) { + 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; + } + } - if (ioPackage.common.name.indexOf('vis-') !== 0) { - 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; - } - } - - var licenseFileExists = fs.existsSync(__dirname + '/../LICENSE'); - var 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(); - }); + 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(); + }); }); `; return template.trim(); diff --git a/src/templates/test/testStartup.js.ts b/src/templates/test/testStartup.js.ts index 2730753e..930eaf39 100644 --- a/src/templates/test/testStartup.js.ts +++ b/src/templates/test/testStartup.js.ts @@ -3,145 +3,142 @@ import { TemplateFunction } from "../../lib/createAdapter"; export = (answers => { const template = ` -/* jshint -W097 */// jshint strict:false -/*jslint node: true */ -var expect = require('chai').expect; -var setup = require('./lib/setup'); +const expect = require("chai").expect; +const setup = require("./lib/setup"); -var objects = null; -var states = null; -var onStateChanged = null; -var onObjectChanged = null; -var sendToID = 1; +let objects = null; +let states = null; +let onStateChanged = null; +// let onObjectChanged = null; +let sendToID = 1; -var adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.')+1); +const adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf(".")+1); function checkConnectionOfAdapter(cb, counter) { - counter = counter || 0; - console.log('Try check #' + counter); - if (counter > 30) { - if (cb) cb('Cannot check connection'); - return; - } - - states.getState('system.adapter.' + adapterShortName + '.0.alive', function (err, state) { - if (err) console.error(err); - if (state && state.val) { - if (cb) cb(); - } else { - setTimeout(function () { - checkConnectionOfAdapter(cb, counter + 1); - }, 1000); - } - }); + counter = counter || 0; + console.log("Try check #" + counter); + if (counter > 30) { + if (cb) cb("Cannot check connection"); + return; + } + + states.getState("system.adapter." + adapterShortName + ".0.alive", function (err, state) { + if (err) console.error(err); + if (state && state.val) { + if (cb) cb(); + } else { + setTimeout(function () { + checkConnectionOfAdapter(cb, counter + 1); + }, 1000); + } + }); } function checkValueOfState(id, value, cb, counter) { - counter = counter || 0; - if (counter > 20) { - if (cb) cb('Cannot check value Of State ' + id); - return; - } - - states.getState(id, function (err, state) { - if (err) console.error(err); - if (value === null && !state) { - if (cb) cb(); - } else - if (state && (value === undefined || state.val === value)) { - if (cb) cb(); - } else { - setTimeout(function () { - checkValueOfState(id, value, cb, counter + 1); - }, 500); - } - }); + counter = counter || 0; + if (counter > 20) { + if (cb) cb("Cannot check value Of State " + id); + return; + } + + states.getState(id, function (err, state) { + if (err) console.error(err); + if (value === null && !state) { + if (cb) cb(); + } else + if (state && (value === undefined || state.val === value)) { + if (cb) cb(); + } else { + setTimeout(function () { + checkValueOfState(id, value, cb, counter + 1); + }, 500); + } + }); } function sendTo(target, command, message, callback) { - onStateChanged = function (id, state) { - if (id === 'messagebox.system.adapter.test.0') { - callback(state.message); - } - }; - - states.pushMessage('system.adapter.' + target, { - command: command, - message: message, - from: 'system.adapter.test.0', - callback: { - message: message, - id: sendToID++, - ack: false, - time: (new Date()).getTime() - } - }); + onStateChanged = function (id, state) { + if (id === "messagebox.system.adapter.test.0") { + callback(state.message); + } + }; + + states.pushMessage("system.adapter." + target, { + command: command, + message: message, + from: "system.adapter.test.0", + callback: { + message: message, + id: sendToID++, + ack: false, + time: (new Date()).getTime() + } + }); } -describe('Test ' + adapterShortName + ' adapter', function() { - before('Test ' + adapterShortName + ' adapter: Start js-controller', function (_done) { - this.timeout(600000); // because of first install from npm +describe("Test " + adapterShortName + " adapter", function() { + before("Test " + adapterShortName + " adapter: Start js-controller", function (_done) { + this.timeout(600000); // because of first install from npm - setup.setupController(function () { - var config = setup.getAdapterConfig(); - // enable adapter - config.common.enabled = true; - config.common.loglevel = 'debug'; + setup.setupController(function () { + const config = setup.getAdapterConfig(); + // enable adapter + config.common.enabled = true; + config.common.loglevel = "debug"; - //config.native.dbtype = 'sqlite'; + //config.native.dbtype = 'sqlite'; - setup.setAdapterConfig(config.common, config.native); + setup.setAdapterConfig(config.common, config.native); - setup.startController(true, function(id, obj) {}, function (id, state) { - if (onStateChanged) onStateChanged(id, state); - }, - function (_objects, _states) { - objects = _objects; - states = _states; - _done(); - }); - }); - }); + setup.startController(true, (id, obj) => { }, function (id, state) { + if (onStateChanged) onStateChanged(id, state); + }, + function (_objects, _states) { + objects = _objects; + states = _states; + _done(); + }); + }); + }); /* - ENABLE THIS WHEN ADAPTER RUNS IN DEAMON MODE TO CHECK THAT IT HAS STARTED SUCCESSFULLY + ENABLE THIS WHEN ADAPTER RUNS IN DEAMON MODE TO CHECK THAT IT HAS STARTED SUCCESSFULLY */ - it('Test ' + adapterShortName + ' adapter: Check if adapter started', function (done) { - this.timeout(60000); - checkConnectionOfAdapter(function (res) { - if (res) console.log(res); - expect(res).not.to.be.equal('Cannot check connection'); - objects.setObject('system.adapter.test.0', { - common: { - - }, - type: 'instance' - }, - function () { - states.subscribeMessage('system.adapter.test.0'); - done(); - }); - }); - }); -/**/ + it("Test " + adapterShortName + " adapter: Check if adapter started", function (done) { + this.timeout(60000); + checkConnectionOfAdapter(function (res) { + if (res) console.log(res); + expect(res).not.to.be.equal("Cannot check connection"); + objects.setObject("system.adapter.test.0", { + common: { + + }, + type: "instance" + }, + function () { + states.subscribeMessage("system.adapter.test.0"); + done(); + }); + }); + }); /* - PUT YOUR OWN TESTS HERE USING - it('Testname', function ( done) { - ... - }); + PUT YOUR OWN TESTS HERE USING + it('Testname', function ( done) { + ... + }); - You can also use "sendTo" method to send messages to the started adapter + You can also use "sendTo" method to send messages to the started adapter */ - after('Test ' + adapterShortName + ' adapter: Stop js-controller', function (done) { - this.timeout(10000); + after("Test " + adapterShortName + " adapter: Stop js-controller", function (done) { + this.timeout(10000); - setup.stopController(function (normalTerminated) { - console.log('Adapter normal terminated: ' + normalTerminated); - done(); - }); - }); + setup.stopController(function (normalTerminated) { + console.log("Adapter normal terminated: " + normalTerminated); + done(); + }); + }); }); `; return template.trim(); diff --git a/src/templates/test/tsconfig.json.ts b/src/templates/test/tsconfig.json.ts new file mode 100644 index 00000000..43de33e4 --- /dev/null +++ b/src/templates/test/tsconfig.json.ts @@ -0,0 +1,20 @@ +import { TemplateFunction } from "../../lib/createAdapter"; + +export = (answers => { + + const useTypeScript = answers.language === "TypeScript"; + const useTypeChecking = answers.tools && answers.tools.indexOf("type checking") > -1; + if (!useTypeScript && !useTypeChecking) return; + + const template = ` +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "noImplicitAny": false + }, + "include": [ + "./**/*.js" + ] +}`; + return template.trim(); +}) as TemplateFunction;