bimba-cli 0.1.4 → 0.1.5

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
@@ -9,7 +9,7 @@ bun add bimba-cli
9
9
 
10
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
11
  ```bash
12
- preload = ["bimba-cli/plugin.ts"]
12
+ preload = ["bimba-cli/plugin.js"]
13
13
  ```
14
14
 
15
15
  You are done!
@@ -24,7 +24,7 @@ Or with the watch argument:
24
24
  bun --watch run src/index.imba
25
25
  ```
26
26
 
27
- ###Frontend development
27
+ ### Frontend development
28
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
29
  ```bash
30
30
  bunx bimba src/index.imba --outdir public
@@ -34,16 +34,36 @@ Or with the watch argument:
34
34
  bunx bimba src/index.imba --outdir public --watch
35
35
  ```
36
36
 
37
- There are additional arguments that you can pass to the bimba:
37
+ Here are all the available argumentsthat you can pass to the bimba:
38
38
 
39
- `--sourcemap` - to tell Bun how to inculde sourcemap files in the final .js. It is `none` by default.
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
40
 
41
- `--minify` - to minify the final JS code in the bundle produced by Bun. It is `false` by default.
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
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.
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
+ ```
44
64
 
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).
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).
47
67
  It does everything needed out of the box.
48
68
 
49
69
  Just let bimba rebuild the sources on change and Lite Server will reload the page in the browser.
@@ -1,13 +1,15 @@
1
1
  #!/usr/bin/env bun
2
2
 
3
3
  import { parseArgs } from "util";
4
- import ansis from 'ansis';
5
- import { imbaPlugin, stats } from './plugin.ts'
4
+ import { imbaPlugin, stats, cache } from './plugin.js'
5
+ import {theme} from './utils.ts';
6
6
  import fs from 'fs'
7
7
  import path from 'path';
8
+ import { rmSync } from "node:fs";
8
9
 
9
- let flags = {};
10
- let folders = {};
10
+
11
+ let flags = {}
12
+ let entrypoint = ''
11
13
 
