@chargebee/chargebee-apps-shared 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.
Files changed (48) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +113 -0
  3. package/SECURITY.md +8 -0
  4. package/dist/config/environment.d.ts +78 -0
  5. package/dist/config/environment.js +150 -0
  6. package/dist/config/path-resolver.d.ts +117 -0
  7. package/dist/config/path-resolver.js +247 -0
  8. package/dist/dependency/dependency-manager.d.ts +66 -0
  9. package/dist/dependency/dependency-manager.js +257 -0
  10. package/dist/errors.d.ts +8 -0
  11. package/dist/errors.js +17 -0
  12. package/dist/index.d.ts +19 -0
  13. package/dist/index.js +36 -0
  14. package/dist/libs/archive-service.d.ts +21 -0
  15. package/dist/libs/archive-service.js +145 -0
  16. package/dist/libs/file-management-service.d.ts +59 -0
  17. package/dist/libs/file-management-service.js +189 -0
  18. package/dist/libs/iparams-service.d.ts +69 -0
  19. package/dist/libs/iparams-service.js +136 -0
  20. package/dist/libs/template-service.d.ts +57 -0
  21. package/dist/libs/template-service.js +152 -0
  22. package/dist/logger/cb-logger.d.ts +56 -0
  23. package/dist/logger/cb-logger.js +13 -0
  24. package/dist/logger/internal-logger.d.ts +81 -0
  25. package/dist/logger/internal-logger.js +146 -0
  26. package/dist/node/file-system.d.ts +66 -0
  27. package/dist/node/file-system.js +76 -0
  28. package/dist/node/process.d.ts +30 -0
  29. package/dist/node/process.js +25 -0
  30. package/dist/sandbox/sandbox-context.d.ts +37 -0
  31. package/dist/sandbox/sandbox-context.js +48 -0
  32. package/dist/sandbox/sandbox-wrapper.d.ts +75 -0
  33. package/dist/sandbox/sandbox-wrapper.js +2 -0
  34. package/dist/types/common.d.ts +135 -0
  35. package/dist/types/common.js +20 -0
  36. package/dist/validator/iparam-constants.d.ts +49 -0
  37. package/dist/validator/iparam-constants.js +52 -0
  38. package/dist/validator/iparam-defns-validator.d.ts +46 -0
  39. package/dist/validator/iparam-defns-validator.js +291 -0
  40. package/dist/validator/iparams-inputs-validator.d.ts +62 -0
  41. package/dist/validator/iparams-inputs-validator.js +289 -0
  42. package/dist/validator/manifest-validator.d.ts +48 -0
  43. package/dist/validator/manifest-validator.js +138 -0
  44. package/dist/validator/package-validator.d.ts +27 -0
  45. package/dist/validator/package-validator.js +43 -0
  46. package/dist/validator/validator-utils.d.ts +13 -0
  47. package/dist/validator/validator-utils.js +26 -0
  48. package/package.json +45 -0
