style: tab -> 2 spaces in all typescript files
This commit is contained in:
158
src/utils.ts
158
src/utils.ts
@@ -10,114 +10,114 @@ const {
|
||||
} = require('node:crypto')
|
||||
|
||||
export function fetch(url: string): Promise<string> {
|
||||
// determine the library to use (based on the url protocol)
|
||||
const lib = url.startsWith('https://') ? https : http;
|
||||
// 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;
|
||||
}
|
||||
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);
|
||||
});
|
||||
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 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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export function getDefaultPrivateKeyPath() : string{
|
||||
return path.join(os.homedir(), '.ssh', 'id_rsa')
|
||||
export function getDefaultPrivateKeyPath(): string {
|
||||
return path.join(os.homedir(), '.ssh', 'id_rsa')
|
||||
}
|
||||
|
||||
export function getDefaultPublicKeyPath() :string{
|
||||
return path.join(os.homedir(), '.ssh', 'id_rsa.pub')
|
||||
export function getDefaultPublicKeyPath(): string {
|
||||
return path.join(os.homedir(), '.ssh', 'id_rsa.pub')
|
||||
}
|
||||
|
||||
export function existDefaultPublicKey() :boolean{
|
||||
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
||||
return fs.existsSync(defaultPublicKeyPath)
|
||||
export function existDefaultPublicKey(): boolean {
|
||||
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
||||
return fs.existsSync(defaultPublicKeyPath)
|
||||
}
|
||||
|
||||
export function existDefaultPrivateKey() :boolean{
|
||||
const defaultPrivateKeyPath = getDefaultPrivateKeyPath();
|
||||
return fs.existsSync(defaultPrivateKeyPath)
|
||||
export function existDefaultPrivateKey(): boolean {
|
||||
const defaultPrivateKeyPath = getDefaultPrivateKeyPath();
|
||||
return fs.existsSync(defaultPrivateKeyPath)
|
||||
}
|
||||
|
||||
export function getDefaultPublicKey(): string {
|
||||
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
||||
const defaultPublicKey = fs.readFileSync(defaultPublicKeyPath, 'utf-8');
|
||||
// remove `\r` `\n`
|
||||
const trimmedDefaultPublicKey = defaultPublicKey.replace(/[\r\n]/g, "");
|
||||
return trimmedDefaultPublicKey;
|
||||
const defaultPublicKeyPath = getDefaultPublicKeyPath();
|
||||
const defaultPublicKey = fs.readFileSync(defaultPublicKeyPath, 'utf-8');
|
||||
// remove `\r` `\n`
|
||||
const trimmedDefaultPublicKey = defaultPublicKey.replace(/[\r\n]/g, "");
|
||||
return trimmedDefaultPublicKey;
|
||||
}
|
||||
|
||||
export function getDefaultPrivateKey(): string {
|
||||
const defaultPrivateKey = getDefaultPrivateKeyPath();
|
||||
return fs.readFileSync(defaultPrivateKey, 'utf-8');
|
||||
const defaultPrivateKey = getDefaultPrivateKeyPath();
|
||||
return fs.readFileSync(defaultPrivateKey, 'utf-8');
|
||||
}
|
||||
|
||||
export function createSSHKey() {
|
||||
if (existDefaultPublicKey() && existDefaultPrivateKey()) {
|
||||
// if both public and private key exists, stop
|
||||
return;
|
||||
}
|
||||
if (existDefaultPublicKey() && existDefaultPrivateKey()) {
|
||||
// if both public and private key exists, stop
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
publicKey,
|
||||
privateKey,
|
||||
} = generateKeyPairSync('rsa', {
|
||||
modulusLength: 4096,
|
||||
publicKeyEncoding: {
|
||||
type: 'spki',
|
||||
format: 'pem',
|
||||
},
|
||||
privateKeyEncoding: {
|
||||
type: 'pkcs8',
|
||||
format: 'pem',
|
||||
cipher: 'aes-256-cbc',
|
||||
passphrase: 'devstar'
|
||||
},
|
||||
});
|
||||
const {
|
||||
publicKey,
|
||||
privateKey,
|
||||
} = generateKeyPairSync('rsa', {
|
||||
modulusLength: 4096,
|
||||
publicKeyEncoding: {
|
||||
type: 'spki',
|
||||
format: 'pem',
|
||||
},
|
||||
privateKeyEncoding: {
|
||||
type: 'pkcs8',
|
||||
format: 'pem',
|
||||
cipher: 'aes-256-cbc',
|
||||
passphrase: 'devstar'
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
fs.writeFileSync(getDefaultPublicKeyPath(), publicKey);
|
||||
fs.writeFileSync(getDefaultPrivateKeyPath(), privateKey);
|
||||
} catch(error) {
|
||||
console.error("Failed to write public/private key into the default ssh public/key file: ", error);
|
||||
}
|
||||
try {
|
||||
fs.writeFileSync(getDefaultPublicKeyPath(), publicKey);
|
||||
fs.writeFileSync(getDefaultPrivateKeyPath(), privateKey);
|
||||
} catch (error) {
|
||||
console.error("Failed to write public/private key into the default ssh public/key file: ", error);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user