@youcan/cli-kit 2.8.0 → 2.8.2

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/dist/node/http.js CHANGED
@@ -1,57 +1,51 @@
1
- import https from 'node:https';
2
- import process from 'node:process';
3
- import { isJson } from '../common/string.js';
4
- import fetch from 'node-fetch';
5
- import { is, mergeDeepLeft } from 'ramda';
6
- import './cli.js';
7
- import 'conf';
8
- import { get as get$2 } from './env.js';
9
- import './filesystem.js';
10
- import 'formdata-node';
11
- import 'formdata-node/file-from-path';
12
- import 'simple-git';
13
- import 'execa';
14
- import 'find-process';
15
- import 'get-port';
16
- import 'tcp-port-used';
17
- import { get as get$1 } from './session.js';
18
- import 'kleur';
19
- import 'dayjs';
20
- import '../ui/components/DevOutput.js';
21
- import 'react';
22
- import 'ink';
23
-
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { isJson } from "../common/string.js";
3
+ import { get as get$1 } from "./env.js";
4
+ import { get as get$2 } from "./session.js";
5
+ import "../index.js";
6
+ import process from "node:process";
7
+ import https from "node:https";
8
+ import fetch from "node-fetch";
9
+ import { is, mergeDeepLeft } from "ramda";
10
+ //#region lib/node/http.ts
11
+ var http_exports = /* @__PURE__ */ __exportAll({
12
+ get: () => get,
13
+ post: () => post
14
+ });
24
15
  async function agent() {
25
- if (get$2('HOST_ENV') === 'dev') {
26
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
27
- }
28
- return new https.Agent({ keepAlive: true, keepAliveMsecs: 5 * 60 * 1000 });
16
+ if (get$1("HOST_ENV") === "dev") process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
17
+ return new https.Agent({
18
+ keepAlive: true,
19
+ keepAliveMsecs: 300 * 1e3
20
+ });
29
21
  }
30
22
  async function defaults() {
31
- const session$1 = await get$1();
32
- return {
33
- agent: await agent(),
34
- headers: {
35
- Accept: 'application/json',
36
- Authorization: session$1 ? `Bearer ${session$1.access_token}` : undefined,
37
- },
38
- };
23
+ const session = await get$2();
24
+ return {
25
+ agent: await agent(),
26
+ headers: {
27
+ Accept: "application/json",
28
+ Authorization: session ? `Bearer ${session.access_token}` : void 0
29
+ }
30
+ };
39
31
  }
40
32
  async function request(endpoint, options = {}) {
41
- if (is(String)(options.body) && isJson(options.body)) {
42
- options = mergeDeepLeft(options, { headers: { 'Content-Type': 'application/json' } });
43
- }
44
- const response = await fetch(endpoint, mergeDeepLeft(options, await defaults()));
45
- if (!response.ok) {
46
- throw new Error(await response.text(), { cause: response });
47
- }
48
- return response.json();
33
+ if (is(String)(options.body) && isJson(options.body)) options = mergeDeepLeft(options, { headers: { "Content-Type": "application/json" } });
34
+ const response = await fetch(endpoint, mergeDeepLeft(options, await defaults()));
35
+ if (!response.ok) throw new Error(await response.text(), { cause: response });
36
+ return response.json();
49
37
  }
50
38
  async function get(endpoint, options = {}) {
51
- return request(`https://${endpoint}`, { ...options, method: 'GET' });
39
+ return request(`https://${endpoint}`, {
40
+ ...options,
41
+ method: "GET"
42
+ });
52
43
  }
53
44
  async function post(endpoint, options = {}) {
54
- return request(`https://${endpoint}`, { ...options, method: 'POST' });
45
+ return request(`https://${endpoint}`, {
46
+ ...options,
47
+ method: "POST"
48
+ });
55
49
  }
