Skip to content

Commit 7c59165

Browse files
Add cli
1 parent 7da01fb commit 7c59165

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
"react-native": "./dist/browser/index.cjs",
3030
"types": "dist/index.d.ts",
3131
"sideEffects": false,
32+
"bin": "./dist/cli.js",
3233
"scripts": {
3334
"bench": "npm run build && node test/benchmark.js",
34-
"build": "npm run clean && npm run build:node:cjs && npm run build:node:esm && npm run build:browser:cjs && npm run build:browser:esm && npm run build:types",
35+
"build": "npm run clean && npm run build:node:cjs && npm run build:node:esm && npm run build:browser:cjs && npm run build:browser:esm && npm run build:cli && npm run build:types",
3536
"build:browser:cjs": "FMT=cjs ENV=browser rollup -c --name ulidx",
3637
"build:browser:esm": "FMT=esm ENV=browser rollup -c --name ulidx",
38+
"build:cli": "FMT=esm ENV=cli rollup -c && chmod +x ./dist/cli.js",
3739
"build:node:cjs": "FMT=cjs ENV=node rollup -c",
3840
"build:node:esm": "FMT=esm ENV=node rollup -c",
3941
"build:types": "tsc -p tsconfig.dec.json --emitDeclarationOnly && find ./dist -name '*.d.ts' -exec sh -c 'cp {} $(dirname {})/$(basename -s .d.ts {}).d.cts' \\;",

rollup.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const EXTENSIONS = [".js", ".ts"];
88
const ENV = process.env.ENV ? process.env.ENV : "node";
99
const FMT = process.env.FMT ? process.env.FMT : "esm";
1010

11+
const entry = ENV === "cli" ? "source/cli.ts" : "source/index.ts";
12+
const output = ENV === "cli" ? "dist" : `dist/${ENV}`;
13+
1114
const plugins = [
1215
typescript({
1316
tsconfig: "tsconfig.json"
@@ -29,10 +32,10 @@ const externals =
2932

3033
export default {
3134
external: externals,
32-
input: "source/index.ts",
35+
input: entry,
3336
output: [
3437
{
35-
dir: `dist/${ENV}`,
38+
dir: output,
3639
format: FMT,
3740
entryFileNames: `[name].${extension}`
3841
}

source/cli.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env node
2+
3+
import { monotonicFactory } from "./ulid.js";
4+
5+
function parseArgs(args: Array<string>): Record<string, string | boolean> {
6+
const output = {};
7+
8+
while (args.length > 0) {
9+
const arg = args.shift();
10+
11+
if (/^\-\-/.test(arg)) {
12+
if (/=/.test(arg)) {
13+
const [key, value] = arg.split("=");
14+
output[key.substring(2)] = value;
15+
} else {
16+
const value = args.shift();
17+
18+
if (/^-/.test(value)) {
19+
args.unshift(value);
20+
} else if (!value) {
21+
output[arg.substring(2)] = true;
22+
} else {
23+
output[arg.substring(2)] = value;
24+
}
25+
}
26+
}
27+
}
28+
29+
return output;
30+
}
31+
32+
const argv = parseArgs(process.argv.slice(2));
33+
34+
const count = /^\d+/.test(argv["count"] as string) ? parseInt(argv["count"] as string, 10) : 1;
35+
36+
const factory = monotonicFactory();
37+
38+
for (let i = 0; i < count; i += 1) {
39+
console.log(factory());
40+
}

0 commit comments

Comments
 (0)