UNPKG

@tsdi/pack

Version:

@tsdi/pack is simple build tasks, base on AOP, Ioc container, via @tsdi. dev build pack activities.

132 lines (130 loc) 4.43 kB
import { __awaiter, __decorate, __metadata } from "tslib"; import { exec } from 'child_process'; import { isBoolean, isArray, lang, isNullOrUndefined, PromiseUtil } from '@tsdi/ioc'; import { Input } from '@tsdi/components'; import { Task } from '@tsdi/activities'; import { NodeActivity } from '../NodeActivity'; const preWordExp = /^[a-zA-Z]/; /** * Shell Task * * @class ShellActivity * @implements {ITask} */ let ShellActivity = class ShellActivity extends NodeActivity { execute(ctx) { return __awaiter(this, void 0, void 0, function* () { let shell = yield ctx.resolveExpression(this.shell); let options = yield ctx.resolveExpression(this.options); let args = yield ctx.resolveExpression(this.args); let argstrs = isArray(args) ? args : this.formatArgs(args); let allowError = yield ctx.resolveExpression(this.allowError); let shells = isArray(shell) ? shell : [shell]; if (this.parallel) { yield Promise.all(shells.map(sh => this.execShell(sh, argstrs, options, allowError))); } else { yield PromiseUtil.step(shells.map(sh => () => this.execShell(sh, argstrs, options, allowError))); } }); } formatShell(shell, args) { if (args.length) { return shell + ' ' + args.join(' '); } return shell; } formatArgs(args) { let strArgs = []; lang.forIn(args, (val, k) => { if (k === 'root' || !preWordExp.test(k)) { return; } if (isArray(val)) { strArgs.push(`--${k} ${val.join(',')}`); } else if (!isNullOrUndefined(val)) { let arg = this.formatArg(val, k, args); if (arg) { strArgs.push(arg); } } }); return strArgs; } formatArg(arg, key, args) { if (isBoolean(arg) && arg) { return `--${key}`; } if (!isNullOrUndefined(arg)) { return `--${key} ${arg}`; } return ''; } execShell(cmd, args, options, allowError) { cmd = this.formatShell(cmd, args); if (!cmd) { return Promise.resolve(); } return new Promise((resolve, reject) => { let shell = exec(cmd, options, (err, stdout, stderr) => { if (err) { reject(err); } else { resolve(stdout); } }); shell.stdout.on('data', data => { this.checkStdout(data, resolve, reject); }); shell.stderr.on('data', err => { this.checkStderr(err, reject, allowError); }); shell.on('exit', (code) => { let msg = `exit child process with code:${code} `; console.log(msg); if (code > 0) { reject(new Error(msg)); } }); }); } checkStderr(err, reject, allowError) { console.error(err); if (allowError === false) { reject(err); } } checkStdout(data, resolve, reject) { console.log(data); } static ρAnn() { return { "name": "ShellActivity", "params": { "execute": ["ctx"], "formatShell": ["shell", "args"], "formatArgs": ["args"], "formatArg": ["arg", "key", "args"], "execShell": ["cmd", "args", "options", "allowError"], "checkStderr": ["err", "reject", "allowError"], "checkStdout": ["data", "resolve", "reject"] } }; } }; __decorate([ Input(), __metadata("design:type", Object) ], ShellActivity.prototype, "shell", void 0); __decorate([ Input(), __metadata("design:type", Object) ], ShellActivity.prototype, "args", void 0); __decorate([ Input(), __metadata("design:type", Object) ], ShellActivity.prototype, "options", void 0); __decorate([ Input(), __metadata("design:type", Object) ], ShellActivity.prototype, "allowError", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], ShellActivity.prototype, "parallel", void 0); ShellActivity = __decorate([ Task('shell') ], ShellActivity); export { ShellActivity }; //# sourceMappingURL=../sourcemaps/tasks/ShellActivity.js.map