refactor: refork tail module
This commit is contained in:
Generated
+734
-1697
File diff suppressed because it is too large
Load Diff
+13
-8
@@ -38,39 +38,44 @@
|
|||||||
"got": "^16.1.0",
|
"got": "^16.1.0",
|
||||||
"gulp": "^5.0.1",
|
"gulp": "^5.0.1",
|
||||||
"gulp-clean": "^0.4.0",
|
"gulp-clean": "^0.4.0",
|
||||||
|
"gulp-connect": "^5.7.0",
|
||||||
"gulp-debian": "^0.1.9",
|
"gulp-debian": "^0.1.9",
|
||||||
"gulp-gzip": "^1.4.2",
|
"gulp-gzip": "^1.4.2",
|
||||||
"gulp-rename": "^1.2.2",
|
"gulp-rename": "^2.1.0",
|
||||||
"gulp-spawn": "^0.4.0",
|
"gulp-spawn": "^0.4.0",
|
||||||
"gulp-tar": "^3.0.0",
|
"gulp-tar": "^3.0.0",
|
||||||
"gulp-webserver": "^0.9.1",
|
|
||||||
"gulp-zip": "^5.0.0",
|
"gulp-zip": "^5.0.0",
|
||||||
"lodash.defaults": "^4.2.0",
|
"lodash.defaults": "^4.2.0",
|
||||||
"lodash.template": "^4.4.0",
|
"lodash.template": "^4.4.0",
|
||||||
"plugin-error": "^1.0.1",
|
"plugin-error": "^1.0.1",
|
||||||
"promise-streams": "^2.1.1",
|
"promise-streams": "^2.1.1",
|
||||||
"tail": "^1.2.3",
|
"tail": "^2.2.6",
|
||||||
"tar": "^7.5.22",
|
"tar": "^7.5.22",
|
||||||
"through2": "^2.0.3",
|
"through2": "^5.0.12",
|
||||||
"tmp-file": "^2.0.1",
|
"tmp-file": "^2.0.1",
|
||||||
"unzip-stream": "^0.3.4",
|
"unzip-stream": "^0.3.4",
|
||||||
"vinyl": "^2.1.0",
|
"vinyl": "^3.0.1",
|
||||||
"vinyl-fs": "^3.0.2"
|
"vinyl-fs": "^4.0.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/async": "^3.2.26",
|
"@types/async": "^3.2.26",
|
||||||
"@types/cli-progress": "^3.11.6",
|
"@types/cli-progress": "^3.11.6",
|
||||||
"@types/fs-extra": "^11.0.4",
|
"@types/fs-extra": "^11.0.4",
|
||||||
"@types/gulp": "^4.0.18",
|
"@types/gulp": "^4.0.18",
|
||||||
|
"@types/gulp-connect": "^5.0.10",
|
||||||
"@types/gulp-rename": "^2.0.7",
|
"@types/gulp-rename": "^2.0.7",
|
||||||
"@types/lodash.template": "^4.5.3",
|
"@types/lodash.template": "^4.5.3",
|
||||||
"@types/node": "^26.6.5",
|
"@types/node": "^26.6.5",
|
||||||
|
"@types/tail": "^2.2.3",
|
||||||
"@types/tar": "^7.0.87",
|
"@types/tar": "^7.0.87",
|
||||||
|
"@types/through2": "^5.0.0",
|
||||||
"@types/unzip-stream": "^0.3.4",
|
"@types/unzip-stream": "^0.3.4",
|
||||||
"@types/vinyl-fs": "^3.0.8",
|
"eslint": "^10.13.0",
|
||||||
"eslint": "^10.12.0",
|
|
||||||
"prettier": "^3.9.10",
|
"prettier": "^3.9.10",
|
||||||
"tsdown": "0.22.14",
|
"tsdown": "0.22.14",
|
||||||
"typescript": "^7.0.2"
|
"typescript": "^7.0.2"
|
||||||
|
},
|
||||||
|
"inlinedDependencies": {
|
||||||
|
"lodash": "4.18.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-48
@@ -1,68 +1,51 @@
|
|||||||
import { Transform, Writable } from "stream";
|
import { Transform, Writable, type TransformCallback, type WritableOptions } from "stream";
|
||||||
import { StringDecoder } from "string_decoder";
|
import { StringDecoder } from "string_decoder";
|
||||||
import { Tail } from "tail";
|
|
||||||
import through from "through2";
|
|
||||||
import Vinyl from "vinyl";
|
|
||||||
|
|
||||||
export class TailVinyl extends Vinyl {
|
export type TailHandler = (line: string) => void;
|
||||||
constructor(params: any) {
|
|
||||||
params.contents = new Transform();
|
|
||||||
super(params);
|
|
||||||
if (params.path) {
|
|
||||||
this.tail = new Tail(params.path);
|
|
||||||
(this.tail.on("line", (line) => {
|
|
||||||
if (params.handler) {
|
|
||||||
line = params.handler(line);
|
|
||||||
}
|
|
||||||
if (line) {
|
|
||||||
this.contents.write(line + "\n");
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
this.tail.on("error", (error: any) => this.contents.write("error: " + error)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dispose() {
|
export class HandlerWriter extends Writable {
|
||||||
if (this.tail) {
|
private handler: any;
|
||||||
this.tail.unwatch();
|
private decoder: StringDecoder;
|
||||||
}
|
private data: string;
|
||||||
this.contents.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class StringWritable extends Writable {
|
constructor(handler: TailHandler, options?: WritableOptions) {
|
||||||
constructor(handler: any, options?: any) {
|
if (!options) options = {};
|
||||||
|
options.objectMode = true;
|
||||||
super(options);
|
super(options);
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
const state = this._writableState;
|
this.decoder = new StringDecoder(options?.defaultEncoding);
|
||||||
this._decoder = new StringDecoder(state.defaultEncoding);
|
|
||||||
this.data = "";
|
this.data = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
_write(chunk, encoding, callback) {
|
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
|
||||||
if (encoding === "buffer") {
|
chunk = this.decoder.write(chunk);
|
||||||
chunk = this._decoder.write(chunk);
|
for (const line of chunk.split("\n")) {
|
||||||
for (const line of chunk.split("\n"))
|
if (line.length) {
|
||||||
if (line.length) {
|
this.handler(line);
|
||||||
this.handler(line);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.data += chunk;
|
this.data += chunk;
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
_final(callback) {
|
_final(callback: (error?: Error | null) => void): void {
|
||||||
this.data += this._decoder.end();
|
this.data += this.decoder.end();
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const tail = (handler: any) => {
|
export class TailTransform extends Transform {
|
||||||
return through.obj(function (file: any, enc: string, callback: any) {
|
constructor(private handler: TailHandler) {
|
||||||
if (file.contents && file.contents.pipe) {
|
super({ objectMode: true });
|
||||||
file.contents.pipe(new StringWritable(handler));
|
}
|
||||||
|
|
||||||
|
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void {
|
||||||
|
if (chunk.contents && chunk.contents.pipe) {
|
||||||
|
chunk.contents.pipe(new HandlerWriter(this.handler));
|
||||||
}
|
}
|
||||||
this.push(file);
|
this.push(chunk);
|
||||||
callback();
|
callback();
|
||||||
});
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
export const tail = (handler: TailHandler) => new TailTransform(handler);
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import { Transform, type TransformCallback } from "stream";
|
||||||
|
import { Tail } from "tail";
|
||||||
|
import Vinyl from "vinyl";
|
||||||
|
|
||||||
|
export type WatchHandler = (line: string) => string | undefined;
|
||||||
|
|
||||||
|
export interface WatchParams {
|
||||||
|
source: string;
|
||||||
|
handler?: WatchHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
class BufferingTransform extends Transform {
|
||||||
|
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void {
|
||||||
|
this.push(chunk);
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class TailVinyl extends Vinyl {
|
||||||
|
private tail?: Tail;
|
||||||
|
|
||||||
|
constructor(params: WatchParams) {
|
||||||
|
super({ path: "out.log", contents: new BufferingTransform() });
|
||||||
|
if (params.source) {
|
||||||
|
this.tail = new Tail(params.source);
|
||||||
|
this.tail.on("line", (line) => {
|
||||||
|
if (params.handler) {
|
||||||
|
line = params.handler(line);
|
||||||
|
}
|
||||||
|
if (line) {
|
||||||
|
this.contents.write(line + "\n");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.tail.on("error", (error) => this.contents.write("error: " + error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clone() {
|
||||||
|
return new Vinyl({
|
||||||
|
cwd: this.cwd,
|
||||||
|
base: this.base,
|
||||||
|
history: this.history.slice(),
|
||||||
|
contents: this.contents,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
dispose() {
|
||||||
|
this.tail?.unwatch();
|
||||||
|
this.contents.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WatchVinylTransform extends Transform {
|
||||||
|
constructor(private params: WatchParams) {
|
||||||
|
super({ objectMode: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void {
|
||||||
|
const log = new TailVinyl(this.params);
|
||||||
|
this.on("end", () => log.dispose());
|
||||||
|
this.push(log);
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const watch = (source: string, handler?: WatchHandler) => new WatchVinylTransform({ source, handler });
|
||||||
+2
-2
@@ -5,7 +5,7 @@ import through from "through2";
|
|||||||
import { Env } from "../common/env";
|
import { Env } from "../common/env";
|
||||||
import exec from "../common/exec";
|
import exec from "../common/exec";
|
||||||
import { System } from "../common/system";
|
import { System } from "../common/system";
|
||||||
import { StringWritable } from "../common/tail";
|
import { HandlerWriter } from "../common/tail";
|
||||||
import { Downloader } from "../core/download";
|
import { Downloader } from "../core/download";
|
||||||
import { Sdk } from "../core/sdk";
|
import { Sdk } from "../core/sdk";
|
||||||
import { Command } from "../run/command";
|
import { Command } from "../run/command";
|
||||||
@@ -223,7 +223,7 @@ export class Android extends Sdk {
|
|||||||
const command = new Command(cmd).exec();
|
const command = new Command(cmd).exec();
|
||||||
this.push(command);
|
this.push(command);
|
||||||
command.contents.pipe(
|
command.contents.pipe(
|
||||||
new StringWritable((line) => {
|
new HandlerWriter((line: string) => {
|
||||||
if (line.includes("SDL") && (line.includes("onDestroy()") || line.includes("onStop()"))) {
|
if (line.includes("SDL") && (line.includes("onDestroy()") || line.includes("onStop()"))) {
|
||||||
callback();
|
callback();
|
||||||
throw "Exit"; // ToDo:
|
throw "Exit"; // ToDo:
|
||||||
|
|||||||
+2
-14
@@ -2,10 +2,9 @@ import fs from "fs";
|
|||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import through from "through2";
|
|
||||||
import { Env } from "../common/env";
|
import { Env } from "../common/env";
|
||||||
import { System } from "../common/system";
|
import { System } from "../common/system";
|
||||||
import { TailVinyl } from "../common/tail";
|
import { watch } from "../common/watch";
|
||||||
import { Sdk } from "../core/sdk";
|
import { Sdk } from "../core/sdk";
|
||||||
import run from "../run";
|
import run from "../run";
|
||||||
|
|
||||||
@@ -120,17 +119,6 @@ export class FlashPlayer extends Sdk {
|
|||||||
this.log.i("_run_ *%s*", filename);
|
this.log.i("_run_ *%s*", filename);
|
||||||
FlashPlayer.trust(filename);
|
FlashPlayer.trust(filename);
|
||||||
FlashPlayer.enableLog();
|
FlashPlayer.enableLog();
|
||||||
return run(`${this.flashPlayerBin} ${filename}`, params)
|
return run(`${this.flashPlayerBin} ${filename}`, params).exec().pipe(watch(FlashPlayer.log));
|
||||||
.exec()
|
|
||||||
.pipe(
|
|
||||||
through.obj(function (file: any, enc: string, callback: any) {
|
|
||||||
const log = new TailVinyl({
|
|
||||||
path: FlashPlayer.log,
|
|
||||||
});
|
|
||||||
this.on("end", () => log.dispose());
|
|
||||||
this.push(log);
|
|
||||||
callback();
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-3
@@ -77,7 +77,10 @@ export class Haxe extends Sdk {
|
|||||||
|
|
||||||
prepare() {
|
prepare() {
|
||||||
return Promise.all([this.neko.prepare(), super.prepare()]).then(() => {
|
return Promise.all([this.neko.prepare(), super.prepare()]).then(() => {
|
||||||
fs.copyFileSync(path.resolve(import.meta.dirname, "..", "template/haxe/activate"), path.resolve(this.path, "activate"));
|
fs.copyFileSync(
|
||||||
|
path.resolve(import.meta.dirname, "..", "template/haxe/activate"),
|
||||||
|
path.resolve(this.path, "activate"),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,7 +131,7 @@ export class Haxe extends Sdk {
|
|||||||
flash: "*.swf",
|
flash: "*.swf",
|
||||||
}[platform] || "**/*";
|
}[platform] || "**/*";
|
||||||
return this.haxelib(args).then(() => {
|
return this.haxelib(args).then(() => {
|
||||||
let r = vfs.src(`${target}/${result}`);
|
let r = vfs.src(`${target}/${result}`, { encoding: false });
|
||||||
if (platform === "android") {
|
if (platform === "android") {
|
||||||
r = r.pipe(rename(`${config.meta.filename}_${config.meta.version}.apk`));
|
r = r.pipe(rename(`${config.meta.filename}_${config.meta.version}.apk`));
|
||||||
}
|
}
|
||||||
@@ -175,7 +178,7 @@ export class Haxe extends Sdk {
|
|||||||
cpp: `${config.meta.filename}/${config.main.split(".").pop()}${debug ? "-debug" : ""}`,
|
cpp: `${config.meta.filename}/${config.main.split(".").pop()}${debug ? "-debug" : ""}`,
|
||||||
}[platform] || "**/*";
|
}[platform] || "**/*";
|
||||||
return this.haxe(args).then(() => {
|
return this.haxe(args).then(() => {
|
||||||
let r = vfs.src(`${target}/${result}`);
|
let r = vfs.src(`${target}/${result}`, { encoding: false });
|
||||||
if (platform === "cpp") {
|
if (platform === "cpp") {
|
||||||
r = r.pipe(rename(config.meta.filename));
|
r = r.pipe(rename(config.meta.filename));
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-14
@@ -1,9 +1,8 @@
|
|||||||
import os from "os";
|
import os from "os";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import through from "through2";
|
|
||||||
import { Env } from "../common/env";
|
import { Env } from "../common/env";
|
||||||
import { System } from "../common/system";
|
import { System } from "../common/system";
|
||||||
import { TailVinyl } from "../common/tail";
|
import { watch } from "../common/watch";
|
||||||
import run from "../run";
|
import run from "../run";
|
||||||
import { FlashPlayer } from "./flashplayer";
|
import { FlashPlayer } from "./flashplayer";
|
||||||
|
|
||||||
@@ -55,18 +54,10 @@ export class RufflePlayer extends FlashPlayer {
|
|||||||
return run(`${this.flashPlayerBin} ${filename}`, params)
|
return run(`${this.flashPlayerBin} ${filename}`, params)
|
||||||
.exec()
|
.exec()
|
||||||
.pipe(
|
.pipe(
|
||||||
through.obj(function (file: any, enc: string, callback: any) {
|
watch(RufflePlayer.log, (line: string) => {
|
||||||
const log = new TailVinyl({
|
if (line.includes("avm_trace")) {
|
||||||
path: RufflePlayer.log,
|
return line.split("avm_trace: ").pop();
|
||||||
handler: (line: string) => {
|
}
|
||||||
if (line.includes("avm_trace")) {
|
|
||||||
return line.split("avm_trace: ").pop();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
this.on("end", () => log.dispose());
|
|
||||||
this.push(log);
|
|
||||||
callback();
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import gulp from "gulp";
|
import connect from "gulp-connect";
|
||||||
import webserver from "gulp-webserver";
|
|
||||||
import { Config } from "../../../core/config";
|
import { Config } from "../../../core/config";
|
||||||
import { Platform } from "../../../core/type";
|
import { Platform } from "../../../core/type";
|
||||||
import { Runner } from "../../core/runner";
|
import { Runner } from "../../core/runner";
|
||||||
@@ -10,15 +9,12 @@ export class Html5Runner extends Runner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
call() {
|
call() {
|
||||||
return gulp.src(this.targetPath).pipe(
|
return connect.server({
|
||||||
webserver({
|
root: this.targetPath,
|
||||||
// TODO: config?
|
host: "0.0.0.0",
|
||||||
host: "0.0.0.0",
|
port: 3000,
|
||||||
port: 3000,
|
fallback: "index.html",
|
||||||
open: true,
|
});
|
||||||
fallback: "index.html",
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+151
-175
@@ -1,193 +1,169 @@
|
|||||||
var cp = require('child_process');
|
import colors from "ansi-colors";
|
||||||
var path = require('path');
|
import cp from "child_process";
|
||||||
var stream = require('stream');
|
import { defaults, template } from "lodash";
|
||||||
var util = require('util');
|
import path from "path";
|
||||||
var defaults = require('lodash.defaults');
|
import stream from "stream";
|
||||||
var applyTemplate = require('lodash.template');
|
import util from "util";
|
||||||
var Vinyl = require('vinyl');
|
import Vinyl from "vinyl";
|
||||||
var colors = require('ansi-colors');
|
|
||||||
|
|
||||||
/**
|
export class Command {
|
||||||
* Creates a new `gulp-run` command.
|
private command: string;
|
||||||
*
|
private isWindows: boolean;
|
||||||
* @param {string} command
|
private options: any;
|
||||||
* @param {object} options
|
|
||||||
*/
|
|
||||||
export function Command(command, options?) {
|
|
||||||
var previousPath;
|
|
||||||
|
|
||||||
this.command = command;
|
constructor(command: string, options?: any) {
|
||||||
|
this.command = command;
|
||||||
|
this.isWindows = process.platform.lastIndexOf("win") === 0;
|
||||||
|
this.options = defaults(options || {}, {
|
||||||
|
cwd: process.cwd(),
|
||||||
|
env: process.env,
|
||||||
|
verbosity: options && options.silent ? 1 : 2,
|
||||||
|
usePowerShell: false,
|
||||||
|
});
|
||||||
|
|
||||||
// We're on Windows if `process.platform` starts with "win", i.e. "win32".
|
let previousPath;
|
||||||
this.isWindows = (process.platform.lastIndexOf('win') === 0);
|
previousPath = this.options.env.PATH;
|
||||||
|
this.options.env.PATH = path.join(this.options.cwd, "node_modules", ".bin");
|
||||||
// the cwd and environment of the command are the same as the main node
|
this.options.env.PATH += path.delimiter;
|
||||||
// process by default.
|
this.options.env.PATH += previousPath;
|
||||||
this.options = defaults(options || {}, {
|
|
||||||
cwd: process.cwd(),
|
|
||||||
env: process.env,
|
|
||||||
verbosity: (options && options.silent) ? 1 : 2,
|
|
||||||
usePowerShell: false
|
|
||||||
});
|
|
||||||
|
|
||||||
// include node_modules/.bin on the path when we execute the command.
|
|
||||||
previousPath = this.options.env.PATH;
|
|
||||||
this.options.env.PATH = path.join(this.options.cwd, 'node_modules', '.bin');
|
|
||||||
this.options.env.PATH += path.delimiter;
|
|
||||||
this.options.env.PATH += previousPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the command, invoking the callback when the command exits.
|
|
||||||
* Returns a Vinyl file wrapping the command's stdout.
|
|
||||||
*
|
|
||||||
* @param {string} stdin
|
|
||||||
* @param {function} callback
|
|
||||||
* @return {Stream}
|
|
||||||
*/
|
|
||||||
Command.prototype.exec = function exec(stdin, callback) {
|
|
||||||
var self = this;
|
|
||||||
var command;
|
|
||||||
var fileName;
|
|
||||||
var directory;
|
|
||||||
var subShell;
|
|
||||||
var log;
|
|
||||||
var err;
|
|
||||||
var stdout;
|
|
||||||
|
|
||||||
// parse the arguments, both are optional.
|
|
||||||
// after parsing, stdin is a vinyl file to use as standard input to
|
|
||||||
// the command (possibly empty), and callback is a function.
|
|
||||||
if (typeof stdin === 'function') {
|
|
||||||
callback = stdin;
|
|
||||||
stdin = undefined;
|
|
||||||
} else if (typeof callback !== 'function') {
|
|
||||||
callback = function noop() {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(stdin instanceof Vinyl)) {
|
exec(stdin?: any, callback?: any) {
|
||||||
fileName = this.command.split(' ')[0];
|
let fileName;
|
||||||
directory = path.join(this.options.cwd, fileName);
|
let directory;
|
||||||
|
let subShell;
|
||||||
|
|
||||||
if (typeof stdin === 'string') {
|
// parse the arguments, both are optional.
|
||||||
stdin = new Vinyl({
|
// after parsing, stdin is a vinyl file to use as standard input to
|
||||||
path: directory,
|
// the command (possibly empty), and callback is a function.
|
||||||
contents: new Buffer(stdin)
|
if (typeof stdin === "function") {
|
||||||
});
|
callback = stdin;
|
||||||
} else if (stdin instanceof Buffer || stdin instanceof stream.Readable) {
|
stdin = undefined;
|
||||||
stdin = new Vinyl({
|
} else if (typeof callback !== "function") {
|
||||||
path: directory,
|
callback = function noop() {};
|
||||||
contents: stdin
|
}
|
||||||
});
|
|
||||||
} else {
|
|
||||||
stdin = new Vinyl(stdin);
|
|
||||||
|
|
||||||
if (!stdin.path) {
|
if (!(stdin instanceof Vinyl)) {
|
||||||
stdin.path = directory;
|
fileName = this.command.split(" ")[0];
|
||||||
|
directory = path.join(this.options.cwd, fileName);
|
||||||
|
|
||||||
|
if (typeof stdin === "string") {
|
||||||
|
stdin = new Vinyl({
|
||||||
|
path: directory,
|
||||||
|
contents: new Buffer(stdin),
|
||||||
|
});
|
||||||
|
} else if (stdin instanceof Buffer || stdin instanceof stream.Readable) {
|
||||||
|
stdin = new Vinyl({
|
||||||
|
path: directory,
|
||||||
|
contents: stdin,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
stdin = new Vinyl(stdin);
|
||||||
|
|
||||||
|
if (!stdin.path) {
|
||||||
|
stdin.path = directory;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// execute the command.
|
// execute the command.
|
||||||
// we spawn the command in a subshell, so things like i/o redirection
|
// we spawn the command in a subshell, so things like i/o redirection
|
||||||
// just work. e.g. `echo hello world >> ./hello.txt` works as expected.
|
// just work. e.g. `echo hello world >> ./hello.txt` works as expected.
|
||||||
command = applyTemplate(this.command)({
|
const command = template(this.command)({
|
||||||
file: stdin
|
file: stdin,
|
||||||
});
|
|
||||||
|
|
||||||
if (this.isWindows && this.options.usePowerShell) {
|
|
||||||
// windows powershell
|
|
||||||
subShell = cp.spawn('powershell.exe', ['-NonInteractive', '-NoLogo', '-Command', command], {
|
|
||||||
env: this.options.env,
|
|
||||||
cwd: this.options.cwd
|
|
||||||
});
|
});
|
||||||
} else if (this.isWindows) {
|
|
||||||
// windows cmd.exe
|
|
||||||
subShell = cp.spawn('cmd.exe', ['/c', command], {
|
|
||||||
env: this.options.env,
|
|
||||||
cwd: this.options.cwd
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// POSIX shell
|
|
||||||
subShell = cp.spawn('sh', ['-c', command], {
|
|
||||||
env: this.options.env,
|
|
||||||
cwd: this.options.cwd
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// setup the output
|
if (this.isWindows && this.options.usePowerShell) {
|
||||||
//
|
// windows powershell
|
||||||
// - if verbosity equals to 3, the command prints directly to the terminal.
|
subShell = cp.spawn("powershell.exe", ["-NonInteractive", "-NoLogo", "-Command", command], {
|
||||||
// - if verbosity equals to 2, the command's stdout and stderr are buffered
|
env: this.options.env,
|
||||||
// and printed to the user's terminal after the command exits (this
|
cwd: this.options.cwd,
|
||||||
// prevents overlaping output of multiple commands)
|
});
|
||||||
// - if verbosity equals to 1, the command's stderr is buffered as in 2, but
|
} else if (this.isWindows) {
|
||||||
// the stdout is silenced.
|
// windows cmd.exe
|
||||||
log = new stream.PassThrough();
|
subShell = cp.spawn("cmd.exe", ["/c", command], {
|
||||||
|
env: this.options.env,
|
||||||
|
cwd: this.options.cwd,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// POSIX shell
|
||||||
|
subShell = cp.spawn("sh", ["-c", command], {
|
||||||
|
env: this.options.env,
|
||||||
|
cwd: this.options.cwd,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function sendLog(context) {
|
// setup the output
|
||||||
var title = util.format(
|
//
|
||||||
'$ %s%s',
|
// - if verbosity equals to 3, the command prints directly to the terminal.
|
||||||
colors.blue(command),
|
// - if verbosity equals to 2, the command's stdout and stderr are buffered
|
||||||
(self.options.verbosity < 2) ? colors.grey(' # Silenced\n') : '\n'
|
// and printed to the user's terminal after the command exits (this
|
||||||
);
|
// prevents overlaping output of multiple commands)
|
||||||
|
// - if verbosity equals to 1, the command's stderr is buffered as in 2, but
|
||||||
|
// the stdout is silenced.
|
||||||
|
const log = new stream.PassThrough();
|
||||||
|
|
||||||
context.write(title);
|
const sendLog = (context: any) => {
|
||||||
}
|
var title = util.format(
|
||||||
|
"$ %s%s",
|
||||||
|
colors.blue(command),
|
||||||
|
this.options.verbosity < 2 ? colors.grey(" # Silenced\n") : "\n",
|
||||||
|
);
|
||||||
|
|
||||||
switch (this.options.verbosity) {
|
context.write(title);
|
||||||
case 3:
|
};
|
||||||
sendLog(process.stdout);
|
|
||||||
subShell.stdout.pipe(process.stdout);
|
switch (this.options.verbosity) {
|
||||||
subShell.stderr.pipe(process.stderr);
|
case 3:
|
||||||
break;
|
sendLog(process.stdout);
|
||||||
case 2:
|
subShell.stdout.pipe(process.stdout);
|
||||||
subShell.stdout.pipe(log);
|
subShell.stderr.pipe(process.stderr);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
subShell.stdout.pipe(log);
|
||||||
// fallthrough
|
// fallthrough
|
||||||
case 1:
|
case 1:
|
||||||
subShell.stderr.pipe(log);
|
subShell.stderr.pipe(log);
|
||||||
sendLog(log);
|
sendLog(log);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// setup the cleanup proceedure for when the command finishes.
|
||||||
|
subShell.once("close", function handleSubShellClose() {
|
||||||
|
// write the buffered output to stdout
|
||||||
|
const content = log.read();
|
||||||
|
|
||||||
|
if (content !== null) {
|
||||||
|
process.stdout.write(content);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
subShell.once("exit", function handleSubShellExit(code) {
|
||||||
|
// report an error if the command exited with a non-zero exit code.
|
||||||
|
if (code !== 0) {
|
||||||
|
const err: Error & { status?: number | null } = new Error(
|
||||||
|
util.format("Command `%s` exited with code %s", command, code),
|
||||||
|
);
|
||||||
|
err.status = code;
|
||||||
|
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
// the file wrapping stdout is as the one wrapping stdin (same metadata)
|
||||||
|
// with different contents.
|
||||||
|
const stdout = stdin.clone({ path: "stdout" });
|
||||||
|
stdout.contents = subShell.stdout.pipe(new stream.PassThrough());
|
||||||
|
|
||||||
|
// finally, write the input to the process's stdin.
|
||||||
|
//stdin.pipe(subShell.stdin); //TODO:
|
||||||
|
|
||||||
|
return stdout;
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup the cleanup proceedure for when the command finishes.
|
toString() {
|
||||||
subShell.once('close', function handleSubShellClose() {
|
return this.command;
|
||||||
// write the buffered output to stdout
|
}
|
||||||
var content = log.read();
|
}
|
||||||
|
|
||||||
if (content !== null) {
|
|
||||||
process.stdout.write(content);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
subShell.once('exit', function handleSubShellExit(code) {
|
|
||||||
// report an error if the command exited with a non-zero exit code.
|
|
||||||
if (code !== 0) {
|
|
||||||
err = new Error(util.format('Command `%s` exited with code %s', command, code));
|
|
||||||
err.status = code;
|
|
||||||
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null);
|
|
||||||
});
|
|
||||||
|
|
||||||
// the file wrapping stdout is as the one wrapping stdin (same metadata)
|
|
||||||
// with different contents.
|
|
||||||
stdout = stdin.clone();
|
|
||||||
stdout.contents = subShell.stdout.pipe(new stream.PassThrough());
|
|
||||||
|
|
||||||
// finally, write the input to the process's stdin.
|
|
||||||
//stdin.pipe(subShell.stdin); //ToDo:
|
|
||||||
|
|
||||||
return stdout;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the command template.
|
|
||||||
*
|
|
||||||
* @return {string}
|
|
||||||
*/
|
|
||||||
Command.prototype.toString = function toString() {
|
|
||||||
return this.command;
|
|
||||||
};
|
|
||||||
|
|||||||
+17
-64
@@ -1,71 +1,24 @@
|
|||||||
/**
|
import { Transform, type TransformCallback } from "stream";
|
||||||
* Pipe shell commands in gulp.
|
|
||||||
* @module gulp-run
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { Command } from "./command";
|
import { Command } from "./command";
|
||||||
|
|
||||||
var inherit = require("util").inherits;
|
export class RunTransform extends Transform {
|
||||||
var Transform = require("stream").Transform;
|
private command: Command;
|
||||||
|
|
||||||
/**
|
constructor(template: string, options?: any) {
|
||||||
* Creates a GulpRunner.
|
super({ objectMode: true });
|
||||||
*
|
this.command = new Command(template, options || {});
|
||||||
* A GulpRunner is a Vinyl transform stream that spawns a child process to
|
|
||||||
* transform the file. A separate process is spawned to handle each file
|
|
||||||
* passing through the stream.
|
|
||||||
*
|
|
||||||
* @param {string} template
|
|
||||||
* @param {object} options
|
|
||||||
*/
|
|
||||||
function GulpRunner(template, options) {
|
|
||||||
if (!(this instanceof GulpRunner)) {
|
|
||||||
return new GulpRunner(template, options);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.command = new Command(template, options || {});
|
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void {
|
||||||
Transform.call(this, { objectMode: true });
|
const stdout = this.command.exec(chunk, callback);
|
||||||
|
this.push(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
exec(stdin?: any, callback?: any) {
|
||||||
|
this.write(stdin, callback);
|
||||||
|
this.end();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export default (template: string, options?: any) => new RunTransform(template, options);
|
||||||
* @extends {Stream.Transform}
|
|
||||||
*/
|
|
||||||
inherit(GulpRunner, Transform);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} file
|
|
||||||
* @param {string} encoding
|
|
||||||
* @param {function} callback
|
|
||||||
* @return {void}
|
|
||||||
*/
|
|
||||||
GulpRunner.prototype._transform = function _transform(file, encoding, callback) {
|
|
||||||
var newfile = this.command.exec(file, callback);
|
|
||||||
this.push(newfile);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes `stdin` to itself and returns itself.
|
|
||||||
*
|
|
||||||
* Whenever an object is written into the GulpRunner, a new process is
|
|
||||||
* spawned taking that data as standard input, and a Vinyl file wrapping the
|
|
||||||
* process's standard output is pushed downstream.
|
|
||||||
*
|
|
||||||
* `stdin` may be a String, Buffer, Readable stream, or Vinyl file.
|
|
||||||
*
|
|
||||||
* @param {mixed} stdin
|
|
||||||
* @param {function} callback
|
|
||||||
* @return {GulpRunner}
|
|
||||||
*/
|
|
||||||
GulpRunner.prototype.exec = function exec(stdin, callback) {
|
|
||||||
this.write(stdin, callback);
|
|
||||||
this.end();
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @static
|
|
||||||
* @type {Command}
|
|
||||||
*/
|
|
||||||
GulpRunner.Command = Command;
|
|
||||||
|
|
||||||
export default GulpRunner;
|
|
||||||
|
|||||||
Vendored
+14
@@ -0,0 +1,14 @@
|
|||||||
|
declare module "vinyl" {
|
||||||
|
export class File {
|
||||||
|
path: string;
|
||||||
|
cwd: string;
|
||||||
|
base: string;
|
||||||
|
contents: any;
|
||||||
|
stat: any;
|
||||||
|
history: any[];
|
||||||
|
constructor(params: any);
|
||||||
|
clone(options?: any);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default File;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user