added log module

This commit is contained in:
2018-05-02 22:05:05 +03:00
parent c89833e80f
commit 3b26ae8b08
9 changed files with 124 additions and 77 deletions

20
haxetool/env.js Normal file
View File

@@ -0,0 +1,20 @@
const path = require('path');
class Env {
static set(key, value) {
process.env[key] = value;
}
static addPath(value, key='PATH') {
if (!process.env[key]) {
process.env[key] = value;
} else if (process.env[key].split(path.delimiter).indexOf(value) === -1) {
process.env[key] = [process.env[key], value].join(path.delimiter);
}
}
}
module.exports = Env;

View File

@@ -1,19 +1,17 @@
const child_process = require('child_process'); const child_process = require('child_process');
const async = require('async'); const async = require('async');
const colors = require('ansi-colors'); const log = require('./log')('exec');
const log = require('fancy-log');
const TAG = colors.green('[exec]');
const verbose = process.argv.indexOf('--verbose') > -1;
const queue = async.queue((task, done) => { const queue = async.queue((task, done) => {
if (verbose) log(TAG, colors.magenta(task.command)); log.d('*%s*', task.command);
//process.chdir(task.dir); //process.chdir(task.dir);
child_process.exec(task.command, {cwd: task.dir, maxBuffer: 1024 * 5000}, (err, stdout, stderr) => { child_process.exec(task.command, {cwd: task.dir, maxBuffer: 1024 * 5000}, (err, stdout, stderr) => {
if (verbose) log(TAG, err ? colors.red(err) : '', stdout, colors.red(stderr));
if (err) { if (err) {
log.v('!%s!', err);
task.failure(stderr || stdout || err); task.failure(stderr || stdout || err);
} else { } else {
log.v('%s', stdout);
task.success({stdout: stdout, stderr: stderr}); task.success({stdout: stdout, stderr: stderr});
} }
done(); done();

View File

@@ -4,11 +4,8 @@ const fse = require('fs-extra');
const os = require('os'); const os = require('os');
const through = require('through2'); const through = require('through2');
const Sdk = require('./sdk'); const Sdk = require('./sdk');
const exec = require('./exec');
const PluginError = require('plugin-error');
const colors = require('ansi-colors');
const log = require('fancy-log');
const Vinyl = require('vinyl'); const Vinyl = require('vinyl');
const run = require('../run/index');
class FlashPlayer extends Sdk { class FlashPlayer extends Sdk {
@@ -78,6 +75,7 @@ class FlashPlayer extends Sdk {
} else { } else {
fs.writeFileSync(filename, `${value}\n`); fs.writeFileSync(filename, `${value}\n`);
} }
fs.writeFileSync(FlashPlayer.log, '');
} }
static trust(value) { static trust(value) {
@@ -95,29 +93,15 @@ class FlashPlayer extends Sdk {
} }
} }
run() { run(filename, params) {
let stream = null; this.log.i('_run_ *%s*', filename);
const bufferContents = (file, enc, callback) => { FlashPlayer.trust(filename);
log(this.tag, colors.cyan("run"), colors.magenta(file.path)); FlashPlayer.enableLog();
FlashPlayer.trust(file.path); return run(`${this.flashPlayerBin} ${filename}`, params).exec()
FlashPlayer.enableLog(); .pipe(through.obj(function (file, enc, callback) {
exec('.', [this.flashPlayerBin, file.path].join(' ')).then(() => { this.push(new Vinyl({path: FlashPlayer.log}));
stream.emit('end');
callback(); callback();
}).catch((error) => {
stream.emit('error', new PluginError({plugin: this.tag, message: error}));
callback();
});
//stream.push(file);
// ToDo: watch when file is exists
// or create log file in FlashPlayer.enableLog()?
stream.push(new Vinyl({
path: FlashPlayer.log
})); }));
//stream.push(file);
};
return stream = through.obj(bufferContents);
} }
} }

View File

@@ -4,9 +4,8 @@ const fse = require('fs-extra');
const path = require('path'); const path = require('path');
const exec = require('./exec'); const exec = require('./exec');
const Sdk = require('./sdk'); const Sdk = require('./sdk');
const Env = require('./env');
const Neko = require('./neko'); const Neko = require('./neko');
const colors = require('ansi-colors');
const log = require('fancy-log');
const vfs = require('vinyl-fs'); const vfs = require('vinyl-fs');
const template = require('lodash.template'); const template = require('lodash.template');
@@ -15,9 +14,9 @@ class Haxe extends Sdk {
getBin(name) { getBin(name) {
if (Sdk.System.isWindows) { if (Sdk.System.isWindows) {
return path.join(this.binPath, name + '.exe'); return path.join(this.path, name + '.exe');
} else if (Sdk.System.isLinux) { } else if (Sdk.System.isLinux) {
const binPath = path.join(this.binPath, name); const binPath = path.join(this.path, name);
if (fs.existsSync(binPath)) { if (fs.existsSync(binPath)) {
fs.chmodSync(binPath, 0o755); fs.chmodSync(binPath, 0o755);
} }
@@ -26,10 +25,6 @@ class Haxe extends Sdk {
throw `Unsupported OS: ${os.type()}`; throw `Unsupported OS: ${os.type()}`;
} }
get binPath() {
return `${this.path}`;
}
get haxeBin() { get haxeBin() {
return this.getBin('haxe'); return this.getBin('haxe');
} }
@@ -52,13 +47,12 @@ class Haxe extends Sdk {
} }
activate() { activate() {
this.neko.activate() this.neko.activate();
process.env.HAXE_VERSION = this.version; Env.set('HAXE_VERSION', this.version);
process.env.HAXE_STD_PATH = path.join(this.binPath, 'std'); Env.set('HAXE_STD_PATH', path.join(this.path, 'std'));
process.env.HAXE_HOME = this.binPath; Env.set('HAXE_HOME', this.path);
if (process.env.PATH.split(path.delimiter).indexOf(this.binPath) === -1) { Env.addPath(this.path);
process.env.PATH = [process.env.PATH, this.binPath].join(path.delimiter); Env.addPath(this.path, 'LD_LIBRARY_PATH');
}
} }
prepare() { prepare() {
@@ -83,8 +77,8 @@ class Haxe extends Sdk {
} }
openfl(command, platform, config, debug=false) { openfl(command, platform, config, debug=false) {
log(this.tag, colors.cyan(`openfl build ${platform}`)); this.log.i('_openfl_ _build_ *%s*', platform);
this.activate() this.activate();
const buildDir = path.join(os.tmpdir(), 'build', config.name); const buildDir = path.join(os.tmpdir(), 'build', config.name);
fse.ensureDirSync(buildDir); fse.ensureDirSync(buildDir);
@@ -102,8 +96,8 @@ class Haxe extends Sdk {
} }
build(platform, config, debug=false) { build(platform, config, debug=false) {
log(this.tag, colors.cyan(`build ${platform}`)); this.log.i('_build_ *%s*', platform);
this.activate() this.activate();
const buildDir = path.join(os.tmpdir(), 'build', config.name); const buildDir = path.join(os.tmpdir(), 'build', config.name);
fse.ensureDirSync(buildDir); fse.ensureDirSync(buildDir);
@@ -135,10 +129,10 @@ class Haxe extends Sdk {
} }
install(packages) { install(packages) {
this.activate() this.activate();
let promise = this.haxelib(['setup', path.join(this.path, 'lib')]); let promise = this.haxelib(['setup', path.join(this.path, 'lib')]);
const next = (args) => () => { const next = (args) => () => {
log(this.tag, colors.cyan('haxelib', 'install'), colors.magenta(args[1])); this.log.i('_haxelib_ _install_ *%s*', args[1]);
return this.haxelib(args); return this.haxelib(args);
}; };

64
haxetool/log.js Normal file
View File

@@ -0,0 +1,64 @@
const colors = require('ansi-colors');
const timestamp = require('time-stamp');
const Level = {
VERBOSE: -1,
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
};
const level = process.argv.indexOf('--verbose') > -1 ? Level.VERBOSE : Level.INFO;
class Log {
static colorize(pattern) {
for (const item of [
['_', colors.cyan],
['\\*', colors.magenta],
['!', colors.red],
]) {
const r = new RegExp(item[0] + "(.*?)" + item[0], 'g');
pattern = pattern.replace(r, item[1]('$1'));
}
return pattern
}
static write(level, tag, pattern, ...args) {
if (level >= this.level) {
const message = `[${colors.gray(timestamp('HH:mm:ss'))}] ${colors.green(tag)} ${this.colorize(pattern)}`;
console.log(message, ...args)
}
}
constructor(tag) {
this.tag = tag;
}
v(...args) {
Log.write(-1, this.tag, ...args);
}
d(...args) {
Log.write(0, this.tag, ...args);
}
i(...args) {
Log.write(1, this.tag, ...args);
}
w(...args) {
Log.write(2, this.tag, ...args);
}
e(...args) {
Log.write(3, this.tag, ...args);
}
}
Log.level = level;
module.exports = (tag) => new Log(tag);

View File

@@ -3,7 +3,7 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const run = require('../run/index'); const run = require('../run/index');
const Sdk = require('./sdk'); const Sdk = require('./sdk');
const System = Sdk.System; const Env = require('./env');
class NekoLink { class NekoLink {
@@ -56,26 +56,25 @@ class Neko extends Sdk {
} }
activate() { activate() {
process.env.NEKO_INSTPATH = this.path; Env.set('NEKO_INSTPATH', this.path);
if (process.env.PATH.split(path.delimiter).indexOf(this.path) === -1) { Env.addPath(this.path);
process.env.PATH = [process.env.PATH, this.path].join(path.delimiter); Env.addPath(this.path, 'LD_LIBRARY_PATH');
}
} }
get nekoBin() { get nekoBin() {
let binname = 'neko'; let binname = 'neko';
if (os.type() == 'Windows_NT') binname += '.exe'; if (os.type() === 'Windows_NT') binname += '.exe';
return path.join(this.path, binname); return path.join(this.path, binname);
} }
run(filename, params) { run(filename, params) {
this.log.i('_run_ *%s*', filename);
this.activate(); this.activate();
console.log(`${this.nekoBin} ${filename}`);
return run(`${this.nekoBin} ${filename}`, params).exec(); return run(`${this.nekoBin} ${filename}`, params).exec();
} }
} }
Neko.ID = 'neko' Neko.ID = 'neko';
Neko.VERSION_2_2_0 = '2.2.0'; Neko.VERSION_2_2_0 = '2.2.0';
Neko.VERSION = Neko.VERSION_2_2_0; Neko.VERSION = Neko.VERSION_2_2_0;

View File

@@ -15,8 +15,6 @@ const {BuildSystem, Platform, Config} = require('./core');
const vfs = require('vinyl-fs'); const vfs = require('vinyl-fs');
const rename = require('gulp-rename'); const rename = require('gulp-rename');
const template = require('lodash.template'); const template = require('lodash.template');
const through = require('through2');
const Vinyl = require('vinyl');
const streamToPromise = (stream) => { const streamToPromise = (stream) => {
@@ -249,15 +247,7 @@ class FlashRunner extends Runner {
call() { call() {
const target = this.targetPath; const target = this.targetPath;
const filename = path.resolve(target, this.config.meta.filename+'.swf'); const filename = path.resolve(target, this.config.meta.filename+'.swf');
const player = this.player.flashPlayerBin; const result = this.player.run(filename, {cwd: target, verbosity: 0});
FlashPlayer.trust(filename);
fs.writeFileSync(FlashPlayer.log, '');
let result = gulp.src(filename)
.pipe(run(player + " <%=file.basename%>", {cwd: target, verbosity: 0}))
.pipe(through.obj(function (file, enc, callback) {
this.push(new Vinyl({path: FlashPlayer.log}));
callback();
}));
return this.log(result); return this.log(result);
} }
} }

View File

@@ -6,9 +6,8 @@ const got = require('got');
const unzip = require('unzip-stream'); const unzip = require('unzip-stream');
const tar = require('tar'); const tar = require('tar');
const ProgressBar = require('progress'); const ProgressBar = require('progress');
const colors = require('ansi-colors');
const log = require('fancy-log');
const fse = require('fs-extra'); const fse = require('fs-extra');
const Log = require('./log');
class System { class System {
@@ -51,7 +50,7 @@ class Sdk {
constructor(name, version) { constructor(name, version) {
this.name = name; this.name = name;
this.tag = colors.green(`[${name}]`); this.log = Log(name);
this.version = version; this.version = version;
this.path = Sdk.path(name, version); this.path = Sdk.path(name, version);
} }
@@ -76,8 +75,7 @@ class Sdk {
} }
prepare(strip=1) { prepare(strip=1) {
log(this.tag, `version: ${colors.magenta(this.version)}`); this.log.i('version: *%s*', this.version);
if (this.prepared) { if (this.prepared) {
return Promise.resolve(); return Promise.resolve();
} else { } else {

View File

@@ -1,6 +1,6 @@
{ {
"name": "gulp-haxetool", "name": "gulp-haxetool",
"version": "0.0.9", "version": "0.0.10",
"description": "HaXe Tool for Gulp", "description": "HaXe Tool for Gulp",
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {