Files
devstar_plugin/src/utils.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

import { exec } from 'child_process';
import * as http from 'http';
import * as https from 'https';
export function fetch(url: string): Promise<string> {
// determine the library to use (based on the url protocol)
const lib = url.startsWith('https://') ? https : http;
return new Promise((resolve, reject) => {
lib.get(url, (response) => {
// make sure the status code is 200
if (response.statusCode !== 200) {
reject(new Error(`Failed to load page, status code: ${response.statusCode}`));
return;
}
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
resolve(data);
});
}).on('error', (err) => {
reject(err);
});
});
}
export const Sleep = (ms:number)=> {
return new Promise(resolve=>setTimeout(resolve, ms))
}
export function getVsCodeCommitId(): Promise<string> {
return new Promise((resolve, reject) => {
exec('code --version', (error, stdout, stderr) => {
if (error) {
reject('Error occurred:' + error.message);
return;
}
if (stderr) {
reject('Error output:' + stderr);
return;
}
const lines = stdout.trim().split('\n');
if (lines.length > 1) {
const commitId = lines[1]; // 第二行是 commit ID
resolve(commitId);
} else {
reject('Unexpected output format:' + stdout);
}
});
})
}