Files
Linux.js---javascript-OS/rootfs overrides/usr/bin/sheebr.js
T
Marc 721c919c7b ChibraxOS 2
Changes:
- kernel 6.0.2
- Broken init scripts
- Switch to bun for better peroformance an easier ffi
- Removed grup
- Efi support without graphics (at least graphics werent tested)
- Suppression de toutes les dépendance non libc
- new splash
- broken audio
- typescript support
- ls rewritten to be more reliable
2022-10-22 13:47:33 +02:00

130 lines
3.3 KiB
JavaScript

//@ts-check
import { startjs } from '../../core/hook'
import Lexer from '../lib/sheebr/lexer'
import * as fs from 'fs'
import environment from '../lib/sheebr/environment'
import aliases from '../lib/sheebr/aliases'
import * as path from 'path'
let current_dir = process.cwd()
start()
async function start(){
while(true){
const input = prompt("sheebr> ");
if(input === "exit") break;
if(!input) {
//work around missing line break
console.log()
continue
}
else await evaluate(input);
}
}
/**
* @param { string } input
* @returns
*/
async function evaluate(input){
let lexer = new Lexer(input)
let args = lexer.scanTokens()
args = evaluateAliases(args)
args = evaluateEnvironmentVariables(args);
switch(args[0]){
case "cd":
if(args.length != 2){
console.log("Wrong number of arguments"); return;}
if(!fs.existsSync(args[1])){console.log("Folder does not exist"); return;}
current_dir = args[1][0] == "/" ? args[1] : current_dir + '/' + args[1];
//console.log(current_dir)
break;
case "set":
environment[args[1]] = args[2];
break;
case "alias":
if(args.length == 1) console.log(aliases);
else if(args.length == 2){
console.error("Wrong number of argument")
}
else{
aliases[args[1]] = args[2].split(" ")
}
break;
case "clear":
console.clear();
break;
default:
await evaluateProgram(args)
}
}
/**
* @param {string[]} args
*/
async function evaluateProgram(args){
let found = false;
for(const envPath of environment.path.split(':')){
let tobreak = false;
if(!fs.existsSync(envPath)) continue
const jsPath = path.join(envPath, `${args[0]}.js`)
const tsPath = path.join(envPath, `${args[0]}.ts`)
console.log(jsPath, tsPath, envPath)
if(fs.existsSync(jsPath) || fs.existsSync(tsPath) ){
found = true;
console.log('found it: ' + path + args[0])
try {
const cmd = fs.existsSync(jsPath) ? jsPath : tsPath
const options = {cwd: current_dir, env: environment, shell: true}
console.log(args)
await startjs(cmd, args, options)
console.log('\x1b[0m')
tobreak = true
} catch(e) {
console.log("Program crashed")
}
}
if(tobreak) break;
}
if(!found){
console.log("Program not found")
}
}
/**
* @param {string[]} args
* @returns
*/
function evaluateEnvironmentVariables(args){
let newArgs = []
for(let arg of args){
if(arg.startsWith('$')){
if (arg.slice(1) in environment)
newArgs.push(JSON.stringify(environment[arg.slice(1)]));
else
console.error(`variable ${arg.slice(1)} does not exist.`);
}
else{
newArgs.push(arg)
}
}
return newArgs;
}
/**
* @param { string[] } args
* @returns
*/
function evaluateAliases(args){
if(args[0] in aliases){
return aliases[args[0]].concat(args.slice(1))
}
return args;
}