@snap/valdi 1.0.1 → 1.0.7

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 (41) hide show
  1. package/.bootstrap/modules/cpp_bridge_module/BUILD.bazel.template +3 -3
  2. package/.bootstrap/modules/cpp_bridge_module/cpp/CalculatorModuleFactory.cpp.template +18 -19
  3. package/.bootstrap/modules/cpp_bridge_module/src/Calculator.d.ts +3 -0
  4. package/.github/README.md +164 -0
  5. package/.metadata/.cursorrules.template +60 -0
  6. package/.metadata/.editorconfig.template +37 -0
  7. package/{.bootstrap/apps/ui_application → .metadata}/AGENTS.md.template +84 -4
  8. package/LINUX_COMPATIBILITY.md +248 -0
  9. package/README.md +110 -23
  10. package/dist/commands/bootstrap.js +80 -19
  11. package/dist/commands/doctor.js +361 -54
  12. package/dist/commands/newModule.js +15 -12
  13. package/dist/core/constants.js +3 -0
  14. package/dist/index.js +5 -0
  15. package/dist/setup/DevSetupHelper.js +81 -6
  16. package/dist/setup/linuxSetup.js +149 -11
  17. package/dist/setup/macOSSetup.js +96 -8
  18. package/dist/utils/githubUtils.js +74 -2
  19. package/dist/utils/linuxDistro.js +255 -0
  20. package/dist/utils/linuxDistro.spec.js +170 -0
  21. package/dist/utils/stringUtils.js +60 -0
  22. package/dist/utils/stringUtils.spec.js +63 -0
  23. package/package.json +11 -3
  24. package/scripts/sync-agents-md.sh +44 -0
  25. package/scripts/update-agents-templates.ts +170 -0
  26. package/src/commands/bootstrap.ts +118 -25
  27. package/src/commands/doctor.ts +469 -59
  28. package/src/commands/newModule.ts +18 -19
  29. package/src/core/constants.ts +3 -0
  30. package/src/index.ts +5 -0
  31. package/src/setup/DevSetupHelper.ts +108 -6
  32. package/src/setup/linuxSetup.ts +235 -12
  33. package/src/setup/macOSSetup.ts +122 -8
  34. package/src/utils/githubUtils.ts +98 -2
  35. package/src/utils/linuxDistro.spec.ts +219 -0
  36. package/src/utils/linuxDistro.ts +326 -0
  37. package/src/utils/stringUtils.spec.ts +77 -1
  38. package/src/utils/stringUtils.ts +88 -0
  39. package/test/ValdiSmokeTest.ts +5 -4
  40. package/test/helpers/TypeScriptClient.ts +2 -2
  41. package/.bootstrap/apps/cli_application/AGENTS.md.template +0 -215
@@ -1,5 +1,5 @@
1
1
  import 'jasmine';
2
- import { toPascalCase, toSnakeCase } from './stringUtils';
2
+ import { sanitizeProjectName, toPascalCase, toSnakeCase, validateProjectName } from './stringUtils';
3
3
 
