@stats-forge/github-stats-forge-cli 0.0.1
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/LICENSE +21 -0
- package/README.md +119 -0
- package/build/cards.d.ts +47 -0
- package/build/cards.d.ts.map +1 -0
- package/build/cards.js +295 -0
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +192 -0
- package/build/prompts.d.ts +58 -0
- package/build/prompts.d.ts.map +1 -0
- package/build/prompts.js +125 -0
- package/build/query.d.ts +37 -0
- package/build/query.d.ts.map +1 -0
- package/build/query.js +55 -0
- package/build/saved-card.d.ts +39 -0
- package/build/saved-card.d.ts.map +1 -0
- package/build/saved-card.js +68 -0
- package/build/spinner.d.ts +24 -0
- package/build/spinner.d.ts.map +1 -0
- package/build/spinner.js +44 -0
- package/build/tokens.d.ts +35 -0
- package/build/tokens.d.ts.map +1 -0
- package/build/tokens.js +57 -0
- package/package.json +59 -0
- package/src/cards.ts +343 -0
- package/src/index.ts +243 -0
- package/src/prompts.ts +164 -0
- package/src/query.ts +70 -0
- package/src/saved-card.ts +106 -0
- package/src/spinner.ts +58 -0
- package/src/tokens.ts +71 -0
package/src/tokens.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import type { PersonalAccessToken } from '@stats-forge/github-stats-forge-core';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @file Where the GitHub token comes from.
|
|
8
|
+
*
|
|
9
|
+
* Three sources, in the order a run should prefer them:
|
|
10
|
+
* the flag, an env file, then the environment the shell already carries.
|
|
11
|
+
* Whatever is left over is asked for interactively, so a first run needs no setup.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** The file loaded when `--env-file` is not given. */
|
|
15
|
+
export const DEFAULT_ENV_FILE = '.env';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Loads an env file into `process.env`, the way `node --env-file` would.
|
|
19
|
+
*
|
|
20
|
+
* @param path File to load, relative to the working directory.
|
|
21
|
+
* @param required Whether a missing file is an error.
|
|
22
|
+
* @returns Whether anything was loaded.
|
|
23
|
+
* @throws {Error} When `required` and the file is not there.
|
|
24
|
+
*/
|
|
25
|
+
export const loadEnvFile = (path: string, required: boolean): boolean => {
|
|
26
|
+
const absolute = resolve(process.cwd(), path);
|
|
27
|
+
if (!existsSync(absolute)) {
|
|
28
|
+
if (required) {
|
|
29
|
+
throw new Error(`No env file at ${absolute}`);
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
process.loadEnvFile(absolute);
|
|
34
|
+
return true;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The tokens an env holds, under the `PAT_1`, `PAT_2`, … names core reads.
|
|
39
|
+
*
|
|
40
|
+
* @param env Environment to read.
|
|
41
|
+
* @returns The tokens, in name order, skipping any that are empty.
|
|
42
|
+
*/
|
|
43
|
+
export const tokensFromEnv = (
|
|
44
|
+
env: Record<string, string | undefined>,
|
|
45
|
+
): Array<PersonalAccessToken> =>
|
|
46
|
+
Object.keys(env)
|
|
47
|
+
.filter((name) => /^PAT_\d+$/.test(name))
|
|
48
|
+
.sort()
|
|
49
|
+
.flatMap((name) => {
|
|
50
|
+
const value = env[name];
|
|
51
|
+
return value ? [{ name, value }] : [];
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The tokens a run will use.
|
|
56
|
+
*
|
|
57
|
+
* @param flags Tokens passed as `--pat`, which win over the environment.
|
|
58
|
+
* @param env Environment to read, once any env file has been loaded into it.
|
|
59
|
+
* @returns The tokens, empty when the run has none yet.
|
|
60
|
+
*/
|
|
61
|
+
export const resolveTokens = (
|
|
62
|
+
flags: ReadonlyArray<string>,
|
|
63
|
+
env: Record<string, string | undefined>,
|
|
64
|
+
): Array<PersonalAccessToken> => {
|
|
65
|
+
const fromFlags = flags
|
|
66
|
+
.map((value) => value.trim())
|
|
67
|
+
.filter(Boolean)
|
|
68
|
+
.map((value, index) => ({ name: `--pat #${index + 1}`, value }));
|
|
69
|
+
|
|
70
|
+
return fromFlags.length > 0 ? fromFlags : tokensFromEnv(env);
|
|
71
|
+
};
|