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