v2 / vlib / os / os_js.js.v
198 lines · 166 sloc · 3.49 KB · b8ef39ab1189d7e24ce444fdf9587259a80f0f2c
Raw
1module os
2
3pub fn mkdir(path string, params MkdirParams) ! {
4 $if js_node {
5 if path == '.' {
6 return
7 }
8 #$fs.mkdirSync(path.valueOf())
9
10 return
11 } $else {
12 return error('could not create folder')
13 }
14}
15
16pub fn is_dir(path string) bool {
17 res := false
18 $if js_node {
19 #res.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory()
20 }
21 return res
22}
23
24pub fn is_link(path string) bool {
25 res := false
26 $if js_node {
27 #res.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink()
28 }
29 return res
30}
31
32struct PathKind {
33 is_dir bool
34 is_link bool
35}
36
37fn kind_of_existing_path(path string) PathKind {
38 is_link := false
39 is_dir := false
40 $if js_node {
41 #is_link.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink()
42 #is_dir.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory()
43 } $else {
44 _ = path
45 }
46 return PathKind{
47 is_dir: is_dir
48 is_link: is_link
49 }
50}
51
52pub fn exists(path string) bool {
53 res := false
54 $if js_node {
55 #res.val = $fs.existsSync(path.str)
56 }
57 return res
58}
59
60pub fn ls(path string) ![]string {
61 if !is_dir(path) {
62 return error('ls(): cannot open dir ${dir}')
63 }
64
65 result := []string{}
66 $if js_node {
67 #let i = 0
68 #$fs.readdirSync(path.str).forEach((path) => result.arr[i++] = new string(path))
69 }
70 return result
71}
72
73pub fn get_raw_line() string {
74 return ''
75}
76
77pub fn executable() string {
78 return ''
79}
80
81pub fn is_executable(path string) bool {
82 eprintln('TODO: There is no isExecutable on fs.stats')
83 return false
84}
85
86pub fn rmdir(path string) ! {
87 $if js_node {
88 err := ''
89 #try {
90 #$fs.rmdirSync(path.str)
91 #return;
92 #} catch (e) {
93 #err.str = 'Failed to remove "' + path.str + '": ' + e.toString()
94 #}
95
96 return error(err)
97 }
98}
99
100pub fn rm(path string) ! {
101 $if js_node {
102 err := ''
103 #try {
104 #$fs.rmSync(path.str)
105 #return;
106 #} catch (e) {
107 #err.str = 'Failed to remove "' + path.str + '": ' + e.toString()
108 #}
109
110 return error(err)
111 }
112}
113
114@[params]
115pub struct CopyParams {
116 fail_if_exists bool
117}
118
119pub fn cp(src string, dst string, config CopyParams) ! {
120 $if js_node {
121 err := ''
122 #try {
123 #$fs.cpSync(src.str,dst.str);
124 #return;
125 #} catch (e) {
126 #err.str = 'failed to copy ' + src.str + ' to ' + dst.str + ': ' + e.toString();
127 #}
128
129 return error(err)
130 }
131}
132
133pub fn rename(src string, dst string) ! {
134 $if js_node {
135 err := ''
136 #try {
137 #$fs.renameSync(src.str,dst.str);
138 #return;
139 #} catch (e) {
140 #err.str = 'failed to rename ' + src.str + ' to ' + dst.str + ': ' + e.toString();
141 #}
142
143 return error(err)
144 }
145}
146
147pub fn read_file(s string) !string {
148 mut err := ''
149 err = err
150 res := ''
151 #try {
152 #res.str = $fs.readFileSync(s.str).toString()
153 #} catch (e) {
154 #err.str = 'Failed to read file: ' + e.toString()
155 #return error(err)
156 #}
157
158 return res
159}
160
161pub fn getwd() string {
162 res := ''
163 #res.str = $process.cwd()
164
165 return res
166}
167
168pub fn getuid() int {
169 res := 0
170 #if (process.getuid) res.val = process.getuid();
171
172 return res
173}
174
175pub fn execvp(cmd string, args []string) ! {
176 panic('os.execvp() is not available on JS backend')
177}
178
179pub fn stdin_resume() {
180 #$process.stdin.resume();
181}
182
183pub fn is_readable(path string) bool {
184 $if js_node {
185 res := false
186 #try { res.val = $fs.accessSync(path.str,$fs.constants.R_OK); } catch { res.val = false; }
187
188 return res
189 } $else {
190 return false
191 }
192}
193
194// get_long_path has no meaning for *nix, but has for windows, where `c:\folder\some~1` for example
195// can be the equivalent of `c:\folder\some spa ces`. On *nix, it just returns a copy of the input path.
196fn get_long_path(path string) !string {
197 return path
198}
199