4
4
  describe('stringUtils', () => {
5
5
  it('converts strings to pascal case', () => {
@@ -15,5 +15,81 @@ describe('stringUtils', () => {
15
15
  expect(toSnakeCase(input)).toBe(expected);
16
16
  });
17
17
  });
18
+
19
+ describe('sanitizeProjectName', () => {
20
+ it('replaces dashes with underscores', () => {
21
+ expect(sanitizeProjectName('my-project')).toBe('my_project');
22
+ expect(sanitizeProjectName('my-cool-app')).toBe('my_cool_app');
23
+ });
24
+
25
+ it('preserves original case', () => {
26
+ expect(sanitizeProjectName('MyProject')).toBe('MyProject');
27
+ expect(sanitizeProjectName('MY_PROJECT')).toBe('MY_PROJECT');
28
+ expect(sanitizeProjectName('testNewModule')).toBe('testNewModule');
29
+ });
30
+
31
+ it('removes invalid characters', () => {
32
+ expect(sanitizeProjectName('my project!')).toBe('myproject');
33
+ expect(sanitizeProjectName('my@project#')).toBe('myproject');
34
+ expect(sanitizeProjectName('my.project')).toBe('myproject');
35
+ });
36
+
37
+ it('prefixes with underscore if starts with number', () => {
38
+ expect(sanitizeProjectName('123project')).toBe('_123project');
39
+ expect(sanitizeProjectName('7-eleven')).toBe('_7_eleven');
40
+ });
41
+
42
+ it('handles mixed cases', () => {
43
+ expect(sanitizeProjectName('My-Cool_Project123')).toBe('My_Cool_Project123');
44
+ });
45
+
46
+ it('handles already valid names', () => {
47
+ expect(sanitizeProjectName('my_project')).toBe('my_project');
48
+ expect(sanitizeProjectName('myproject')).toBe('myproject');
49
+ expect(sanitizeProjectName('my_project_123')).toBe('my_project_123');
50
+ });
51
+ });
52
+
53
+ describe('validateProjectName', () => {
54
+ it('rejects empty names', () => {
55
+ expect(validateProjectName('')).toBeTruthy();
56
+ expect(validateProjectName(' ')).toBeTruthy();
57
+ });
58
+
59
+ it('rejects reserved words (case-insensitive)', () => {
60
+ expect(validateProjectName('test')).toContain('reserved word');
61
+ expect(validateProjectName('Test')).toContain('reserved word');
62
+ expect(validateProjectName('TEST')).toContain('reserved word');
63
+ expect(validateProjectName('build')).toContain('reserved word');
64
+ expect(validateProjectName('workspace')).toContain('reserved word');
65
+ expect(validateProjectName('native')).toContain('reserved word');
66
+ expect(validateProjectName('package')).toContain('reserved word');
67
+ });
68
+
69
+ it('accepts valid names with mixed case', () => {
70
+ expect(validateProjectName('my_project')).toBeNull();
71
+ expect(validateProjectName('myproject')).toBeNull();
72
+ expect(validateProjectName('my_cool_app')).toBeNull();
73
+ expect(validateProjectName('project123')).toBeNull();
74
+ expect(validateProjectName('MyProject')).toBeNull();
75
+ expect(validateProjectName('testNewModule')).toBeNull();
76
+ });
77
+
78
+ it('accepts names with dashes (they will be sanitized)', () => {
79
+ expect(validateProjectName('my-project')).toBeNull();
80
+ expect(validateProjectName('my-cool-app')).toBeNull();
81
+ });
82
+
83
+ it('rejects names with only invalid characters', () => {
84
+ expect(validateProjectName('!!!')).toBeTruthy();
85
+ expect(validateProjectName('...')).toBeTruthy();
86
+ expect(validateProjectName('@#$')).toBeTruthy();
87
+ });
88
+
89
+ it('handles names that start with numbers', () => {
90
+ // These are valid because sanitization will prefix with underscore
91
+ expect(validateProjectName('123project')).toBeNull();
92
+ });
93
+ });
18
94
  });
19
95
 
@@ -1,5 +1,93 @@
1
1
  const wordSplitRegex = /[\W_]/;
2
2
 
