bimba-cli 0.1.4 → 0.1.6

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.
package/README.md CHANGED
@@ -1,49 +1,69 @@
1
- This tool helps to work with [Imba](https://imba.io) projects under [Bun](https://bun.sh). That is why it is called Bun+IMBA = BIMBA 😉
2
-
3
- It includes the plugin for Bun to compile .imba files and also the CLI tool for buiding .imba files, since the plugins can't be passed to Bun via shell command `bun build`.
4
-
5
- First of all install this tool like any other npm package:
6
- ```bash
7
- bun add bimba-cli
8
- ```
9
-
10
- Then create a `bunfig.toml` file in the root folder of your project, and add only one line in it (I could not find any workaround to do this automatically):
11
- ```bash
12
- preload = ["bimba-cli/plugin.ts"]
13
- ```
14
-
15
- You are done!
16
-
17
- ### Backend development
18
- Now to run an .imba file in Bun's environment you can use the usual Bun syntax:
19
- ```bash
20
- bun run src/index.imba
21
- ```
22
- Or with the watch argument:
23
- ```bash
24
- bun --watch run src/index.imba
25
- ```
26
-
27
- ###Frontend development
28
- For frontend you will need to compile and bundle your source code from .imba to .js. And here the bimba-cli will help:
29
- ```bash
30
- bunx bimba src/index.imba --outdir public
31
- ```
32
- Or with the watch argument:
33
- ```bash
34
- bunx bimba src/index.imba --outdir public --watch
35
- ```
36
-
37
- There are additional arguments that you can pass to the bimba:
38
-
39
- `--sourcemap` - to tell Bun how to inculde sourcemap files in the final .js. It is `none` by default.
40
-
41
- `--minify` - to minify the final JS code in the bundle produced by Bun. It is `false` by default.
42
-
43
- `platform` - the value of this argument will be passed to the Imba compiler. By default it is `browswer`. The value `node` does not work under Bun.
44
-
45
- ####Live reload
46
- Initially I implemented the live reload functionality, but then decided to refuse it. There is a pretty good VS Code extension: [Lite Server](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer).
47
- It does everything needed out of the box.
48
-
1
+ This tool helps to work with [Imba](https://imba.io) projects under [Bun](https://bun.sh). That is why it is called Bun+IMBA = BIMBA 😉
2
+
3
+ It includes the plugin for Bun to compile .imba files and also the CLI tool for buiding .imba files, since the plugins can't be passed to Bun via shell command `bun build`.
4
+
5
+ First of all install this tool like any other npm package:
6
+ ```bash
7
+ bun add bimba-cli -d
8
+ ```
9
+
10
+ Then create a `bunfig.toml` file in the root folder of your project, and add only one line in it (I could not find any workaround to do this automatically):
11
+ ```bash
12
+ preload = ["bimba-cli/plugin.js"]
13
+ ```
14
+
15
+ You are done!
16
+
17
+ ### Backend development
18
+ Now to run an .imba file in Bun's environment you can use the usual Bun syntax:
19
+ ```bash
20
+ bun run src/index.imba
21
+ ```
22
+ Or with the watch argument:
23
+ ```bash
24
+ bun --watch run src/index.imba
25
+ ```
26
+
27
+ ### Frontend development
28
+ For frontend you will need to compile and bundle your source code from .imba to .js. And here the bimba-cli will help:
29
+ ```bash
30
+ bunx bimba src/index.imba --outdir public
31
+ ```
32
+ Or with the watch argument:
33
+ ```bash
34
+ bunx bimba src/index.imba --outdir public --watch
35
+ ```
36
+
37
+ Here are all the available argumentsthat you can pass to the bimba:
38
+
39
+ `--watch` - Monitors changes in the directory where the entry point is located, and rebuilds the projects when the change occures. Keep entrypoint file in the subfolder, otherwise Bun will trigger several times since the cache dir update also triggers rebuilding.
40
+
41
+ `--clearcache` - If is set, the cache directory is deleted when bimba exits. Works only in the watch mode, since when bundling cache will be used next time to speed up the compiling time.
42
+
43
+ ```bash
44
+ bunx bimba src/index.imba --outdir public --watch --clearcache
45
+ ```
46
+
47
+ `--sourcemap` - Tells Bun how to inculde sourcemap files in the final .js. It is `none` by default.
48
+
49
+ ```bash
50
+ bunx bimba src/index.imba --outdir public --sourcemap inline
51
+ ```
52
+
53
+ `--minify` - If is set the final JS code in the bundle produced by Bun will be minified. It is `false` by default.
54
+
55
+ ```bash
56
+ bunx bimba src/index.imba --outdir public --minify
57
+ ```
58
+
59
+ `--platform` - The value of this argument will be passed to the Imba compiler. By default it is `browswer`. The value `node` does not work under Bun.
60
+
61
+ ```bash
62
+ bunx bimba src/index.imba --outdir public --platform browser
63
+ ```
64
+
65
+ #### Live reload
66
+ Initially I have implemented the live reload functionality, but then decided to refuse it. There is a pretty good VS Code extension: [Lite Server](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer).
67
+ It does everything needed out of the box.
68
+
49
69
  Just let bimba rebuild the sources on change and Lite Server will reload the page in the browser.
@@ -1,129 +1,120 @@
1
- #!/usr/bin/env bun
2
-
3
- import { parseArgs } from "util";
4
- import ansis from 'ansis';
5
- import { imbaPlugin, stats } from './plugin.ts'
6
- import fs from 'fs'
7
- import path from 'path';
8
-
9
- let flags = {};
10
- let folders = {};
11
-
12
- try {
13
- const { values, positionals } = parseArgs({
14
- args: Bun.argv,
15
- options: {
16
- watch: { type: 'boolean' },
17
- outdir: { type: 'string' },
18
- help: { type: 'boolean' },
19
- minify: { type: 'boolean' },
20
- target: { type: 'string' },
21
- sourcemap: { type: 'string' },
22
- },
23
- strict: true,
24
- allowPositionals: true,
25
- });
26
- flags = values;
27
- flags.entry = Bun.argv[2];
28
- folders = positionals;
29
- }
30
- catch (error) {
31
- if (error instanceof Error)
32
- console.log(error.message);
33
- else
34
- console.log("Could not resolve CLI arguments. Read help to know them: " + theme.flags('--entry file.imba'));
35
- process.exit(1);
36
- }
37
-
38
- const theme = {
39
- flags: ansis.fg(5),
40
- count: ansis.fg(15).bold,
41
- start: ansis.fg(252).bg(233),
42
- filedir: ansis.fg(15),
43
- success: ansis.fg(40),
44
- failure: ansis.fg(196),
45
- time: ansis.fg(41),
46
- link: ansis.fg(15),
47
- online: ansis.fg(40).bg(22)
48
- };
49
-
50
- // help: more on bun building params here: https://bun.sh/docs/bundler
51
- if(flags.help) {
52
- console.log("");
53
- console.log("Bimba requeres an .imba file and a folder where to put compiled .js files.");
54
- console.log("For example like this: "+theme.filedir('bimba file.imba --outdir public'));
55
- console.log("");
56
- console.log(" "+theme.flags('--outdir <folder>')+" Compile imba files to the specified folder");
57
- console.log(" "+theme.flags('--minify')+" Minify compiled .js files");
58
- console.log(" "+theme.flags('--sourcemap <inline|external|none>')+" How should sourcemap files be included in the .js");
59
- console.log(" "+theme.flags('--platform <browser|node>')+" Flag that will be passed to Imba compiler ('node' value does not work in Bun)");
60
- console.log(" "+theme.flags('--watch')+" Watch for changes in the entrypoint folder");
61
- console.log("");
62
- process.exit(1);
63
- }
64
-
65
-
66
- // no entrypoint or outdir
67
- if(!flags.entry || !flags.outdir) {
68
- console.log("");
69
- console.log("You should provide entrypoint and the output dir: "+theme.flags('bimba file.imba --outdir public'));
70
- console.log("For more information: "+theme.flags('--help'));
71
- console.log("");
72
- process.exit(1);
73
- }
74
-
75
- // build
76
- bundle();
77
- watch(bundle);
78
-
79
- function watch(callback) {
80
- if (flags.watch) {
81
- const watcher = fs.watch(path.dirname(flags.entry), {recursive: true}, async (event, filename) => ( callback() ));
82
-
83
- process.on("SIGINT", () => {
84
- if(watcher) {
85
- watcher.close();
86
- process.exit(0);
87
- }
88
- });
89
- }
90
- }
91
-
92
- async function bundle() {
93
- if (!fs.existsSync(flags.entry)) {
94
- console.log(theme.failure('Error.') + ` The specified entrypoint does not exist: ${theme.filedir(flags.entry)}`);
95
- process.exit(1);
96
- }
97
-
98
- //if (!fs.existsSync(flags.bundle)){ fs.mkdirSync(flags.bundle);}
99
-
100
- stats.failed = 0
101
- stats.compiled = 0
102
- stats.errors = 0
103
- stats.bundled = 0
104
-
105
- const start = Date.now();
106
-
107
- console.log("──────────────────────────────────────────────────────────────────────");
108
- console.log(theme.start(`Start building the Imba entrypoint: ${theme.filedir(flags.entry)}`));
109
-
110
- const result = await Bun.build({
111
- entrypoints: [flags.entry],
112
- outdir: flags.outdir,
113
- target: flags.target || 'browser',
114
- sourcemap: flags.sourcemap || 'none',
115
- minify: flags.minify || true,
116
- plugins: [imbaPlugin]
117
- });
118
-
119
- if(stats.failed)
120
- console.log(theme.start(theme.failure("Failure.") +` Imba compiler failed to proceed ${theme.count(stats.failed)} file${stats.failed > 1 ? 's' : ''}`));
121
- else
122
- console.log(theme.start(theme.success("Success.") +` It took ${theme.time(Date.now() - start)} ms to bundle ${theme.count(stats.bundled)} file${stats.bundled > 1 ? 's' : ''} to the folder: ${theme.filedir(flags.outdir)}`));
123
-
124
- if(!result.success && !stats.errors){
125
- for (const log of result.logs) {
126
- console.log(log);
127
- }
128
- }
1
+ #!/usr/bin/env bun
2
+
3
+ import { parseArgs } from "util";
4
+ import { imbaPlugin, stats, cache } from './plugin.js'
5
+ import {theme} from './utils.js';
6
+ import fs from 'fs'
7
+ import path from 'path';
8
+ import { rmSync } from "node:fs";
9
+
10
+
11
+ let flags = {}
12
+ let entrypoint = ''
13
+
14
+ try {
15
+ const { values, positionals } = parseArgs({
16
+ args: Bun.argv,
17
+ options: {
18
+ watch: { type: 'boolean' },
19
+ outdir: { type: 'string' },
20
+ help: { type: 'boolean' },
21
+ clearcache: { type: 'boolean' },
22
+ minify: { type: 'boolean' },
23
+ target: { type: 'string' },
24
+ sourcemap: { type: 'string' },
25
+ },
26
+ strict: true,
27
+ allowPositionals: true,
28
+ });
29
+ flags = values;
30
+ entrypoint = Bun.argv[2];
31
+ }
32
+ catch (error) {
33
+ if (error instanceof Error)
34
+ console.log(error.message);
35
+ else
36
+ console.log("Could not resolve CLI arguments. Read help to know them: " + theme.flags('--entry file.imba'));
37
+ process.exit(0);
38
+ }
39
+
40
+ // help: more on bun building params here: https://bun.sh/docs/bundler
41
+ if(flags.help) {
42
+ console.log("");
43
+ console.log("Bimba requeres an .imba file and a folder where to put compiled .js files.");
44
+ console.log("For example like this: "+theme.filedir('bimba file.imba --outdir public'));
45
+ console.log("");
46
+ console.log(" "+theme.flags('--outdir <folder>')+" Compile imba files to the specified folder");
47
+ console.log(" "+theme.flags('--minify')+" Minify compiled .js files");
48
+ console.log(" "+theme.flags('--sourcemap <inline|external|none>')+" How should sourcemap files be included in the .js");
49
+ console.log(" "+theme.flags('--platform <browser|node>')+" Flag that will be passed to Imba compiler ('node' value does not work in Bun)");
50
+ console.log(" "+theme.flags('--watch')+" Watch for changes in the entrypoint folder");
51
+ console.log(" "+theme.flags('--clearcache')+" Clear cache on exit, works only when in watch mode");
52
+ console.log("");
53
+ process.exit(0);
54
+ }
55
+
56
+
57
+ // no entrypoint or outdir
58
+ if(!entrypoint || !flags.outdir) {
59
+ console.log("");
60
+ console.log("You should provide entrypoint and the output dir: "+theme.flags('bimba file.imba --outdir public'));
61
+ console.log("For more information: "+theme.flags('--help'));
62
+ console.log("");
63
+ process.exit(1);
64
+ }
65
+
66
+ // build
67
+ bundle();
68
+ watch(bundle);
69
+
70
+ function watch(callback) {
71
+ if (flags.watch) {
72
+ const watcher = fs.watch(path.dirname(entrypoint), {recursive: true}, async (event, filename) => ( callback() ));
73
+
74
+ process.on("SIGINT", () => {
75
+ if(flags.clearcache) rmSync(cache, { recursive: true, force: true });
76
+
77
+ if(watcher) {
78
+ watcher.close();
79
+ process.exit(0);
80
+ }
81
+ });
82
+ }
83
+ }
84
+
85
+ async function bundle() {
86
+ if (!fs.existsSync(entrypoint)) {
87
+ console.log(theme.failure('Error.') + ` The specified entrypoint does not exist: ${theme.filedir(entrypoint)}`);
88
+ process.exit(0);
89
+ }
90
+
91
+ stats.failed = 0
92
+ stats.compiled = 0
93
+ stats.errors = 0
94
+ stats.bundled = 0
95
+
96
+ const start = Date.now();
97
+
98
+ console.log("──────────────────────────────────────────────────────────────────────");
99
+ console.log(theme.start(`Start building the Imba entrypoint: ${theme.filedir(entrypoint)}`));
100
+
101
+ const result = await Bun.build({
102
+ entrypoints: [entrypoint],
103
+ outdir: flags.outdir,
104
+ target: flags.target || 'browser',
105
+ sourcemap: flags.sourcemap || 'none',
106
+ minify: flags.minify || true,
107
+ plugins: [imbaPlugin]
108
+ });
109
+
110
+ if(stats.failed)
111
+ console.log(theme.start(theme.failure("Failure.") +` Imba compiler failed to proceed ${theme.count(stats.failed)} file${stats.failed > 1 ? 's' : ''}`));
112
+ else
113
+ console.log(theme.start(theme.success("Success.") +` It took ${theme.time(Date.now() - start)} ms to bundle ${theme.count(stats.bundled)} file${stats.bundled > 1 ? 's' : ''} to the folder: ${theme.filedir(flags.outdir)}`));
114
+
115
+ if(!result.success && !stats.errors){
116
+ for (const log of result.logs) {
117
+ console.log(log);
118
+ }
119
+ }
129
120
  }
package/package.json CHANGED
@@ -1,17 +1,16 @@
1
- {
2
- "name": "bimba-cli",
3
- "version": "0.1.4",
4
- "repository": "github:HeapVoid/bimba",
5
- "module": "index.ts",
6
- "bin": {
7
- "bimba": "./index.ts"
8
- },
9
- "description": "The CLI tool to run Imba projects under Bun",
10
- "keywords": ["imba", "bun", "plugin"],
11
- "license": "MIT",
12
- "type": "module",
13
- "dependencies": {
14
- "ansis": "^2.2.0",
15
- "imba": "^2.0.0-alpha.234"
16
- }
1
+ {
2
+ "name": "bimba-cli",
3
+ "version": "0.1.6",
4
+ "repository": "github:HeapVoid/bimba",
5
+ "module": "index.js",
6
+ "bin": {
7
+ "bimba": "./index.js"
8
+ },
9
+ "description": "The CLI tool to run Imba projects under Bun",
10
+ "keywords": ["imba", "bun", "plugin"],
11
+ "license": "MIT",
12
+ "type": "module",
13
+ "devDependencies": {
14
+ "imba": "^2.0.0-alpha.234"
15
+ }
17
16
  }
@@ -1,21 +1,12 @@
1
- import { plugin, type BunPlugin } from "bun";
2
- import ansis from 'ansis';
1
+ import { plugin } from "bun";
2
+ import {theme} from './utils.js';
3
3
  import * as compiler from 'imba/compiler'
4
4
  import dir from 'path'
5
5
  import fs from 'fs'
6
+ import { Glob } from "bun";
7
+ import { unlink } from "node:fs/promises";
6
8
 
7
- // theme for messages printed in terminal
8
- // color pallete can be seen here: https://raw.githubusercontent.com/webdiscus/ansis/master/docs/img/ansi256.png
9
- // for more details read the project page: https://github.com/webdiscus/ansis
10
- const theme = {
11
- action: ansis.fg(237),
12
- folder: ansis.fg(240),
13
- filename: ansis.fg(15),
14
- success: ansis.fg(40),
15
- failure: ansis.fg(15).bg(124),
16
- };
17
-
18
- const cache = process.cwd() + '/.cache/';
9
+ export const cache = process.cwd() + '/.cache/';
19
10
  if (!fs.existsSync(cache)){ fs.mkdirSync(cache);}
20
11
 
21
12
  // this should be reset from outside to get results of entrypoint building
@@ -27,7 +18,7 @@ export let stats = {
27
18
  errors: 0
28
19
  };
29
20
 
30
- export const imbaPlugin: BunPlugin = {
21
+ export const imbaPlugin = {
31
22
  name: "imba",
32
23
  async setup(build) {
33
24
 
@@ -82,7 +73,7 @@ export const imbaPlugin: BunPlugin = {
82
73
  let contents = '';
83
74
 
84
75
  // return the cached version if it exists
85
- const cached = cache + Bun.hash(path + fs.statSync(path).mtimeMs) + '_' + f.name + '.js';;
76
+ const cached = cache + Bun.hash(path) + '_' + fs.statSync(path).mtimeMs + '.js';
86
77
  if (fs.existsSync(cached)) {
87
78
  stats.bundled++;
88
79
  stats.cached++;
@@ -93,6 +84,10 @@ export const imbaPlugin: BunPlugin = {
93
84
  };
94
85
  }
95
86
 
87
+ // clear previous cached version
88
+ const glob = new Glob(Bun.hash(path) + '_' + "*.js");
89
+ for await (const file of glob.scan(cache)) unlink(cache + file);
90
+
96
91
  // if no cached version read and compile it with the imba compiler
97
92
  const file = await Bun.file(path).text();
98
93
  const out = compiler.compile(file, {
@@ -137,32 +132,13 @@ plugin(imbaPlugin);
137
132
  // print pretty messages produced by the imba compiler
138
133
  // -------------------------------------------------------------------------------
139
134
 
140
- type imbaCompilerError = {
141
- toSnippet: Function;
142
- toError: Function;
143
- message: string;
144
- range: {
145
- start: {
146
- line: number
147
- }
148
- }
149
- }
150
-
151
135
  // print an error generated by the imba compiler
152
- function printerr(err: imbaCompilerError) {
136
+ function printerr(err) {
153
137
 
154
138
  // halper function to produce empty strings
155
139
  const fill = (len = 0) => {return new Array(len + 1).join(' ')}
156
140
 
157
- // set color theme for an error message
158
- const colors = {
159
- code: ansis.fg(252).bg(238),
160
- margin: ansis.fg(229).bg(145),
161
- error: ansis.fg(196).bg(52),
162
- ecode: ansis.fg(196).bg(238).bold,
163
- };
164
-
165
- // gather the needed information from the compiler error
141
+ // gather the needed information from the compiler error
166
142
  const snippet = err.toSnippet().split("\n");
167
143
  const display = {
168
144
  error: " " + err.message + " ",
@@ -186,21 +162,21 @@ function printerr(err: imbaCompilerError) {
186
162
  // print line with the error message
187
163
  console.log(
188
164
  display.outdent +
189
- colors.margin(fill(Math.min(start, display.margin.length))) +
190
- colors.code(fill(Math.max(0, start - display.margin.length))) +
191
- colors.error(display.error) +
192
- colors.margin(fill(Math.max(0, display.margin.length - end))) +
193
- colors.code(fill(Math.min(total - display.margin.length, total - end)))
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)))
194
170
  );
195
171
 
196
172
  // print line with the source code
197
173
  console.log(
198
174
  display.outdent +
199
- colors.margin(display.margin) +
200
- colors.code(display.source.slice(0,display.errs)) +
201
- colors.error(display.source.slice(display.errs,display.erre)) +
202
- colors.code(display.source.slice(display.erre)) +
203
- colors.code(fill(total - display.source.length - display.margin.length))
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))
204
180
  );
205
181
 
206
182
  // print empty line to balance the view
@@ -208,8 +184,8 @@ function printerr(err: imbaCompilerError) {
208
184
  // for example a link to online docs about the error
209
185
  console.log(
210
186
  display.outdent +
211
- colors.margin(fill(display.margin.length)) +
212
- colors.code(fill(total - display.margin.length))
187
+ theme.margin(fill(display.margin.length)) +
188
+ theme.code(fill(total - display.margin.length))
213
189
  );
214
190
 
215
191
  // print emtpy line
package/utils.js ADDED
@@ -0,0 +1,30 @@
1
+ function colorize(text:string, fg?:number, bg?:number) {
2
+ let result = ''
3
+ if(fg) result += "\x1b[38;5;" + fg.toString() + "m"
4
+ if(bg) result += "\x1b[48;5;" + bg.toString() + "m"
5
+ result += text
6
+ if(fg || bg) result += "\x1b[0m"
7
+ return result
8
+ }
9
+
10
+ // theme for messages printed in terminal
11
+ // https://i.stack.imgur.com/KTSQa.png
12
+ export const theme = {
13
+ code: (text:string) => colorize(text, 252, 238),
14
+ margin: (text:string) => colorize(text, 229, 145),
15
+ error: (text:string) => colorize(text, 196 , 52),
16
+ ecode: (text:string) => colorize(text, 196, 238),
17
+ action: (text:string) => colorize(text, 237),
18
+ folder: (text:string) => colorize(text, 240),
19
+ filename: (text:string) => colorize(text, 15),
20
+ success: (text:string) => colorize(text, 40),
21
+ failure: (text:string) => colorize(text, 15, 124),
22
+
23
+ flags: (text:string) => colorize(text, 5),
24
+ count: (text:string) => colorize(text, 15),
25
+ start: (text:string) => colorize(text, 252, 233),
26
+ filedir: (text:string) => colorize(text, 15),
27
+ time: (text:string) => colorize(text, 41),
28
+ link: (text:string) => colorize(text, 15),
29
+ online: (text:string) => colorize(text, 40, 22)
30
+ }
package/tsconfig.json DELETED
@@ -1,22 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "lib": ["ESNext"],
4
- "target": "ESNext",
5
- "module": "ESNext",
6
- "moduleDetection": "force",
7
- "jsx": "react-jsx",
8
- "allowJs": true,
9
-
10
- /* Bundler mode */
11
- "moduleResolution": "bundler",
12
- "allowImportingTsExtensions": true,
13
- "verbatimModuleSyntax": true,
14
- "noEmit": true,
15
-
16
- /* Linting */
17
- "skipLibCheck": true,
18
- "strict": true,
19
- "noFallthroughCasesInSwitch": true,
20
- "forceConsistentCasingInFileNames": true
21
- }
22
- }