56
-
57
- export { get, post };
50
+ //#endregion
51
+ export { get, http_exports, post };
package/dist/node/path.js CHANGED
@@ -1,20 +1,28 @@
1
- import path from 'node:path';
2
- import process from 'node:process';
3
-
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import process from "node:process";
3
+ import path from "node:path";
4
+ //#region lib/node/path.ts
5
+ var path_exports = /* @__PURE__ */ __exportAll({
6
+ basename: () => basename,
7
+ cwd: () => cwd,
8
+ dirname: () => dirname,
9
+ join: () => join,
10
+ resolve: () => resolve
11
+ });
4
12
  function resolve(...paths) {
5
- return path.resolve(...paths);
13
+ return path.resolve(...paths);
6
14
  }
7
15
  function cwd() {
8
- return path.normalize(process.env.INIT_CWD ? process.env.INIT_CWD : process.cwd());
16
+ return path.normalize(process.env.INIT_CWD ? process.env.INIT_CWD : process.cwd());
9
17
  }
10
18
  function join(...paths) {
11
- return path.join(...paths);
19
+ return path.join(...paths);
12
20
  }
13
21
  function dirname(filepath) {
14
- return path.dirname(filepath);
22
+ return path.dirname(filepath);
15
23
  }
16
24
  function basename(filepath) {
17
- return path.basename(filepath);
25
+ return path.basename(filepath);
18
26
  }
19
-
20
- export { basename, cwd, dirname, join, resolve };
27
+ //#endregion
28
+ export { basename, cwd, dirname, join, path_exports, resolve };
@@ -1,111 +1,92 @@
1
- import 'change-case';
2
- import { listen } from './callback.js';
3
- import './cli.js';
4
- import { manager } from './config.js';
5
- import { randomHex, sha256, base64URLEncode } from './crypto.js';
6
- import { apiHostname, sellerAreaHostname, oauthClientId } from './env.js';
7
- import './filesystem.js';
8
- import 'formdata-node';
9
- import 'formdata-node/file-from-path';
10
- import 'simple-git';
11
- import { isPortAvailable, getPortProcessName, killPortProcess, open } from './system.js';
12
- import { get as get$1, post } from './http.js';
13
- import 'kleur';
14
- import 'dayjs';
15
- import '../ui/components/DevOutput.js';
16
- import 'react';
17
- import 'ink';
18
-
19
- const LS_PORT = 3000;
20
- const LS_HOST = 'localhost';
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { listen } from "./callback.js";
3
+ import { manager } from "./config.js";
4
+ import { base64URLEncode, randomHex, sha256 } from "./crypto.js";
5
+ import { apiHostname, oauthClientId, sellerAreaHostname } from "./env.js";
6
+ import { getPortProcessName, isPortAvailable, killPortProcess, open } from "./system.js";
7
+ import { get as get$1, post } from "./http.js";
8
+ import "../index.js";
9
+ //#region lib/node/session.ts
10
+ var session_exports = /* @__PURE__ */ __exportAll({
11
+ authenticate: () => authenticate,
12
+ get: () => get
13
+ });
14
+ const LS_PORT = 3e3;
15
+ const LS_HOST = "localhost";
21
16
  function generatePkcePair(length) {
22
- const verifier = randomHex(length);
23
- const encoder = new TextEncoder();
24
- const data = encoder.encode(verifier);
25
- const hash = sha256(data);
26
- return [verifier, base64URLEncode(hash)];
17
+ const verifier = randomHex(length);
18
+ return [verifier, base64URLEncode(sha256(new TextEncoder().encode(verifier)))];
27
19
  }
28
20
  async function isSessionValid(session) {
29
- try {
30
- const store = await get$1(`${apiHostname()}/me`, { headers: { Authorization: `Bearer ${session.access_token}` } });
31
- return store.status === 1;
32
- }
33
- catch (err) {
34
- return false;
35
- }
21
+ try {
22
+ return (await get$1(`${apiHostname()}/me`, { headers: { Authorization: `Bearer ${session.access_token}` } })).status === 1;
23
+ } catch (err) {
24
+ return false;
25
+ }
36
26
  }
