This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
435 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ node_modules | |
bower_components | ||
build | ||
output | ||
api-console-cli.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,44 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const exec = require('child_process').exec; | ||
|
||
const winston = require('winston'); | ||
/** | ||
* Base class for CLI commands. | ||
* | ||
*/ | ||
class ApiBase { | ||
/** | ||
* Sets global options. | ||
* | ||
* @param {Object} opts Command options. | ||
*/ | ||
constructor(opts) { | ||
opts = opts || {}; | ||
this.verbose = opts.verbose || false; | ||
} | ||
// Prints arguments to the console. | ||
log() { | ||
if (this.verbose) { | ||
console.log.apply(console, arguments); | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
this.opts = opts; | ||
this.logger = this.__setupLogger(); | ||
} | ||
|
||
/** | ||
* Ensures that given path exists in filesystem. | ||
* | ||
* @param {String} path A path to test | ||
* @return {Promise} Resolced promise when the path exists. Rejects when unable to create the | ||
* path. | ||
*/ | ||
ensurePath(path) { | ||
return this.pathExists(path) | ||
.catch((exists) => { | ||
if (!exists) { | ||
return this._createDir(path); | ||
} | ||
return Promise.resolve(); | ||
}); | ||
} | ||
// Creates a directory recursively. | ||
_createDir(dirPath) { | ||
return new Promise((resolve, reject) => { | ||
fs.mkdir(dirPath, (error) => { | ||
if (error && error.code === 'ENOENT') { | ||
this._createDir(path.dirname(dirPath)) | ||
.then(() => { | ||
return this._createDir(dirPath); | ||
}) | ||
.then(resolve) | ||
.catch(reject); | ||
} else if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Checks if `path` exists in the filesystem. | ||
* | ||
* @param {String} path A path to check | ||
* @return {Promise} A promise resolves itself if `path` exists and rejects if don't. | ||
* Creates a logger object to log debug output. | ||
* @return {Object} | ||
*/ | ||
pathExists(path) { | ||
return new Promise((resolve, reject) => { | ||
fs.access(path, fs.constants.F_OK, (err) => { | ||
if (err) { | ||
return reject(); | ||
} | ||
resolve(); | ||
}); | ||
__setupLogger() { | ||
const level = this.opts.verbose ? 'debug' : 'warn'; | ||
this.debugFile = path.join(process.cwd(), 'api-console-cli.log'); | ||
const format = winston.format.combine( | ||
winston.format.colorize(), | ||
winston.format.simple() | ||
); | ||
const logger = winston.createLogger({ | ||
level, | ||
format, | ||
exitOnError: false, | ||
transports: [ | ||
new winston.transports.Console(), | ||
new winston.transports.File({filename: this.debugFile, level: 'error'}) | ||
] | ||
}); | ||
} | ||
|
||
/** | ||
* Execute shell command | ||
* | ||
* @param {String} cmd Command to execute | ||
* @param {String?} dir A directoy where to execute the command. | ||
* @return {Promise} Promise resolves itself if the command was executed successfully and | ||
* rejects it there was an error. | ||
*/ | ||
exec(cmd, dir) { | ||
dir = dir || undefined; | ||
return new Promise((resolve, reject) => { | ||
var opts = {}; | ||
if (dir) { | ||
opts.cwd = dir; | ||
} | ||
this.log(`Executing command: ${cmd} in dir: ${dir}`); | ||
exec(cmd, opts, (err, stdout, stderr) => { | ||
if (err) { | ||
let currentDir = process.cwd(); | ||
if (opts.cwd) { | ||
currentDir = opts.cwd; | ||
} | ||
reject(new Error('Unable to execute command: ' + err.message + | ||
'. Was in dir: ' + currentDir + '. stdout: ', stdout, '. stderr: ', stderr)); | ||
return; | ||
} | ||
resolve(stdout); | ||
}); | ||
}); | ||
} | ||
|
||
// Checks if given `url` is a local or remote file. | ||
isRemoteFile(url) { | ||
if (!url) { | ||
// current dir? | ||
return false; | ||
} | ||
if (url.indexOf('http') === 0) { | ||
return true; | ||
} | ||
if (url.indexOf('ftp') === 0) { | ||
return true; | ||
} | ||
return false; | ||
return logger; | ||
} | ||
} | ||
exports.ApiBase = ApiBase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.