Skip to content

Commit 39e9587

Browse files
committed
💥 Examples
1 parent 9fcec8d commit 39e9587

5 files changed

Lines changed: 61 additions & 36 deletions

File tree

examples/background-process.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env nodejsscript
2+
/* jshint esversion: 8,-W097, -W040, node: true, expr: true, undef: true */
3+
import { echo, exec, fetch, exit } from "nodejsscript";
4+
5+
const serve= exec("npx serve", { async: true });
6+
for await (const chunk of serve.stdout)
7+
if (chunk.includes('Accepting connections')) break;
8+
9+
await fetch("http://localhost:3000").then(res=> res.text()).then(echo);
10+
exit(0);

examples/backup-github.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env nodejsscript
2+
import { cd, echo, exec, mkdir, question } from "nodejsscript";
3+
4+
const username= await question('What is your GitHub username?');
5+
const token= await question('Do you have GitHub token in env?', {
6+
completions: Object.keys(process.env),
7+
});
8+
9+
let headers= {};
10+
if (process.env[token]) {
11+
headers= {
12+
Authorization: `token ${process.env[token]}`,
13+
};
14+
}
15+
const data= await fetch(`https://api.github.com/users/${username}/repos?per_page=1000`, { headers }).then(res=> res.json());
16+
const urls= data.map(x=> x.git_url.replace('git://github.com/', 'git@github.com:'));
17+
18+
mkdir("-p", "backups");
19+
cd("./backups");
20+
21+
for(const url of urls)
22+
exec(`git clone ${url}`);

examples/cli.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env nodejsscript
2+
import { cli, echo, mkdir, pipe, rm, tempdir, test } from "nodejsscript";
3+
import { join } from "node:path";
4+
5+
cli("", true)
6+
.version("0.1.0")
7+
.describe("NodeJS Script cli test")
8+
.option("--clear", "Clears cerated temp dir")
9+
.action(async function main({ clear }){
10+
const name= join(tempdir, "foo bar");
11+
pipe(mkdir, echo)(name);
12+
echo(test("-d", name));
13+
14+
if(clear)
15+
rm("-R", name);
16+
echo(test("-d", name));
17+
})
18+
.parse(process.argv);

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,17 @@ export function exec$(command, options= {}){
5050
import chalk from "chalk";
5151
export { chalk, chalk as s };
5252

53-
import fetch from 'node-fetch';
54-
export { fetch };
53+
import nodeFetch from 'node-fetch';
54+
/**
55+
* Fetch function
56+
* @param {import('node-fetch').RequestInfo} url - Absolute url or Request instance
57+
* @param {import('node-fetch').RequestInit} [init] - Fetch options
58+
* @return {Promise<import('node-fetch').Response>}
59+
*/
60+
export function fetch(url, init){
61+
if(config.verbose) echo("fetch(", url, ",", init, ")");
62+
return nodeFetch(url, init);
63+
}
5564

5665
import { log } from "node:console";
5766
export function echo(...messages){

test/run.mjs

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)