@snap/valdi 1.0.1 → 1.0.6

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 (39) hide show
  1. package/.bootstrap/apps/cli_application/AGENTS.md.template +76 -0
  2. package/.bootstrap/apps/ui_application/AGENTS.md.template +79 -3
  3. package/.bootstrap/modules/cpp_bridge_module/BUILD.bazel.template +3 -3
  4. package/.bootstrap/modules/cpp_bridge_module/cpp/CalculatorModuleFactory.cpp.template +18 -19
  5. package/.bootstrap/modules/cpp_bridge_module/src/Calculator.d.ts +3 -0
  6. package/.github/README.md +164 -0
  7. package/.metadata/.cursorrules.template +53 -0
  8. package/.metadata/.editorconfig.template +37 -0
  9. package/LINUX_COMPATIBILITY.md +248 -0
  10. package/README.md +99 -15
  11. package/dist/commands/bootstrap.js +79 -19
  12. package/dist/commands/doctor.js +361 -54
  13. package/dist/commands/newModule.js +15 -12
  14. package/dist/core/constants.js +2 -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 +117 -25
  27. package/src/commands/doctor.ts +469 -59
  28. package/src/commands/newModule.ts +18 -19
  29. package/src/core/constants.ts +2 -0
  30. package/src/setup/DevSetupHelper.ts +108 -6
  31. package/src/setup/linuxSetup.ts +235 -12
  32. package/src/setup/macOSSetup.ts +122 -8
  33. package/src/utils/githubUtils.ts +98 -2
  34. package/src/utils/linuxDistro.spec.ts +219 -0
  35. package/src/utils/linuxDistro.ts +326 -0
  36. package/src/utils/stringUtils.spec.ts +77 -1
  37. package/src/utils/stringUtils.ts +88 -0
  38. package/test/ValdiSmokeTest.ts +5 -4
  39. package/test/helpers/TypeScriptClient.ts +2 -2