package/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011-2024 Chargebee, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person
6
+ obtaining a copy of this software and associated documentation
7
+ files (the "Software"), to deal in the Software without
8
+ restriction, including without limitation the rights to use,
9
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the
11
+ Software is furnished to do so, subject to the following
12
+ conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # @chargebee/chargebee-apps-shared
2
+
3
+ Core interfaces, types, and utilities shared across all Chargebee Apps CLI packages.
4
+
5
+ ## Overview
6
+
7
+ This package serves as the foundation for the Chargebee Apps CLI ecosystem, providing:
8
+ - **Common interfaces** that all implementations must follow
9
+ - **Shared types** used across packages
10
+ - **Core utilities** for logging, sandbox management, and validation
11
+ - **Contracts** that ensure consistency between public and private implementations
12
+
13
+ ## Architecture
14
+
15
+ The shared package follows a strict interface-first design, ensuring that both public-facing and internal implementations adhere to the same contracts.
16
+
17
+ ```
18
+ @chargebee/chargebee-apps-shared
19
+ ├── src/
20
+ │ ├── config/ # Environment detection and path resolution
21
+ │ ├── dependency/ # Dependency management and npm operations
22
+ │ ├── types/ # Common TypeScript interfaces and types
23
+ │ ├── logger/ # Logging interfaces and utilities
24
+ │ ├── sandbox/ # Sandbox execution interfaces
25
+ │ ├── event-handler/ # Event handling interfaces
26
+ │ └── index.ts # Main exports
27
+ ```
28
+
29
+ ## Components
30
+
31
+ This package contains core interfaces and utilities organized into focused modules. Each module has detailed documentation with code examples and usage patterns.
32
+
33
+ ### Configuration and Environment
34
+ - **EnvironmentConfig** - Detects execution environment (development, production, test)
35
+ - **PathResolver** - Centralized path resolution based on environment
36
+
37
+ ### Dependency Management
38
+ - **DependencyManager** - Isolated npm installations and secure module loading
39
+
40
+ ### Core Interfaces and Types
41
+ - **File System Operations** (`CBFileSystem`) - Abstracted file operations
42
+ - **Process Operations** (`CBProcess`) - Abstracted process operations
43
+ - **Logger Interface** (`ICBLogger`) - Standardized logging for user code
44
+ - **Event Data Structures** (`EventRecord`, `Manifest`) - Event and configuration types
45
+ - **Command Options** - Standardized CLI option interfaces
46
+
47
+ ### Utilities
48
+ - **Internal Logger** (`__logger`) - CLI operation logging
49
+ - **Sandbox Context** (`SandboxContext`) - Execution context correlation
50
+ - **Validation Utilities** - Application structure validation
51
+
52
+ ### Sandbox Framework
53
+ - **Sandbox Wrapper** (`ISandboxWrapper`) - Secure code execution interface
54
+ - **Event Handler** (`IEventHandler`) - Event processing interface
55
+
56
+ ## Usage
57
+
58
+ This package is imported by other packages in the workspace to implement the defined interfaces:
59
+
60
+ ```typescript
61
+ import {
62
+ ICBLogger,
63
+ CBFileSystem,
64
+ ISandboxWrapper,
65
+ DependencyManager,
66
+ PathResolver,
67
+ EnvironmentConfig
68
+ } from '@chargebee/chargebee-apps-shared';
69
+
70
+ // Environment detection
71
+ const envConfig = new EnvironmentConfig();
72
+ console.log(`Running in ${envConfig.getEnvironment()} mode`);
73
+
74
+ // Path resolution
75
+ const pathResolver = new PathResolver();
76
+ const templatesPath = pathResolver.getPublicLibsTemplatesDir();
77
+
78
+ // Dependency management
79
+ const dependencyManager = new DependencyManager();
80
+ await dependencyManager.ensureDependencies('/app/path', manifest);
81
+
82
+ // Implement interfaces in your package
83
+ export class MyLogger implements ICBLogger {
84
+ // Implementation details...
85
+ }
86
+ ```
87
+
88
+ ## Development
89
+
90
+ Standard npm scripts: `build`, `test`, `test:coverage`, and `type-check`.
91
+
92
+ ## Dependencies
93
+
94
+ **Runtime**: semver (for dependency version validation)
95
+ **Development**: TypeScript 5.0+, Jest, @types/node
96
+
97
+ ## Package Structure
98
+
99
+ - [`src/config/`](src/config/README.md) - Environment detection and path resolution utilities
100
+ - [`src/dependency/`](src/dependency/README.md) - Dependency management with isolated npm installations and secure module loading
101
+ - [`src/types/`](src/types/README.md) - Core types and interfaces with usage examples
102
+ - [`src/logger/`](src/logger/README.md) - Logging interfaces and utilities with implementation patterns
103
+ - [`src/sandbox/`](src/sandbox/README.md) - Sandbox execution interfaces with security examples
104
+ - [`src/event-handler/`](src/event-handler/README.md) - Event handling interfaces with processing patterns
105
+ - `tests/unit/` - Unit tests
106
+
107
+ ## Contributing
108
+
109
+ When adding new interfaces or types:
110
+ 1. Define clear contracts with comprehensive JSDoc comments
111
+ 2. Ensure backward compatibility when possible
112
+ 3. Add comprehensive unit tests
113
+ 4. Consider impact on both public and private implementations
package/SECURITY.md ADDED
@@ -0,0 +1,8 @@
1
+ # Security Policy
2
+
3
+ At Chargebee, we take data integrity and security very seriously. Due to the nature of the product and service we provide, we are committed to working with individuals to stay updated on the latest security techniques and fix any security weakness in our application or infrastructure reported to us responsibly by external parties.
4
+
5
+ https://www.chargebee.com/security/responsible-disclosure-policy/
6
+
7
+ ## Reporting a vulnerability
8
+ Reach out to us at security@chargebee.com. Please do not open GitHub issues or pull requests as this makes the problem immediately visible to everyone, including malicious actors. Chargebee's security team will triage your report and respond according to its impact.
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Environment type enum
3
+ */
4
+ export declare enum EnvironmentType {
5
+ DEVELOPMENT = "development",
6
+ PRODUCTION = "production"
7
+ }
8
+ /**
9
+ * Environment configuration interface
10
+ */
11
+ export interface IEnvironmentConfig {
12
+ /**
13
+ * Get the current environment type
14
+ * @returns {EnvironmentType} The current environment
15
+ */
16
+ getEnvironment(): EnvironmentType;
17
+ /**
18
+ * Check if running in development mode
19
+ * @returns {boolean} True if in development mode
20
+ */
21
+ isDevelopment(): boolean;
22
+ /**
23
+ * Check if running in production mode
24
+ * @returns {boolean} True if in production mode
25
+ */
26
+ isProduction(): boolean;
27
+ }
28
+ /**
29
+ * Environment configuration class that detects the current environment
30
+ * @class EnvironmentConfig
31
+ * @implements {IEnvironmentConfig}
32
+ *
33
+ * @description
34
+ * This class provides environment detection based on multiple indicators:
35
+ * 1. NODE_ENV environment variable
36
+ * 2. Presence of workspace structure (workspaces field in package.json)
37
+ * 3. Module resolution capability (can resolve published packages)
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const envConfig = new EnvironmentConfig();
42
+ * if (envConfig.isDevelopment()) {
43
+ * console.log('Running in development mode');
44
+ * }
45
+ * ```
46
+ */
47
+ export declare class EnvironmentConfig implements IEnvironmentConfig {
48
+ private environment;
49
+ constructor();
50
+ /**
51
+ * Detects the current environment based on various indicators
52
+ * @returns {EnvironmentType} The detected environment type
53
+ * @private
54
+ */
55
+ private detectEnvironment;
56
+ /**
57
+ * Finds the project root by traversing up the directory tree
58
+ * @param {string} startDir - Directory to start searching from
59
+ * @returns {string | null} Path to project root or null if not found
60
+ * @private
61
+ */
62
+ private findProjectRoot;
63
+ /**
64
+ * Gets the current environment type
65
+ * @returns {EnvironmentType} The current environment
66
+ */
67
+ getEnvironment(): EnvironmentType;
68
+ /**
69
+ * Checks if running in development mode
70
+ * @returns {boolean} True if in development mode
71
+ */
72
+ isDevelopment(): boolean;
73
+ /**
74
+ * Checks if running in production mode
75
+ * @returns {boolean} True if in production mode
76
+ */
77
+ isProduction(): boolean;
78
+ }
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.EnvironmentConfig = exports.EnvironmentType = void 0;
40
+ const path_1 = __importDefault(require("path"));
41
+ const fs = __importStar(require("fs"));
42
+ /**
43
+ * Environment type enum
44
+ */
45
+ var EnvironmentType;
46
+ (function (EnvironmentType) {
47
+ EnvironmentType["DEVELOPMENT"] = "development";
48
+ EnvironmentType["PRODUCTION"] = "production";
49
+ })(EnvironmentType || (exports.EnvironmentType = EnvironmentType = {}));
50
+ /**
51
+ * Environment configuration class that detects the current environment
52
+ * @class EnvironmentConfig
53
+ * @implements {IEnvironmentConfig}
54
+ *
55
+ * @description
56
+ * This class provides environment detection based on multiple indicators:
57
+ * 1. NODE_ENV environment variable
58
+ * 2. Presence of workspace structure (workspaces field in package.json)
59
+ * 3. Module resolution capability (can resolve published packages)
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const envConfig = new EnvironmentConfig();
64
+ * if (envConfig.isDevelopment()) {
65
+ * console.log('Running in development mode');
66
+ * }
67
+ * ```
68
+ */
69
+ class EnvironmentConfig {
70
+ constructor() {
71
+ this.environment = this.detectEnvironment();
72
+ }
73
+ /**
74
+ * Detects the current environment based on various indicators
75
+ * @returns {EnvironmentType} The detected environment type
76
+ * @private
77
+ */
78
+ detectEnvironment() {
79
+ // Check NODE_ENV first
80
+ const nodeEnv = process.env['NODE_ENV']?.toLowerCase();
81
+ if (nodeEnv === 'production') {
82
+ return EnvironmentType.PRODUCTION;
83
+ }
84
+ if (nodeEnv === 'development') {
85
+ return EnvironmentType.DEVELOPMENT;
86
+ }
87
+ // If NODE_ENV is not set, detect based on workspace structure
88
+ // In development, we should be in a monorepo with workspaces
89
+ // In production, packages are published and installed independently
90
+ try {
91
+ const currentDir = __dirname;
92
+ const projectRoot = this.findProjectRoot(currentDir);
93
+ if (projectRoot) {
94
+ const packageJsonPath = path_1.default.join(projectRoot, 'package.json');
95
+ if (fs.existsSync(packageJsonPath)) {
96
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
97
+ // If workspaces field exists, we're in development
98
+ if (packageJson.workspaces) {
99
+ return EnvironmentType.DEVELOPMENT;
100
+ }
101
+ }
102
+ }
103
+ }
104
+ catch (error) {
105
+ // If we can't find workspace structure, assume production
106
+ }
107
+ // Default to production if we can't determine
108
+ return EnvironmentType.PRODUCTION;
109
+ }
110
+ /**
111
+ * Finds the project root by traversing up the directory tree
112
+ * @param {string} startDir - Directory to start searching from
113
+ * @returns {string | null} Path to project root or null if not found
114
+ * @private
115
+ */
116
+ findProjectRoot(startDir) {
117
+ let currentDir = startDir;
118
+ const root = path_1.default.parse(currentDir).root;
119
+ while (currentDir !== root) {
120
+ const packageJsonPath = path_1.default.join(currentDir, 'package.json');
121
+ if (fs.existsSync(packageJsonPath)) {
122
+ return currentDir;
123
+ }
124
+ currentDir = path_1.default.dirname(currentDir);
125
+ }
126
+ return null;
127
+ }
128
+ /**
129
+ * Gets the current environment type
130
+ * @returns {EnvironmentType} The current environment
131
+ */
132
+ getEnvironment() {
133
+ return this.environment;
134
+ }
135
+ /**
136
+ * Checks if running in development mode
137
+ * @returns {boolean} True if in development mode
138
+ */
139
+ isDevelopment() {
140
+ return this.environment === EnvironmentType.DEVELOPMENT;
141
+ }
142
+ /**
143
+ * Checks if running in production mode
144
+ * @returns {boolean} True if in production mode
145
+ */
146
+ isProduction() {
147
+ return this.environment === EnvironmentType.PRODUCTION;
148
+ }
149
+ }
150
+ exports.EnvironmentConfig = EnvironmentConfig;
@@ -0,0 +1,117 @@
1
+ import { IEnvironmentConfig } from './environment';
2
+ /**
3
+ * Path type enum for different package paths
4
+ */
5
+ export declare enum PathType {
6
+ /** Root directory of the CLI workspace */
7
+ CLI_ROOT = "CLI_ROOT",
8
+ /** Private application distribution directory */
9
+ PRIVATE_APP_DIST = "PRIVATE_APP_DIST",
10
+ /** Private application root directory */
11
+ PRIVATE_APP_ROOT = "PRIVATE_APP_ROOT",
12
+ /** Public libs templates directory */
13
+ PUBLIC_LIBS_TEMPLATES = "PUBLIC_LIBS_TEMPLATES",
14
+ /** Public libs UI web directory */
15
+ PUBLIC_LIBS_UI_WEB = "PUBLIC_LIBS_UI_WEB"
16
+ }
17
+ /**
18
+ * Path resolver interface
19
+ */
20
+ export interface IPathResolver {
21
+ /**
22
+ * Resolves a path based on the current environment
23
+ * @param {PathType} pathType - Type of path to resolve
24
+ * @returns {string} Resolved path
25
+ */
26
+ resolvePath(pathType: PathType): string;
27
+ /**
28
+ * Gets the CLI workspace root path
29
+ * @returns {string} Path to CLI workspace root
30
+ */
31
+ getCliRoot(): string;
32
+ /**
33
+ * Gets the private application distribution directory path
34
+ * @returns {string} Path to private application dist directory
35
+ */
36
+ getPrivateAppDistDir(): string;
37
+ /**
38
+ * Gets the private application root directory path
39
+ * @returns {string} Path to private application root directory
40
+ */
41
+ getPrivateAppRootDir(): string;
42
+ /**
43
+ * Gets the public libs templates directory path
44
+ * @returns {string} Path to public libs templates directory
45
+ */
46
+ getPublicLibsTemplatesDir(): string;
47
+ /**
48
+ * Gets the public libs UI web directory path
49
+ * @returns {string} Path to public libs UI web directory
50
+ */
51
+ getPublicLibsUiWebDir(): string;
52
+ }
53
+ /**
54
+ * Path resolver class for resolving paths based on environment
55
+ * @class PathResolver
56
+ * @implements {IPathResolver}
57
+ *
58
+ * @description
59
+ * This class provides centralized path resolution for all packages in the CLI.
60
+ * It detects the environment (development vs production) and resolves paths accordingly:
61
+ * - In development: Uses workspace-relative paths
62
+ * - In production: Uses require.resolve to find installed packages
63
+ *
64
+ * This eliminates the need for try-catch error handling scattered across the codebase.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const pathResolver = new PathResolver();
69
+ * const templatesDir = pathResolver.getPublicLibsTemplatesDir();
70
+ * console.log(`Templates directory: ${templatesDir}`);
71
+ * ```
72
+ */
73
+ export declare class PathResolver implements IPathResolver {
74
+ private envConfig;
75
+ private cliRoot;
76
+ constructor(envConfig?: IEnvironmentConfig);
77
+ /**
78
+ * Resolves a path based on the path type and environment
79
+ * @param {PathType} pathType - Type of path to resolve
80
+ * @returns {string} Resolved path
81
+ * @throws {Error} If path cannot be resolved
82
+ */
83
+ resolvePath(pathType: PathType): string;
84
+ /**
85
+ * Gets the CLI workspace root path
86
+ * @returns {string} Path to CLI workspace root
87
+ */
88
+ getCliRoot(): string;
89
+ /**
90
+ * Gets the private application distribution directory path
91
+ * @returns {string} Path to private application dist directory
92
+ */
93
+ getPrivateAppDistDir(): string;
94
+ /**
95
+ * Gets the private application root directory path
96
+ * @returns {string} Path to private application root directory
97
+ */
98
+ getPrivateAppRootDir(): string;
99
+ /**
100
+ * Gets the public libs templates directory path
101
+ * @returns {string} Path to public libs templates directory
102
+ */
103
+ getPublicLibsTemplatesDir(): string;
104
+ /**
105
+ * Gets the public libs UI web directory path
106
+ * @returns {string} Path to public libs UI web directory
107
+ */
108
+ getPublicLibsUiWebDir(): string;
109
+ /**
110
+ * Finds the project root by looking for package.json with workspaces
111
+ * @param {string} startDir - Directory to start searching from
112
+ * @returns {string} Path to project root
113
+ * @throws {Error} If project root cannot be found
114
+ * @private
115
+ */
116
+ private findProjectRootWithWorkspaces;
117
+ }