@codedrifters/utils 0.0.20 → 0.0.22

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 (2) hide show
  1. package/README.md +3 -303
  2. package/package.json +5 -2
package/README.md CHANGED
@@ -1,307 +1,7 @@
1
1
  # @codedrifters/utils
2
2
 
3
- Common utilities and helper functions for CodeDrifter projects. This package provides reusable utility functions for working with Git, string manipulation, and other common tasks.
3
+ Common utilities and helper functions for CodeDrifters projects. Lightweight helpers for Git operations (`findGitBranch`, `findGitRepoName`), string manipulation (`hashString`, `trimStringLength`), and shared AWS type definitions (`AwsStageType`, `DeploymentTargetRoleType`) used across deployment configurations.
4
4
 
5
- ## Table of Contents
5
+ **Documentation:** See the [@codedrifters/utils docs](../../../docs/src/content/docs/packages/@codedrifters/utils/index.md) in the monorepo's Starlight site.
6
6
 
7
- - [Installation](#installation)
8
- - [Utility Functions](#utility-functions)
9
- - [Git Utilities](#git-utilities)
10
- - [String Utilities](#string-utilities)
11
- - [AWS Types](#aws-types)
12
- - [API Reference](#api-reference)
13
- - [Further Documentation](#further-documentation)
14
-
15
- ## Installation
16
-
17
- **Important:** Always configure dependencies through Projen/Configulator, not by manually installing packages via command line. This ensures consistent dependency management across your project.
18
-
19
- ### In a Monorepo (Recommended)
20
-
21
- If you're using `@codedrifters/configulator` in a monorepo, add the package as a dependency in your sub-project configuration:
22
-
23
- ```typescript
24
- import { TypeScriptProject } from '@codedrifters/configulator';
25
- import { MonorepoProject } from '@codedrifters/configulator';
26
-
27
- const myProject = new TypeScriptProject({
28
- name: 'my-project',
29
- packageName: '@myorg/my-project',
30
- outdir: 'packages/my-project',
31
- parent: root, // Your MonorepoProject instance
32
-
33
- deps: [
34
- '@codedrifters/utils',
35
- ],
36
- });
37
- ```
38
-
39
- ### In a Standalone Project
40
-
41
- If you're using Projen directly, add the package to your `deps` array:
42
-
43
- ```typescript
44
- import { typescript } from 'projen';
45
-
46
- const project = new typescript.TypeScriptProject({
47
- name: 'my-project',
48
-
49
- deps: [
50
- '@codedrifters/utils',
51
- ],
52
- });
53
- ```
54
-
55
- ### After Adding to Configuration
56
-
57
- After updating your projenrc configuration file, run:
58
-
59
- ```bash
60
- npx projen
61
- ```
62
-
63
- This will update your `package.json` with the new dependency. Then run:
64
-
65
- ```bash
66
- pnpm install
67
- ```
68
-
69
- to install the newly configured dependencies.
70
-
71
- ## Utility Functions
72
-
73
- ### Git Utilities
74
-
75
- Helper functions for working with Git in deployments and build processes.
76
-
77
- #### `findGitBranch()`
78
-
79
- Returns the current git branch name.
80
-
81
- ```typescript
82
- import { findGitBranch } from '@codedrifters/utils/lib/git-utils';
83
-
84
- const branch = findGitBranch();
85
- // Returns: "feature/1234" or "main" etc.
86
- ```
87
-
88
- **Example Usage:**
89
-
90
- ```typescript
91
- import { findGitBranch } from '@codedrifters/utils/lib/git-utils';
92
-
93
- // Use in CDK deployment
94
- const branch = findGitBranch();
95
- const environment = branch === 'main' ? 'production' : 'staging';
96
- ```
97
-
98
- #### `findGitRepoName()`
99
-
100
- Returns the repository name. Works in both local environments and GitHub Actions.
101
-
102
- ```typescript
103
- import { findGitRepoName } from '@codedrifters/utils/lib/git-utils';
104
-
105
- const repoName = findGitRepoName();
106
- // Returns: "codedrifters/packages" or similar
107
- ```
108
-
109
- **How it works:**
110
-
111
- - In GitHub Actions, this uses the `GITHUB_REPOSITORY` environment variable
112
- - Locally, it extracts the name from the git remote URL
113
-
114
- **Example Usage:**
115
-
116
- ```typescript
117
- import { findGitRepoName } from '@codedrifters/utils/lib/git-utils';
118
-
119
- const repoName = findGitRepoName();
120
- console.log(`Deploying from repository: ${repoName}`);
121
- ```
122
-
123
- ### String Utilities
124
-
125
- Helper functions for string manipulation and hashing.
126
-
127
- #### `hashString(inString: string, trimLength?: number)`
128
-
129
- Creates a SHA-256 hash of a string, optionally trimmed to a specific length.
130
-
131
- ```typescript
132
- import { hashString } from '@codedrifters/utils/lib/string-utils';
133
-
134
- const hash = hashString('my-string');
135
- // Returns: Full SHA-256 hash
136
-
137
- const shortHash = hashString('my-string', 8);
138
- // Returns: First 8 characters of hash
139
- ```
140
-
141
- **Parameters:**
142
- - `inString: string` - The string to hash
143
- - `trimLength?: number` - Optional length to trim the hash to (defaults to 999)
144
-
145
- **Example Usage:**
146
-
147
- ```typescript
148
- import { hashString } from '@codedrifters/utils/lib/string-utils';
149
-
150
- // Create a unique identifier from a string
151
- const id = hashString('user-123', 16);
152
- // Returns: First 16 characters of SHA-256 hash
153
-
154
- // Full hash for security purposes
155
- const secureHash = hashString('sensitive-data');
156
- ```
157
-
158
- #### `trimStringLength(inputString: string, maxLength: number)`
159
-
160
- Truncates a string to a maximum length if it exceeds it.
161
-
162
- ```typescript
163
- import { trimStringLength } from '@codedrifters/utils/lib/string-utils';
164
-
165
- const short = trimStringLength('very long string', 10);
166
- // Returns: "very long " (truncated to 10 chars)
167
-
168
- const unchanged = trimStringLength('short', 10);
169
- // Returns: "short" (no change needed)
170
- ```
171
-
172
- **Parameters:**
173
- - `inputString: string` - The string to truncate
174
- - `maxLength: number` - Maximum length of the string
175
-
176
- **Example Usage:**
177
-
178
- ```typescript
179
- import { trimStringLength } from '@codedrifters/utils/lib/string-utils';
180
-
181
- // Ensure string fits within AWS resource name limits
182
- const resourceName = trimStringLength('very-long-resource-name', 63);
183
- // Useful for AWS resource naming constraints
184
- ```
185
-
186
- ### AWS Types
187
-
188
- Type definitions for AWS deployment stages and environment types, commonly used in CDK deployment configurations.
189
-
190
- #### `AWS_STAGE_TYPE` and `AwsStageType`
191
-
192
- Constants and types for deployment stage classification (dev, stage, prod).
193
-
194
- ```typescript
195
- import { AWS_STAGE_TYPE, AwsStageType } from '@codedrifters/utils/lib/aws/aws-types';
196
-
197
- // Use the constant
198
- const stage = AWS_STAGE_TYPE.DEV; // "dev"
199
- const prodStage = AWS_STAGE_TYPE.PROD; // "prod"
200
-
201
- // Use the type
202
- function configureDeployment(stage: AwsStageType) {
203
- // stage can be "dev", "stage", or "prod"
204
- }
205
- ```
206
-
207
- **Available Values:**
208
- - `AWS_STAGE_TYPE.DEV` - Development environment
209
- - `AWS_STAGE_TYPE.STAGE` - Staging environment
210
- - `AWS_STAGE_TYPE.PROD` - Production environment
211
-
212
- #### `DEPLOYMENT_TARGET_ROLE` and `DeploymentTargetRoleType`
213
-
214
- Constants and types for deployment target role (primary vs secondary region).
215
-
216
- ```typescript
217
- import { DEPLOYMENT_TARGET_ROLE, DeploymentTargetRoleType } from '@codedrifters/utils/lib/aws/aws-types';
218
-
219
- // Use the constant
220
- const role = DEPLOYMENT_TARGET_ROLE.PRIMARY; // "primary"
221
- const replicaRole = DEPLOYMENT_TARGET_ROLE.SECONDARY; // "secondary"
222
-
223
- // Use the type
224
- function configureRegion(role: DeploymentTargetRoleType) {
225
- // role can be "primary" or "secondary"
226
- }
227
- ```
228
-
229
- **Available Values:**
230
- - `DEPLOYMENT_TARGET_ROLE.PRIMARY` - Primary deployment target (account/region)
231
- - `DEPLOYMENT_TARGET_ROLE.SECONDARY` - Secondary/replica deployment target
232
-
233
- **Example Usage:**
234
-
235
- ```typescript
236
- import { AWS_STAGE_TYPE, DEPLOYMENT_TARGET_ROLE } from '@codedrifters/utils/lib/aws/aws-types';
237
- import { AwsDeploymentTarget } from '@codedrifters/configulator';
238
-
239
- // Configure a deployment target (preferred: deploymentTargetRole)
240
- new AwsDeploymentTarget(project, {
241
- account: '123456789012',
242
- region: 'us-east-1',
243
- awsStageType: AWS_STAGE_TYPE.PROD,
244
- deploymentTargetRole: DEPLOYMENT_TARGET_ROLE.PRIMARY,
245
- });
246
- ```
247
-
248
- #### `AWS_ENVIRONMENT_TYPE` and `AwsEnvironmentType` (deprecated)
249
-
250
- **Deprecated.** Use `DEPLOYMENT_TARGET_ROLE` and `DeploymentTargetRoleType` instead. These names are maintained for backward compatibility.
251
-
252
- - `AWS_ENVIRONMENT_TYPE` - Same as `DEPLOYMENT_TARGET_ROLE` (PRIMARY, SECONDARY)
253
- - `AwsEnvironmentType` - Same as `DeploymentTargetRoleType`
254
-
255
- ## API Reference
256
-
257
- ### Exports
258
-
259
- The package exports the following utility functions:
260
-
261
- **Git Utilities:**
262
- - `findGitBranch()` - Get current git branch name
263
- - `findGitRepoName()` - Get repository name (works in local and CI environments)
264
-
265
- **String Utilities:**
266
- - `hashString(inString: string, trimLength?: number)` - Create SHA-256 hash of a string
267
- - `trimStringLength(inputString: string, maxLength: number)` - Truncate string to maximum length
268
-
269
- **AWS Types:**
270
- - `AWS_STAGE_TYPE` - Constants for deployment stages (DEV, STAGE, PROD)
271
- - `AwsStageType` - Type for deployment stage values
272
- - `DEPLOYMENT_TARGET_ROLE` - Constants for deployment target role (PRIMARY, SECONDARY)
273
- - `DeploymentTargetRoleType` - Type for deployment target role values
274
- - `AWS_ENVIRONMENT_TYPE` - (Deprecated) Use `DEPLOYMENT_TARGET_ROLE` instead
275
- - `AwsEnvironmentType` - (Deprecated) Use `DeploymentTargetRoleType` instead
276
-
277
- ### Import Paths
278
-
279
- You can import utilities in two ways:
280
-
281
- **Direct import from lib:**
282
- ```typescript
283
- import { findGitBranch } from '@codedrifters/utils/lib/git-utils';
284
- import { hashString } from '@codedrifters/utils/lib/string-utils';
285
- import { AWS_STAGE_TYPE, AwsStageType } from '@codedrifters/utils/lib/aws/aws-types';
286
- ```
287
-
288
- **Import from main package (if re-exported):**
289
- ```typescript
290
- import { findGitBranch, hashString, AWS_STAGE_TYPE, AwsStageType } from '@codedrifters/utils';
291
- ```
292
-
293
- ## Further Documentation
294
-
295
- ### Package Information
296
-
297
- - [NPM Package](https://www.npmjs.com/package/@codedrifters/utils) - View on NPM
298
- - [GitHub Repository](https://github.com/codedrifters/packages) - Source code
299
-
300
- ### Related Packages
301
-
302
- - [@codedrifters/constructs](../constructs/README.md) - AWS CDK constructs that use these utilities
303
- - [@codedrifters/configulator](../configulator/README.md) - Projen configuration utilities
304
-
305
- ---
306
-
307
- **Note:** This package is designed to be lightweight and dependency-free where possible. It uses Node.js built-in modules (`node:child_process`, `node:crypto`) for maximum compatibility.
7
+ **NPM:** [@codedrifters/utils](https://www.npmjs.com/package/@codedrifters/utils)
package/package.json CHANGED
@@ -10,6 +10,7 @@
10
10
  "organization": true
11
11
  },
12
12
  "devDependencies": {
13
+ "@microsoft/api-extractor": "7.58.7",
13
14
  "@types/node": "25.6.0",
14
15
  "@typescript-eslint/eslint-plugin": "^8",
15
16
  "@typescript-eslint/parser": "^8",
@@ -19,7 +20,9 @@
19
20
  "eslint-config-prettier": "^10.1.8",
20
21
  "eslint-import-resolver-typescript": "^4.4.4",
21
22
  "eslint-plugin-import": "^2.32.0",
23
+ "eslint-plugin-jsdoc": "^62.9.0",
22
24
  "eslint-plugin-prettier": "^5.5.5",
25
+ "eslint-plugin-tsdoc": "^0.5.2",
23
26
  "prettier": "^3.8.3",
24
27
  "typescript": "^5.9.3",
25
28
  "vitest": "3.2.4"
@@ -27,13 +30,13 @@
27
30
  "devEngines": {
28
31
  "packageManager": {
29
32
  "name": "pnpm",
30
- "version": "10.33.0",
33
+ "version": "10.33.2",
31
34
  "onFail": "ignore"
32
35
  }
33
36
  },
34
37
  "main": "lib/index.js",
35
38
  "license": "MIT",
36
- "version": "0.0.20",
39
+ "version": "0.0.22",
37
40
  "types": "lib/index.d.ts",
38
41
  "//": "~~ Generated by projen. To modify, edit .projenrc.js and run \"pnpm exec projen\".",
39
42
  "scripts": {