add postAttachCommand feature
Some checks are pending
docker / build (push) Waiting to run

This commit is contained in:
2025-10-19 01:13:49 +08:00
parent fd338d2dad
commit 7cd85a3315

View File

@@ -104,9 +104,10 @@ export class Xterm {
private intervalID: NodeJS.Timeout;
private writeFunc = (data: ArrayBuffer) => this.writeData(new Uint8Array(data));
private status = false;
private titleStatus = false;
private checkStatus = false;
private connectStatus = false;
private hostTitle = "";
private postAttachCommand = [];
private postAttachCommandStatus = false;
private workdir = "";
private beforeCommand?: string;
constructor(
private options: XtermOptions,
@@ -352,26 +353,17 @@ export class Xterm {
)
.then(response => response.json())
.then(data => {
if (this.workdir === ''){
this.workdir = data.workdir;
}
if (data.status !== '4' && data.status !== '0') {
this.sendData(data.command);
} else {
clearInterval(this.intervalID);
if (data.status === '4') {
fetch(
'http://' + options.get('domain') + ':'+ options.get('port') +'/' +
options.get('user') +
'/' +
options.get('repo') +
'/devcontainer/command?' +
params
)
.then(response => response.json())
.then(data => {
this.sendData(data.command);
})
.catch(error => {
console.error('Error:', error);
});
const parts = data.command.split('\n');
this.sendData(parts[0]+"\n");
this.postAttachCommand = parts;
}
}
})
@@ -422,50 +414,40 @@ export class Xterm {
const data = rawData.slice(1);
switch (cmd) {
case Command.OUTPUT:
console.log(this.status + ': ' + textDecoder.decode(data) + this.connectStatus + ' ' + this.checkStatus + ' ' + this.titleStatus );
console.log(this.status + ': ' + textDecoder.decode(data));
const options = new URLSearchParams(decodeURIComponent(window.location.search));
if (options.get('type') === 'docker') {
// 保存host的标题
if (this.hostTitle === ""){
this.hostTitle = textDecoder.decode(data).replace(/\s/g, '');
}
// 检测是否退出devcontainer标题等于host的标题
if (this.status && textDecoder.decode(data).replace(/\s/g, '').includes(this.hostTitle)){
this.status = false
}
// this.status = true 连接完成
if (
this.status === false &&
textDecoder.decode(data).replace(/\s/g, '').includes('Successfully connected to the container'.replace(/\s/g, ''))
textDecoder.decode(data).replace(/\s/g, '').includes('Successfully connected to the devcontainer'.replace(/\s/g, ''))
) {
if(this.connectStatus == true){
this.status = true;
}
this.connectStatus = true;
}
if (this.checkStatus) {
if (textDecoder.decode(data).replace(/\s/g, '').includes('You have out the container. Please refresh the terminal to reconnect.'.replace(/\s/g, ''))) {
this.checkStatus = false;
this.status = false;
this.connectStatus = false;
}
if(textDecoder.decode(data).includes('\x1b')){
this.checkStatus = false;
}
}
if (this.titleStatus && textDecoder.decode(data).includes('\x1b')) {
this.titleStatus = false;
this.checkStatus = true;
this.sendData(
`echo $WEB_TERMINAL\n`
);
this.status = true;
}
// 连接完成之前不输出标题和docker命令
if (
!(this.status === false && (textDecoder.decode(data).includes('\x1b') || textDecoder.decode(data).replace(/\s/g, '').includes('docker'))) &&
this.titleStatus !== true &&
this.checkStatus !== true
!(this.status === false && (textDecoder.decode(data).includes('\x1b') || textDecoder.decode(data).replace(/\s/g, '').includes('docker')))
){
this.writeFunc(data);
}
if (textDecoder.decode(data).replace(/\s/g, '').includes('exit')) {
this.titleStatus = true;
if (this.status && textDecoder.decode(data).replace(/\s/g, '').includes(this.workdir) && !this.postAttachCommandStatus){
for (let i = 1; i < this.postAttachCommand.length; i++){
this.sendData(this.postAttachCommand[i]+"\n");
}
this.postAttachCommandStatus = true;
}
} else {
this.writeFunc(data);
}
console.log(this.status + ': ' + textDecoder.decode(data) + this.connectStatus + ' ' + this.checkStatus + ' ' + this.titleStatus );
console.log(this.status + ': ' + textDecoder.decode(data));
break;
case Command.SET_WINDOW_TITLE:
console.log('SET_WINDOW_TITLESET_WINDOW_TITLE');