From 4e5a69e39d23a73cf327c6434b065ffd8b7163c0 Mon Sep 17 00:00:00 2001 From: SejoongDeJang Date: Fri, 23 Oct 2020 14:40:27 +0900 Subject: [PATCH 1/8] Add the logger.js to support multiple output --- command/certificate.js | 10 ++++---- command/init.js | 16 ++++++------- command/start.js | 10 ++++---- command/watch.js | 4 ++-- index.js | 18 ++++++++------- lib/appLaunchHelper.js | 12 +++++----- lib/cli.js | 1 + lib/deviceConnectHelper.js | 12 ++++------ lib/hostAppHelper.js | 28 +++++++++++------------ lib/logger.js | 47 ++++++++++++++++++++++++++++++++++++++ lib/userInfoHelper.js | 32 +++++++++++++------------- lib/util.js | 18 +++++++-------- lib/watchHelper.js | 34 +++++++++++++-------------- 13 files changed, 144 insertions(+), 98 deletions(-) create mode 100644 lib/logger.js diff --git a/command/certificate.js b/command/certificate.js index e1a1cca..47d3b2a 100644 --- a/command/certificate.js +++ b/command/certificate.js @@ -6,7 +6,7 @@ const path = require('path'); module.exports = { run: async () => { - console.log(chalk.cyanBright('Generate a certification............\n')); + logger.log(chalk.cyanBright('Generate a certification............\n')); const resourceDir = util.RESOURCE_PATH; @@ -33,7 +33,7 @@ module.exports = { emailInfo: certInfo.emailInfo ? certInfo.emailInfo : '' }; tizenCertManager.createCert(authorInfo); - console.log( + logger.log( chalk.cyanBright( '[Certification] Completed to generate a Tizen certification' ) @@ -59,7 +59,7 @@ module.exports = { authorProfile, distributorProfile ); - console.log( + logger.log( chalk.cyanBright( '[Certification] Completed to register a profile' ) @@ -67,7 +67,7 @@ module.exports = { profileManager.setActivateProfile(profileName); - console.log( + logger.log( chalk.cyanBright( `[Certification] Completed to genarate a certification. ${path.resolve( path.join(resourceDir, 'profiles.xml') @@ -75,7 +75,7 @@ module.exports = { ) ); } catch (e) { - console.error(chalk.red(`[Certification] Failed to run: ${e}`)); + logger.error(chalk.red(`[Certification] Failed to run: ${e}`)); } } }; diff --git a/command/init.js b/command/init.js index 8793416..4ec34ca 100644 --- a/command/init.js +++ b/command/init.js @@ -10,7 +10,7 @@ let WITSIGNORE_PATH = ''; module.exports = { run: async () => { - console.log( + logger.log( chalk.cyanBright(`Start configuration for Wits............\n`) ); @@ -20,7 +20,7 @@ module.exports = { const wInfo = userInfoHelper.getRefinedData(); await userInfoHelper.askQuestion(wInfo.connectionInfo); } catch (e) { - console.error(chalk.red(`Failed to run: ${e}`)); + logger.error(chalk.red(`Failed to run: ${e}`)); } }, prepareConfigure: async () => { @@ -66,27 +66,27 @@ module.exports = { function makeWitsignoreFile() { try { if (util.isFileExist(WITSIGNORE_PATH)) { - console.log('.witsignore is already exist.'); + logger.log('.witsignore is already exist.'); return; } util.createEmptyFile(WITSIGNORE_PATH, 'node_modules'); - console.log('witsignore is prepared.'); + logger.log('witsignore is prepared.'); } catch (error) { - console.error(chalk.red(`Failed to makeWitsignoreFile ${error}`)); + logger.error(chalk.red(`Failed to makeWitsignoreFile ${error}`)); } } function makeWitsconfigFile() { try { if (util.isFileExist(WITSCONFIG_PATH) && isExistCustomFile()) { - console.log('.witsconfig.json is already exist.'); + logger.log('.witsconfig.json is already exist.'); return; } util.createEmptyFile(WITSCONFIG_PATH, '{}'); - console.log('.witsconfig.json is prepared.'); + logger.log('.witsconfig.json is prepared.'); } catch (error) { - console.error(chalk.red(`Failed to makeWitsconfigFile ${error}`)); + logger.error(chalk.red(`Failed to makeWitsconfigFile ${error}`)); } } diff --git a/command/start.js b/command/start.js index c265718..ce7f3ff 100644 --- a/command/start.js +++ b/command/start.js @@ -7,7 +7,7 @@ const watchHelper = require('../lib/watchHelper.js'); module.exports = { run: async () => { - console.log(chalk.cyanBright(`Start running Wits............\n`)); + logger.log(chalk.cyanBright(`Start running Wits............\n`)); await util.initTools(); @@ -17,14 +17,14 @@ module.exports = { try { deviceInfo = await userInfoHelper.getDeviceInfo(data.deviceIp); } catch (error) { - console.log(`Failed to getDeviceInfo: ${error}`); + logger.log(`Failed to getDeviceInfo: ${error}`); } await hostAppHelper.setHostAppEnv(data, deviceInfo); hostAppHelper .buildPackage() .then(() => { - console.log( + logger.log( chalk.cyanBright( '============================== Start to install the package' ) @@ -46,12 +46,12 @@ module.exports = { ) : appLaunchHelper.launchApp(deviceName, hostAppId); } catch (e) { - console.log(e); + logger.log(e); util.exit(); } }) .catch(e => { - console.error(chalk.red(`Failed to buildPackage: ${e}`)); + logger.error(chalk.red(`Failed to buildPackage: ${e}`)); util.exit(); }); } diff --git a/command/watch.js b/command/watch.js index 7227a80..d92284e 100644 --- a/command/watch.js +++ b/command/watch.js @@ -7,7 +7,7 @@ const chalk = require('chalk'); module.exports = { run: async () => { - console.log( + logger.log( chalk.cyanBright(`Start running Wits watch mode............\n`) ); @@ -35,7 +35,7 @@ module.exports = { ) : appLaunchHelper.launchApp(deviceName, hostAppId); } catch (e) { - console.log(e); + logger.log(e); util.exit(); } } diff --git a/index.js b/index.js index b8bb890..fcc729d 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,11 @@ const util = require('./lib/util.js'); const userInfoHelper = require('./lib/userInfoHelper.js'); +const { setOutputChannel } = require('./lib/logger'); const setWitsconfigInfo = async data => { try { const initCommand = require('./command/init.js'); - console.log('WITs::setWitsconfigInfo'); + logger.log('WITs::setWitsconfigInfo'); if (data.hasOwnProperty('baseAppPath')) { util.CURRENT_PROJECT_PATH = userInfoHelper.getBaseAppPath( @@ -34,7 +35,7 @@ const setWitsconfigInfo = async data => { } */ } catch (error) { - console.log(`setWitsconfigInfo:::${error}`); + logger.log(`setWitsconfigInfo:::${error}`); } }; @@ -44,12 +45,12 @@ const start = async () => { throw new Error('There is invalid WITS_USER_DATA'); } const startCommand = require('./command/start.js'); - console.log('WITs::start'); + logger.log('WITs::start'); await startCommand.run(); return; } catch (error) { - console.log(`start:::${error}`); + logger.log(`start:::${error}`); } }; @@ -59,12 +60,12 @@ const watch = async () => { throw new Error('There is invalid WITS_USER_DATA'); } const watchCommand = require('./command/watch.js'); - console.log('WITs::watch'); + logger.log('WITs::watch'); await watchCommand.run(); return; } catch (error) { - console.log(`watch:::${error}`); + logger.log(`watch:::${error}`); } }; @@ -74,7 +75,7 @@ const disconnect = () => { try { watchHelper.closeSocketServer(); } catch (error) { - console.log(`disconnect:::${error}`); + logger.log(`disconnect:::${error}`); } }; @@ -82,5 +83,6 @@ module.exports = { setWitsconfigInfo, start, watch, - disconnect + disconnect, + setOutputChannel }; diff --git a/lib/appLaunchHelper.js b/lib/appLaunchHelper.js index 1dd4300..d8941ca 100644 --- a/lib/appLaunchHelper.js +++ b/lib/appLaunchHelper.js @@ -25,7 +25,7 @@ module.exports = { const result = execSync(APP_INSTALL_COMMAND).toString(); if (result.includes('failed[')) { - console.error(chalk.red(`\nFailed to install Wits`)); + logger.error(chalk.red(`\nFailed to install Wits`)); util.exit(); } }, @@ -36,7 +36,7 @@ module.exports = { }).toString(); if (result.includes('failed[')) { - console.warn(`\n[warning] Failed to uninstall Wits`); + logger.warn(`\n[warning] Failed to uninstall Wits`); } }, launchApp: (deviceName, hostAppId) => { @@ -77,7 +77,7 @@ module.exports = { try { launchChrome(debugIP + ':' + debugPort); } catch (e) { - console.log( + logger.log( `Please install a Chrome browser or input ${debugIP}:${debugPort} into the address bar of the chromium based browser. ${e}` ); } @@ -99,7 +99,7 @@ function setPortForward(deviceName, port) { try { launchChrome(LOCAL_HOST + ':' + port); } catch (e) { - console.log( + logger.log( `Please install a Chrome browser or input ${LOCAL_HOST}:${port} into the address bar of the chromium based browser. ${e}` ); } @@ -115,9 +115,9 @@ function launchChrome(url) { ] }) .then(chrome => { - console.log(`Chrome debugging port running on ${chrome.port}`); + logger.log(`Chrome debugging port running on ${chrome.port}`); }) .catch(e => { - console.log(chalk.red(`Please install a Chrome browser. ${e}`)); + logger.log(chalk.red(`Please install a Chrome browser. ${e}`)); }); } diff --git a/lib/cli.js b/lib/cli.js index edd36a7..7ed7897 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,5 +1,6 @@ #!/usr/bin/env node +require('./logger'); const commander = require('commander'); const program = new commander.Command(); const package = require('../package.json'); diff --git a/lib/deviceConnectHelper.js b/lib/deviceConnectHelper.js index cbb9b34..70fe929 100644 --- a/lib/deviceConnectHelper.js +++ b/lib/deviceConnectHelper.js @@ -21,16 +21,14 @@ module.exports = { util.isIpAddress(deviceIpAddress) && deviceIpAddress !== EMULATOR_IP ) { - console.log(`connect to....${deviceIpAddress}`); + logger.log(`connect to....${deviceIpAddress}`); const CONNECT_TV_COMMAND = `${util.TOOLS_SDB_PATH} connect ${deviceIpAddress}:${TV_CONNECT_PORT}`; const connectResult = execSync(CONNECT_TV_COMMAND).toString(); if (connectResult.includes('connected')) { - console.log(`Success to connect ${deviceIpAddress}`); + logger.log(`Success to connect ${deviceIpAddress}`); } else { - console.error( - chalk.red(`Failed to connect ${deviceIpAddress}`) - ); + logger.error(chalk.red(`Failed to connect ${deviceIpAddress}`)); util.exit(); } } @@ -41,7 +39,7 @@ module.exports = { const deviceNameList = getConnectedDeviceList(deviceIpAddress); if (deviceNameList.length === NONE_DEVICE) { - console.error(chalk.red(`No connected devices.`)); + logger.error(chalk.red(`No connected devices.`)); util.exit(); } else if (deviceNameList.length === SINGLE_DEVICE) { const deviceName = deviceNameList[0]; @@ -89,7 +87,7 @@ function getConnectedDeviceList(deviceIpAddress) { devicesInfo.shift(); deviceNameList = parsingDeviceName(devicesInfo); } else { - console.error(chalk.red(`Failed to connect ${deviceIpAddress}`)); + logger.error(chalk.red(`Failed to connect ${deviceIpAddress}`)); util.exit(); } return deviceNameList; diff --git a/lib/hostAppHelper.js b/lib/hostAppHelper.js index ee7bff8..b46bf59 100644 --- a/lib/hostAppHelper.js +++ b/lib/hostAppHelper.js @@ -28,9 +28,7 @@ module.exports = { .replace(regExp.APPLICATION_ID_ATTRIBUTE, ''); return id + WITS_NAME_TAG; } catch (e) { - console.error( - chalk.red(`Failed to read base app config.xml. ${e}`) - ); + logger.error(chalk.red(`Failed to read base app config.xml. ${e}`)); util.exit(); } }, @@ -42,7 +40,7 @@ module.exports = { util.CURRENT_PROJECT_PATH ); try { - console.log( + logger.log( chalk.cyanBright( `\nStart packaging Samsung Tizen TV Platform......\n` ) @@ -53,7 +51,7 @@ module.exports = { hostAppId ); if (!util.isFileExist(profilePath)) { - console.error( + logger.error( chalk.red( `Please check ${profilePath} is valid. For making a new certification, please do "wits -c"` ) @@ -62,15 +60,15 @@ module.exports = { } await app.buildWidget(profilePath); - console.log( + logger.log( chalk.cyanBright( '============================== Build Package completed!' ) ); - console.log(''); + logger.log(''); return; } catch (error) { - console.log('Failed to buildPackage : ' + error); + logger.log('Failed to buildPackage : ' + error); } } }; @@ -83,7 +81,7 @@ async function makeHostAppConfigFile(baseAppPath) { 'utf8' ); } catch (e) { - console.error(chalk.red(`Failed to read user config.xml. ${e}`)); + logger.error(chalk.red(`Failed to read user config.xml. ${e}`)); util.exit(); return; } @@ -99,7 +97,7 @@ async function makeHostAppConfigFile(baseAppPath) { if (parsedXmlData && parsedXmlData.widget) { setDefaultConfigData(parsedXmlData.widget); } else { - console.log(`User config.xml is not supported format.`); + logger.log(`User config.xml is not supported format.`); util.exit(); } @@ -117,7 +115,7 @@ async function makeHostAppConfigFile(baseAppPath) { 'utf8' ); } catch (e) { - console.error(chalk.red(`Failed to write Wits config.xml. ${e}`)); + logger.error(chalk.red(`Failed to write Wits config.xml. ${e}`)); util.exit(); return; } @@ -233,7 +231,7 @@ async function setBaseJSData(userAnswer, deviceInfo) { ); return; } catch (e) { - console.error(chalk.red(`Failed to set Wits baseJS data to file ${e}`)); + logger.error(chalk.red(`Failed to set Wits baseJS data to file ${e}`)); util.exit(); return; } @@ -257,7 +255,7 @@ function setBaseHtmlData(userAnswer) { 'utf8' ); } catch (e) { - console.error(chalk.red(`Failed to set Wits baseHtml data to file`)); + logger.error(chalk.red(`Failed to set Wits baseHtml data to file`)); util.exit(); } } @@ -273,12 +271,12 @@ function getContentSrc(baseAppPath) { .match(regExp.CONTENT_SRC)[0] .replace(regExp.CONTENT_SRC_ATTRIBUTE, ''); } catch (e) { - console.warn( + logger.warn( `[warning] Failed to read config.xml. Set Content src to default.` ); } - console.log(`content src is : ${contentSrc}`); + logger.log(`content src is : ${contentSrc}`); return contentSrc; } diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 0000000..8e60e55 --- /dev/null +++ b/lib/logger.js @@ -0,0 +1,47 @@ +const outputChannel = []; + +const setOutputChannel = output => { + if (typeof output !== 'function') { + throw new Error(`output should be function`); + } + outputChannel.push(output); +}; + +function error() { + var args = [].slice.call(arguments); + [console.error, ...outputChannel].forEach(print => { + print(...args); + }); +} + +function warn() { + var args = [].slice.call(arguments); + [console.warn, ...outputChannel].forEach(print => { + print(...args); + }); +} + +function log() { + var args = [].slice.call(arguments); + [console.log, ...outputChannel].forEach(print => { + print(...args); + }); +} + +function debug() { + var args = [].slice.call(arguments); + console.debug(...args); +} + +const logger = { + error, + warn, + log, + debug +}; + +global.logger = logger; + +module.exports = { + setOutputChannel +}; diff --git a/lib/userInfoHelper.js b/lib/userInfoHelper.js index bc3140a..d6192ff 100644 --- a/lib/userInfoHelper.js +++ b/lib/userInfoHelper.js @@ -62,7 +62,7 @@ module.exports = { }, getDeviceInfo: async deviceIp => { if ((deviceIp === null) | (deviceIp === undefined)) { - console.error( + logger.error( chalk.red( 'There is no deviceIp, Please do "wits -i" for configuration.' ) @@ -93,7 +93,7 @@ module.exports = { return result; } catch (e) { - console.warn(`[warning] Failed to getLatestWitsconfigInfo >> ${e}`); + logger.warn(`[warning] Failed to getLatestWitsconfigInfo >> ${e}`); } }, getBaseAppPath: baseAppPath => { @@ -175,7 +175,7 @@ module.exports = { 'utf8' ); } catch (e) { - console.warn('[warning] Failed to set recently connection info'); + logger.warn('[warning] Failed to set recently connection info'); } } }; @@ -220,17 +220,17 @@ function getProfilePath() { } function displayStoredInfo(data) { - console.log(``); - console.log(` > [ Stored Information ]`); - console.log(``); - console.log(` > baseAppPath : ${data.baseAppPath}`); - console.log(` > width : ${data.width}`); - console.log(` > deviceIp : ${data.deviceIp}`); - console.log(` > isDebugMode : ${data.isDebugMode}`); - console.log(``); - console.log(` > profile path : ${data.profilePath}`); - console.log(` > hostIp : ${data.hostIp}`); - console.log(``); + logger.log(``); + logger.log(` > [ Stored Information ]`); + logger.log(``); + logger.log(` > baseAppPath : ${data.baseAppPath}`); + logger.log(` > width : ${data.width}`); + logger.log(` > deviceIp : ${data.deviceIp}`); + logger.log(` > isDebugMode : ${data.isDebugMode}`); + logger.log(``); + logger.log(` > profile path : ${data.profilePath}`); + logger.log(` > hostIp : ${data.hostIp}`); + logger.log(``); } function getWitsconfigData() { @@ -250,7 +250,7 @@ function getWitsconfigData() { ); // if (_.isEqual(witsconfigInfo, {})) { - // console.warn( + // logger.warn( // 'Please check .witsconfig.json is empty. If so, "wits -i" is required.' // ); // } @@ -258,7 +258,7 @@ function getWitsconfigData() { module.exports.WITS_USER_DATA = witsconfigInfo; return witsconfigInfo; } catch (e) { - console.error(chalk.red(`Failed to getWitsconfig: ${e}`)); + logger.error(chalk.red(`Failed to getWitsconfig: ${e}`)); util.exit(); } } diff --git a/lib/util.js b/lib/util.js index a78268e..14f37a8 100644 --- a/lib/util.js +++ b/lib/util.js @@ -50,7 +50,7 @@ module.exports = { fs.writeFileSync(path.join(filepath), content, 'utf8'); fs.chmodSync(path.join(filepath), '0775'); } catch (error) { - console.error( + logger.error( chalk.red(`Failed to createEmptyFile ${filepath} ${error}`) ); util.exit(); @@ -62,7 +62,7 @@ module.exports = { try { mkdirp.sync(dirname); } catch (error) { - console.error( + logger.error( chalk.red(`Failed to createEmptyDirectory ${dirname} ${error}`) ); } @@ -73,7 +73,7 @@ module.exports = { try { fs.unlinkSync(filepath); } catch (e) { - console.error( + logger.error( chalk.red(`Failed to removeFile ${filepath} ${e}`) ); throw e; @@ -88,7 +88,7 @@ module.exports = { module.exports.removeFile(src); } } catch (e) { - console.error(chalk.red(`Failed to moveFile: ${e}`)); + logger.error(chalk.red(`Failed to moveFile: ${e}`)); throw e; } }, @@ -99,7 +99,7 @@ module.exports = { fs.createReadStream(src).pipe(fs.createWriteStream(dest)); } } catch (e) { - console.error(chalk.red(`Failed to copyFile: ${e}`)); + logger.error(chalk.red(`Failed to copyFile: ${e}`)); throw e; } }, @@ -172,10 +172,10 @@ module.exports = { }, displayBanner() { - console.log(' _ ____________ '); - console.log('| | /| / / _/_ __/__'); - console.log('| |/ |/ // / / / (_-<'); - console.log('|__/|__/___/ /_/ /___/\n'); + logger.log(' _ ____________ '); + logger.log('| | /| / / _/_ __/__'); + logger.log('| |/ |/ // / / / (_-<'); + logger.log('|__/|__/___/ /_/ /___/\n'); }, chmodAll(path) { diff --git a/lib/watchHelper.js b/lib/watchHelper.js index fea7cc6..1edb4b9 100644 --- a/lib/watchHelper.js +++ b/lib/watchHelper.js @@ -32,12 +32,12 @@ module.exports = { appLaunchHelper.terminateApp(deviceName, hostAppId); http.listen(socketPort, data.hostIp, () => { - console.log(`listening on ${socketPort}`); + logger.log(`listening on ${socketPort}`); }); mediator = io.on('connection', socket => { - console.log(`a user connected`); - console.log(`new client connected, id = ${socket.id} `); + logger.log(`a user connected`); + logger.log(`new client connected, id = ${socket.id} `); socket.emit('response', { rsp: { @@ -46,22 +46,22 @@ module.exports = { }); socket.on('disconnect', () => { - console.log(`disconnect, id = ${socket.id}`); + logger.log(`disconnect, id = ${socket.id}`); socket.disconnect(true); watcher.close(); }); socket.once('connect_error', () => { - console.log(`socket once::::connect_error`); + logger.log(`socket once::::connect_error`); }); socket.on('push_request', () => { - console.log(`socket on::::push_request`); + logger.log(`socket on::::push_request`); startPushFiles(data.baseAppPath, hostAppPath); }); socket.on('watch_request', path => { - console.log(`socket on::::watch_request`); + logger.log(`socket on::::watch_request`); watchAppCode(path.basePath, path.destPath); }); }); @@ -88,7 +88,7 @@ function getWitsIgnore(baseAppPath) { ignore = ignoreData.replace(regExp.FIND_ALL_CR, '').split('\n'); } } catch (e) { - console.warn(`[warning] Failed to get Wits ignore ${e}`); + logger.warn(`[warning] Failed to get Wits ignore ${e}`); } return ignore; } @@ -98,7 +98,7 @@ async function getContentFiles(baseAppPath) { try { data = await recursiveReadDir(baseAppPath, [ignoreFunc]); } catch (e) { - console.error(chalk.red(`Failed to get content files`)); + logger.error(chalk.red(`Failed to get content files`)); util.exit(); return; } @@ -126,7 +126,7 @@ function startPushFiles(baseAppPath, hostAppPath) { getContentFiles(baseAppPath).then(files => { totalFileNum = files.length; - console.log(`Total File Number : ${totalFileNum}`); + logger.log(`Total File Number : ${totalFileNum}`); const contentFilesInfo = { files: files, curIdx: START_PUSH_INDEX, @@ -168,11 +168,11 @@ function pushFile(baseAppPath, hostAppPath, filesInfo) { function watchAppCode(basePath, destPath) { watcher = watch(basePath, { recursive: true }, (evt, name) => { - console.log(`${name} ${evt}`); + logger.log(`${name} ${evt}`); const filePath = name .replace(regExp.BACKSLASH, '/') .replace(basePath, ''); - console.log(`watch file : ${filePath}`); + logger.log(`watch file : ${filePath}`); if (!isIgnore(filePath)) { if (evt === WATCHER_EVENT_UPDATE) { pushUpdated(basePath, destPath, filePath); @@ -187,7 +187,7 @@ function isIgnore(path) { let isIgnore = false; witsIgnores.some(ignore => { if (path.includes(ignore)) { - console.log(`This watch file is ignore.`); + logger.log(`This watch file is ignore.`); isIgnore = true; return true; } @@ -196,17 +196,17 @@ function isIgnore(path) { } function pushUpdated(basePath, destPath, filePath) { - console.log(filePath); + logger.log(filePath); const UPDATE_FILE_PUSH_COMMAND = `${util.TOOLS_SDB_PATH} -s ${deviceName} push "${basePath}${filePath}" "${destPath}${filePath}"`; exec(UPDATE_FILE_PUSH_COMMAND, (code, stdout, stderr) => { if (stderr) { const COMPATIBILITY_ERROR = 'version compatibility problems'; if (!stderr.includes(COMPATIBILITY_ERROR)) { - console.log(`Failed ${stderr}`); + logger.log(`Failed ${stderr}`); util.exit(); } } - console.log(`Program output : ${stdout}`); + logger.log(`Program output : ${stdout}`); if (stdout.includes('file(s) pushed')) { mediator.emit('changed'); } @@ -214,6 +214,6 @@ function pushUpdated(basePath, destPath, filePath) { } function emitRemoved(basePath, destPath, filePath) { - console.log(filePath); + logger.log(filePath); mediator.emit(WATCHER_EVENT_REMOVE, destPath + filePath); } From d8a94b89885ab3ee5d8e40719657798a6d6d6622 Mon Sep 17 00:00:00 2001 From: SejoongDeJang Date: Mon, 26 Oct 2020 15:01:11 +0900 Subject: [PATCH 2/8] Replace the global delcaration to local --- command/certificate.js | 1 + command/init.js | 1 + command/start.js | 1 + command/watch.js | 1 + index.js | 2 +- lib/appLaunchHelper.js | 1 + lib/cli.js | 1 - lib/deviceConnectHelper.js | 1 + lib/hostAppHelper.js | 2 +- lib/logger.js | 5 ++--- lib/userInfoHelper.js | 1 + lib/util.js | 5 +++-- lib/watchHelper.js | 1 + 13 files changed, 15 insertions(+), 8 deletions(-) diff --git a/command/certificate.js b/command/certificate.js index 47d3b2a..0005ec1 100644 --- a/command/certificate.js +++ b/command/certificate.js @@ -3,6 +3,7 @@ const common = require('@tizentv/webide-common-tizentv'); const chalk = require('chalk'); const util = require('../lib/util'); const path = require('path'); +const { logger } = require('../lib/logger'); module.exports = { run: async () => { diff --git a/command/init.js b/command/init.js index 4ec34ca..76cc151 100644 --- a/command/init.js +++ b/command/init.js @@ -4,6 +4,7 @@ const chalk = require('chalk'); const xml2js = require('xml2js'); const util = require('../lib/util.js'); const userInfoHelper = require('../lib/userInfoHelper.js'); +const { logger } = require('../lib/logger'); let WITSCONFIG_PATH = ''; let WITSIGNORE_PATH = ''; diff --git a/command/start.js b/command/start.js index ce7f3ff..b890646 100644 --- a/command/start.js +++ b/command/start.js @@ -4,6 +4,7 @@ const userInfoHelper = require('../lib/userInfoHelper.js'); const hostAppHelper = require('../lib/hostAppHelper.js'); const appLaunchHelper = require('../lib/appLaunchHelper.js'); const watchHelper = require('../lib/watchHelper.js'); +const { logger } = require('../lib/logger'); module.exports = { run: async () => { diff --git a/command/watch.js b/command/watch.js index d92284e..3a900e7 100644 --- a/command/watch.js +++ b/command/watch.js @@ -4,6 +4,7 @@ const appLaunchHelper = require('../lib/appLaunchHelper.js'); const watchHelper = require('../lib/watchHelper.js'); const util = require('../lib/util.js'); const chalk = require('chalk'); +const { logger } = require('../lib/logger'); module.exports = { run: async () => { diff --git a/index.js b/index.js index fcc729d..4a97ac8 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ const util = require('./lib/util.js'); const userInfoHelper = require('./lib/userInfoHelper.js'); -const { setOutputChannel } = require('./lib/logger'); +const { setOutputChannel, logger } = require('./lib/logger'); const setWitsconfigInfo = async data => { try { diff --git a/lib/appLaunchHelper.js b/lib/appLaunchHelper.js index d8941ca..e64f950 100644 --- a/lib/appLaunchHelper.js +++ b/lib/appLaunchHelper.js @@ -4,6 +4,7 @@ const { execSync } = require('child_process'); const chromeLauncher = require('chrome-launcher'); const regExp = require('./regexp.js'); const util = require('./util.js'); +const { logger } = require('./logger'); const PACKAGE_BASE_PATH = path.join(util.WITS_BASE_PATH, '../', 'container'); const WITS_PACKAGE = 'WITs.wgt'; diff --git a/lib/cli.js b/lib/cli.js index 7ed7897..edd36a7 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,6 +1,5 @@ #!/usr/bin/env node -require('./logger'); const commander = require('commander'); const program = new commander.Command(); const package = require('../package.json'); diff --git a/lib/deviceConnectHelper.js b/lib/deviceConnectHelper.js index 70fe929..7c79b6b 100644 --- a/lib/deviceConnectHelper.js +++ b/lib/deviceConnectHelper.js @@ -3,6 +3,7 @@ const { execSync } = require('child_process'); const util = require('./util.js'); const regExp = require('./regexp.js'); +const { logger } = require('./logger'); const EMULATOR_IP = '0.0.0.0'; const TV_CONNECT_PORT = '26101'; diff --git a/lib/hostAppHelper.js b/lib/hostAppHelper.js index b46bf59..d6997f0 100644 --- a/lib/hostAppHelper.js +++ b/lib/hostAppHelper.js @@ -7,7 +7,7 @@ const common = require('@tizentv/webide-common-tizentv'); const util = require('./util.js'); const userInfoHelper = require('./userInfoHelper.js'); const regExp = require('./regexp.js'); -const e = require('express'); +const { logger } = require('./logger'); const CONFIG_FILE = 'config.xml'; const WITS_NAME_TAG = 'WITs'; diff --git a/lib/logger.js b/lib/logger.js index 8e60e55..ca2cbcb 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -40,8 +40,7 @@ const logger = { debug }; -global.logger = logger; - module.exports = { - setOutputChannel + setOutputChannel, + logger }; diff --git a/lib/userInfoHelper.js b/lib/userInfoHelper.js index d6192ff..6a0c3c6 100644 --- a/lib/userInfoHelper.js +++ b/lib/userInfoHelper.js @@ -8,6 +8,7 @@ const _ = require('lodash'); const util = require('./util.js'); const deviceConnectHelper = require('./deviceConnectHelper.js'); const regExp = require('./regexp.js'); +const { logger } = require('./logger'); const EMULATOR_IP = '0.0.0.0'; const WITS_CONFIG_FILE_NAME = '.witsconfig.json'; diff --git a/lib/util.js b/lib/util.js index 14f37a8..f0298d9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -4,11 +4,12 @@ const path = require('path'); const ip = require('ip'); const mkdirp = require('mkdirp'); const tools = require('@tizentv/tools'); -const platform = os.platform(); +const chalk = require('chalk'); const regExp = require('./regexp.js'); -const chalk = require('chalk'); +const { logger } = require('./logger'); +const platform = os.platform(); const CURRENT_PROJECT_PATH = process.cwd(); module.exports = { diff --git a/lib/watchHelper.js b/lib/watchHelper.js index 1edb4b9..b3f12c2 100644 --- a/lib/watchHelper.js +++ b/lib/watchHelper.js @@ -13,6 +13,7 @@ const regExp = require('./regexp.js'); const appLaunchHelper = require('./appLaunchHelper.js'); const hostAppHelper = require('./hostAppHelper.js'); const util = require('./util.js'); +const { logger } = require('./logger'); const WATCHER_EVENT_UPDATE = 'update'; const WATCHER_EVENT_REMOVE = 'remove'; From 089125074718f553d1be2826fe1a0fb29d94075c Mon Sep 17 00:00:00 2001 From: SejoongDeJang Date: Tue, 27 Oct 2020 11:53:02 +0900 Subject: [PATCH 3/8] Add the unsetOutputChannel --- README.md | 43 +++++++++++++++++++------------------------ index.js | 9 +++++++-- lib/logger.js | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index aefc6b5..648cf65 100644 --- a/README.md +++ b/README.md @@ -88,33 +88,46 @@ We will not describe how to do these installations as there are many ways to do .witsconfig.json, .witsignore files are only added at the your tizen web application. -### WITs command options +### WITs CLI -#### 1. `wits` +#### `wits` For showing which options you can use -#### 2. `wits -i` / `wits --init` +#### `wits -i` / `wits --init` For configuring WITs Please note that, It should be run when you use first time on your tizen application project. .witsconfig.json and .witsignore files are generated on your tizen app project. After then, you can modify your information to them. +![witsi](https://user-images.githubusercontent.com/1733182/77503919-3ddef280-6ea2-11ea-9bb4-06f3cb9ebbc6.gif) -#### 3. `wits -c` / `wits --certificate` +#### `wits -c` / `wits --certificate` For creating a certification(Supported Tizen certification only). As following steps, you can create a certification on `~/{path-to}/wits/resource/profiles.xml`. +![witsc](https://user-images.githubusercontent.com/1733182/92706471-7fe7ec00-f38f-11ea-8d47-47b13f956906.gif) -#### 4. `wits -s` / `wits --start` +#### `wits -s` / `wits --start` All in one. For connecting to TV, installing and launching your app and using Live Reload If `wits -i` hasn't run before, It is not allowed to run. +![witss](https://user-images.githubusercontent.com/1733182/77503927-420b1000-6ea2-11ea-88f5-49ab0c5fc227.gif) -#### 5. `wits -w` / `wits --watch` +#### `wits -w` / `wits --watch` For conneting to TV, using Live Reload After connecting, every time you make changes on `your tizen app project`, It is reflected to TV device instantly. +![witsw](https://user-images.githubusercontent.com/1733182/77503928-43d4d380-6ea2-11ea-8ece-4f5182cb7d6d.gif) + +### WITs API + +WITs support followed APIs + +- `setWitsconfigInfo(WitsInfoData data)` : This API is for setting WITs environment, It should be called before start function or watch function. +- +- +- For detail, check the [How to use WITs as APIs](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs). ### .witsconfig.json of WITs @@ -145,24 +158,6 @@ deprecated stglib ``` -## Running Your App - -### wits -i - -![witsi](https://user-images.githubusercontent.com/1733182/77503919-3ddef280-6ea2-11ea-9bb4-06f3cb9ebbc6.gif) - -### wits -c - -![witsc](https://user-images.githubusercontent.com/1733182/92706471-7fe7ec00-f38f-11ea-8d47-47b13f956906.gif) - -### wits -s - -![witss](https://user-images.githubusercontent.com/1733182/77503927-420b1000-6ea2-11ea-88f5-49ab0c5fc227.gif) - -### wits -w - -![witsw](https://user-images.githubusercontent.com/1733182/77503928-43d4d380-6ea2-11ea-8ece-4f5182cb7d6d.gif) - ## FAQ - [WITs FAQ](https://github.com/Samsung/Wits/wiki/Frequently-Asked-Questions) diff --git a/index.js b/index.js index 4a97ac8..1c80092 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,10 @@ const util = require('./lib/util.js'); const userInfoHelper = require('./lib/userInfoHelper.js'); -const { setOutputChannel, logger } = require('./lib/logger'); +const { + setOutputChannel, + unsetOutputChannel, + logger +} = require('./lib/logger'); const setWitsconfigInfo = async data => { try { @@ -84,5 +88,6 @@ module.exports = { start, watch, disconnect, - setOutputChannel + setOutputChannel, + unsetOutputChannel }; diff --git a/lib/logger.js b/lib/logger.js index ca2cbcb..c94ca28 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -4,32 +4,52 @@ const setOutputChannel = output => { if (typeof output !== 'function') { throw new Error(`output should be function`); } - outputChannel.push(output); + const id = generateId(); + outputChannel.push({ id, output }); + return id; +}; + +function generateId() { + const min = Math.ceil(1000); + const max = Math.floor(9999); + return Math.floor(Math.random() * (max - min)) + min; +} + +const unsetOutputChannel = requestId => { + const removedId = outputChannel.findIndex(({ id }) => requestId === id); + if (removedId < 0) { + console.error(`The request id(${requestId}) is not exist`); + return; + } + outputChannel.splice(removedId, 1); }; function error() { - var args = [].slice.call(arguments); - [console.error, ...outputChannel].forEach(print => { + const args = [].slice.call(arguments); + const outputCallback = outputChannel.map(({ output }) => output); + [console.error, ...outputCallback].forEach(print => { print(...args); }); } function warn() { - var args = [].slice.call(arguments); - [console.warn, ...outputChannel].forEach(print => { + const args = [].slice.call(arguments); + const outputCallback = outputChannel.map(({ output }) => output); + [console.warn, ...outputCallback].forEach(print => { print(...args); }); } function log() { - var args = [].slice.call(arguments); - [console.log, ...outputChannel].forEach(print => { + const args = [].slice.call(arguments); + const outputCallback = outputChannel.map(({ output }) => output); + [console.log, ...outputCallback].forEach(print => { print(...args); }); } function debug() { - var args = [].slice.call(arguments); + const args = [].slice.call(arguments); console.debug(...args); } @@ -42,5 +62,6 @@ const logger = { module.exports = { setOutputChannel, + unsetOutputChannel, logger }; From 9a1b583d74e6f47aad06650568de8468038e9055 Mon Sep 17 00:00:00 2001 From: SejoongDeJang Date: Tue, 27 Oct 2020 12:10:39 +0900 Subject: [PATCH 4/8] Add the WITs API --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 648cf65..f8f1338 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,14 @@ After connecting, every time you make changes on `your tizen app project`, It is WITs support followed APIs -- `setWitsconfigInfo(WitsInfoData data)` : This API is for setting WITs environment, It should be called before start function or watch function. -- -- -- For detail, check the [How to use WITs as APIs](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs). +- [setWitsconfigInfo(WitsInfoData data)](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#setwitsconfiginfo) : This API is for setting WITs environment, It should be called before start function or watch function. +- [start()](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#start) : This API is a sequence for building and installing your application, connecting PC and Target TV, pushing files, supporting live-reload feature. +- [watch()](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#watch) : This API is a sequence for connecting PC and Target TV, pushing files, supporting live-reload feature. (Except for re-building and re-installing your application.) +- [disconnect()](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#disconnect) : This API is for disconnecting communications between PC and Target TV. +- [setOutputChannel(OutputCallback callback)](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#setoutputchannel) : This is for getting WITs' log information. The return value is integer value between 1000 and 9999, and this value will be used to unsetOutputChannel(). +- [unsetOutputChannel()](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#unsetoutputchannel) : This is for unregistering the output callback. Call the unsetOutputChannel() with the return value of the setOutputChannel(). + +For detail, check whole APIs in [How to use WITs as APIs](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs). ### .witsconfig.json of WITs From 81f0f08cd6022adcd17d1ab6ff38860f6c0dd467 Mon Sep 17 00:00:00 2001 From: SejoongDeJang Date: Tue, 27 Oct 2020 14:03:56 +0900 Subject: [PATCH 5/8] Edit the readme --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f8f1338..6a92faa 100644 --- a/README.md +++ b/README.md @@ -100,29 +100,33 @@ For configuring WITs Please note that, It should be run when you use first time on your tizen application project. .witsconfig.json and .witsignore files are generated on your tizen app project. After then, you can modify your information to them. + ![witsi](https://user-images.githubusercontent.com/1733182/77503919-3ddef280-6ea2-11ea-9bb4-06f3cb9ebbc6.gif) #### `wits -c` / `wits --certificate` For creating a certification(Supported Tizen certification only). As following steps, you can create a certification on `~/{path-to}/wits/resource/profiles.xml`. + ![witsc](https://user-images.githubusercontent.com/1733182/92706471-7fe7ec00-f38f-11ea-8d47-47b13f956906.gif) #### `wits -s` / `wits --start` All in one. For connecting to TV, installing and launching your app and using Live Reload If `wits -i` hasn't run before, It is not allowed to run. + ![witss](https://user-images.githubusercontent.com/1733182/77503927-420b1000-6ea2-11ea-88f5-49ab0c5fc227.gif) #### `wits -w` / `wits --watch` For conneting to TV, using Live Reload After connecting, every time you make changes on `your tizen app project`, It is reflected to TV device instantly. + ![witsw](https://user-images.githubusercontent.com/1733182/77503928-43d4d380-6ea2-11ea-8ece-4f5182cb7d6d.gif) ### WITs API -WITs support followed APIs +WITs supports followed APIs - [setWitsconfigInfo(WitsInfoData data)](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#setwitsconfiginfo) : This API is for setting WITs environment, It should be called before start function or watch function. - [start()](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#start) : This API is a sequence for building and installing your application, connecting PC and Target TV, pushing files, supporting live-reload feature. From b25515a6d33e195331155ad23e3626475d222d50 Mon Sep 17 00:00:00 2001 From: sejoong_de_jang Date: Tue, 27 Oct 2020 14:09:22 +0900 Subject: [PATCH 6/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6a92faa..dc3776a 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ After connecting, every time you make changes on `your tizen app project`, It is ### WITs API -WITs supports followed APIs +WITs supports the followed APIs - [setWitsconfigInfo(WitsInfoData data)](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#setwitsconfiginfo) : This API is for setting WITs environment, It should be called before start function or watch function. - [start()](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#start) : This API is a sequence for building and installing your application, connecting PC and Target TV, pushing files, supporting live-reload feature. From a4f2fd6bfc8fb1759bdcee4f2957d8d644087103 Mon Sep 17 00:00:00 2001 From: sejoong_de_jang Date: Tue, 27 Oct 2020 14:12:11 +0900 Subject: [PATCH 7/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dc3776a..27a26f4 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ After connecting, every time you make changes on `your tizen app project`, It is ### WITs API -WITs supports the followed APIs +WITs supports the following APIs - [setWitsconfigInfo(WitsInfoData data)](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#setwitsconfiginfo) : This API is for setting WITs environment, It should be called before start function or watch function. - [start()](https://github.com/Samsung/Wits/wiki/How-to-use-WITs-as-APIs#start) : This API is a sequence for building and installing your application, connecting PC and Target TV, pushing files, supporting live-reload feature. From 556b00610ce81233271424c79c6f00dd184cbc79 Mon Sep 17 00:00:00 2001 From: SejoongDeJang Date: Tue, 27 Oct 2020 14:19:22 +0900 Subject: [PATCH 8/8] Version up to v2.4.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 30e90a8..4dd6cd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tizentv/wits", - "version": "2.4.5", + "version": "2.4.6", "description": "Instant live reload tool for Tizen Web Application development", "main": "index.js", "author": "Samsung Electronics Co., Ltd.",