@coze/cli 0.0.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/LICENSE.md +21 -0
- package/README.md +40 -0
- package/bin.mjs +2 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +36 -0
- package/dist/cli.mjs +957 -0
- package/dist/commands/base.d.ts +15 -0
- package/dist/commands/base.js +28 -0
- package/dist/commands/login.d.ts +6 -0
- package/dist/commands/login.js +18 -0
- package/dist/commands/logout.d.ts +8 -0
- package/dist/commands/logout.js +20 -0
- package/dist/helpers/constants.d.ts +10 -0
- package/dist/helpers/constants.js +44 -0
- package/dist/helpers/logger.d.ts +7 -0
- package/dist/helpers/logger.js +21 -0
- package/dist/helpers/package.d.ts +8 -0
- package/dist/helpers/package.js +33 -0
- package/dist/helpers/request.d.ts +3 -0
- package/dist/helpers/request.js +24 -0
- package/dist/helpers/types.d.ts +16 -0
- package/dist/helpers/types.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.mjs +263 -0
- package/dist/npm.d.ts +28 -0
- package/dist/npm.js +109 -0
- package/dist/oauth.d.ts +9 -0
- package/dist/oauth.js +61 -0
- package/package.json +64 -0
package/dist/npm.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import npmDefinitions from '@npmcli/config/lib/definitions/index.js';
|
|
2
|
+
import Config from '@npmcli/config';
|
|
3
|
+
import { logger } from './helpers/logger';
|
|
4
|
+
import { DEFAULT_REGION, isLocalDev, REGISTRY_HOST, } from './helpers/constants';
|
|
5
|
+
const { shorthands, definitions, flatten } = npmDefinitions;
|
|
6
|
+
const CONFIG_TYPE = 'user';
|
|
7
|
+
const REFRESH_TOKEN_KEY = 'refresh_token';
|
|
8
|
+
const DEFAULT_PACKAGE_SCOPE = 'coze-kit';
|
|
9
|
+
const DEFAULT_BUSINESS_SOURCE = 'coze';
|
|
10
|
+
export class NpmConfig {
|
|
11
|
+
constructor({ projectDir, scope, region, source } = {}) {
|
|
12
|
+
this.config = {
|
|
13
|
+
scope: scope !== null && scope !== void 0 ? scope : DEFAULT_PACKAGE_SCOPE,
|
|
14
|
+
authToken: '',
|
|
15
|
+
refreshToken: '',
|
|
16
|
+
registryUrl: '',
|
|
17
|
+
region,
|
|
18
|
+
source: source !== null && source !== void 0 ? source : DEFAULT_BUSINESS_SOURCE,
|
|
19
|
+
};
|
|
20
|
+
const dir = projectDir !== null && projectDir !== void 0 ? projectDir : process.cwd();
|
|
21
|
+
this.proxy = new Config({
|
|
22
|
+
npmPath: dir,
|
|
23
|
+
definitions,
|
|
24
|
+
shorthands,
|
|
25
|
+
flatten,
|
|
26
|
+
cwd: dir,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
get registryScope() {
|
|
30
|
+
return `@${this.config.scope}:registry`;
|
|
31
|
+
}
|
|
32
|
+
get registryUrl() {
|
|
33
|
+
const registryHost = REGISTRY_HOST[this.region];
|
|
34
|
+
return isLocalDev(this.region)
|
|
35
|
+
? `http://${registryHost}/`
|
|
36
|
+
: `https://${registryHost}/`;
|
|
37
|
+
}
|
|
38
|
+
get registryAuthUrl() {
|
|
39
|
+
const registryHost = REGISTRY_HOST[this.region];
|
|
40
|
+
return `//${registryHost}/:_authToken`;
|
|
41
|
+
}
|
|
42
|
+
get refreshKey() {
|
|
43
|
+
return `${this.config.source}_${REFRESH_TOKEN_KEY}`;
|
|
44
|
+
}
|
|
45
|
+
get region() {
|
|
46
|
+
// has specified region
|
|
47
|
+
if (this.config.region) {
|
|
48
|
+
return this.config.region;
|
|
49
|
+
}
|
|
50
|
+
const registry = this.proxy.get(this.registryScope);
|
|
51
|
+
if (!registry) {
|
|
52
|
+
return DEFAULT_REGION;
|
|
53
|
+
}
|
|
54
|
+
const registryHost = new URL(registry).host;
|
|
55
|
+
for (const [region, host] of Object.entries(REGISTRY_HOST)) {
|
|
56
|
+
if (registryHost === host) {
|
|
57
|
+
return region;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return DEFAULT_REGION;
|
|
61
|
+
}
|
|
62
|
+
async loadConfig() {
|
|
63
|
+
var _a;
|
|
64
|
+
if (this.proxy.loaded) {
|
|
65
|
+
return this.config;
|
|
66
|
+
}
|
|
67
|
+
await this.proxy.load();
|
|
68
|
+
const token = (_a = this.proxy.get(this.registryAuthUrl, CONFIG_TYPE)) !== null && _a !== void 0 ? _a : '';
|
|
69
|
+
Object.assign(this.config, {
|
|
70
|
+
region: this.region,
|
|
71
|
+
registryUrl: this.proxy.get(this.registryScope) || this.registryUrl,
|
|
72
|
+
authToken: token,
|
|
73
|
+
refreshToken: this.proxy.get(this.refreshKey, CONFIG_TYPE) || '',
|
|
74
|
+
});
|
|
75
|
+
return this.config;
|
|
76
|
+
}
|
|
77
|
+
async storeToken(tokenSet) {
|
|
78
|
+
// clear other region info
|
|
79
|
+
for (const [region] of Object.entries(REGISTRY_HOST)) {
|
|
80
|
+
if (region !== this.region) {
|
|
81
|
+
this.proxy.delete(this.registryScope, CONFIG_TYPE);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
this.proxy.set(this.registryScope, this.registryUrl, CONFIG_TYPE);
|
|
85
|
+
this.proxy.set(this.registryAuthUrl, `${this.config.source}:${tokenSet.access_token || ''}`, CONFIG_TYPE);
|
|
86
|
+
this.proxy.set(this.refreshKey, tokenSet.refresh_token || '', CONFIG_TYPE);
|
|
87
|
+
await this.proxy.save(CONFIG_TYPE);
|
|
88
|
+
Object.assign(this.config, {
|
|
89
|
+
authToken: tokenSet.access_token,
|
|
90
|
+
refreshToken: tokenSet.refresh_token,
|
|
91
|
+
});
|
|
92
|
+
logger.success(`token saved for registry: ${this.registryUrl}`);
|
|
93
|
+
}
|
|
94
|
+
async clearToken() {
|
|
95
|
+
this.proxy.delete(this.registryScope, CONFIG_TYPE);
|
|
96
|
+
this.proxy.delete(this.registryAuthUrl, CONFIG_TYPE);
|
|
97
|
+
this.proxy.delete(this.refreshKey, CONFIG_TYPE);
|
|
98
|
+
await this.proxy.save(CONFIG_TYPE);
|
|
99
|
+
Object.assign(this.config, {
|
|
100
|
+
authToken: '',
|
|
101
|
+
refreshToken: '',
|
|
102
|
+
});
|
|
103
|
+
logger.success(`token cleared for registry: ${this.registryUrl}`);
|
|
104
|
+
}
|
|
105
|
+
getProxy() {
|
|
106
|
+
return this.proxy;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=npm.js.map
|
package/dist/oauth.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type INpmConfig, type IAuthClient } from './helpers/types';
|
|
2
|
+
import { type Region } from './helpers/constants';
|
|
3
|
+
export declare class OauthClient implements IAuthClient {
|
|
4
|
+
private client;
|
|
5
|
+
constructor(region?: Region);
|
|
6
|
+
activate(npmConfig: INpmConfig): Promise<void>;
|
|
7
|
+
deactivate(npmConfig: INpmConfig): Promise<void>;
|
|
8
|
+
private auth;
|
|
9
|
+
}
|
package/dist/oauth.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/* eslint-disable security/detect-object-injection -- ignore */
|
|
2
|
+
import { Issuer } from 'openid-client';
|
|
3
|
+
import { logger } from './helpers/logger';
|
|
4
|
+
import { CLIENT_ID_MAP, DEFAULT_REGION, ISSUER_MAP, } from './helpers/constants';
|
|
5
|
+
export class OauthClient {
|
|
6
|
+
constructor(region = DEFAULT_REGION) {
|
|
7
|
+
const cozeHubIssuer = new Issuer({
|
|
8
|
+
issuer: ISSUER_MAP[region],
|
|
9
|
+
device_authorization_endpoint: `${ISSUER_MAP[region]}/api/permission/oauth2/device/code`,
|
|
10
|
+
token_endpoint: `${ISSUER_MAP[region]}/api/permission/oauth2/token`,
|
|
11
|
+
revocation_endpoint: `${ISSUER_MAP[region]}/api/permission/oauth2/revoke`,
|
|
12
|
+
scope: 'createMessage',
|
|
13
|
+
});
|
|
14
|
+
this.client = new cozeHubIssuer.Client({
|
|
15
|
+
client_id: CLIENT_ID_MAP[region],
|
|
16
|
+
token_endpoint_auth_method: 'none',
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async activate(npmConfig) {
|
|
20
|
+
const { refreshToken } = await npmConfig.loadConfig();
|
|
21
|
+
if (refreshToken) {
|
|
22
|
+
try {
|
|
23
|
+
const tokenSet = await this.client.refresh(refreshToken);
|
|
24
|
+
await npmConfig.storeToken(tokenSet);
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
logger.error('refresh failed: ', e);
|
|
28
|
+
await this.auth(npmConfig);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
await this.auth(npmConfig);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async deactivate(npmConfig) {
|
|
36
|
+
const { refreshToken } = await npmConfig.loadConfig();
|
|
37
|
+
await this.client.revoke(refreshToken);
|
|
38
|
+
await npmConfig.clearToken();
|
|
39
|
+
}
|
|
40
|
+
async auth(npmConfig) {
|
|
41
|
+
const handle = await this.client.deviceAuthorization({
|
|
42
|
+
duration_seconds: 86399,
|
|
43
|
+
});
|
|
44
|
+
const completeUrl = `${handle.verification_uri}?user_code=${handle.user_code}`;
|
|
45
|
+
const open = await import('open').then(mod => mod.default);
|
|
46
|
+
await open(completeUrl);
|
|
47
|
+
let tokenSet = null;
|
|
48
|
+
while (!tokenSet) {
|
|
49
|
+
try {
|
|
50
|
+
tokenSet = await handle.poll();
|
|
51
|
+
logger.success('authenticated');
|
|
52
|
+
await npmConfig.storeToken(tokenSet);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
logger.error('poll failed:', error);
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=oauth.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@coze/cli",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "command-line tool for component development",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "Coze Team <coze@feedback.com>",
|
|
7
|
+
"maintainers": [],
|
|
8
|
+
"main": "dist/index.mjs",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"bin": {
|
|
11
|
+
"coze": "./bin.mjs"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"LICENSE",
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md",
|
|
17
|
+
"package.json",
|
|
18
|
+
"bin.mjs",
|
|
19
|
+
"!dist/commands/ui",
|
|
20
|
+
"!**/*.map",
|
|
21
|
+
"!**/*.tsbuildinfo"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "rslib build",
|
|
25
|
+
"lint": "eslint ./ --cache",
|
|
26
|
+
"release": "node scripts/release.js",
|
|
27
|
+
"start": "node bin.mjs",
|
|
28
|
+
"test": "vitest --run --passWithNoTests",
|
|
29
|
+
"test:cov": "npm run test -- --coverage"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@inquirer/prompts": "^3.2.0",
|
|
33
|
+
"@module-federation/rsbuild-plugin": "^0.6.12",
|
|
34
|
+
"@npmcli/config": "~8.3.4",
|
|
35
|
+
"@rsbuild/core": "^1.0.11",
|
|
36
|
+
"@rsbuild/plugin-less": "^1.0.3",
|
|
37
|
+
"@rsbuild/plugin-react": "^1.0.3",
|
|
38
|
+
"@rslib/core": "^0.1.2",
|
|
39
|
+
"axios": "^1.7.7",
|
|
40
|
+
"chokidar": "^4.0.1",
|
|
41
|
+
"commander": "~12.1.0",
|
|
42
|
+
"cross-spawn": "^7.0.3",
|
|
43
|
+
"express": "^4.21.1",
|
|
44
|
+
"form-data": "^4.0.1",
|
|
45
|
+
"lodash": "^4.17.21",
|
|
46
|
+
"open": "~10.1.0",
|
|
47
|
+
"openid-client": "~5.7.0",
|
|
48
|
+
"picocolors": "^1.0.0",
|
|
49
|
+
"prettier": "^2",
|
|
50
|
+
"semver": "^7.3.7"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@module-federation/sdk": "*",
|
|
54
|
+
"@types/node": "^18",
|
|
55
|
+
"@types/npmcli__config": "~6.0.3",
|
|
56
|
+
"@types/open": "~6.2.1",
|
|
57
|
+
"@vitest/coverage-v8": "~1.4.0",
|
|
58
|
+
"mock-fs": "~5.3.0",
|
|
59
|
+
"msw": "~2.0.11",
|
|
60
|
+
"sucrase": "^3.32.0",
|
|
61
|
+
"typescript": "~5.0.4",
|
|
62
|
+
"vitest": "~1.4.0"
|
|
63
|
+
}
|
|
64
|
+
}
|