fix(core): update

This commit is contained in:
2024-03-17 00:53:32 +01:00
commit 0e93db4320
17 changed files with 5283 additions and 0 deletions

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartdeno',
version: '1.0.2',
description: 'a module to run deno from node'
}

View File

@ -0,0 +1,77 @@
import * as plugins from './plugins.js';
import * as paths from './paths.js';
import { promises as fs } from 'fs';
import { platform } from 'os';
interface IDenoRelease {
name: string;
assets: IAsset[];
}
interface IAsset {
name: string;
browser_download_url: string;
}
export class DenoDownloader {
private async getDenoDownloadUrl(): Promise<string> {
const osPlatform = platform(); // 'darwin', 'linux', 'win32', etc.
const arch = process.arch; // 'x64', 'arm64', etc.
let osPart: string;
switch (osPlatform) {
case 'darwin':
osPart = 'apple-darwin';
break;
case 'win32':
osPart = 'pc-windows-msvc';
break;
case 'linux':
osPart = 'unknown-linux-gnu';
break;
default:
throw new Error(`Unsupported platform: ${osPlatform}`);
}
const archPart = arch === 'x64' ? 'x86_64' : 'aarch64';
const releasesResponse = await fetch('https://api.github.com/repos/denoland/deno/releases/latest');
const release: IDenoRelease = await releasesResponse.json();
const executableName = `deno-${archPart}-${osPart}.zip`; // Adjust if naming convention changes
const asset = release.assets.find(a => a.name === executableName);
if (!asset) {
throw new Error(`Deno release for ${osPlatform} (${arch}) not found.`);
}
return asset.browser_download_url;
}
private async downloadDeno(url: string, outputPath: string): Promise<void> {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to download Deno: ${response.statusText}`);
}
const buffer = await response.arrayBuffer();
await fs.writeFile(outputPath, Buffer.from(buffer));
}
public async download(outputPath: string = './deno.zip'): Promise<void> {
try {
const url = await this.getDenoDownloadUrl();
await this.downloadDeno(url, outputPath);
console.log(`Deno downloaded successfully to ${outputPath}`);
} catch (error) {
console.error(`Error downloading Deno: ${error.message}`);
}
const smartarchive = await plugins.smartarchive.SmartArchive.fromArchiveFile(outputPath);
const directory = plugins.path.dirname(outputPath);
console.log(`Extracting deno.zip to ${directory}`);
await smartarchive.exportToFs(directory);
const smartshellInstance = new plugins.smarthshell.Smartshell({
executor: 'bash'
});
await smartshellInstance.exec(`(cd ${paths.nogitDir} && chmod +x deno)`);
}
}

21
ts/classes.smartdeno.ts Normal file
View File

@ -0,0 +1,21 @@
import { DenoDownloader } from './classes.denodownloader.js';
import * as plugins from './plugins.js';
import * as paths from './paths.js';
export class SmartDeno {
private denoDownloader = new DenoDownloader();
private smartshellInstance = new plugins.smarthshell.Smartshell({
executor: 'bash'
});
public async init(optionsArg: {
forceLocalDeno?: boolean;
} = {}) {
const denoAlreadyInPath = await plugins.smarthshell.which('deno', {
nothrow: true
});
if (!denoAlreadyInPath || optionsArg.forceLocalDeno) {
await this.denoDownloader.download(plugins.path.join(paths.nogitDir, 'deno.zip'));
}
}
}

3
ts/index.ts Normal file
View File

@ -0,0 +1,3 @@
import * as plugins from './plugins.js';
export * from './classes.smartdeno.js';

7
ts/paths.ts Normal file
View File

@ -0,0 +1,7 @@
import * as plugins from './plugins.js';
export const packageDir = plugins.path.join(
plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
'../'
);
export const nogitDir = plugins.path.join(packageDir, '.nogit');

17
ts/plugins.ts Normal file
View File

@ -0,0 +1,17 @@
// node native scope
import * as path from 'path';
export {
path,
}
// @push.rocks scope
import * as smartarchive from '@push.rocks/smartarchive';
import * as smartpath from '@push.rocks/smartpath';
import * as smarthshell from '@push.rocks/smartshell';
export {
smartarchive,
smartpath,
smarthshell,
}