2021-08-23 14:25:02 +03:00
|
|
|
module os
|
|
|
|
|
2022-10-20 13:56:06 +03:00
|
|
|
pub fn mkdir(path string, params MkdirParams) ! {
|
2021-08-23 14:25:02 +03:00
|
|
|
$if js_node {
|
|
|
|
if path == '.' {
|
2022-10-20 13:56:06 +03:00
|
|
|
return
|
2021-08-23 14:25:02 +03:00
|
|
|
}
|
|
|
|
#$fs.mkdirSync(path.valueOf())
|
|
|
|
|
2022-10-20 13:56:06 +03:00
|
|
|
return
|
2021-08-23 14:25:02 +03:00
|
|
|
} $else {
|
2022-10-20 13:56:06 +03:00
|
|
|
return error('could not create folder')
|
2021-08-23 14:25:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_dir(path string) bool {
|
|
|
|
res := false
|
2021-08-25 14:40:53 +03:00
|
|
|
$if js_node {
|
|
|
|
#res.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory()
|
|
|
|
}
|
2021-08-23 14:25:02 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_link(path string) bool {
|
|
|
|
res := false
|
2021-08-25 14:40:53 +03:00
|
|
|
$if js_node {
|
|
|
|
#res.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink()
|
|
|
|
}
|
2021-08-23 14:25:02 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-08-22 17:10:46 +03:00
|
|
|
struct PathKind {
|
|
|
|
is_dir bool
|
|
|
|
is_link bool
|
|
|
|
}
|
|
|
|
|
|
|
|
fn kind_of_existing_path(path string) PathKind {
|
|
|
|
is_link := false
|
|
|
|
is_dir := false
|
|
|
|
$if js_node {
|
|
|
|
#is_link.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink()
|
|
|
|
#is_dir.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory()
|
|
|
|
}
|
|
|
|
return PathKind{
|
|
|
|
is_dir: is_dir
|
|
|
|
is_link: is_link
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 14:25:02 +03:00
|
|
|
pub fn exists(path string) bool {
|
|
|
|
res := false
|
2021-08-25 14:40:53 +03:00
|
|
|
$if js_node {
|
|
|
|
#res.val = $fs.existsSync(path.str)
|
|
|
|
}
|
2021-08-23 14:25:02 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn ls(path string) ![]string {
|
2021-08-23 14:25:02 +03:00
|
|
|
if !is_dir(path) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('ls(): cannot open dir ${dir}')
|
2021-08-23 14:25:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
result := []string{}
|
2021-08-25 14:40:53 +03:00
|
|
|
$if js_node {
|
|
|
|
#let i = 0
|
2021-09-08 20:30:46 +03:00
|
|
|
#$fs.readdirSync(path.str).forEach((path) => result.arr[i++] = new string(path))
|
2021-08-25 14:40:53 +03:00
|
|
|
}
|
2021-08-23 14:25:02 +03:00
|
|
|
return result
|
|
|
|
}
|
2021-08-25 14:40:53 +03:00
|
|
|
|
|
|
|
pub fn get_raw_line() string {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn executable() string {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_executable(path string) bool {
|
|
|
|
eprintln('TODO: There is no isExecutable on fs.stats')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn rmdir(path string) ! {
|
2021-08-25 14:40:53 +03:00
|
|
|
$if js_node {
|
|
|
|
err := ''
|
|
|
|
#try {
|
|
|
|
#$fs.rmdirSync(path.str)
|
|
|
|
#return;
|
|
|
|
#} catch (e) {
|
|
|
|
#err.str = 'Failed to remove "' + path.str + '": ' + e.toString()
|
|
|
|
#}
|
|
|
|
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn rm(path string) ! {
|
2021-08-25 14:40:53 +03:00
|
|
|
$if js_node {
|
|
|
|
err := ''
|
|
|
|
#try {
|
|
|
|
#$fs.rmSync(path.str)
|
|
|
|
#return;
|
|
|
|
#} catch (e) {
|
|
|
|
#err.str = 'Failed to remove "' + path.str + '": ' + e.toString()
|
|
|
|
#}
|
|
|
|
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn cp(src string, dst string) ! {
|
2021-08-25 14:40:53 +03:00
|
|
|
$if js_node {
|
|
|
|
err := ''
|
|
|
|
#try {
|
|
|
|
#$fs.cpSync(src.str,dst.str);
|
|
|
|
#return;
|
|
|
|
#} catch (e) {
|
|
|
|
#err.str = 'failed to copy ' + src.str + ' to ' + dst.str + ': ' + e.toString();
|
|
|
|
#}
|
|
|
|
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 20:02:04 +03:00
|
|
|
pub fn rename(src string, dst string) ! {
|
|
|
|
$if js_node {
|
|
|
|
err := ''
|
|
|
|
#try {
|
|
|
|
#$fs.renameSync(src.str,dst.str);
|
|
|
|
#return;
|
|
|
|
#} catch (e) {
|
|
|
|
#err.str = 'failed to rename ' + src.str + ' to ' + dst.str + ': ' + e.toString();
|
|
|
|
#}
|
|
|
|
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn read_file(s string) !string {
|
2021-08-25 14:40:53 +03:00
|
|
|
mut err := ''
|
|
|
|
err = err
|
|
|
|
res := ''
|
|
|
|
#try {
|
|
|
|
#res.str = $fs.readFileSync(s.str).toString()
|
|
|
|
#} catch (e) {
|
|
|
|
#err.str = 'Failed to read file: ' + e.toString()
|
|
|
|
#return error(err)
|
|
|
|
#}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getwd() string {
|
|
|
|
res := ''
|
|
|
|
#res.str = $process.cwd()
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2021-12-03 13:25:36 +03:00
|
|
|
|
|
|
|
pub fn getuid() int {
|
|
|
|
res := 0
|
|
|
|
#if (process.getuid) res.val = process.getuid();
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn execvp(cmd string, args []string) ! {
|
2021-12-03 13:25:36 +03:00
|
|
|
panic('os.execvp() is not available on JS backend')
|
|
|
|
}
|
2021-12-13 21:18:12 +03:00
|
|
|
|
|
|
|
pub fn stdin_resume() {
|
|
|
|
#$process.stdin.resume();
|
|
|
|
}
|
2021-12-23 12:36:42 +03:00
|
|
|
|
|
|
|
pub fn is_readable(path string) bool {
|
|
|
|
$if js_node {
|
|
|
|
res := false
|
|
|
|
#try { res.val = $fs.accessSync(path.str,$fs.constants.R_OK); } catch { res.val = false; }
|
|
|
|
|
|
|
|
return res
|
|
|
|
} $else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|