37
27
  async function exchange(code, verifier) {
38
- const params = {
39
- code,
40
- client_id: oauthClientId(),
41
- grant_type: 'authorization_code',
42
- redirect_uri: `http://${LS_HOST}:${LS_PORT}/`,
43
- code_verifier: verifier,
44
- };
45
- const result = await post(`${apiHostname()}/oauth/token`, {
46
- body: new URLSearchParams(Object.entries(params)),
47
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
48
- });
49
- return result.access_token;
28
+ const params = {
29
+ code,
30
+ client_id: oauthClientId(),
31
+ grant_type: "authorization_code",
32
+ redirect_uri: `http://${LS_HOST}:${LS_PORT}/`,
33
+ code_verifier: verifier
34
+ };
35
+ return (await post(`${apiHostname()}/oauth/token`, {
36
+ body: new URLSearchParams(Object.entries(params)),
37
+ headers: { "Content-Type": "application/x-www-form-urlencoded" }
38
+ })).access_token;
50
39
  }
51
40
  async function authorize(command, state = randomHex(30)) {
52
- const AUTHORIZATION_URL = sellerAreaHostname();
53
- if (!await isPortAvailable(LS_PORT)) {
54
- command.output.warn(`Port ${LS_PORT} is unavailable, but it is required for authentication.`);
55
- const { confirmed } = await command.prompt({
56
- type: 'confirm',
57
- name: 'confirmed',
58
- initial: true,
59
- message: `Would you like to terminate ${await getPortProcessName(LS_PORT)}?`,
60
- });
61
- if (!confirmed) {
62
- throw new Error('Exiting..');
63
- }
64
- await killPortProcess(LS_PORT);
65
- }
66
- const [verifier, challenge] = await generatePkcePair(64);
67
- const params = {
68
- state,
69
- response_type: 'code',
70
- scope: '*',
71
- client_id: oauthClientId(),
72
- redirect_uri: `http://${LS_HOST}:${LS_PORT}/`,
73
- code_challenge: challenge,
74
- code_challenge_method: 'S256',
75
- };
76
- await command.output.anykey('Press any key to open the login page on your browser');
77
- const url = `http://${AUTHORIZATION_URL}/admin/oauth/authorize?${new URLSearchParams(params).toString()}`;
78
- open(url);
79
- const result = await listen(command, LS_HOST, LS_PORT, url);
80
- if (result.state !== state) {
81
- throw new Error('Authorization state mismatch..');
82
- }
83
- return { code: result.code, verifier };
41
+ const AUTHORIZATION_URL = sellerAreaHostname();
42
+ if (!await isPortAvailable(LS_PORT)) {
43
+ command.output.warn(`Port ${LS_PORT} is unavailable, but it is required for authentication.`);
44
+ const { confirmed } = await command.prompt({
45
+ type: "confirm",
46
+ name: "confirmed",
47
+ initial: true,
48
+ message: `Would you like to terminate ${await getPortProcessName(LS_PORT)}?`
49
+ });
50
+ if (!confirmed) throw new Error("Exiting..");
51
+ await killPortProcess(LS_PORT);
52
+ }
53
+ const [verifier, challenge] = await generatePkcePair(64);
54
+ const params = {
55
+ state,
56
+ response_type: "code",
57
+ scope: "*",
58
+ client_id: oauthClientId(),
59
+ redirect_uri: `http://${LS_HOST}:${LS_PORT}/`,
60
+ code_challenge: challenge,
61
+ code_challenge_method: "S256"
62
+ };
63
+ await command.output.anykey("Press any key to open the login page on your browser");
64
+ const url = `http://${AUTHORIZATION_URL}/admin/oauth/authorize?${new URLSearchParams(params).toString()}`;
65
+ open(url);
66
+ const result = await listen(command, LS_HOST, LS_PORT, url);
67
+ if (result.state !== state) throw new Error("Authorization state mismatch..");
68
+ return {
69
+ code: result.code,
70
+ verifier
71
+ };
84
72
  }
85
73
  async function get() {
86
- return manager({ projectName: 'youcan-cli' })
87
- .get('store_session') ?? null;
74
+ return manager({ projectName: "youcan-cli" }).get("store_session") ?? null;
88
75
  }
