@youcan/cli-kit 2.1.4 → 2.3.0
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/common/string.d.ts +2 -2
- package/dist/common/string.js +11 -11
- package/dist/index.d.ts +19 -17
- package/dist/index.js +4 -0
- package/dist/internal/node/constants.d.ts +5 -5
- package/dist/internal/node/constants.js +10 -10
- package/dist/internal/node/ui.d.ts +11 -11
- package/dist/internal/node/ui.js +41 -40
- package/dist/node/callback.d.ts +5 -5
- package/dist/node/callback.js +53 -53
- package/dist/node/cli.d.ts +25 -21
- package/dist/node/cli.js +97 -62
- package/dist/node/config.d.ts +20 -20
- package/dist/node/config.js +20 -22
- package/dist/node/context/helpers.d.ts +1 -1
- package/dist/node/context/helpers.js +5 -5
- package/dist/node/context/local.d.ts +2 -2
- package/dist/node/context/local.js +2 -2
- package/dist/node/crypto.d.ts +12 -9
- package/dist/node/crypto.js +23 -23
- package/dist/node/env.d.ts +5 -6
- package/dist/node/env.js +36 -47
- package/dist/node/filesystem.d.ts +34 -29
- package/dist/node/filesystem.js +112 -82
- package/dist/node/form.d.ts +9 -9
- package/dist/node/form.js +39 -39
- package/dist/node/git.d.ts +10 -10
- package/dist/node/git.js +46 -46
- package/dist/node/github.d.ts +6 -6
- package/dist/node/github.js +8 -8
- package/dist/node/http.d.ts +4 -4
- package/dist/node/http.js +37 -34
- package/dist/node/path.d.ts +5 -5
- package/dist/node/path.js +14 -14
- package/dist/node/session.d.ts +8 -8
- package/dist/node/session.js +92 -78
- package/dist/node/system.d.ts +26 -21
- package/dist/node/system.js +87 -60
- package/dist/node/tasks.d.ts +8 -7
- package/dist/node/tasks.js +33 -25
- package/dist/node/worker.d.ts +16 -19
- package/dist/node/worker.js +43 -30
- package/dist/services/cloudflared.d.ts +12 -0
- package/dist/services/cloudflared.js +206 -0
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/dist/ui/components/DevOutput.d.ts +27 -0
- package/dist/ui/components/DevOutput.js +60 -0
- package/dist/ui/components/Error.d.ts +6 -0
- package/dist/ui/components/Error.js +18 -0
- package/dist/ui/components/HotKeys.d.ts +12 -0
- package/dist/ui/components/HotKeys.js +25 -0
- package/dist/ui/components/utils/symbols.d.ts +3 -0
- package/dist/ui/components/utils/symbols.js +7 -0
- package/dist/ui/index.d.ts +3 -0
- package/dist/ui/index.js +3 -0
- package/package.json +7 -2
package/dist/node/git.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export declare function binaryExists(): Promise<boolean>;
|
|
2
|
-
export interface CloneOptions {
|
|
3
|
-
url: string;
|
|
4
|
-
destination: string;
|
|
5
|
-
progressUpdater?: (statusString: string) => void;
|
|
6
|
-
shallow?: boolean;
|
|
7
|
-
latestTag?: boolean;
|
|
8
|
-
}
|
|
9
|
-
export declare function assertGitExists(): Promise<void>;
|
|
10
|
-
export declare function clone(cloneOptions: CloneOptions): Promise<void>;
|
|
1
|
+
export declare function binaryExists(): Promise<boolean>;
|
|
2
|
+
export interface CloneOptions {
|
|
3
|
+
url: string;
|
|
4
|
+
destination: string;
|
|
5
|
+
progressUpdater?: (statusString: string) => void;
|
|
6
|
+
shallow?: boolean;
|
|
7
|
+
latestTag?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function assertGitExists(): Promise<void>;
|
|
10
|
+
export declare function clone(cloneOptions: CloneOptions): Promise<void>;
|
package/dist/node/git.js
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
import git from 'simple-git';
|
|
2
2
|
import { exec } from './system.js';
|
|
3
3
|
|
|
4
|
-
async function binaryExists() {
|
|
5
|
-
try {
|
|
6
|
-
await exec('git', ['--version']);
|
|
7
|
-
return true;
|
|
8
|
-
}
|
|
9
|
-
catch {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
async function assertGitExists() {
|
|
14
|
-
if (!(await binaryExists())) {
|
|
15
|
-
throw new Error('Git is required for the setup to continue.');
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
async function clone(cloneOptions) {
|
|
19
|
-
await assertGitExists();
|
|
20
|
-
const { url, destination, shallow, latestTag } = cloneOptions;
|
|
21
|
-
const [repository, branch] = url.split('#');
|
|
22
|
-
const options = { '--recurse-submodules': null };
|
|
23
|
-
// ignore latest tag if branch is provided
|
|
24
|
-
if (branch) {
|
|
25
|
-
options['--branch'] = branch;
|
|
26
|
-
}
|
|
27
|
-
if (shallow && latestTag) {
|
|
28
|
-
throw new Error('Cannot get a shallow clone of the latest branch.');
|
|
29
|
-
}
|
|
30
|
-
if (shallow) {
|
|
31
|
-
options['--depth'] = 1;
|
|
32
|
-
}
|
|
33
|
-
const simpleGitOptions = {
|
|
34
|
-
config: ['core.askpass=true'],
|
|
35
|
-
};
|
|
36
|
-
await git(simpleGitOptions)
|
|
37
|
-
.clone(repository, destination, options);
|
|
38
|
-
if (latestTag) {
|
|
39
|
-
const localRepo = git(destination);
|
|
40
|
-
const latestTag = await getLocalLatestTag(localRepo, url);
|
|
41
|
-
await localRepo.checkout(latestTag);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
async function getLocalLatestTag(repository, url) {
|
|
45
|
-
const latest = (await repository.tags()).latest;
|
|
46
|
-
if (!latest) {
|
|
47
|
-
throw new Error(`Couldn't infer the latest tag from ${url}`);
|
|
48
|
-
}
|
|
49
|
-
return latest;
|
|
4
|
+
async function binaryExists() {
|
|
5
|
+
try {
|
|
6
|
+
await exec('git', ['--version']);
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
async function assertGitExists() {
|
|
14
|
+
if (!(await binaryExists())) {
|
|
15
|
+
throw new Error('Git is required for the setup to continue.');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function clone(cloneOptions) {
|
|
19
|
+
await assertGitExists();
|
|
20
|
+
const { url, destination, shallow, latestTag } = cloneOptions;
|
|
21
|
+
const [repository, branch] = url.split('#');
|
|
22
|
+
const options = { '--recurse-submodules': null };
|
|
23
|
+
// ignore latest tag if branch is provided
|
|
24
|
+
if (branch) {
|
|
25
|
+
options['--branch'] = branch;
|
|
26
|
+
}
|
|
27
|
+
if (shallow && latestTag) {
|
|
28
|
+
throw new Error('Cannot get a shallow clone of the latest branch.');
|
|
29
|
+
}
|
|
30
|
+
if (shallow) {
|
|
31
|
+
options['--depth'] = 1;
|
|
32
|
+
}
|
|
33
|
+
const simpleGitOptions = {
|
|
34
|
+
config: ['core.askpass=true'],
|
|
35
|
+
};
|
|
36
|
+
await git(simpleGitOptions)
|
|
37
|
+
.clone(repository, destination, options);
|
|
38
|
+
if (latestTag) {
|
|
39
|
+
const localRepo = git(destination);
|
|
40
|
+
const latestTag = await getLocalLatestTag(localRepo, url);
|
|
41
|
+
await localRepo.checkout(latestTag);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async function getLocalLatestTag(repository, url) {
|
|
45
|
+
const latest = (await repository.tags()).latest;
|
|
46
|
+
if (!latest) {
|
|
47
|
+
throw new Error(`Couldn't infer the latest tag from ${url}`);
|
|
48
|
+
}
|
|
49
|
+
return latest;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
export { assertGitExists, binaryExists, clone };
|
package/dist/node/github.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export interface RepositoryReference {
|
|
2
|
-
baseUrl: string;
|
|
3
|
-
branch?: string;
|
|
4
|
-
path?: string;
|
|
5
|
-
}
|
|
6
|
-
export declare function parseRepositoryReference(reference: string): RepositoryReference;
|
|
1
|
+
export interface RepositoryReference {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
branch?: string;
|
|
4
|
+
path?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function parseRepositoryReference(reference: string): RepositoryReference;
|
package/dist/node/github.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
function parseRepositoryReference(reference) {
|
|
2
|
-
const url = new URL(reference);
|
|
3
|
-
const [, user, repo, ...repoPath] = url.pathname.split('/');
|
|
4
|
-
return {
|
|
5
|
-
baseUrl: `${url.origin}/${user}/${repo}`,
|
|
6
|
-
branch: url.hash ? url.hash.slice(1) : undefined,
|
|
7
|
-
path: repoPath.length > 0 ? repoPath.join('/') : undefined,
|
|
8
|
-
};
|
|
1
|
+
function parseRepositoryReference(reference) {
|
|
2
|
+
const url = new URL(reference);
|
|
3
|
+
const [, user, repo, ...repoPath] = url.pathname.split('/');
|
|
4
|
+
return {
|
|
5
|
+
baseUrl: `${url.origin}/${user}/${repo}`,
|
|
6
|
+
branch: url.hash ? url.hash.slice(1) : undefined,
|
|
7
|
+
path: repoPath.length > 0 ? repoPath.join('/') : undefined,
|
|
8
|
+
};
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export { parseRepositoryReference };
|
package/dist/node/http.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RequestInit } from 'node-fetch';
|
|
2
|
-
export declare function scheme(): 'http' | 'https';
|
|
3
|
-
export declare function get<T>(endpoint: string, options?: RequestInit): Promise<T>;
|
|
4
|
-
export declare function post<T>(endpoint: string, options?: RequestInit): Promise<T>;
|
|
1
|
+
import type { RequestInit } from 'node-fetch';
|
|
2
|
+
export declare function scheme(): 'http' | 'https';
|
|
3
|
+
export declare function get<T>(endpoint: string, options?: RequestInit): Promise<T>;
|
|
4
|
+
export declare function post<T>(endpoint: string, options?: RequestInit): Promise<T>;
|
package/dist/node/http.js
CHANGED
|
@@ -2,9 +2,9 @@ import fetch from 'node-fetch';
|
|
|
2
2
|
import { is, mergeDeepLeft } from 'ramda';
|
|
3
3
|
import './cli.js';
|
|
4
4
|
import 'simple-git';
|
|
5
|
-
import 'execa';
|
|
6
|
-
import 'tcp-port-used';
|
|
7
5
|
import 'find-process';
|
|
6
|
+
import 'tcp-port-used';
|
|
7
|
+
import 'execa';
|
|
8
8
|
import { get as get$1 } from './env.js';
|
|
9
9
|
import 'formdata-node';
|
|
10
10
|
import 'formdata-node/file-from-path';
|
|
@@ -13,40 +13,43 @@ import 'conf';
|
|
|
13
13
|
import 'dayjs';
|
|
14
14
|
import { get as get$2 } from './session.js';
|
|
15
15
|
import './filesystem.js';
|
|
16
|
+
import '../ui/components/DevOutput.js';
|
|
17
|
+
import 'react';
|
|
18
|
+
import 'ink';
|
|
16
19
|
import { isJson } from '../common/string.js';
|
|
17
20
|
|
|
18
|
-
function scheme() {
|
|
19
|
-
return get$1('HOST_ENV') === 'dev' ? 'http' : 'https';
|
|
20
|
-
}
|
|
21
|
-
async function agent() {
|
|
22
|
-
const { Agent } = await import(scheme());
|
|
23
|
-
return new Agent({ keepAlive: true, keepAliveMsecs: 5 * 60 * 1000 });
|
|
24
|
-
}
|
|
25
|
-
async function defaults() {
|
|
26
|
-
const session$1 = await get$2();
|
|
27
|
-
return {
|
|
28
|
-
agent: await agent(),
|
|
29
|
-
headers: {
|
|
30
|
-
Accept: 'application/json',
|
|
31
|
-
Authorization: session$1 ? `Bearer ${session$1.access_token}` : undefined,
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
async function request(endpoint, options = {}) {
|
|
36
|
-
if (is(String)(options.body) && isJson(options.body)) {
|
|
37
|
-
options = mergeDeepLeft(options, { headers: { 'Content-Type': 'application/json' } });
|
|
38
|
-
}
|
|
39
|
-
const response = await fetch(endpoint, mergeDeepLeft(options, await defaults()));
|
|
40
|
-
if (!response.ok) {
|
|
41
|
-
throw new Error(await response.text(), { cause: response });
|
|
42
|
-
}
|
|
43
|
-
return response.json();
|
|
44
|
-
}
|
|
45
|
-
async function get(endpoint, options = {}) {
|
|
46
|
-
return request(`${scheme()}://${endpoint}`, { ...options, method: 'GET' });
|
|
47
|
-
}
|
|
48
|
-
async function post(endpoint, options = {}) {
|
|
49
|
-
return request(`${scheme()}://${endpoint}`, { ...options, method: 'POST' });
|
|
21
|
+
function scheme() {
|
|
22
|
+
return get$1('HOST_ENV') === 'dev' ? 'http' : 'https';
|
|
23
|
+
}
|
|
24
|
+
async function agent() {
|
|
25
|
+
const { Agent } = await import(scheme());
|
|
26
|
+
return new Agent({ keepAlive: true, keepAliveMsecs: 5 * 60 * 1000 });
|
|
27
|
+
}
|
|
28
|
+
async function defaults() {
|
|
29
|
+
const session$1 = await get$2();
|
|
30
|
+
return {
|
|
31
|
+
agent: await agent(),
|
|
32
|
+
headers: {
|
|
33
|
+
Accept: 'application/json',
|
|
34
|
+
Authorization: session$1 ? `Bearer ${session$1.access_token}` : undefined,
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
async function request(endpoint, options = {}) {
|
|
39
|
+
if (is(String)(options.body) && isJson(options.body)) {
|
|
40
|
+
options = mergeDeepLeft(options, { headers: { 'Content-Type': 'application/json' } });
|
|
41
|
+
}
|
|
42
|
+
const response = await fetch(endpoint, mergeDeepLeft(options, await defaults()));
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
throw new Error(await response.text(), { cause: response });
|
|
45
|
+
}
|
|
46
|
+
return response.json();
|
|
47
|
+
}
|
|
48
|
+
async function get(endpoint, options = {}) {
|
|
49
|
+
return request(`${scheme()}://${endpoint}`, { ...options, method: 'GET' });
|
|
50
|
+
}
|
|
51
|
+
async function post(endpoint, options = {}) {
|
|
52
|
+
return request(`${scheme()}://${endpoint}`, { ...options, method: 'POST' });
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
export { get, post, scheme };
|
package/dist/node/path.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export declare function resolve(...paths: string[]): string;
|
|
2
|
-
export declare function cwd(): string;
|
|
3
|
-
export declare function join(...paths: string[]): string;
|
|
4
|
-
export declare function dirname(filepath: string): string;
|
|
5
|
-
export declare function basename(filepath: string): string;
|
|
1
|
+
export declare function resolve(...paths: string[]): string;
|
|
2
|
+
export declare function cwd(): string;
|
|
3
|
+
export declare function join(...paths: string[]): string;
|
|
4
|
+
export declare function dirname(filepath: string): string;
|
|
5
|
+
export declare function basename(filepath: string): string;
|
package/dist/node/path.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
|
|
3
|
-
function resolve(...paths) {
|
|
4
|
-
return path.resolve(...paths);
|
|
5
|
-
}
|
|
6
|
-
function cwd() {
|
|
7
|
-
return path.normalize(process.env.INIT_CWD ? process.env.INIT_CWD : process.cwd());
|
|
8
|
-
}
|
|
9
|
-
function join(...paths) {
|
|
10
|
-
return path.join(...paths);
|
|
11
|
-
}
|
|
12
|
-
function dirname(filepath) {
|
|
13
|
-
return path.dirname(filepath);
|
|
14
|
-
}
|
|
15
|
-
function basename(filepath) {
|
|
16
|
-
return path.basename(filepath);
|
|
3
|
+
function resolve(...paths) {
|
|
4
|
+
return path.resolve(...paths);
|
|
5
|
+
}
|
|
6
|
+
function cwd() {
|
|
7
|
+
return path.normalize(process.env.INIT_CWD ? process.env.INIT_CWD : process.cwd());
|
|
8
|
+
}
|
|
9
|
+
function join(...paths) {
|
|
10
|
+
return path.join(...paths);
|
|
11
|
+
}
|
|
12
|
+
function dirname(filepath) {
|
|
13
|
+
return path.dirname(filepath);
|
|
14
|
+
}
|
|
15
|
+
function basename(filepath) {
|
|
16
|
+
return path.basename(filepath);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export { basename, cwd, dirname, join, resolve };
|
package/dist/node/session.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type Cli } from '..';
|
|
2
|
-
export interface StoreSession {
|
|
3
|
-
id: string;
|
|
4
|
-
slug: string;
|
|
5
|
-
access_token: string;
|
|
6
|
-
}
|
|
7
|
-
export declare function get(): Promise<StoreSession | null>;
|
|
8
|
-
export declare function authenticate(command: Cli.Command): Promise<StoreSession>;
|
|
1
|
+
import { type Cli } from '..';
|
|
2
|
+
export interface StoreSession {
|
|
3
|
+
id: string;
|
|
4
|
+
slug: string;
|
|
5
|
+
access_token: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function get(): Promise<StoreSession | null>;
|
|
8
|
+
export declare function authenticate(command: Cli.Command): Promise<StoreSession>;
|
package/dist/node/session.js
CHANGED
|
@@ -1,94 +1,108 @@
|
|
|
1
1
|
import './cli.js';
|
|
2
2
|
import 'simple-git';
|
|
3
3
|
import { isPortAvailable, getPortProcessName, killPortProcess, open } from './system.js';
|
|
4
|
-
import { apiHostname, oauthClientId,
|
|
4
|
+
import { apiHostname, oauthClientId, sellerAreaHostname } from './env.js';
|
|
5
5
|
import { get as get$1, post } from './http.js';
|
|
6
6
|
import 'formdata-node';
|
|
7
7
|
import 'formdata-node/file-from-path';
|
|
8
8
|
import 'kleur';
|
|
9
9
|
import { manager } from './config.js';
|
|
10
|
-
import { randomHex } from './crypto.js';
|
|
10
|
+
import { randomHex, sha256, base64URLEncode } from './crypto.js';
|
|
11
11
|
import 'dayjs';
|
|
12
12
|
import { listen } from './callback.js';
|
|
13
13
|
import './filesystem.js';
|
|
14
|
+
import '../ui/components/DevOutput.js';
|
|
15
|
+
import 'react';
|
|
16
|
+
import 'ink';
|
|
14
17
|
import 'change-case';
|
|
15
18
|
|
|
16
|
-
const LS_PORT = 3000;
|
|
17
|
-
const LS_HOST = 'localhost';
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
19
|
+
const LS_PORT = 3000;
|
|
20
|
+
const LS_HOST = 'localhost';
|
|
21
|
+
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)];
|
|
27
|
+
}
|
|
28
|
+
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
|
+
}
|
|
36
|
+
}
|
|
37
|
+
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;
|
|
50
|
+
}
|
|
51
|
+
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 };
|
|
84
|
+
}
|
|
85
|
+
async function get() {
|
|
86
|
+
return manager({ projectName: 'youcan-cli' })
|
|
87
|
+
.get('store_session') ?? null;
|
|
88
|
+
}
|
|
89
|
+
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
|
+
const session = {
|
|
99
|
+
slug: store.slug,
|
|
100
|
+
id: store.id,
|
|
101
|
+
access_token: accessToken,
|
|
102
|
+
};
|
|
103
|
+
manager({ projectName: 'youcan-cli' })
|
|
104
|
+
.set('store_session', session);
|
|
105
|
+
return session;
|
|
92
106
|
}
|
|
93
107
|
|
|
94
108
|
export { authenticate, get };
|
package/dist/node/system.d.ts
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import type { Readable, Writable } from 'stream';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export declare function
|
|
19
|
-
export declare function
|
|
20
|
-
export declare function
|
|
21
|
-
export declare function
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import type { Readable, Writable } from 'stream';
|
|
4
|
+
import type { ExecaError } from 'execa';
|
|
5
|
+
export interface ExecOptions {
|
|
6
|
+
cwd?: string;
|
|
7
|
+
env?: {
|
|
8
|
+
[key: string]: string | undefined;
|
|
9
|
+
};
|
|
10
|
+
stdin?: Readable | 'inherit';
|
|
11
|
+
stdout?: Writable | 'inherit';
|
|
12
|
+
stderr?: Writable | 'inherit';
|
|
13
|
+
stdio?: 'inherit';
|
|
14
|
+
input?: string;
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
errorHandler?: (error: unknown | ExecaError) => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export declare function exec(command: string, args: string[], options?: ExecOptions): Promise<void>;
|
|
19
|
+
export declare function isPortAvailable(port: number): Promise<boolean>;
|
|
20
|
+
export declare function getNextAvailablePort(port: number): Promise<number>;
|
|
21
|
+
export declare function getPortProcessName(port: number): Promise<string>;
|
|
22
|
+
export declare function killPortProcess(port: number): Promise<void>;
|
|
23
|
+
export declare function open(url: string): Promise<void>;
|
|
24
|
+
export declare function sleep(seconds: number): Promise<void>;
|
|
25
|
+
export type PackageManagerType = 'pnpm' | 'npm' | 'yarn';
|
|
26
|
+
export declare function inferUserPackageManager(): PackageManagerType;
|