12
14
  try {
13
15
  const { values, positionals } = parseArgs({
@@ -16,6 +18,7 @@ try {
16
18
  watch: { type: 'boolean' },
17
19
  outdir: { type: 'string' },
18
20
  help: { type: 'boolean' },
21
+ clearcache: { type: 'boolean' },
19
22
  minify: { type: 'boolean' },
20
23
  target: { type: 'string' },
21
24
  sourcemap: { type: 'string' },
@@ -24,29 +27,16 @@ try {
24
27
  allowPositionals: true,
25
28
  });
26
29
  flags = values;
27
- flags.entry = Bun.argv[2];
28
- folders = positionals;
30
+ entrypoint = Bun.argv[2];
29
31
  }
30
32
  catch (error) {
31
33
  if (error instanceof Error)
32
34
  console.log(error.message);
33
35
  else
34
36
  console.log("Could not resolve CLI arguments. Read help to know them: " + theme.flags('--entry file.imba'));
35
- process.exit(1);
37
+ process.exit(0);
36
38
  }
37
39
 
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
40
  // help: more on bun building params here: https://bun.sh/docs/bundler
51
41
  if(flags.help) {
52
42
  console.log("");
@@ -58,13 +48,14 @@ if(flags.help) {
58
48
  console.log(" "+theme.flags('--sourcemap <inline|external|none>')+" How should sourcemap files be included in the .js");
59
49
  console.log(" "+theme.flags('--platform <browser|node>')+" Flag that will be passed to Imba compiler ('node' value does not work in Bun)");
60
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");
61
52
  console.log("");
62
- process.exit(1);
53
+ process.exit(0);
63
54
  }
64
55
 
65
56
 
66
57
  // no entrypoint or outdir
67
- if(!flags.entry || !flags.outdir) {
58
+ if(!entrypoint || !flags.outdir) {
68
59
  console.log("");
69
60
  console.log("You should provide entrypoint and the output dir: "+theme.flags('bimba file.imba --outdir public'));
70
61
  console.log("For more information: "+theme.flags('--help'));
@@ -78,9 +69,11 @@ watch(bundle);
78
69
 
79
70
  function watch(callback) {
80
71
  if (flags.watch) {
81
- const watcher = fs.watch(path.dirname(flags.entry), {recursive: true}, async (event, filename) => ( callback() ));
72
+ const watcher = fs.watch(path.dirname(entrypoint), {recursive: true}, async (event, filename) => ( callback() ));
82
73
 
83
74
  process.on("SIGINT", () => {
75
+ if(flags.clearcache) rmSync(cache, { recursive: true, force: true });
76
+
84
77
  if(watcher) {
85
78
  watcher.close();
86
79
  process.exit(0);
@@ -90,13 +83,11 @@ function watch(callback) {
90
83
  }
91
84
 
92
85
  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);
86
+ if (!fs.existsSync(entrypoint)) {
87
+ console.log(theme.failure('Error.') + ` The specified entrypoint does not exist: ${theme.filedir(entrypoint)}`);
88
+ process.exit(0);
96
89
  }
97
90
 
98
- //if (!fs.existsSync(flags.bundle)){ fs.mkdirSync(flags.bundle);}
99
-
100
91
  stats.failed = 0
101
92
  stats.compiled = 0
102
93
  stats.errors = 0
@@ -105,10 +96,10 @@ async function bundle() {
105
96
  const start = Date.now();
106
97
 
107
98
  console.log("──────────────────────────────────────────────────────────────────────");
108
- console.log(theme.start(`Start building the Imba entrypoint: ${theme.filedir(flags.entry)}`));
99
+ console.log(theme.start(`Start building the Imba entrypoint: ${theme.filedir(entrypoint)}`));
109
100
 
110
101
  const result = await Bun.build({
111
- entrypoints: [flags.entry],
102
+ entrypoints: [entrypoint],
112
103
  outdir: flags.outdir,
113
104
  target: flags.target || 'browser',
114
105
  sourcemap: flags.sourcemap || 'none',
package/package.json CHANGED
@@ -1,17 +1,16 @@
1
1
  {
2
2
  "name": "bimba-cli",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "repository": "github:HeapVoid/bimba",
5
- "module": "index.ts",
5
+ "module": "index.js",
6
6
  "bin": {
7
- "bimba": "./index.ts"
7
+ "bimba": "./index.js"
8
8
  },
9
9
  "description": "The CLI tool to run Imba projects under Bun",
10
10
  "keywords": ["imba", "bun", "plugin"],
11
11
  "license": "MIT",
12
12
  "type": "module",
13
- "dependencies": {
14
- "ansis": "^2.2.0",
13
+ "devDependencies": {
15
14
  "imba": "^2.0.0-alpha.234"
16
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.ts';
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.ts ADDED
@@ -0,0 +1,46 @@
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
+ // 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
+ // // set color theme for an error message
19
+ // const colors = {
20
+ // code: ansis.fg(252).bg(238),
21
+ // margin: ansis.fg(229).bg(145),
22
+ // error: ansis.fg(196).bg(52),
23
+ // ecode: ansis.fg(196).bg(238).bold,
24
+ // };
25
+
26
+ // theme for messages printed in terminal
27
+ // https://i.stack.imgur.com/KTSQa.png
28
+ export const theme = {
29
+ code: (text:string) => colorize(text, 252, 238),
30
+ margin: (text:string) => colorize(text, 229, 145),
31
+ error: (text:string) => colorize(text, 196 , 52),
32
+ ecode: (text:string) => colorize(text, 196, 238),
33
+ action: (text:string) => colorize(text, 237),
34
+ folder: (text:string) => colorize(text, 240),
35
+ filename: (text:string) => colorize(text, 15),
36
+ success: (text:string) => colorize(text, 40),
37
+ failure: (text:string) => colorize(text, 15, 124),
38
+
39
+ flags: (text:string) => colorize(text, 5),
40
+ count: (text:string) => colorize(text, 15),
41
+ start: (text:string) => colorize(text, 252, 233),
42
+ filedir: (text:string) => colorize(text, 15),
43
+ time: (text:string) => colorize(text, 41),
44
+ link: (text:string) => colorize(text, 15),
45
+ online: (text:string) => colorize(text, 40, 22)
46
+ }
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
- }