89
76
  async function authenticate(command) {
90
- const existingSession = manager({ projectName: 'youcan-cli' })
91
- .get('store_session');
92
- if (existingSession && await isSessionValid(existingSession)) {
93
- return existingSession;
94
- }
95
- const { code, verifier } = await authorize(command);
96
- const accessToken = await exchange(code, verifier);
97
- const store = await get$1(`${apiHostname()}/me`, { headers: { Authorization: `Bearer ${accessToken}` } });
98
- if (!store.is_dev) {
99
- throw new Error('The CLI can only be used with dev stores, you create one through YouCan Partners.');
100
- }
101
- const session = {
102
- slug: store.slug,
103
- id: store.id,
104
- access_token: accessToken,
105
- };
106
- manager({ projectName: 'youcan-cli' })
107
- .set('store_session', session);
108
- return session;
77
+ const existingSession = manager({ projectName: "youcan-cli" }).get("store_session");
78
+ if (existingSession && await isSessionValid(existingSession)) return existingSession;
79
+ const { code, verifier } = await authorize(command);
80
+ const accessToken = await exchange(code, verifier);
81
+ const store = await get$1(`${apiHostname()}/me`, { headers: { Authorization: `Bearer ${accessToken}` } });
82
+ if (!store.is_dev) throw new Error("The CLI can only be used with dev stores, you create one through YouCan Partners.");
83
+ const session = {
84
+ slug: store.slug,
85
+ id: store.id,
86
+ access_token: accessToken
87
+ };
88
+ manager({ projectName: "youcan-cli" }).set("store_session", session);
89
+ return session;
109
90
  }
110
-
111
- export { authenticate, get };
91
+ //#endregion
92
+ export { authenticate, get, session_exports };
@@ -1,121 +1,105 @@
1
- import process from 'node:process';
2
- import { execa } from 'execa';
3
- import findProcess from 'find-process';
4
- import getPort, { portNumbers } from 'get-port';
5
- import tpu from 'tcp-port-used';
6
-
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import process from "node:process";
3
+ import { execa } from "execa";
4
+ import findProcess from "find-process";
5
+ import getPort, { portNumbers } from "get-port";
6
+ import tpu from "tcp-port-used";
7
+ //#region lib/node/system.ts
8
+ var system_exports = /* @__PURE__ */ __exportAll({
9
+ exec: () => exec,
10
+ getPortOrNextOrRandom: () => getPortOrNextOrRandom,
11
+ getPortProcessName: () => getPortProcessName,
12
+ inferUserPackageManager: () => inferUserPackageManager,
13
+ isPortAvailable: () => isPortAvailable,
14
+ killPortProcess: () => killPortProcess,
15
+ open: () => open,
16
+ sleep: () => sleep
17
+ });
7
18
  function buildExec(command, args, options) {
8
- const env = options?.env ?? process.env;
9
- const commandProcess = execa(command, args, {
10
- env,
11
- cwd: options?.cwd,
12
- input: options?.input,
13
- stdio: options?.stdio,
14
- stdin: options?.stdin,
15
- signal: options?.signal,
16
- stdout: options?.stdout === 'inherit' ? 'inherit' : undefined,
17
- stderr: options?.stderr === 'inherit' ? 'inherit' : undefined,
18
- windowsHide: false,
19
- });
20
- return commandProcess;
19
+ return execa(command, args, {
20
+ env: options?.env ?? process.env,
21
+ cwd: options?.cwd,
22
+ input: options?.input,
23
+ stdio: options?.stdio,
24
+ stdin: options?.stdin,
25
+ signal: options?.signal,
26
+ stdout: options?.stdout === "inherit" ? "inherit" : void 0,
27
+ stderr: options?.stderr === "inherit" ? "inherit" : void 0,
28
+ windowsHide: false
29
+ });
21
30
  }
