bimba-cli 0.1.9 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/plugin.js +197 -193
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bimba-cli",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
4
4
  "repository": "github:HeapVoid/bimba",
5
5
  "module": "index.js",
6
6
  "bin": {
package/plugin.js CHANGED
@@ -1,193 +1,197 @@
1
- import { plugin } from "bun";
2
- import {theme} from './utils.js';
3
- import * as compiler from 'imba/compiler'
4
- import dir from 'path'
5
- import fs from 'fs'
6
- import { Glob } from "bun";
7
- import { unlink } from "node:fs/promises";
8
-
9
- export const cache = process.cwd() + '/.cache/';
10
- if (!fs.existsSync(cache)){ fs.mkdirSync(cache);}
11
-
12
- // this should be reset from outside to get results of entrypoint building
13
- export let stats = {
14
- failed: 0,
15
- compiled: 0,
16
- cached: 0,
17
- bundled: 0,
18
- errors: 0
19
- };
20
-
21
- export const imbaPlugin = {
22
- name: "imba",
23
- async setup(build) {
24
-
25
- // when there is import without file extension
26
- build.onResolve({filter: /^.*[^.]{5}$/ }, ({ path, importer }) => {
27
-
28
- let filename = path;
29
- // resolve relative path
30
- if (path.startsWith('.')) { filename = dir.resolve(dir.dirname(importer), filename) };
31
-
32
- // assume that the file is .js
33
- try { return {path: Bun.resolveSync(filename + '.js', '.')}}
34
- catch (error) {
35
- // assume that the file is .mjs
36
- try { return {path: Bun.resolveSync(filename + '.mjs', '.')}}
37
- catch (error) {
38
- // assume that the file is .cs
39
- try { return {path: Bun.resolveSync(filename + '.cjs', '.')}}
40
- catch (error) {
41
- // assume that the file is .imba
42
- try { return {path: Bun.resolveSync(filename + '.imba', '.')}}
43
- catch (error) {
44
- // if direct resolution failed
45
- filename += '.imba';
46
-
47
- // assume that the relative path should be resolved relative to importer
48
- let fn = dir.resolve(dir.dirname(importer), filename);
49
- if (fs.existsSync(fn)) return {path: fn};
50
- // assume that the relative path should be resolved relative to node_modules
51
- fn = dir.resolve('./node_modules', filename);
52
- if (fs.existsSync(fn)) return {path: fn};
53
- // assume that the relative path should be resolved relative to project root
54
- fn = dir.resolve(process.cwd(), filename);
55
- if (fs.existsSync(fn)) return {path: fn};
56
-
57
- // if the path still is unresolved throw error and leave the further resolution on Bun's resolver
58
- if (error instanceof Error) {
59
- throw new Error(error.message);
60
- }
61
- else
62
- throw new Error('Could not resolve file: ' + path);
63
- }
64
- }
65
- }
66
- }
67
- })
68
-
69
- // when an .imba file is imported...
70
- build.onLoad({ filter: /\.imba$/ }, async ({ path }) => {
71
-
72
- const f = dir.parse(path)
73
- let contents = '';
74
-
75
- // return the cached version if it exists
76
- const cached = cache + Bun.hash(path) + '_' + fs.statSync(path).mtimeMs + '.js';
77
- if (fs.existsSync(cached)) {
78
- stats.bundled++;
79
- stats.cached++;
80
- //console.log(theme.action("cached: ") + theme.folder(f.dir + '/') + theme.filename(f.base) + " - " + theme.success("ok"));
81
- return {
82
- contents: await Bun.file(cached).text(),
83
- loader: "js",
84
- };
85
- }
86
-
87
- // clear previous cached version
88
- const glob = new Glob(Bun.hash(path) + '_' + "*.js");
89
- for await (const file of glob.scan(cache)) if (fs.existsSync(cache + file)) unlink(cache + file);
90
-
91
- // if no cached version read and compile it with the imba compiler
92
- const file = await Bun.file(path).text();
93
- const out = compiler.compile(file, {
94
- sourcePath: path,
95
- platform: 'browser'
96
- })
97
-
98
- // print about file complitaion
99
- console.write(theme.action("compiling: ") + theme.folder(f.dir + '/') + theme.filename(f.base) + " - ");
100
-
101
- // the file has been successfully compiled
102
- if (!out.errors || !out.errors.length) {
103
- stats.bundled++;
104
- stats.compiled++;
105
- contents = out.js;
106
- await Bun.write(cached, contents);
107
- console.write(theme.success("cached" + "\n"));
108
- }
109
- // there were errors during compilation
110
- else {
111
- console.write(theme.failure(" fail ") + "\n");
112
- stats.failed++;
113
- for (let i = 0; i < out.errors.length; i++) {
114
- if(out.errors[i]) printerr(out.errors[i]);
115
- }
116
- stats.errors++;
117
- }
118
-
119
- // and return the compiled source code as "js"
120
- return {
121
- contents,
122
- loader: "js",
123
- };
124
- });
125
- }
126
- };
127
-
128
- plugin(imbaPlugin);
129
-
130
-
131
- // -------------------------------------------------------------------------------
132
- // print pretty messages produced by the imba compiler
133
- // -------------------------------------------------------------------------------
134
-
135
- // print an error generated by the imba compiler
136
- function printerr(err) {
137
-
138
- // halper function to produce empty strings
139
- const fill = (len = 0) => {return new Array(len + 1).join(' ')}
140
-
141
- // gather the needed information from the compiler error
142
- const snippet = err.toSnippet().split("\n");
143
- const display = {
144
- error: " " + err.message + " ",
145
- outdent: fill(10),
146
- source: snippet[1] + " ",
147
- margin: " line " + err.range.start.line + " ",
148
- errs: snippet[2].indexOf('^'),
149
- erre: snippet[2].lastIndexOf('^') + 1,
150
- };
151
-
152
- // calculate parameters for priniting a message
153
- const center = display.margin.length + display.errs + Math.floor((display.erre - display.errs) / 2);
154
- const half = Math.ceil((display.error.length - 1) / 2);
155
- const start = Math.max(0, center - half);
156
- const end = start + display.error.length;
157
- const total = Math.max(display.margin.length + display.source.length, end);
158
-
159
- // print emtpy line
160
- console.log('');
161
-
162
- // print line with the error message
163
- console.log(
164
- display.outdent +
165
- theme.margin(fill(Math.min(start, display.margin.length))) +
166
- theme.code(fill(Math.max(0, start - display.margin.length))) +
167
- theme.error(display.error) +
168
- theme.margin(fill(Math.max(0, display.margin.length - end))) +
169
- theme.code(fill(Math.min(total - display.margin.length, total - end)))
170
- );
171
-
172
- // print line with the source code
173
- console.log(
174
- display.outdent +
175
- theme.margin(display.margin) +
176
- theme.code(display.source.slice(0,display.errs)) +
177
- theme.error(display.source.slice(display.errs,display.erre)) +
178
- theme.code(display.source.slice(display.erre)) +
179
- theme.code(fill(total - display.source.length - display.margin.length))
180
- );
181
-
182
- // print empty line to balance the view
183
- // later we can put something usefull here
184
- // for example a link to online docs about the error
185
- console.log(
186
- display.outdent +
187
- theme.margin(fill(display.margin.length)) +
188
- theme.code(fill(total - display.margin.length))
189
- );
190
-
191
- // print emtpy line
192
- console.log('');
193
- }
1
+ import { plugin } from "bun";
2
+ import {theme} from './utils.js';
3
+ import * as compiler from 'imba/compiler'
4
+ import {resolve, join, dirname, parse} from 'path'
5
+ import fs from 'fs'
6
+ import { Glob } from "bun";
7
+ import { unlink } from "node:fs/promises";
8
+
9
+ export const cache = join(process.cwd(),'.cache');
10
+ if (!fs.existsSync(cache)){ fs.mkdirSync(cache);}
11
+
12
+ // this should be reset from outside to get results of entrypoint building
13
+ export let stats = {
14
+ failed: 0,
15
+ compiled: 0,
16
+ cached: 0,
17
+ bundled: 0,
18
+ errors: 0
19
+ };
20
+
21
+ export const imbaPlugin = {
22
+ name: "imba",
23
+ async setup(build) {
24
+
25
+ // when there is import without file extension
26
+ build.onResolve({filter: /^.*[^.]{5}$/ }, ({ path, importer }) => {
27
+
28
+ let filename = path;
29
+ // resolve relative path
30
+ if (path.startsWith('.')) { filename = resolve(dirname(importer), filename) };
31
+
32
+ // assume that the file is .js
33
+ try { return {path: Bun.resolveSync(filename + '.js', '.')}}
34
+ catch (error) {
35
+ // assume that the file is .mjs
36
+ try { return {path: Bun.resolveSync(filename + '.mjs', '.')}}
37
+ catch (error) {
38
+ // assume that the file is .cs
39
+ try { return {path: Bun.resolveSync(filename + '.cjs', '.')}}
40
+ catch (error) {
41
+ // assume that the file is .imba
42
+ try { return {path: Bun.resolveSync(filename + '.imba', '.')}}
43
+ catch (error) {
44
+ // if direct resolution failed
45
+ filename += '.imba';
46
+
47
+ // assume that the relative path should be resolved relative to importer
48
+ let fn = resolve(dirname(importer), filename);
49
+ if (fs.existsSync(fn)) return {path: fn};
50
+ // assume that the relative path should be resolved relative to node_modules
51
+ fn = resolve('./node_modules', filename);
52
+ if (fs.existsSync(fn)) return {path: fn};
53
+ // assume that the relative path should be resolved relative to project root
54
+ fn = resolve(process.cwd(), filename);
55
+ if (fs.existsSync(fn)) return {path: fn};
56
+
57
+ // if the path still is unresolved throw error and leave the further resolution on Bun's resolver
58
+ if (error instanceof Error) {
59
+ throw new Error(error.message);
60
+ }
61
+ else
62
+ throw new Error('Could not resolve file: ' + path);
63
+ }
64
+ }
65
+ }
66
+ }
67
+ })
68
+
69
+ // when an .imba file is imported...
70
+ build.onLoad({ filter: /\.imba$/ }, async ({ path }) => {
71
+
72
+ const f = parse(path)
73
+ let contents = '';
74
+
75
+ // return the cached version if it exists
76
+ const path_hash = Bun.hash(path)
77
+ const time_hash = Bun.hash(fs.statSync(path).mtimeMs)
78
+ const cached = join(cache, path_hash + '_' + time_hash + '.js');
79
+ if (fs.existsSync(cached)) {
80
+ stats.bundled++;
81
+ stats.cached++;
82
+ return {
83
+ contents: await Bun.file(cached).text(),
84
+ loader: "js",
85
+ };
86
+ }
87
+
88
+ // clear previous cached version
89
+ const glob = new Glob(path_hash + '_' + "*.js");
90
+ for await (const file of glob.scan(cache)) {
91
+ const prev_cached = join(cache, file);
92
+ if (fs.existsSync(prev_cached)) unlink(prev_cached);
93
+ }
94
+
95
+ // if no cached version read and compile it with the imba compiler
96
+ const file = await Bun.file(path).text();
97
+ const out = compiler.compile(file, {
98
+ sourcePath: path,
99
+ platform: 'browser'
100
+ })
101
+
102
+ // print about file complitaion
103
+ console.write(theme.action("compiling: ") + theme.folder(f.dir + '/') + theme.filename(f.base) + " - ");
104
+
105
+ // the file has been successfully compiled
106
+ if (!out.errors || !out.errors.length) {
107
+ stats.bundled++;
108
+ stats.compiled++;
109
+ contents = out.js;
110
+ await Bun.write(cached, contents);
111
+ console.write(theme.success("cached" + "\n"));
112
+ }
113
+ // there were errors during compilation
114
+ else {
115
+ console.write(theme.failure(" fail ") + "\n");
116
+ stats.failed++;
117
+ for (let i = 0; i < out.errors.length; i++) {
118
+ if(out.errors[i]) printerr(out.errors[i]);
119
+ }
120
+ stats.errors++;
121
+ }
122
+
123
+ // and return the compiled source code as "js"
124
+ return {
125
+ contents,
126
+ loader: "js",
127
+ };
128
+ });
129
+ }
130
+ };
131
+
132
+ plugin(imbaPlugin);
133
+
134
+
135
+ // -------------------------------------------------------------------------------
136
+ // print pretty messages produced by the imba compiler
137
+ // -------------------------------------------------------------------------------
138
+
139
+ // print an error generated by the imba compiler
140
+ function printerr(err) {
141
+
142
+ // halper function to produce empty strings
143
+ const fill = (len = 0) => {return new Array(len + 1).join(' ')}
144
+
145
+ // gather the needed information from the compiler error
146
+ const snippet = err.toSnippet().split("\n");
147
+ const display = {
148
+ error: " " + err.message + " ",
149
+ outdent: fill(10),
150
+ source: snippet[1] + " ",
151
+ margin: " line " + err.range.start.line + " ",
152
+ errs: snippet[2].indexOf('^'),
153
+ erre: snippet[2].lastIndexOf('^') + 1,
154
+ };
155
+
156
+ // calculate parameters for priniting a message
157
+ const center = display.margin.length + display.errs + Math.floor((display.erre - display.errs) / 2);
158
+ const half = Math.ceil((display.error.length - 1) / 2);
159
+ const start = Math.max(0, center - half);
160
+ const end = start + display.error.length;
161
+ const total = Math.max(display.margin.length + display.source.length, end);
162
+
163
+ // print emtpy line
164
+ console.log('');
165
+
166
+ // print line with the error message
167
+ console.log(
168
+ display.outdent +
169
+ theme.margin(fill(Math.min(start, display.margin.length))) +
170
+ theme.code(fill(Math.max(0, start - display.margin.length))) +
171
+ theme.error(display.error) +
172
+ theme.margin(fill(Math.max(0, display.margin.length - end))) +
173
+ theme.code(fill(Math.min(total - display.margin.length, total - end)))
174
+ );
175
+
176
+ // print line with the source code
177
+ console.log(
178
+ display.outdent +
179
+ theme.margin(display.margin) +
180
+ theme.code(display.source.slice(0,display.errs)) +
181
+ theme.error(display.source.slice(display.errs,display.erre)) +
182
+ theme.code(display.source.slice(display.erre)) +
183
+ theme.code(fill(total - display.source.length - display.margin.length))
184
+ );
185
+
186
+ // print empty line to balance the view
187
+ // later we can put something usefull here
188
+ // for example a link to online docs about the error
189
+ console.log(
190
+ display.outdent +
191
+ theme.margin(fill(display.margin.length)) +
192
+ theme.code(fill(total - display.margin.length))
193
+ );
194
+
195
+ // print emtpy line
196
+ console.log('');
197
+ }