3
+ // Bazel and common programming language reserved words that should not be used as project names
4
+ const RESERVED_PROJECT_NAMES = new Set([
5
+ // Bazel reserved words
6
+ 'test',
7
+ 'tests',
8
+ 'build',
9
+ 'workspace',
10
+ 'native',
11
+ 'rule',
12
+ 'package',
13
+ 'glob',
14
+ 'select',
15
+ 'repository',
16
+ 'external',
17
+ 'bazel',
18
+ // Common programming language keywords that might cause issues
19
+ 'class',
20
+ 'function',
21
+ 'var',
22
+ 'let',
23
+ 'const',
24
+ 'if',
25
+ 'else',
26
+ 'for',
27
+ 'while',
28
+ 'return',
29
+ 'import',
30
+ 'export',
31
+ 'module',
32
+ 'require',
33
+ // Other problematic names
34
+ 'main',
35
+ 'lib',
36
+ 'src',
37
+ 'bin',
38
+ 'data',
39
+ 'config',
40
+ ]);
41
+
42
+ /**
43
+ * Sanitizes a project name to be safe for use in Bazel rules and generated code.
44
+ * - Replaces dashes with underscores
45
+ * - Removes any characters that are not alphanumeric or underscore
46
+ * - Ensures the name starts with a letter or underscore
47
+ * - Preserves original case (Bazel target names are case-sensitive)
48
+ */
49
+ export function sanitizeProjectName(name: string): string {
50
+ // Replace dashes with underscores
51
+ let sanitized = name.replace(/-/g, '_');
52
+
53
+ // Remove any characters that are not alphanumeric or underscore
54
+ sanitized = sanitized.replace(/[^a-zA-Z0-9_]/g, '');
55
+
56
+ // Ensure it starts with a letter or underscore
57
+ if (sanitized.length > 0 && /^[0-9]/.test(sanitized)) {
58
+ sanitized = '_' + sanitized;
59
+ }
60
+
61
+ return sanitized;
62
+ }
63
+
64
+ /**
65
+ * Validates a project name and returns an error message if invalid, or null if valid.
66
+ */
67
+ export function validateProjectName(name: string): string | null {
68
+ if (!name || name.trim().length === 0) {
69
+ return 'Project name cannot be empty.';
70
+ }
71
+
72
+ const sanitized = sanitizeProjectName(name);
73
+
74
+ if (sanitized.length === 0) {
75
+ return 'Project name must contain at least one alphanumeric character.';
76
+ }
77
+
78
+ // Check if the sanitized name (case-insensitive) is a reserved word
79
+ if (RESERVED_PROJECT_NAMES.has(sanitized.toLowerCase())) {
80
+ return `Project name "${name}" (sanitized to "${sanitized}") is a reserved word and cannot be used. Please choose a different name.`;
81
+ }
82
+
83
+ // Warn if the name was significantly changed during sanitization
84
+ if (sanitized !== name.replace(/-/g, '_')) {
85
+ return `Project name "${name}" contains invalid characters. It will be sanitized to "${sanitized}". Please use only letters, numbers, underscores, and dashes.`;
86
+ }
87
+
88
+ return null;
89
+ }
90
+
3
91
  export function toPascalCase(str: string): string {
4
92
  const words = str.trim().toLowerCase().split(wordSplitRegex);
5
93
  return words.reduce((acc, curr) => {
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
2
  import assert from 'node:assert';
3
- import { mkdir, unlink } from 'node:fs/promises';
3
+ import { mkdir, rm } from 'node:fs/promises';
4
4
  import { basename, join } from 'node:path';
5
5
  import { afterEach, beforeEach, describe, it } from 'node:test';
6
6
  import { lookForOutput } from './helpers/AsyncHelpers';
@@ -15,6 +15,7 @@ import {
15
15
  } from './helpers/ProjectHelpers';
16
16
  import { type ProjectConfig } from './helpers/ProjectHelpers';
17
17
  import { type TSServerClient, createClient, lineAndOffset } from './helpers/TypeScriptClient';
18
+ import { error } from 'node:console';
18
19
 
19
20
 
20
21
  const PROJECT_ROOT = process.env['PROJECT_ROOT']!;
@@ -257,9 +258,9 @@ async function bootstrap(
257
258
  applicationType: 'ui_application' | 'cli_application',
258
259
  ) {
259
260
  // Delete and recreate the bootstrapped project
260
- await unlink(projectRootPath).catch(() => {
261
- // Logging the failure, it's ok if it doesn't exist though
262
- console.log(projectRootPath, 'does not exist yet');
261
+ await rm(projectRootPath, { recursive: true, force: true }).catch((error) => {
262
+ // Logging the failure; if it doesn't exist the force flag will handle it
263
+ console.error('Failed to remove', projectRootPath, ':', error);
263
264
  });
264
265
  await mkdir(projectRootPath, { recursive: true });
265
266
 
@@ -34,7 +34,7 @@ export function createClient(workingDir: string): TSServerClient {
34
34
 
35
35
  const send = (
36
36
  request: Record<string, unknown>,
37
- { timeout = 1000 }: { timeout?: number } = {},
37
+ { timeout = 300000 }: { timeout?: number } = {},
38
38
  ): Promise<Record<string, unknown>> => {
39
39
  const seq = reserveSequence();
40
40
  const body = JSON.stringify({
@@ -113,7 +113,7 @@ export async function lineAndOffset(searchToken: string, file: string): Promise<
113
113
  return { line, offset: Math.max(0, offset), position };
114
114
  }
115
115
 
116
- function withTimeout<T>(promise: Promise<T>, ms: number | false = 400): Promise<T> {
116
+ function withTimeout<T>(promise: Promise<T>, ms: number | false = 300000): Promise<T> {
117
117
  if (ms === false) {
118
118
  return promise;
119
119
  }
@@ -1,215 +0,0 @@
1
- # AGENTS.md - Guide for AI Coding Assistants
2
-
3
- This document provides context and guidelines for AI coding assistants working on this Valdi CLI application.
4
-
5
- ## Overview
6
-
7
- This is a Valdi command-line application that runs in a terminal. Valdi compiles TypeScript to native code, enabling you to write performant CLI tools in TypeScript with access to native platform APIs.
8
-
9
- ## Project Structure
10
-
11
- - **`modules/`** - Your application modules
12
- - Each module contains TypeScript source files
13
- - `BUILD.bazel` files define how modules are built
14
- - `src/` contains your application logic code
15
- - **`WORKSPACE`** - Bazel workspace configuration
16
- - **`config.yaml`** - Valdi project configuration
17
- - **`.bazelrc`** - Bazel build settings
18
-
19
- ## Key Technologies
20
-
21
- - **Valdi** - Framework that compiles TypeScript to native code
22
- - **TypeScript** - Primary development language
23
- - **Bazel** - Build system (note: `bzl` is an alias for `bazel`)
24
-
25
- ## Development Workflow
26
-
27
- ### Initial Setup
28
-
29
- ```bash
30
- # Install Valdi CLI (if not already installed)
31
- cd path/to/valdi/npm_modules/cli
32
- npm run cli:install
33
-
34
- # Setup development environment
35
- valdi dev_setup
36
-
37
- # Sync project (run after changing dependencies or config)
38
- valdi projectsync
39
- ```
40
-
41
- ### Building and Running
42
-
43
- ```bash
44
- # Build the CLI application
45
- bazel build //modules/{{MODULE_NAME}}:{{MODULE_NAME}}
46
-
47
- # Run the application
48
- bazel run //modules/{{MODULE_NAME}}:{{MODULE_NAME}}
49
-
50
- # Build and run with arguments
51
- bazel run //modules/{{MODULE_NAME}}:{{MODULE_NAME}} -- --your-args-here
52
- ```
53
-
54
- ### Common Commands
55
-
56
- ```bash
57
- # Sync project configuration
58
- valdi projectsync
59
-
60
- # Build specific targets
61
- bazel build //modules/{{MODULE_NAME}}:{{MODULE_NAME}}
62
-
63
- # Run tests
64
- bazel test //modules/{{MODULE_NAME}}:tests
65
-
66
- # Clean build cache
67
- bazel clean
68
- ```
69
-
70
- ## TypeScript Development
71
-
72
- ### Entry Point
73
-
74
- Your CLI application typically has an entry point file (e.g., `index.ts`):
75
-
76
- ```typescript
77
- // Example CLI application entry point
78
- function main(args: string[]) {
79
- console.log('Hello from Valdi CLI!');
80
- console.log('Arguments:', args);
81
-
82
- // Your application logic here
83
- }
84
-
85
- // Entry point
86
- main(process.argv.slice(2));
87
- ```
88
-
89
- ### Using Standard Library Modules
90
-
91
- ```typescript
92
- // HTTP requests
93
- import { HTTPClient } from 'valdi_http/src/HTTPClient';
94
-
95
- // Utilities
96
- import { Base64 } from 'coreutils/src/Base64';
97
- import { UUID } from 'coreutils/src/uuidUtils';
98
-
99
- // File system operations
100
- import { FileSystem } from 'file_system/src/FileSystem';
101
-
102
- // Persistence
103
- import { PersistentStorage } from 'persistence/src/PersistentStorage';
104
- ```
105
-
106
- ## Available Standard Library Modules
107
-
108
- - `valdi_core` - Core runtime APIs
109
- - `valdi_http` - Promise-based HTTP client for network requests
110
- - `valdi_rxjs` - RxJS integration for reactive programming
111
- - `persistence` - Key-value storage with encryption and TTL support
112
- - `file_system` - Low-level file I/O operations
113
- - `foundation`, `coreutils` - Common utilities (arrays, Base64, LRU cache, UUID, etc.)
114
- - `worker` - Worker service support for background execution
115
-
116
- ## Debugging
117
-
118
- - **VSCode Integration** - Set breakpoints and debug TypeScript code
119
- - **Console logging** - Use `console.log()`, `console.error()`, etc.
120
- - **Command line arguments** - Pass arguments after `--` in bazel run
121
-
122
- ## Common Patterns
123
-
124
- ### Command Line Arguments
125
-
126
- ```typescript
127
- interface CliArgs {
128
- input?: string;
129
- output?: string;
130
- verbose?: boolean;
131
- }
132
-
133
- function parseArgs(args: string[]): CliArgs {
134
- const parsed: CliArgs = {};
135
-
136
- for (let i = 0; i < args.length; i++) {
137
- if (args[i] === '--input' && i + 1 < args.length) {
138
- parsed.input = args[i + 1];
139
- i++;
140
- } else if (args[i] === '--output' && i + 1 < args.length) {
141
- parsed.output = args[i + 1];
142
- i++;
143
- } else if (args[i] === '--verbose') {
144
- parsed.verbose = true;
145
- }
146
- }
147
-
148
- return parsed;
149
- }
150
- ```
151
-
152
- ### Async Operations
153
-
154
- ```typescript
155
- async function fetchData(url: string): Promise<void> {
156
- try {
157
- const client = new HTTPClient();
158
- const response = await client.get(url);
159
- console.log('Response:', response);
160
- } catch (error) {
161
- console.error('Error fetching data:', error);
162
- }
163
- }
164
- ```
165
-
166
- ### File Operations
167
-
168
- ```typescript
169
- import { FileSystem } from 'file_system/src/FileSystem';
170
-
171
- function readConfigFile(path: string): string {
172
- try {
173
- const fs = FileSystem.getInstance();
174
- return fs.readFileSync(path, 'utf8');
175
- } catch (error) {
176
- console.error('Error reading file:', error);
177
- return '';
178
- }
179
- }
180
- ```
181
-
182
- ## Common Pitfalls
183
-
184
- 1. **Always run `valdi projectsync`** after changing dependencies or config files
185
- 2. **Use async/await** for asynchronous operations
186
- 3. **Error handling** - Always handle errors gracefully in CLI applications
187
- 4. **Build cache** - If builds seem stuck, try `bazel clean`
188
- 5. **Arguments** - Remember to use `--` before passing arguments to your CLI app
189
-
190
- ## Important Files
191
-
192
- - **`config.yaml`** - Project configuration (dependencies, settings)
193
- - **`WORKSPACE`** - Bazel workspace and external dependencies
194
- - **`BUILD.bazel`** - Build rules for each module
195
- - **`.bazelrc`** - Bazel build flags and configuration
196
-
197
- ## Getting Help
198
-
199
- - **Documentation** - Valdi docs in the framework repository at `/docs/`
200
- - **Discord** - Join the [Valdi Discord community](https://discord.gg/uJyNEeYX2U)
201
- - **Examples** - Check `/apps/` directory in Valdi repository for examples
202
- - **CLI Documentation** - See `/docs/docs/workflow-cli-application.md`
203
-
204
- ## Key Points for AI Assistants
205
-
206
- 1. **This is a CLI application** - No UI components, terminal-based
207
- 2. **TypeScript compiles to native** - Performant native executable
208
- 3. **Use Bazel commands** - Primary tool for building and running CLI apps
209
- 4. **Standard library available** - Access to HTTP, file system, persistence, etc.
210
- 5. **Cross-platform** - Can build for Linux, macOS, and potentially other platforms
211
-
212
- ---
213
-
214
- *This document is intended for AI coding assistants to quickly understand this Valdi CLI application. For comprehensive Valdi framework documentation, refer to the main Valdi repository at https://github.com/Snapchat/Valdi*
215
-