22
31
  async function exec(command, args, options) {
23
- const commandProcess = buildExec(command, args, options);
24
- if (options?.stderr && options.stderr !== 'inherit') {
25
- commandProcess.stderr?.pipe(options.stderr, { end: false });
26
- }
27
- if (options?.stdout && options.stdout !== 'inherit') {
28
- commandProcess.stdout?.pipe(options.stdout, { end: false });
29
- }
30
- let aborted = false;
31
- process.once('SIGINT', () => {
32
- if (commandProcess.pid) {
33
- try {
34
- commandProcess.kill('SIGTERM');
35
- }
36
- catch (err) {
37
- }
38
- }
39
- });
40
- process.once('SIGTERM', () => {
41
- if (commandProcess.pid) {
42
- try {
43
- commandProcess.kill('SIGTERM');
44
- }
45
- catch (err) {
46
- }
47
- }
48
- });
49
- options?.signal?.addEventListener('abort', () => {
50
- const pid = commandProcess.pid;
51
- if (pid) {
52
- aborted = true;
53
- try {
54
- const killProcess = () => {
55
- try {
56
- commandProcess.kill('SIGKILL');
57
- }
58
- catch (err) {
59
- }
60
- };
61
- commandProcess.kill('SIGTERM');
62
- setTimeout(killProcess, 500);
63
- }
64
- catch (err) {
65
- }
66
- }
67
- });
68
- try {
69
- await commandProcess;
70
- }
71
- catch (err) {
72
- if (aborted) {
73
- return;
74
- }
75
- if (options?.errorHandler) {
76
- await options?.errorHandler(err);
77
- return;
78
- }
79
- throw err;
80
- }
32
+ const commandProcess = buildExec(command, args, options);
33
+ if (options?.stderr && options.stderr !== "inherit") commandProcess.stderr?.pipe(options.stderr, { end: false });
34
+ if (options?.stdout && options.stdout !== "inherit") commandProcess.stdout?.pipe(options.stdout, { end: false });
35
+ let aborted = false;
36
+ process.once("SIGINT", () => {
37
+ if (commandProcess.pid) try {
38
+ commandProcess.kill("SIGTERM");
39
+ } catch (err) {}
40
+ });
41
+ process.once("SIGTERM", () => {
42
+ if (commandProcess.pid) try {
43
+ commandProcess.kill("SIGTERM");
44
+ } catch (err) {}
45
+ });
46
+ options?.signal?.addEventListener("abort", () => {
47
+ if (commandProcess.pid) {
48
+ aborted = true;
49
+ try {
50
+ const killProcess = () => {
51
+ try {
52
+ commandProcess.kill("SIGKILL");
53
+ } catch (err) {}
54
+ };
55
+ commandProcess.kill("SIGTERM");
56
+ setTimeout(killProcess, 500);
57
+ } catch (err) {}
58
+ }
59
+ });
60
+ try {
61
+ await commandProcess;
62
+ } catch (err) {
63
+ if (aborted) return;
64
+ if (options?.errorHandler) {
65
+ await options?.errorHandler(err);
66
+ return;
67
+ }
68
+ throw err;
69
+ }
81
70
  }
82
71
  async function isPortAvailable(port) {
83
- return !await tpu.check(port);
72
+ return !await tpu.check(port);
84
73
  }
85
74
  async function getPortOrNextOrRandom(port) {
86
- return await getPort({ port: portNumbers(port, port + 1000) });
75
+ return await getPort({ port: portNumbers(port, port + 1e3) });
87
76
  }
88
77
  async function getPortProcessName(port) {
89
- const info = await findProcess('port', port);
90
- return (info && info.length > 0) ? `(${info[0]?.name})` : '';
78
+ const info = await findProcess("port", port);
79
+ return info && info.length > 0 ? `(${info[0]?.name})` : "";
91
80
  }
92
81
  async function killPortProcess(port) {
93
- const { killPortProcess: kill } = await import('kill-port-process');
94
- await kill(port);
82
+ const { killPortProcess: kill } = await import("kill-port-process");
83
+ await kill(port);
95
84
  }
96
85
  async function open(url) {
97
- const _open = await import('open');
98
- await _open.default(url);
86
+ await (await import("open")).default(url);
99
87
  }