@@ -0,0 +1,170 @@
1
+ #!/usr/bin/env ts-node
2
+ /**
3
+ * Updates AGENTS.md templates in bootstrap directory with correct Valdi patterns
4
+ * Ensures consistency with root AGENTS.md anti-hallucination guidelines
5
+ */
6
+
7
+ import fs from 'fs';
8
+ import path from 'path';
9
+
10
+ const CLI_ROOT = path.join(__dirname, '..');
11
+ const OPEN_SOURCE_ROOT = path.join(CLI_ROOT, '../..');
12
+
13
+ // Common Valdi patterns section that should be consistent across all AGENTS.md files
14
+ const VALDI_PATTERNS_SECTION = `
15
+ ## 🚨 CRITICAL: Valdi is NOT React!
16
+
17
+ **AI assistants often suggest React patterns that DON'T EXIST in Valdi.** This is because TSX/JSX syntax looks like React, but Valdi compiles to native code.
18
+
19
+ ### āŒ FORBIDDEN Patterns (Don't exist in Valdi)
20
+
21
+ \`\`\`typescript
22
+ // āŒ WRONG - React hooks don't exist
23
+ const [count, setCount] = useState(0);
24
+ useEffect(() => { ... }, []);
25
+
26
+ // āŒ WRONG - Functional components don't exist
27
+ const MyComponent = () => <view />;
28
+
29
+ // āŒ WRONG - Common AI mistakes
30
+ this.markNeedsRender(); // Doesn't exist!
31
+ this.props.title; // Should be: this.viewModel.title
32
+ onMount() { } // Should be: onCreate()
33
+ onUpdate() { } // Should be: onViewModelUpdate()
34
+ onUnmount() { } // Should be: onDestroy()
35
+ this.context.get(Service); // Wrong pattern, use withProviders()
36
+ \`\`\`
37
+
38
+ ### āœ… CORRECT Valdi Patterns
39
+
40
+ \`\`\`typescript
41
+ // āœ… Stateful component with state
42
+ import { StatefulComponent } from 'valdi_core/src/Component';
43
+
44
+ class MyComponent extends StatefulComponent<ViewModel, State> {
45
+ state = { count: 0 };
46
+
47
+ // Lifecycle methods
48
+ onCreate() { } // Component created
49
+ onViewModelUpdate(prev: ViewModel) { } // Props changed
50
+ onDestroy() { } // Before removal
51
+
52
+ // State updates
53
+ handleClick() {
54
+ this.setState({ count: this.state.count + 1 }); // Auto re-renders
55
+ }
56
+
57
+ // Render (returns void, JSX is statement)
58
+ onRender() {
59
+ <button
60
+ title={\`Count: \${this.state.count}\`}
61
+ onPress={this.handleClick}
62
+ />;
63
+ }
64
+ }
65
+
66
+ // āœ… Stateless component (props only)
67
+ import { Component } from 'valdi_core/src/Component';
68
+
69
+ class SimpleComponent extends Component<ViewModel> {
70
+ onRender() {
71
+ <label value={this.viewModel.title} />; // viewModel, not props!
72
+ }
73
+ }
74
+ \`\`\`
75
+
76
+ ### Key Differences from React
77
+
78
+ | Concept | React | Valdi |
79
+ |---------|-------|-------|
80
+ | **Components** | Functions or classes | Classes only (Component or StatefulComponent) |
81
+ | **State** | \`useState\` hook | \`state = { }\` + \`setState()\` |
82
+ | **Props** | \`this.props\` | \`this.viewModel\` |
83
+ | **Lifecycle** | \`useEffect\` | \`onCreate\`, \`onViewModelUpdate\`, \`onDestroy\` |
84
+ | **Re-render** | \`setCount(...)\` | \`this.setState(...)\` |
85
+ | **Return** | \`return <view />\` | \`<view />;\` (statement, returns void) |
86
+
87
+ For complete framework documentation, see: https://github.com/Snapchat/Valdi
88
+ `;
89
+
90
+ function updateAgentsTemplate(templatePath: string, _isCliApp: boolean) {
91
+ console.log(`\nUpdating: ${path.relative(CLI_ROOT, templatePath)}`);
92
+
93
+ let content = fs.readFileSync(templatePath, 'utf-8');
94
+
95
+ // Fix incorrect lifecycle method comments
96
+ content = content.replace(
97
+ /\/\/ onMount\(\) - Called when component is first mounted/g,
98
+ '// onCreate() - Called when component is first created'
99
+ );
100
+ content = content.replace(
101
+ /\/\/ onUnmount\(\) - Called before component is removed/g,
102
+ '// onDestroy() - Called before component is removed'
103
+ );
104
+ content = content.replace(
105
+ /\/\/ onUpdate\(prevProps\) - Called when component updates/g,
106
+ '// onViewModelUpdate(previousViewModel) - Called when viewModel updates'
107
+ );
108
+
109
+ // Find where to insert the patterns section (after Overview, before Project Structure or Development Workflow)
110
+ const insertAfter = '## Overview';
111
+ const insertIndex = content.indexOf(insertAfter);
112
+
113
+ if (insertIndex === -1) {
114
+ console.warn(' āš ļø Could not find Overview section, skipping pattern insertion');
115
+ } else {
116
+ // Find the end of the Overview section (next ## heading)
117
+ const nextHeadingMatch = content.substring(insertIndex + insertAfter.length).match(/\n## /);
118
+ if (nextHeadingMatch) {
119
+ const insertPosition = insertIndex + insertAfter.length + nextHeadingMatch.index!;
120
+
121
+ // Check if patterns section already exists
122
+ if (!content.includes('🚨 CRITICAL: Valdi is NOT React!')) {
123
+ content =
124
+ content.substring(0, insertPosition) +
125
+ '\n' +
126
+ VALDI_PATTERNS_SECTION +
127
+ '\n' +
128
+ content.substring(insertPosition);
129
+ console.log(' āœ… Added Valdi patterns section');
130
+ } else {
131
+ console.log(' ā„¹ļø Patterns section already exists, preserving custom content');
132
+ }
133
+ }
134
+ }
135
+
136
+ // Update Provider pattern example if it exists
137
+ if (content.includes('import { Provider }')) {
138
+ console.log(' āš ļø Found old Provider pattern, consider updating to withProviders() HOC');
139
+ }
140
+
141
+ fs.writeFileSync(templatePath, content, 'utf-8');
142
+ console.log(' āœ… Template updated');
143
+ }
144
+
145
+ function main() {
146
+ console.log('Updating AGENTS.md bootstrap templates with correct Valdi patterns...\n');
147
+
148
+ const uiTemplate = path.join(CLI_ROOT, '.bootstrap/apps/ui_application/AGENTS.md.template');
149
+ const cliTemplate = path.join(CLI_ROOT, '.bootstrap/apps/cli_application/AGENTS.md.template');
150
+
151
+ if (fs.existsSync(uiTemplate)) {
152
+ updateAgentsTemplate(uiTemplate, false);
153
+ } else {
154
+ console.warn(`UI template not found: ${uiTemplate}`);
155
+ }
156
+
157
+ if (fs.existsSync(cliTemplate)) {
158
+ updateAgentsTemplate(cliTemplate, true);
159
+ } else {
160
+ console.warn(`CLI template not found: ${cliTemplate}`);
161
+ }
162
+
163
+ console.log('\nāœ… All templates updated!');
164
+ console.log('\nNext steps:');
165
+ console.log(' 1. Review the changes in the templates');
166
+ console.log(' 2. Test with: valdi bootstrap (in a test directory)');
167
+ console.log(' 3. Commit the updated templates');
168
+ }
169
+
170
+ main();
@@ -1,7 +1,7 @@
1
1
  import path from 'path';
2
2
  import inquirer from 'inquirer';
3
3
  import type { Argv } from 'yargs';
4
- import { ANSI_COLORS, BOOTSTRAP_DIR_PATH, TEMPLATE_BASE_PATHS, VALDI_CONFIG_PATHS } from '../core/constants';
4
+ import { ANSI_COLORS, BOOTSTRAP_DIR_PATH, META_DIR_PATH, TEMPLATE_BASE_PATHS, VALDI_CONFIG_PATHS } from '../core/constants';
5
5
  import { CliError } from '../core/errors';
6
6
  import type { ArgumentsResolver } from '../utils/ArgumentsResolver';
7
7
  import { BazelClient } from '../utils/BazelClient';
@@ -19,9 +19,9 @@ import {
19
19
  resolveFilePath,
20
20
  } from '../utils/fileUtils';
21
21
  import { wrapInColor } from '../utils/logUtils';
22
- import { toPascalCase } from '../utils/stringUtils';
22
+ import { toPascalCase, sanitizeProjectName, validateProjectName } from '../utils/stringUtils';
23
23
  import { getAllProjectSyncTargets, runProjectSync } from './projectsync';
24
- import { resolveLatestReleaseRef } from '../utils/githubUtils';
24
+ import { getLatestReleaseTag } from '../utils/githubUtils';
25
25
 
26
26
  interface CommandParameters {
27
27
  confirmBootstrap: boolean;
@@ -29,6 +29,8 @@ interface CommandParameters {
29
29
  applicationType: string;
30
30
  valdiImport: string;
31
31
  valdiWidgetsImport: string;
32
+ valdiVersion: string;
33
+ valdiWidgetsVersion: string;
32
34
  skipProjectsync: boolean;
33
35
  withCleanup: boolean;
34
36
  }
@@ -55,6 +57,11 @@ const ALL_APPLICATION_TEMPLATES: readonly ApplicationTemplate[] = [
55
57
  const VALDI_GIT_URL = 'https://github.com/Snapchat/Valdi';
56
58
  const VALDI_WIDGETS_GIT_URL = 'https://github.com/Snapchat/Valdi_Widgets';
57
59
 
60
+ /** Pinned Valdi release used by default for reproducible bootstraps. Bump when cutting a new Valdi release. */
61
+ const DEFAULT_VALDI_RELEASE_TAG = 'beta-0.0.2';
62
+ /** Pinned Valdi_Widgets release used by default. Should match the Valdi release cycle. */
63
+ const DEFAULT_VALDI_WIDGETS_RELEASE_TAG = 'beta-0.0.2';
64
+
58
65
  const DEFAULT_VALDI_IMPORT = `
59
66
  http_archive(
60
67
  name = "valdi",
@@ -119,19 +126,64 @@ async function getApplicationType(argv: ArgumentsResolver<CommandParameters>): P
119
126
 
120
127
  async function getProjectName(argv: ArgumentsResolver<CommandParameters>): Promise<string> {
121
128
  return argv.getArgumentOrResolve('projectName', async () => {
122
- const result = await inquirer.prompt<{ projectName: string }>([
123
- {
124
- type: 'input',
125
- name: 'projectName',
126
- message: 'Please provide a name for this project:',
127
- default: defaultProjectName,
128
- },
129
- ]);
130
-
131
- return result.projectName;
129
+ let projectName = '';
130
+ let isValid = false;
131
+
132
+ while (!isValid) {
133
+ const result = await inquirer.prompt<{ projectName: string }>([
134
+ {
135
+ type: 'input',
136
+ name: 'projectName',
137
+ message: 'Please provide a name for this project:',
138
+ default: defaultProjectName,
139
+ },
140
+ ]);
141
+
142
+ projectName = result.projectName;
143
+ const validationError = validateProjectName(projectName);
144
+
145
+ if (validationError) {
146
+ console.log(wrapInColor(`\nāŒ ${validationError}\n`, ANSI_COLORS.RED_COLOR));
147
+ continue;
148
+ }
149
+
150
+ const sanitized = sanitizeProjectName(projectName);
151
+ if (sanitized !== projectName) {
152
+ console.log(
153
+ wrapInColor(
154
+ `\nāš ļø Project name will be sanitized from "${projectName}" to "${sanitized}"`,
155
+ ANSI_COLORS.YELLOW_COLOR,
156
+ ),
157
+ );
158
+ const confirm = await getUserConfirmation('Do you want to continue with this name?', true);
159
+ if (!confirm) {
160
+ continue;
161
+ }
162
+ }
163
+
164
+ isValid = true;
165
+ }
166
+
167
+ return sanitizeProjectName(projectName);
132
168
  });
133
169
  }
134
170
 
171
+ function resolveValdiReleaseTag(
172
+ argv: ArgumentsResolver<CommandParameters>,
173
+ gitUrl: string,
174
+ defaultTag: string,
175
+ versionOption: 'valdiVersion' | 'valdiWidgetsVersion' = 'valdiVersion',
176
+ ): Promise<string> {
177
+ const override = argv.getArgument(versionOption);
178
+ if (!override) {
179
+ return Promise.resolve(defaultTag);
180
+ }
181
+ if (override === 'latest') {
182
+ return getLatestReleaseTag(gitUrl);
183
+ }
184
+ return Promise.resolve(override);
185
+ }
186
+
135
187
  function getValdiImport(argv: ArgumentsResolver<CommandParameters>, valdiReleaseRef: string): string {
136
188
  const valdiImport = argv.getArgument('valdiImport');
137
189
  if (valdiImport) {
@@ -178,6 +230,8 @@ function initializeConfigFiles(
178
230
  TemplateFile.init(TEMPLATE_BASE_PATHS.README),
179
231
  TemplateFile.init(TEMPLATE_BASE_PATHS.GIT_IGNORE),
180
232
  TemplateFile.init(TEMPLATE_BASE_PATHS.WATCHMAN_CONFIG),
233
+ TemplateFile.init(TEMPLATE_BASE_PATHS.EDITOR_CONFIG),
234
+ TemplateFile.init(TEMPLATE_BASE_PATHS.CURSOR_RULES),
181
235
  ];
182
236
 
183
237
  TEMPLATE_FILES.forEach(templateFile => {
@@ -187,15 +241,30 @@ function initializeConfigFiles(
187
241
  templateFile.expandTemplate();
188
242
  });
189
243
 
190
- // Setup hello world application
191
- console.log(wrapInColor(`Initializing ${template.name} application...`, ANSI_COLORS.YELLOW_COLOR));
192
- const sourcePath = path.join(BOOTSTRAP_DIR_PATH, 'apps', template.path);
193
- const destPath = process.cwd();
194
-
244
+ // Copy GitHub templates directory (if it exists)
195
245
  const replacements: Replacements = {
196
246
  MODULE_NAME: projectName,
197
247
  MODULE_NAME_PASCAL_CASED: toPascalCase(projectName),
198
248
  };
249
+ const githubSourcePath = path.join(BOOTSTRAP_DIR_PATH, '.github');
250
+ if (fileExists(githubSourcePath)) {
251
+ console.log(wrapInColor('Creating GitHub templates...', ANSI_COLORS.YELLOW_COLOR));
252
+ const githubDestPath = path.join(process.cwd(), '.github');
253
+ copyBootstrapFiles(githubSourcePath, githubDestPath, replacements);
254
+ }
255
+
256
+ // Copy .cursor rules directory (if it exists)
257
+ const cursorSourcePath = path.join(META_DIR_PATH, '.cursor');
258
+ if (fileExists(cursorSourcePath)) {
259
+ console.log(wrapInColor('Creating Cursor AI rules...', ANSI_COLORS.YELLOW_COLOR));
260
+ const cursorDestPath = path.join(process.cwd(), '.cursor');
261
+ copyBootstrapFiles(cursorSourcePath, cursorDestPath, replacements);
262
+ }
263
+
264
+ // Setup hello world application
265
+ console.log(wrapInColor(`Initializing ${template.name} application...`, ANSI_COLORS.YELLOW_COLOR));
266
+ const sourcePath = path.join(BOOTSTRAP_DIR_PATH, 'apps', template.path);
267
+ const destPath = process.cwd();
199
268
 
200
269
  copyBootstrapFiles(sourcePath, destPath, replacements);
201
270
  }
@@ -235,16 +304,31 @@ async function valdiBootstrap(argv: ArgumentsResolver<CommandParameters>) {
235
304
 
236
305
  // Prompt user for input
237
306
  // - Application Name
238
- const projectName = await getProjectName(argv);
307
+ let projectName = await getProjectName(argv);
308
+
309
+ // Validate project name if provided via command line argument
310
+ if (argv.getArgument('projectName')) {
311
+ const validationError = validateProjectName(projectName);
312
+ if (validationError) {
313
+ throw new CliError(validationError);
314
+ }
315
+ projectName = sanitizeProjectName(projectName);
316
+ }
317
+
239
318
  if (!projectName) {
240
319
  throw new CliError('Project name cannot be empty.');
241
320
  }
242
321
 
243
- const valdiCommitHash = await resolveLatestReleaseRef(`${VALDI_GIT_URL}.git`);
244
- const valdiWidgetsCommitHash = await resolveLatestReleaseRef(`${VALDI_WIDGETS_GIT_URL}.git`);
322
+ const valdiReleaseTag = await resolveValdiReleaseTag(argv, VALDI_GIT_URL, DEFAULT_VALDI_RELEASE_TAG);
323
+ const valdiWidgetsReleaseTag = await resolveValdiReleaseTag(
324
+ argv,
325
+ VALDI_WIDGETS_GIT_URL,
326
+ DEFAULT_VALDI_WIDGETS_RELEASE_TAG,
327
+ 'valdiWidgetsVersion',
328
+ );
245
329
 
246
- const valdiImport = getValdiImport(argv, valdiCommitHash);
247
- const valdiWidgetsImport = getValdiWidgetsImport(argv, valdiWidgetsCommitHash);
330
+ const valdiImport = getValdiImport(argv, valdiReleaseTag);
331
+ const valdiWidgetsImport = getValdiWidgetsImport(argv, valdiWidgetsReleaseTag);
248
332
 
249
333
  // Creating basic config files and Hello World application
250
334
  console.log(wrapInColor('Initializing config files...', ANSI_COLORS.BLUE_COLOR));
@@ -293,16 +377,24 @@ export const builder = (yargs: Argv<CommandParameters>) => {
293
377
  })
294
378
  .option('valdiImport', {
295
379
  describe:
296
- 'Path to a local checkout of the Valdi repo. If not specified, uses the latest release from GitHub.',
380
+ 'Path to a local checkout of the Valdi repo. If not specified, uses the pinned release (see valdiVersion).',
297
381
  type: 'string',
298
382
  alias: 'l',
299
383
  })
300
384
  .option('valdiWidgetsImport', {
301
385
  describe:
302
- 'Path to a local checkout of the Valdi_Widgets repo. If not specified, uses the latest release from GitHub.',
386
+ 'Path to a local checkout of the Valdi_Widgets repo. If not specified, uses the pinned release (see valdiWidgetsVersion).',
303
387
  type: 'string',
304
388
  alias: 'w',
305
389
  })
390
+ .option('valdiVersion', {
391
+ describe: `Valdi release tag to use (e.g. v1.0.0). Use "latest" to fetch the GitHub latest release. Default: ${DEFAULT_VALDI_RELEASE_TAG}`,
392
+ type: 'string',
393
+ })
394
+ .option('valdiWidgetsVersion', {
395
+ describe: `Valdi_Widgets release tag to use. Use "latest" to fetch the GitHub latest release. Default: ${DEFAULT_VALDI_WIDGETS_RELEASE_TAG}`,
396
+ type: 'string',
397
+ })
306
398
  .option('skipProjectsync', {
307
399
  describe: 'Skip projectsync for testing purposes',
308
400
  type: 'boolean',