如何在同一个函数中重新声明标准输出



2我正在尝试使用标准输出 我想使用不同的命令 git 并输出结果。 但是我怎样才能重新声明标准?

感谢您的帮助:)

public async test(){
//some script
const command = 'git rev-parse '+lastTag // lastTag is a label for a tag
let out = "";
const { stdout } = await this.exec(command)
out = stdout.replace(/n/g,'')
console.log(out)
const command2 = 'git rev-list --count HEAD ^'+out 
let out2 = "";
const { stdout } = await this.exec(command2) // error redeclare stdout
out2 = stdout.replace(/n/g,'')
console.log(out2)
}

要么:

  1. 不要使用常量
  2. 将代码分解为较小的函数

例如:

public async test(){
//some script
const command = 'git rev-parse '+lastTag // lastTag is a label for a tag
let out = "";
let { stdout } = await this.exec(command)
out = stdout.replace(/n/g,'')
console.log(out)
const command2 = 'git rev-list --count HEAD ^'+out 
let out2 = "";
{ stdout } = await this.exec(command2)
out2 = stdout.replace(/n/g,'')
console.log(out2)
}

async function runcommand(command) {
let out = "";
const { stdout } = await this.exec(command)
out = stdout.replace(/n/g,'')
return out;
}
async function test(){
const out = await funcommand('git rev-parse ' + lastTag);
console.log(out)
const out2 = await runcommand('git rev-list --count HEAD ^' + out);
console.log(out2)
}

最新更新