Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
simple env()
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Jun 7, 2023
1 parent d82b2ba commit 32943d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
28 changes: 13 additions & 15 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import path from "node:path";
import fs from 'node:fs';
import os from 'node:os';

// Welcome PR, translate to english if need: https://www.txthinking.com/talks/articles/coding-principle.article

var _cwd = process.cwd();
var _env = null;
var _env = {};
global.echo = console.log;
global.question = prompt;
global.sleep = Bun.sleepSync;
Expand All @@ -12,8 +14,12 @@ global.exit = process.exit;
global.now = () => {
return parseInt(Date.now() / 1000);
};
global.env = (kv) => {
_env = kv;
global.env = (k, v) => {
if(typeof k == 'string'){
_env[k] = v;
return
}
_env = {...env, ...k};
};
global.cd = (dir) => {
if(path.isAbsolute(dir)){
Expand All @@ -25,13 +31,9 @@ global.cd = (dir) => {
global.$ = (first) => {
var cmd = first instanceof Array ? first[0] : first;
echo(cmd);
var e = process.env;
if(_env){
e = {...e, ..._env};
}
var p = Bun.spawnSync(["sh", "-c", cmd], {
cwd: _cwd,
env: e,
env: {...process.env, ..._env},
stdin: null,
stdout: Bun.stdout,
stderr: Bun.stderr,
Expand All @@ -43,13 +45,9 @@ global.$ = (first) => {
global.$1 = (first) => {
var cmd = first instanceof Array ? first[0] : first;
echo(cmd);
var e = process.env;
if(_env){
e = {...e, ..._env};
}
var p = Bun.spawnSync(["sh", "-c", cmd], {
cwd: _cwd,
env: e,
env: {...process.env, ..._env},
stdin: null,
stdout: 'pipe',
stderr: Bun.stderr,
Expand Down Expand Up @@ -121,7 +119,7 @@ global.cp = (from, a, b) => {
var out = "";
if(from.endsWith(".zip")){
out = "_.zip";
if(!which("ls")){
if(!which("7z")){
throw 'need 7z, recommend $ nami install 7z';
}
}
Expand Down Expand Up @@ -180,7 +178,7 @@ $ jb /path/to/file.js
$ jb https://example.com/file.js
$ jb '${s}'
v20230606
v20230607
/~https://github.com/txthinking/jb
`;
Expand Down
12 changes: 3 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,10 @@ var flags = [
$(`ls ${flags.join(' ')}`)
```

If the executed program failed, stderr will be thrown.
If the executed program failed, error will be thrown.

```js
try {
$`brook unknownsubcommand`
} catch (e) {
console.log(`Error: ${e}`)
}
$`brook unknownsubcommand`
```

### ``$1`command` ``
Expand All @@ -101,9 +97,7 @@ $(`echo ${count}`)
Set env

```js
env({
HELLO: "JB",
})
env('HELLO', "JB")
$`echo $HELLO` // => JB
```

Expand Down

0 comments on commit 32943d9

Please sign in to comment.