100
88
  async function sleep(seconds) {
101
- return new Promise((resolve) => {
102
- setTimeout(resolve, 1000 * seconds);
103
- });
89
+ return new Promise((resolve) => {
90
+ setTimeout(resolve, 1e3 * seconds);
91
+ });
104
92
  }
105
93
  function inferUserPackageManager() {
106
- const defaultPackageManager = 'npm';
107
- const packageManagersMap = {
108
- '^npm/.*': 'npm',
109
- '^pnpm/.*': 'pnpm',
110
- '^yarn/.*': 'yarn',
111
- };
112
- const packageManagerUserAgent = process.env.npm_config_user_agent;
113
- for (const key in packageManagersMap) {
114
- if (new RegExp(key).test(packageManagerUserAgent)) {
115
- return packageManagersMap[key];
116
- }
117
- }
118
- return defaultPackageManager;
94
+ const defaultPackageManager = "npm";
95
+ const packageManagersMap = {
96
+ "^npm/.*": "npm",
97
+ "^pnpm/.*": "pnpm",
98
+ "^yarn/.*": "yarn"
99
+ };
100
+ const packageManagerUserAgent = process.env.npm_config_user_agent;
101
+ for (const key in packageManagersMap) if (new RegExp(key).test(packageManagerUserAgent)) return packageManagersMap[key];
102
+ return defaultPackageManager;
119
103
  }
120
-
121
- export { exec, getPortOrNextOrRandom, getPortProcessName, inferUserPackageManager, isPortAvailable, killPortProcess, open, sleep };
104
+ //#endregion
105
+ export { exec, getPortOrNextOrRandom, getPortProcessName, inferUserPackageManager, isPortAvailable, killPortProcess, open, sleep, system_exports };
@@ -1,39 +1,34 @@
1
- import process, { exit } from 'node:process';
2
- import { Loader } from '../internal/node/ui.js';
3
-
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { Loader } from "../internal/node/ui.js";
3
+ import process, { exit } from "node:process";
4
+ //#region lib/node/tasks.ts
5
+ var tasks_exports = /* @__PURE__ */ __exportAll({ run: () => run });
4
6
  async function runTask(task, ctx) {
5
- if (task.skip?.(ctx)) {
6
- return;
7
- }
8
- return await task.task(ctx, task);
7
+ if (task.skip?.(ctx)) return;
8
+ return await task.task(ctx, task);
9
9
  }
10
10
  async function run(ctx, tasks) {
11
- for await (const task of tasks) {
12
- const runner = async () => {
13
- const subtasks = await runTask(task, ctx);
14
- if (Array.isArray(subtasks) && subtasks.length > 0 && subtasks.every(t => 'task' in t)) {
15
- for await (const subtask of subtasks) {
16
- await runTask(subtask, ctx);
17
- }
18
- }
19
- };
20
- if (task.loadable === false) {
21
- process.stdout.write(`${task.title}\n`);
22
- await runner();
23
- return ctx;
24
- }
25
- await Loader.exec(task.title, async (loader) => {
26
- try {
27
- await runner();
28
- loader.stop();
29
- }
30
- catch (err) {
31
- loader.error(String(err));
32
- exit(1);
33
- }
34
- });
35
- }
36
- return ctx;
11
+ for await (const task of tasks) {
12
+ const runner = async () => {
13
+ const subtasks = await runTask(task, ctx);
14
+ if (Array.isArray(subtasks) && subtasks.length > 0 && subtasks.every((t) => "task" in t)) for await (const subtask of subtasks) await runTask(subtask, ctx);
15
+ };
16
+ if (task.loadable === false) {
17
+ process.stdout.write(`${task.title}\n`);
18
+ await runner();
19
+ return ctx;
20
+ }
21
+ await Loader.exec(task.title, async (loader) => {
22
+ try {
23
+ await runner();
24
+ loader.stop();
25
+ } catch (err) {
26
+ loader.error(String(err));
27
+ exit(1);
28
+ }
29
+ });
30
+ }
31
+ return ctx;
37
32
  }
38
-
39
- export { run };
33
+ //#endregion
34
+ export { run, tasks_exports };