@shoper/cli 0.1.0-7 → 0.1.0-8
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/build/cli/auth/api/cli_auth_api.js +15 -2
- package/build/cli/auth/cli_auth_constants.js +6 -0
- package/build/cli/auth/cli_auth_errors_factory.js +10 -0
- package/build/cli/auth/cli_auth_initializer.js +19 -0
- package/build/cli/auth/cli_auth_utils.js +22 -0
- package/build/cli/auth/model/cli_credentials.js +10 -0
- package/build/cli/auth/service/cli_auth_service.js +34 -0
- package/build/cli/auth/tokens/api/cli_auth_tokens_api.js +43 -1
- package/build/cli/auth/tokens/cli_auth_tokens_constants.js +6 -0
- package/build/cli/auth/tokens/cli_auth_tokens_errors_factory.js +10 -0
- package/build/cli/auth/tokens/cli_auth_tokens_initalizer.js +35 -1
- package/build/cli/auth/tokens/cli_auth_tokens_utils.js +26 -0
- package/build/cli/auth/tokens/service/cli_auth_tokens_service.js +84 -4
- package/build/cli/auth/tokens/service/cli_auth_tokens_service_constants.js +2 -0
- package/build/cli/class/base_cli_command.js +3 -0
- package/build/cli/class/errors/app_error/app_error.js +17 -0
- package/build/cli/class/errors/app_error/app_error_constants.js +7 -0
- package/build/cli/class/errors/file_system_errors_factory.js +26 -0
- package/build/cli/class/errors/http_errors_factory.js +21 -0
- package/build/cli/cli_constants.js +1 -0
- package/build/cli/commands/auth/cli_auth_add_token_command.js +32 -0
- package/build/cli/commands/auth/cli_auth_list_tokens_command.js +17 -0
- package/build/cli/commands/auth/cli_auth_remove_token_command.js +37 -0
- package/build/cli/commands/auth/cli_auth_switch_token_command.js +36 -0
- package/build/cli/commands/cli_update_command.js +7 -3
- package/build/cli/commands/commands_constants.js +5 -1
- package/build/cli/commands/files_diff_command.js +174 -0
- package/build/cli/commands/utils/prompt_for_token_utils.js +25 -0
- package/build/cli/core/cli_setup.js +20 -7
- package/build/cli/features/caches/json_cache/json_cache.js +50 -0
- package/build/cli/features/caches/memory_cache.js +6 -0
- package/build/cli/features/data_directory/cli_data_directory_constants.js +0 -1
- package/build/cli/features/data_directory/cli_data_directory_utils.js +3 -4
- package/build/cli/features/data_directory/service/cli_data_directory_service.js +2 -2
- package/build/cli/features/execution_context/execution_context_service.js +3 -5
- package/build/cli/features/http_requester/http_client.js +32 -1
- package/build/cli/features/version/service/cli_version_service.js +2 -4
- package/build/cli/hooks/authorization/ensure_authorization_hook.js +11 -6
- package/build/cli/hooks/authorization/ensure_authorization_hook_constants.js +8 -1
- package/build/index.js +34 -1
- package/build/theme/class/fetch_resources/fetch_resources.js +126 -0
- package/build/theme/class/fetch_resources/fetch_resources_constants.js +2 -0
- package/build/theme/class/fetch_resources/fetch_resources_errors_factory.js +11 -0
- package/build/theme/class/fetch_resources/fetch_resources_utils.js +35 -0
- package/build/theme/commands/theme_commands_constants.js +8 -0
- package/build/theme/commands/theme_init_command.js +53 -0
- package/build/theme/commands/theme_list_command.js +16 -0
- package/build/theme/commands/theme_pull_command.js +124 -0
- package/build/theme/commands/theme_push_command.js +65 -0
- package/build/theme/commands/theme_show_changes_command.js +60 -0
- package/build/theme/commands/theme_verify_command.js +43 -0
- package/build/theme/features/theme/actions/api/theme_actions_api.js +19 -0
- package/build/theme/features/theme/actions/service/theme_actions_service.js +92 -0
- package/build/theme/features/theme/actions/service/theme_actions_service_constants.js +2 -0
- package/build/theme/features/theme/actions/theme_actions_constants.js +15 -0
- package/build/theme/features/theme/actions/theme_actions_initializer.js +27 -0
- package/build/theme/features/theme/actions/theme_actions_utils.js +8 -0
- package/build/theme/features/theme/directory/theme_directory_utils.js +61 -14
- package/build/theme/features/theme/fetch/api/theme_fetch_api.js +16 -0
- package/build/theme/features/theme/fetch/http/theme_fetch_http_api.js +18 -0
- package/build/theme/features/theme/fetch/service/theme_fetch_service.js +113 -0
- package/build/theme/features/theme/fetch/theme_fetch_constants.js +7 -0
- package/build/theme/features/theme/fetch/theme_fetch_initializer.js +23 -0
- package/build/theme/features/theme/init/api/theme_init_api.js +13 -0
- package/build/theme/features/theme/init/http/theme_init_http_api.js +18 -0
- package/build/theme/features/theme/init/service/theme_init_service.js +28 -0
- package/build/theme/features/theme/init/theme_init_constants.js +2 -0
- package/build/theme/features/theme/init/theme_init_initializer.js +22 -0
- package/build/theme/features/theme/merge/api/theme_merge_api.js +28 -0
- package/build/theme/features/theme/merge/service/theme_merge_service.js +56 -0
- package/build/theme/features/theme/merge/theme_merge_constants.js +9 -0
- package/build/theme/features/theme/merge/theme_merge_initializer.js +18 -0
- package/build/theme/features/theme/publish/theme_publish_constants.js +2 -0
- package/build/theme/features/theme/publish/theme_publish_utils.js +7 -0
- package/build/theme/features/theme/push/api/theme_push_api.js +13 -0
- package/build/theme/features/theme/push/http_api/theme_push_http_api.js +21 -0
- package/build/theme/features/theme/push/service/theme_push_service.js +194 -0
- package/build/theme/features/theme/push/service/theme_push_service_constants.js +1 -0
- package/build/theme/features/theme/push/theme_push_constants.js +4 -0
- package/build/theme/features/theme/push/theme_push_errors_factory.js +41 -0
- package/build/theme/features/theme/push/theme_push_initializer.js +27 -0
- package/build/theme/features/theme/theme_constants.js +6 -0
- package/build/theme/features/theme/utils/checksums/theme_checksums_error_factory.js +10 -0
- package/build/theme/features/theme/utils/checksums/theme_checksums_utils.js +94 -0
- package/build/theme/features/theme/utils/directories/theme_resources_with_id_directory_utils.js +80 -0
- package/build/theme/features/theme/utils/theme_images_utils.js +31 -0
- package/build/theme/features/themes/list/api/themes_list_api.js +13 -0
- package/build/theme/features/themes/{http/shoper_themes_http_api.js → list/http/themes_list_http_api.js} +3 -2
- package/build/theme/features/themes/{model/theme_metadata.js → list/model/theme_list_metadata.js} +3 -1
- package/build/theme/features/themes/list/services/themes_list_service.js +37 -0
- package/build/theme/features/themes/list/themes_list_constants.js +2 -0
- package/build/theme/features/themes/list/themes_list_initializer.js +20 -0
- package/build/theme/hooks/ensure_theme_meta_data_untouched.js +17 -0
- package/build/theme/hooks/theme_checksums/ensure_theme_current_checksums_up_to_date.js +21 -0
- package/build/theme/hooks/theme_checksums/ensure_theme_current_checksums_up_to_date_constants.js +7 -0
- package/build/theme/hooks/themes_actions/ensure_themes_actions_hook.js +45 -0
- package/build/theme/hooks/themes_actions/ensure_themes_actions_hook_constants.js +7 -0
- package/build/theme/index.js +13 -4
- package/build/theme/utils/directory_validator/directory_validator_constants.js +13 -0
- package/build/theme/utils/directory_validator/directory_validator_utils.js +187 -0
- package/build/utils/checksums/checksums_utils.js +95 -0
- package/build/utils/checksums/checksums_utils_constants.js +1 -0
- package/build/utils/date_utils.js +3 -0
- package/build/utils/download_file/download_file_errors_factory.js +9 -0
- package/build/utils/download_file/download_file_utils.js +46 -0
- package/build/utils/fs/errors/stream_read_error.js +11 -0
- package/build/utils/fs/errors/stream_write_error.js +11 -0
- package/build/utils/fs/fs_utils.js +152 -0
- package/build/utils/http_utils.js +10 -0
- package/build/utils/path_utils.js +34 -0
- package/build/utils/stream_transforms/json_indent_transform.js +21 -0
- package/build/utils/url_utils.js +9 -0
- package/build/utils/zip/create_zip_utils.js +80 -0
- package/build/utils/zip/errors/create_zip_error.js +11 -0
- package/build/utils/zip/errors/open_zip_error.js +11 -0
- package/build/utils/zip/extract_zip_utils.js +104 -0
- package/oclif.config.js +4 -2
- package/package.json +34 -7
- package/build/cli/hooks/migration/migration_hook.js +0 -4
- package/build/theme/commands/list_command.js +0 -12
- package/build/theme/commands/pull_command.js +0 -24
- package/build/theme/features/theme/directory/theme_directories_utils.js +0 -10
- package/build/theme/features/theme/pull/api/shoper_theme_pull_api.js +0 -9
- package/build/theme/features/theme/pull/http/shoper_theme_pull_http_api.js +0 -14
- package/build/theme/features/theme/pull/service/shoper_theme_pull_service.js +0 -68
- package/build/theme/features/theme/pull/shoper_theme_pull_initializer.js +0 -22
- package/build/theme/features/themes/api/shoper_themes_api.js +0 -14
- package/build/theme/features/themes/services/shoper_themes_service.js +0 -15
- package/build/theme/features/themes/shoper_themes_constants.js +0 -2
- package/build/theme/features/themes/shoper_themes_initalizer.js +0 -20
- package/build/utils/fs.js +0 -44
- package/build/utils/path.js +0 -13
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { FeatureApi } from '@dreamcommerce/star_core';
|
|
2
|
+
import { CLI_AUTH_API_NAME } from '../cli_auth_constants.js';
|
|
3
|
+
export class CliAuthApi extends FeatureApi {
|
|
4
|
+
moduleName = CLI_AUTH_API_NAME;
|
|
5
|
+
#cliAuthService;
|
|
6
|
+
constructor(cliAuthService) {
|
|
7
|
+
super();
|
|
8
|
+
this.#cliAuthService = cliAuthService;
|
|
9
|
+
}
|
|
10
|
+
getCredentials() {
|
|
11
|
+
return this.#cliAuthService.getCredentials();
|
|
12
|
+
}
|
|
13
|
+
hasCredentialsExpired() {
|
|
14
|
+
return this.#cliAuthService.hasCredentialsExpired();
|
|
15
|
+
}
|
|
3
16
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AppError } from '../class/errors/app_error/app_error.js';
|
|
2
|
+
export class CliAuthErrorsFactory {
|
|
3
|
+
static createCredentialNotFoundError() {
|
|
4
|
+
return new AppError({
|
|
5
|
+
code: 'cli_auth.credential_not_found',
|
|
6
|
+
message: 'Credential not found',
|
|
7
|
+
level: 'error'
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { FEATURE_CORES_TYPES, SyncFeatureInitializer } from '@dreamcommerce/star_core';
|
|
2
|
+
import { CliAuthService } from './service/cli_auth_service.js';
|
|
3
|
+
import { CliAuthApi } from './api/cli_auth_api.js';
|
|
4
|
+
import { CLI_AUTH_TOKENS_API_NAME } from './tokens/cli_auth_tokens_constants.js';
|
|
5
|
+
import { CLI_AUTH_FEATURE_NAME } from './cli_auth_constants.js';
|
|
6
|
+
export class CliAuthInitializer extends SyncFeatureInitializer {
|
|
7
|
+
static featureName = CLI_AUTH_FEATURE_NAME;
|
|
8
|
+
init() {
|
|
9
|
+
const cliAuthTokensApi = this.getApiSync(CLI_AUTH_TOKENS_API_NAME);
|
|
10
|
+
return {
|
|
11
|
+
cores: [
|
|
12
|
+
{
|
|
13
|
+
type: FEATURE_CORES_TYPES.api,
|
|
14
|
+
instance: new CliAuthApi(new CliAuthService(cliAuthTokensApi))
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CLI_AUTH_SCOPES } from './cli_auth_constants.js';
|
|
2
|
+
export class CliAuthUtils {
|
|
3
|
+
static checkCredentials(credentialsApi, command) {
|
|
4
|
+
const credentials = credentialsApi.getCredentials();
|
|
5
|
+
if (!credentials)
|
|
6
|
+
return false;
|
|
7
|
+
if (credentialsApi.hasCredentialsExpired()) {
|
|
8
|
+
console.log('Credentials are expired, please reauthorize');
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
return CliAuthUtils.ensureScopeAccessRights(credentials.scopes, command.id);
|
|
12
|
+
}
|
|
13
|
+
// We assume for now that scopes names are same as topics names
|
|
14
|
+
static ensureScopeAccessRights(tokenScopes, commandId) {
|
|
15
|
+
if (!CliAuthUtils.isScopedCommand(Object.values(CLI_AUTH_SCOPES), commandId))
|
|
16
|
+
return true;
|
|
17
|
+
return CliAuthUtils.isScopedCommand(tokenScopes, commandId);
|
|
18
|
+
}
|
|
19
|
+
static isScopedCommand(scopes, commandId) {
|
|
20
|
+
return scopes.some((scope) => commandId.includes(scope));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CliCredentials } from '../model/cli_credentials.js';
|
|
2
|
+
import { CliAuthErrorsFactory } from '../cli_auth_errors_factory.js';
|
|
3
|
+
export class CliAuthService {
|
|
4
|
+
#cliAuthTokensApi;
|
|
5
|
+
#credentials = null;
|
|
6
|
+
constructor(cliAuthTokensApi) {
|
|
7
|
+
this.#cliAuthTokensApi = cliAuthTokensApi;
|
|
8
|
+
this._setCredentials();
|
|
9
|
+
}
|
|
10
|
+
_setCredentials() {
|
|
11
|
+
const defaultTokenPayload = this.#cliAuthTokensApi.getDefaultTokenPayload();
|
|
12
|
+
if (!defaultTokenPayload)
|
|
13
|
+
return;
|
|
14
|
+
this.#credentials = new CliCredentials({
|
|
15
|
+
shopUrl: defaultTokenPayload.iss,
|
|
16
|
+
scopes: defaultTokenPayload.scope.split(';'),
|
|
17
|
+
expires: defaultTokenPayload.exp
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
getCredentials() {
|
|
21
|
+
if (this.#credentials)
|
|
22
|
+
return this.#credentials;
|
|
23
|
+
this._setCredentials();
|
|
24
|
+
return this.#credentials;
|
|
25
|
+
}
|
|
26
|
+
hasCredentialsExpired() {
|
|
27
|
+
if (!this.#credentials)
|
|
28
|
+
throw CliAuthErrorsFactory.createCredentialNotFoundError();
|
|
29
|
+
const defaultTokenIndex = this.#cliAuthTokensApi.getDefaultTokenIndex();
|
|
30
|
+
if (!defaultTokenIndex)
|
|
31
|
+
return true;
|
|
32
|
+
return this.#cliAuthTokensApi.hasTokenExpired(defaultTokenIndex);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -1 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
import { CLI_AUTH_TOKENS_API_NAME } from '../cli_auth_tokens_constants.js';
|
|
2
|
+
import { FeatureApi } from '@dreamcommerce/star_core';
|
|
3
|
+
export class CliAuthTokensApi extends FeatureApi {
|
|
4
|
+
moduleName = CLI_AUTH_TOKENS_API_NAME;
|
|
5
|
+
#service;
|
|
6
|
+
constructor(service) {
|
|
7
|
+
super();
|
|
8
|
+
this.#service = service;
|
|
9
|
+
}
|
|
10
|
+
addToken(token) {
|
|
11
|
+
this.#service.addToken(token);
|
|
12
|
+
}
|
|
13
|
+
removeToken(index) {
|
|
14
|
+
this.#service.removeToken(index);
|
|
15
|
+
}
|
|
16
|
+
setDefaultToken(index) {
|
|
17
|
+
this.#service.setDefaultToken(index);
|
|
18
|
+
}
|
|
19
|
+
getTokenPayload(index) {
|
|
20
|
+
return this.#service.getTokenPayload(index);
|
|
21
|
+
}
|
|
22
|
+
hasToken(index) {
|
|
23
|
+
return this.#service.hasToken(index);
|
|
24
|
+
}
|
|
25
|
+
getAllTokensPayloads() {
|
|
26
|
+
return this.#service.getAllTokensPayloads();
|
|
27
|
+
}
|
|
28
|
+
getDefaultTokenIndex() {
|
|
29
|
+
return this.#service.getDefaultTokenIndex();
|
|
30
|
+
}
|
|
31
|
+
getDefaultToken() {
|
|
32
|
+
return this.#service.getDefaultToken();
|
|
33
|
+
}
|
|
34
|
+
getDefaultTokenPayload() {
|
|
35
|
+
return this.#service.getDefaultTokenPayload();
|
|
36
|
+
}
|
|
37
|
+
hasTokenExpired(index) {
|
|
38
|
+
return this.#service.hasTokenExpired(index);
|
|
39
|
+
}
|
|
40
|
+
getTokensCount() {
|
|
41
|
+
return this.#service.getTokensCount();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AppError } from '../../class/errors/app_error/app_error.js';
|
|
2
|
+
export class CliAuthTokensErrorsFactory {
|
|
3
|
+
static createTokenNotFoundError(index) {
|
|
4
|
+
return new AppError({
|
|
5
|
+
code: 'cli_auth_tokens.token_not_found',
|
|
6
|
+
message: `Token with index "${index}" not found`,
|
|
7
|
+
level: 'error'
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -1 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
import { FEATURE_CORES_TYPES, SyncFeatureInitializer } from '@dreamcommerce/star_core';
|
|
2
|
+
import { CliAuthTokensApi } from './api/cli_auth_tokens_api.js';
|
|
3
|
+
import { CLiAuthTokensService } from './service/cli_auth_tokens_service.js';
|
|
4
|
+
import { CLI_AUTH_TOKENS_FEATURE_NAME, CLI_AUTH_TOKENS_FILE_NAME } from './cli_auth_tokens_constants.js';
|
|
5
|
+
import { CLI_DATA_DIRECTORY_API_NAME } from '../../features/data_directory/cli_data_directory_constants.js';
|
|
6
|
+
import { JsonCache } from '../../features/caches/json_cache/json_cache.js';
|
|
7
|
+
// schema
|
|
8
|
+
// {
|
|
9
|
+
// default: string;
|
|
10
|
+
// tokens: {
|
|
11
|
+
// [key: string]: {
|
|
12
|
+
// type: string;
|
|
13
|
+
// token: string;
|
|
14
|
+
// expires: string;
|
|
15
|
+
// }
|
|
16
|
+
// }
|
|
17
|
+
export class CliAuthTokensInitializer extends SyncFeatureInitializer {
|
|
18
|
+
static featureName = CLI_AUTH_TOKENS_FEATURE_NAME;
|
|
19
|
+
init() {
|
|
20
|
+
const cliDataDirectoryApi = this.getApiSync(CLI_DATA_DIRECTORY_API_NAME);
|
|
21
|
+
const tokensStore = new JsonCache({
|
|
22
|
+
path: cliDataDirectoryApi.getDataDirectoryFullPath(),
|
|
23
|
+
configFileMode: 0o600,
|
|
24
|
+
name: CLI_AUTH_TOKENS_FILE_NAME
|
|
25
|
+
});
|
|
26
|
+
return {
|
|
27
|
+
cores: [
|
|
28
|
+
{
|
|
29
|
+
type: FEATURE_CORES_TYPES.api,
|
|
30
|
+
instance: new CliAuthTokensApi(new CLiAuthTokensService(tokensStore))
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import jwt from 'jsonwebtoken';
|
|
2
|
+
export class CLiAuthTokensUtils {
|
|
3
|
+
static getTokenPayload(token) {
|
|
4
|
+
try {
|
|
5
|
+
return jwt.decode(token);
|
|
6
|
+
}
|
|
7
|
+
catch (err) {
|
|
8
|
+
console.error('Error decoding token:', err);
|
|
9
|
+
throw new Error('Invalid token');
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
static getTokenName(token) {
|
|
13
|
+
const payload = this.getTokenPayload(token);
|
|
14
|
+
const tokenName = payload?.name;
|
|
15
|
+
if (!tokenName) {
|
|
16
|
+
throw new Error('Token name not found in payload');
|
|
17
|
+
}
|
|
18
|
+
return tokenName;
|
|
19
|
+
}
|
|
20
|
+
static hasTokenExpired(token) {
|
|
21
|
+
const tokenPayload = CLiAuthTokensUtils.getTokenPayload(token);
|
|
22
|
+
if (!tokenPayload)
|
|
23
|
+
return true;
|
|
24
|
+
return tokenPayload.exp - Math.floor(Date.now() / 1000) < 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,6 +1,86 @@
|
|
|
1
|
+
import { CLI_AUTH_TOKENS_TYPES } from '../cli_auth_tokens_constants.js';
|
|
2
|
+
import { CLiAuthTokensUtils } from '../cli_auth_tokens_utils.js';
|
|
3
|
+
import { CLI_AUTH_DEFAULT_TOKEN_NAME, CLI_AUTH_TOKENS_KEY } from './cli_auth_tokens_service_constants.js';
|
|
4
|
+
import { CliAuthTokensErrorsFactory } from '../cli_auth_tokens_errors_factory.js';
|
|
1
5
|
export class CLiAuthTokensService {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
#tokensStore;
|
|
7
|
+
constructor(tokensStore) {
|
|
8
|
+
this.#tokensStore = tokensStore;
|
|
9
|
+
}
|
|
10
|
+
addToken(token) {
|
|
11
|
+
const tokeItem = {
|
|
12
|
+
token,
|
|
13
|
+
type: CLI_AUTH_TOKENS_TYPES.jwt
|
|
14
|
+
};
|
|
15
|
+
const tokens = this._getTokenItems() ?? [];
|
|
16
|
+
tokens.push(tokeItem);
|
|
17
|
+
this._setTokens(tokens);
|
|
18
|
+
}
|
|
19
|
+
hasToken(index) {
|
|
20
|
+
return index <= this.getTokensCount();
|
|
21
|
+
}
|
|
22
|
+
removeToken(index) {
|
|
23
|
+
if (!this.hasToken(index))
|
|
24
|
+
return;
|
|
25
|
+
const tokens = this._getTokenItems();
|
|
26
|
+
const newTokens = tokens.filter((_, currIndex) => currIndex !== index - 1);
|
|
27
|
+
this._setTokens(newTokens);
|
|
28
|
+
}
|
|
29
|
+
getDefaultTokenPayload() {
|
|
30
|
+
const defaultTokenIndex = this.getDefaultTokenIndex();
|
|
31
|
+
if (!defaultTokenIndex)
|
|
32
|
+
return null;
|
|
33
|
+
return this.getTokenPayload(defaultTokenIndex);
|
|
34
|
+
}
|
|
35
|
+
getDefaultTokenIndex() {
|
|
36
|
+
return this.#tokensStore.get(CLI_AUTH_DEFAULT_TOKEN_NAME);
|
|
37
|
+
}
|
|
38
|
+
getTokenPayload(index) {
|
|
39
|
+
const tokenItem = this._getTokenItem(index - 1);
|
|
40
|
+
if (!tokenItem)
|
|
41
|
+
return null;
|
|
42
|
+
return CLiAuthTokensUtils.getTokenPayload(tokenItem.token);
|
|
43
|
+
}
|
|
44
|
+
hasTokenExpired(index) {
|
|
45
|
+
const tokenItem = this._getTokenItem(index - 1);
|
|
46
|
+
if (!tokenItem)
|
|
47
|
+
return true;
|
|
48
|
+
return CLiAuthTokensUtils.hasTokenExpired(tokenItem.token);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Index starts from 1
|
|
52
|
+
*/
|
|
53
|
+
setDefaultToken(index) {
|
|
54
|
+
const tokens = this._getTokenItems();
|
|
55
|
+
if (tokens.length < index)
|
|
56
|
+
throw CliAuthTokensErrorsFactory.createTokenNotFoundError(index);
|
|
57
|
+
this.#tokensStore.set(CLI_AUTH_DEFAULT_TOKEN_NAME, index);
|
|
58
|
+
}
|
|
59
|
+
_getTokenItems() {
|
|
60
|
+
return this.#tokensStore.get(CLI_AUTH_TOKENS_KEY) ?? [];
|
|
61
|
+
}
|
|
62
|
+
_getTokenItem(index) {
|
|
63
|
+
const tokens = this._getTokenItems();
|
|
64
|
+
return tokens[index] ?? null;
|
|
65
|
+
}
|
|
66
|
+
_setTokens(tokens) {
|
|
67
|
+
this.#tokensStore.set(CLI_AUTH_TOKENS_KEY, tokens);
|
|
68
|
+
}
|
|
69
|
+
getTokensCount() {
|
|
70
|
+
const tokens = this._getTokenItems();
|
|
71
|
+
return tokens.length;
|
|
72
|
+
}
|
|
73
|
+
getDefaultToken() {
|
|
74
|
+
const defaultTokenIndex = this.getDefaultTokenIndex();
|
|
75
|
+
if (!defaultTokenIndex)
|
|
76
|
+
return null;
|
|
77
|
+
const tokenItem = this._getTokenItem(defaultTokenIndex - 1);
|
|
78
|
+
if (!tokenItem)
|
|
79
|
+
return null;
|
|
80
|
+
return tokenItem.token;
|
|
81
|
+
}
|
|
82
|
+
getAllTokensPayloads() {
|
|
83
|
+
const tokens = this._getTokenItems();
|
|
84
|
+
return tokens.map((tokenItem) => CLiAuthTokensUtils.getTokenPayload(tokenItem.token));
|
|
85
|
+
}
|
|
6
86
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { APP_ERRORS_LEVELS } from './app_error_constants.js';
|
|
2
|
+
//TODO errory do zaprojektownia
|
|
3
|
+
export class AppError extends Error {
|
|
4
|
+
code;
|
|
5
|
+
level;
|
|
6
|
+
tags;
|
|
7
|
+
details;
|
|
8
|
+
stack;
|
|
9
|
+
constructor({ code, details, message, level = APP_ERRORS_LEVELS.error, tags, stack }) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.details = details;
|
|
13
|
+
this.level = level;
|
|
14
|
+
this.tags = tags;
|
|
15
|
+
this.stack = stack;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AppError } from './app_error/app_error.js';
|
|
2
|
+
export class FileSystemErrorsFactory {
|
|
3
|
+
static createDiskFullError() {
|
|
4
|
+
return new AppError({
|
|
5
|
+
code: 'file_system.disk_full',
|
|
6
|
+
message: 'Disk is full',
|
|
7
|
+
level: 'error'
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
static createErrorMovingFile(source, destination, err) {
|
|
11
|
+
return new AppError({
|
|
12
|
+
code: 'file_system.error_moving_file',
|
|
13
|
+
message: `Failed to move file from ${source} to ${destination}`,
|
|
14
|
+
level: 'error',
|
|
15
|
+
stack: err.stack
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
static createErrorCopyingFile(source, destination, err) {
|
|
19
|
+
return new AppError({
|
|
20
|
+
code: 'file_system.error_copying_file',
|
|
21
|
+
message: `Failed to copy file from ${source} to ${destination}`,
|
|
22
|
+
level: 'error',
|
|
23
|
+
stack: err.stack
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AppError } from './app_error/app_error.js';
|
|
2
|
+
export class HttpErrorsFactory {
|
|
3
|
+
static createUnauthorizedError() {
|
|
4
|
+
return new AppError({
|
|
5
|
+
message: 'Unauthorized',
|
|
6
|
+
code: 'unauthorized'
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
static createForbiddenError() {
|
|
10
|
+
return new AppError({
|
|
11
|
+
message: 'Forbidden',
|
|
12
|
+
code: 'forbidden'
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
static createNotFoundError() {
|
|
16
|
+
return new AppError({
|
|
17
|
+
message: 'Not found',
|
|
18
|
+
code: 'not_found'
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BaseCliCommand } from '../../class/base_cli_command.js';
|
|
2
|
+
import { CLI_AUTH_TOKENS_API_NAME } from '../../auth/tokens/cli_auth_tokens_constants.js';
|
|
3
|
+
import inquirer from 'inquirer';
|
|
4
|
+
export class CliAuthAddTokenCommand extends BaseCliCommand {
|
|
5
|
+
static description = 'Add token';
|
|
6
|
+
//TODO
|
|
7
|
+
static examples = [
|
|
8
|
+
{
|
|
9
|
+
description: 'Add token.',
|
|
10
|
+
command: '<%= config.bin %> <%= command.id %>'
|
|
11
|
+
}
|
|
12
|
+
];
|
|
13
|
+
async run() {
|
|
14
|
+
const cliAuthTokensApi = this.getApi(CLI_AUTH_TOKENS_API_NAME);
|
|
15
|
+
const { token } = await inquirer.prompt([
|
|
16
|
+
{
|
|
17
|
+
type: 'input',
|
|
18
|
+
name: 'token',
|
|
19
|
+
message: 'Enter token:'
|
|
20
|
+
}
|
|
21
|
+
]);
|
|
22
|
+
try {
|
|
23
|
+
cliAuthTokensApi.addToken(token);
|
|
24
|
+
cliAuthTokensApi.setDefaultToken(cliAuthTokensApi.getTokensCount());
|
|
25
|
+
console.log('Token added successfully');
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
//TODO handle error
|
|
29
|
+
this.error(`${err}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseCliCommand } from '../../class/base_cli_command.js';
|
|
2
|
+
import { CLI_AUTH_TOKENS_API_NAME } from '../../auth/tokens/cli_auth_tokens_constants.js';
|
|
3
|
+
export class CliAuthListTokensCommand extends BaseCliCommand {
|
|
4
|
+
static description = 'List all added tokens';
|
|
5
|
+
static examples = [`$ cli token:list`];
|
|
6
|
+
async run() {
|
|
7
|
+
const tokensApi = this.getApi(CLI_AUTH_TOKENS_API_NAME);
|
|
8
|
+
const tokensPayloads = tokensApi.getAllTokensPayloads();
|
|
9
|
+
if (!tokensPayloads.length) {
|
|
10
|
+
this.log('No tokens found');
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
tokensPayloads.forEach((tokenPayload) => {
|
|
14
|
+
this.log(`Token name: ${tokenPayload.name}, storeUrl: ${tokenPayload.iss}`);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BaseCliCommand } from '../../class/base_cli_command.js';
|
|
2
|
+
import { Args } from '@oclif/core';
|
|
3
|
+
import { CLI_AUTH_TOKENS_API_NAME } from '../../auth/tokens/cli_auth_tokens_constants.js';
|
|
4
|
+
export class CliAuthRemoveTokenCommand extends BaseCliCommand {
|
|
5
|
+
static summary = 'Remove the access token';
|
|
6
|
+
static description = 'Remove the access token with the provided index.';
|
|
7
|
+
static examples = [
|
|
8
|
+
{
|
|
9
|
+
description: 'Remove token with index 2',
|
|
10
|
+
command: '<%= config.bin %> <%= command.id %> 2'
|
|
11
|
+
}
|
|
12
|
+
];
|
|
13
|
+
static args = {
|
|
14
|
+
index: Args.integer({
|
|
15
|
+
description: 'Index of the token to remove',
|
|
16
|
+
required: true
|
|
17
|
+
})
|
|
18
|
+
};
|
|
19
|
+
async run() {
|
|
20
|
+
const cliAuthTokensApi = this.getApi(CLI_AUTH_TOKENS_API_NAME);
|
|
21
|
+
const data = await this.parse(CliAuthRemoveTokenCommand);
|
|
22
|
+
const { args } = data;
|
|
23
|
+
const { index } = args;
|
|
24
|
+
if (!cliAuthTokensApi.hasToken(index)) {
|
|
25
|
+
this.log(`Token with index "${index}" does not exist.`);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
cliAuthTokensApi.removeToken(index);
|
|
30
|
+
cliAuthTokensApi.setDefaultToken(cliAuthTokensApi.getTokensCount());
|
|
31
|
+
this.log(`Access token ${index} removed successfully.`);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
this.error('Failed to remove the access token. Please try again later.');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { BaseCliCommand } from '../../class/base_cli_command.js';
|
|
2
|
+
import { CLI_AUTH_TOKENS_API_NAME } from '../../auth/tokens/cli_auth_tokens_constants.js';
|
|
3
|
+
import { Args } from '@oclif/core';
|
|
4
|
+
export class CliAuthSwitchTokenCommand extends BaseCliCommand {
|
|
5
|
+
static description = 'Set token with provided index as default';
|
|
6
|
+
//TODO
|
|
7
|
+
static examples = [
|
|
8
|
+
{
|
|
9
|
+
description: 'Set token 2 as default.',
|
|
10
|
+
command: '<%= config.bin %> <%= command.id %> 2'
|
|
11
|
+
}
|
|
12
|
+
];
|
|
13
|
+
static args = {
|
|
14
|
+
index: Args.integer({
|
|
15
|
+
description: 'Index of the token to set as default',
|
|
16
|
+
required: true
|
|
17
|
+
})
|
|
18
|
+
};
|
|
19
|
+
async run() {
|
|
20
|
+
const cliAuthTokensApi = this.getApi(CLI_AUTH_TOKENS_API_NAME);
|
|
21
|
+
const { args } = await this.parse(CliAuthSwitchTokenCommand);
|
|
22
|
+
const { index } = args;
|
|
23
|
+
if (!cliAuthTokensApi.hasToken(index)) {
|
|
24
|
+
this.log(`Token with index "${index}" does not exist.`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
cliAuthTokensApi.setDefaultToken(index);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
//TODO
|
|
32
|
+
this.log(`Error: ${err}`);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { BaseCommand } from '../class/base_command.js';
|
|
2
1
|
import { CLI_VERSION_API_NAME } from '../features/version/cli_version_constants.js';
|
|
3
2
|
import { CLI_NAME } from '../cli_constants.js';
|
|
4
|
-
|
|
3
|
+
import { BaseCliCommand } from '../class/base_cli_command.js';
|
|
4
|
+
export class CliUpdateCommand extends BaseCliCommand {
|
|
5
5
|
static summary = 'Update the Shoper CLI';
|
|
6
6
|
static description = 'Update the Shoper CLI to the latest version.';
|
|
7
7
|
static examples = [`$ shoper update`];
|
|
@@ -14,7 +14,11 @@ export class CliUpdateCommand extends BaseCommand {
|
|
|
14
14
|
const stdout = await cliVersionApi.update();
|
|
15
15
|
this.log(stdout);
|
|
16
16
|
}
|
|
17
|
-
catch {
|
|
17
|
+
catch (error) {
|
|
18
|
+
//npm code for EACCES
|
|
19
|
+
if (error?.code === 243) {
|
|
20
|
+
this.error(error);
|
|
21
|
+
}
|
|
18
22
|
this.error('Failed to update the CLI. Please try again later.');
|
|
19
23
|
}
|
|
20
24
|
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export const CLI_COMMANDS_NAMES = {
|
|
2
2
|
help: 'help',
|
|
3
3
|
version: 'version',
|
|
4
|
-
update: 'update'
|
|
4
|
+
update: 'update',
|
|
5
|
+
authListTokens: 'auth:list-tokens',
|
|
6
|
+
authAddToken: 'auth:add-token',
|
|
7
|
+
authRemoveToken: 'auth:remove-token',
|
|
8
|
+
switchToken: 'auth:switch-token'
|
|
5
9
|
};
|