@snap/valdi 1.0.11 → 1.1.5
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/.metadata/.bazelrc.template +6 -5
- package/.metadata/MODULE.bazel.template +62 -0
- package/bundled-skills/registry.json +13 -2
- package/bundled-skills/skills/valdi-animations/skill.md +197 -0
- package/bundled-skills/skills/valdi-async/tests/src/reference.tsx +1 -1
- package/bundled-skills/skills/valdi-component-dedup/skill.md +263 -0
- package/bundled-skills/skills/valdi-component-tests/tests/test/ProfileCardTest.spec.tsx +1 -1
- package/bundled-skills/skills/valdi-custom-view/skill.md +43 -1
- package/bundled-skills/skills/valdi-error-handling/skill.md +238 -0
- package/bundled-skills/skills/valdi-gestures/skill.md +198 -0
- package/bundled-skills/skills/valdi-inspect/skill.md +19 -1
- package/bundled-skills/skills/valdi-localization/skill.md +142 -0
- package/bundled-skills/skills/valdi-mock-breakdown/skill.md +327 -0
- package/bundled-skills/skills/valdi-mock-iterate/skill.md +216 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/diff.py +392 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/extract_colors.py +277 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/find_icon.py +303 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/snapshot-and-diff.sh +53 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/test_diff.py +207 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/test_extract_colors.py +167 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/test_find_icon.py +188 -0
- package/bundled-skills/skills/valdi-mock-to-module/scripts/watch-and-diff.sh +181 -0
- package/bundled-skills/skills/valdi-mock-to-module/skill.md +451 -0
- package/bundled-skills/skills/valdi-navigation/skill.md +187 -0
- package/bundled-skills/skills/valdi-overview/skill.md +7 -4
- package/bundled-skills/skills/valdi-perf/skill.md +223 -0
- package/bundled-skills/skills/valdi-polyglot-module/skill.md +80 -0
- package/bundled-skills/skills/valdi-preview/skill.md +303 -0
- package/bundled-skills/skills/valdi-setup/skill.md +55 -5
- package/bundled-skills/skills/valdi-testing/skill.md +23 -7
- package/bundled-skills/skills/valdi-tsx/skill.md +69 -5
- package/bundled-skills/skills/valdi-tsx/tests/src/reference.tsx +29 -5
- package/dist/commands/agentCheck.js +184 -0
- package/dist/commands/analyze.js +16 -0
- package/dist/commands/analyze_commands/duplicates.js +134 -0
- package/dist/commands/bootstrap.js +57 -73
- package/dist/commands/doctor.js +40 -203
- package/dist/commands/hotreload.js +15 -0
- package/dist/commands/inspect_commands/snapshot.js +71 -6
- package/dist/commands/inspect_commands/snapshot.spec.js +105 -0
- package/dist/commands/preview.js +81 -0
- package/dist/commands/projectsync.js +9 -7
- package/dist/commands/skills_commands/install.js +6 -1
- package/dist/commands/skills_commands/update.js +2 -1
- package/dist/core/analyze/clusterer.js +150 -0
- package/dist/core/analyze/fingerprint.js +2 -0
- package/dist/core/analyze/knownPatterns.js +76 -0
- package/dist/core/analyze/parser.js +177 -0
- package/dist/core/analyze/reporter.js +83 -0
- package/dist/core/analyze/scanner.js +172 -0
- package/dist/core/analyze/similarity.js +56 -0
- package/dist/core/constants.js +1 -1
- package/dist/core/packageFiles.spec.js +1 -1
- package/dist/preview/previewGenerator.js +240 -0
- package/dist/preview/previewGenerator.spec.js +203 -0
- package/dist/preview/viewModelParser.js +86 -0
- package/dist/preview/viewModelParser.spec.js +171 -0
- package/dist/setup/DevSetupHelper.js +0 -51
- package/dist/setup/linuxSetup.js +0 -2
- package/dist/setup/macOSSetup.js +1 -4
- package/dist/utils/BazelClient.js +15 -0
- package/dist/utils/daemonClient.js +3 -1
- package/dist/utils/skillsAdapters.js +86 -5
- package/dist/utils/skillsAdapters.spec.js +60 -0
- package/dist/utils/skillsRegistry.js +8 -0
- package/package.json +16 -8
- package/.metadata/WORKSPACE.template +0 -57
- package/dist/setup/versions.js +0 -8
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.printHumanReport = printHumanReport;
|
|
4
|
+
exports.formatJsonReport = formatJsonReport;
|
|
5
|
+
const constants_1 = require("../constants");
|
|
6
|
+
const logUtils_1 = require("../../utils/logUtils");
|
|
7
|
+
function printHumanReport(report, verbose) {
|
|
8
|
+
console.log((0, logUtils_1.wrapInColor)('Valdi Component Duplication Report', constants_1.ANSI_COLORS.BLUE_COLOR));
|
|
9
|
+
console.log('='.repeat(50));
|
|
10
|
+
console.log();
|
|
11
|
+
const { scanStats } = report;
|
|
12
|
+
console.log(`Scanned ${(0, logUtils_1.wrapInColor)(String(scanStats.filesScanned), constants_1.ANSI_COLORS.GREEN_COLOR)} files across ${(0, logUtils_1.wrapInColor)(String(scanStats.modulesScanned), constants_1.ANSI_COLORS.GREEN_COLOR)} modules in ${scanStats.durationMs}ms`);
|
|
13
|
+
console.log(`Scan roots: ${report.config.scanRoots.join(', ')}`);
|
|
14
|
+
console.log(`Shared libraries: ${report.config.sharedLibNames.join(', ') || '(none configured)'}`);
|
|
15
|
+
console.log();
|
|
16
|
+
if (report.knownPatterns.length > 0) {
|
|
17
|
+
console.log((0, logUtils_1.wrapInColor)('Known Patterns', constants_1.ANSI_COLORS.BLUE_COLOR));
|
|
18
|
+
console.log('-'.repeat(30));
|
|
19
|
+
for (const pattern of report.knownPatterns) {
|
|
20
|
+
printKnownPattern(pattern, verbose);
|
|
21
|
+
}
|
|
22
|
+
console.log();
|
|
23
|
+
}
|
|
24
|
+
if (report.genericClusters.length > 0) {
|
|
25
|
+
console.log((0, logUtils_1.wrapInColor)('Similar Component Clusters', constants_1.ANSI_COLORS.BLUE_COLOR));
|
|
26
|
+
console.log('-'.repeat(30));
|
|
27
|
+
const clustersToShow = verbose ? report.genericClusters : report.genericClusters.slice(0, 10);
|
|
28
|
+
for (const cluster of clustersToShow) {
|
|
29
|
+
printCluster(cluster, verbose);
|
|
30
|
+
}
|
|
31
|
+
if (!verbose && report.genericClusters.length > 10) {
|
|
32
|
+
console.log((0, logUtils_1.wrapInColor)(` ... and ${report.genericClusters.length - 10} more clusters (use --verbose to see all)`, constants_1.ANSI_COLORS.GRAY_COLOR));
|
|
33
|
+
}
|
|
34
|
+
console.log();
|
|
35
|
+
}
|
|
36
|
+
console.log('='.repeat(50));
|
|
37
|
+
console.log((0, logUtils_1.wrapInColor)('Summary', constants_1.ANSI_COLORS.BLUE_COLOR));
|
|
38
|
+
console.log(` Known pattern matches: ${report.summary.totalKnownPatternMatches}`);
|
|
39
|
+
console.log(` Similar clusters: ${report.summary.totalClusters}`);
|
|
40
|
+
console.log(` Total duplicated files: ${report.summary.totalDuplicatedFiles}`);
|
|
41
|
+
}
|
|
42
|
+
function printKnownPattern(pattern, verbose) {
|
|
43
|
+
const severityColor = pattern.severity === 'HIGH'
|
|
44
|
+
? constants_1.ANSI_COLORS.RED_COLOR
|
|
45
|
+
: pattern.severity === 'MEDIUM'
|
|
46
|
+
? constants_1.ANSI_COLORS.YELLOW_COLOR
|
|
47
|
+
: constants_1.ANSI_COLORS.GRAY_COLOR;
|
|
48
|
+
console.log(` ${(0, logUtils_1.wrapInColor)(`[${pattern.severity}]`, severityColor)} ${pattern.patternName} — ${pattern.matchCount} matches`);
|
|
49
|
+
console.log(` ${pattern.recommendation}`);
|
|
50
|
+
if (pattern.existingImplementations.length > 0) {
|
|
51
|
+
console.log(` Existing implementations:`);
|
|
52
|
+
for (const impl of pattern.existingImplementations) {
|
|
53
|
+
console.log(` ${(0, logUtils_1.wrapInColor)('→', constants_1.ANSI_COLORS.GREEN_COLOR)} ${impl}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (verbose) {
|
|
57
|
+
const filesToShow = pattern.files.slice(0, 20);
|
|
58
|
+
for (const file of filesToShow) {
|
|
59
|
+
console.log(` ${(0, logUtils_1.wrapInColor)('·', constants_1.ANSI_COLORS.GRAY_COLOR)} ${file}`);
|
|
60
|
+
}
|
|
61
|
+
if (pattern.files.length > 20) {
|
|
62
|
+
console.log((0, logUtils_1.wrapInColor)(` ... and ${pattern.files.length - 20} more`, constants_1.ANSI_COLORS.GRAY_COLOR));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
console.log();
|
|
66
|
+
}
|
|
67
|
+
function printCluster(cluster, verbose) {
|
|
68
|
+
console.log(` Cluster #${cluster.id} (${cluster.files.length} files, avg similarity: ${(cluster.avgSimilarity * 100).toFixed(0)}%)`);
|
|
69
|
+
console.log(` ${cluster.suggestedExtraction}`);
|
|
70
|
+
if (cluster.sharedViewModelProps.length > 0) {
|
|
71
|
+
console.log(` Shared ViewModel props: ${cluster.sharedViewModelProps.join(', ')}`);
|
|
72
|
+
}
|
|
73
|
+
for (const file of cluster.files) {
|
|
74
|
+
console.log(` ${(0, logUtils_1.wrapInColor)('·', constants_1.ANSI_COLORS.GRAY_COLOR)} ${file.module}/${file.className} — ${file.path}`);
|
|
75
|
+
}
|
|
76
|
+
if (verbose && cluster.sharedImports.length > 0) {
|
|
77
|
+
console.log(` Shared imports: ${cluster.sharedImports.slice(0, 5).join(', ')}`);
|
|
78
|
+
}
|
|
79
|
+
console.log();
|
|
80
|
+
}
|
|
81
|
+
function formatJsonReport(report) {
|
|
82
|
+
return JSON.stringify(report, null, 2);
|
|
83
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveScanConfig = resolveScanConfig;
|
|
4
|
+
exports.discoverTsxFiles = discoverTsxFiles;
|
|
5
|
+
exports.extractModuleName = extractModuleName;
|
|
6
|
+
exports.isSharedLibFile = isSharedLibFile;
|
|
7
|
+
const tslib_1 = require("tslib");
|
|
8
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
9
|
+
const path = tslib_1.__importStar(require("path"));
|
|
10
|
+
const INTERNAL_SCAN_ROOT = 'src/composer_modules/src/composer';
|
|
11
|
+
const INTERNAL_SHARED_LIBS = ['coreui'];
|
|
12
|
+
const EXTERNAL_SHARED_LIBS = ['valdi_widgets'];
|
|
13
|
+
function resolveScanConfig(cwd, scanRootOverrides, sharedLibOverrides) {
|
|
14
|
+
if (scanRootOverrides && scanRootOverrides.length > 0) {
|
|
15
|
+
const resolvedRoots = scanRootOverrides.map(r => path.resolve(cwd, r));
|
|
16
|
+
const libNames = sharedLibOverrides ?? EXTERNAL_SHARED_LIBS;
|
|
17
|
+
const libPaths = libNames.map(name => discoverSharedLibPath(cwd, resolvedRoots, name));
|
|
18
|
+
return {
|
|
19
|
+
scanRoots: resolvedRoots,
|
|
20
|
+
sharedLibPaths: libPaths.filter((p) => p !== undefined),
|
|
21
|
+
sharedLibNames: libNames,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const internalRoot = path.join(cwd, INTERNAL_SCAN_ROOT);
|
|
25
|
+
if (fs.existsSync(internalRoot)) {
|
|
26
|
+
const libNames = sharedLibOverrides ?? INTERNAL_SHARED_LIBS;
|
|
27
|
+
const libPaths = libNames.map(name => discoverSharedLibPath(cwd, [internalRoot], name));
|
|
28
|
+
return {
|
|
29
|
+
scanRoots: [internalRoot],
|
|
30
|
+
sharedLibPaths: libPaths.filter((p) => p !== undefined),
|
|
31
|
+
sharedLibNames: libNames,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const srcDir = path.join(cwd, 'src');
|
|
35
|
+
if (fs.existsSync(srcDir)) {
|
|
36
|
+
const libNames = sharedLibOverrides ?? EXTERNAL_SHARED_LIBS;
|
|
37
|
+
const libPaths = libNames.map(name => discoverSharedLibPath(cwd, [srcDir], name));
|
|
38
|
+
return {
|
|
39
|
+
scanRoots: [srcDir],
|
|
40
|
+
sharedLibPaths: libPaths.filter((p) => p !== undefined),
|
|
41
|
+
sharedLibNames: libNames,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const libNames = sharedLibOverrides ?? EXTERNAL_SHARED_LIBS;
|
|
45
|
+
const libPaths = libNames.map(name => discoverSharedLibPath(cwd, [cwd], name));
|
|
46
|
+
return {
|
|
47
|
+
scanRoots: [cwd],
|
|
48
|
+
sharedLibPaths: libPaths.filter((p) => p !== undefined),
|
|
49
|
+
sharedLibNames: libNames,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function discoverSharedLibPath(workspaceRoot, scanRoots, libName) {
|
|
53
|
+
for (const root of scanRoots) {
|
|
54
|
+
const candidate = path.join(root, libName);
|
|
55
|
+
if (fs.existsSync(candidate))
|
|
56
|
+
return candidate;
|
|
57
|
+
}
|
|
58
|
+
const parentDir = path.dirname(workspaceRoot);
|
|
59
|
+
const siblingCandidates = [
|
|
60
|
+
libName,
|
|
61
|
+
libName
|
|
62
|
+
.split('_')
|
|
63
|
+
.map(s => s.charAt(0).toUpperCase() + s.slice(1))
|
|
64
|
+
.join('_'),
|
|
65
|
+
libName.replace(/_/g, '-'),
|
|
66
|
+
libName.replace(/_([a-z])/g, (_, c) => c.toUpperCase()),
|
|
67
|
+
];
|
|
68
|
+
for (const name of siblingCandidates) {
|
|
69
|
+
const candidate = path.join(parentDir, name);
|
|
70
|
+
if (fs.existsSync(candidate))
|
|
71
|
+
return candidate;
|
|
72
|
+
}
|
|
73
|
+
const bazelSymlinks = findBazelWorkspaceSymlinks(workspaceRoot);
|
|
74
|
+
for (const bazelDir of bazelSymlinks) {
|
|
75
|
+
const externalDir = path.join(bazelDir, 'external');
|
|
76
|
+
if (!fs.existsSync(externalDir))
|
|
77
|
+
continue;
|
|
78
|
+
try {
|
|
79
|
+
const entries = fs.readdirSync(externalDir);
|
|
80
|
+
for (const entry of entries) {
|
|
81
|
+
if (entry.includes(libName)) {
|
|
82
|
+
const candidate = path.join(externalDir, entry);
|
|
83
|
+
if (fs.statSync(candidate).isDirectory())
|
|
84
|
+
return candidate;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const nodeModules = path.join(workspaceRoot, 'node_modules', libName);
|
|
92
|
+
if (fs.existsSync(nodeModules))
|
|
93
|
+
return nodeModules;
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
function findBazelWorkspaceSymlinks(workspaceRoot) {
|
|
97
|
+
const results = [];
|
|
98
|
+
try {
|
|
99
|
+
const entries = fs.readdirSync(workspaceRoot);
|
|
100
|
+
for (const entry of entries) {
|
|
101
|
+
if (entry.startsWith('bazel-') &&
|
|
102
|
+
!entry.startsWith('bazel-out') &&
|
|
103
|
+
!entry.startsWith('bazel-bin') &&
|
|
104
|
+
!entry.startsWith('bazel-testlogs')) {
|
|
105
|
+
const fullPath = path.join(workspaceRoot, entry);
|
|
106
|
+
try {
|
|
107
|
+
if (fs.lstatSync(fullPath).isSymbolicLink() || fs.statSync(fullPath).isDirectory()) {
|
|
108
|
+
results.push(fullPath);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
}
|
|
118
|
+
return results;
|
|
119
|
+
}
|
|
120
|
+
function discoverTsxFiles(config, moduleFilter) {
|
|
121
|
+
const files = [];
|
|
122
|
+
for (const root of config.scanRoots) {
|
|
123
|
+
if (!fs.existsSync(root))
|
|
124
|
+
continue;
|
|
125
|
+
walkDir(root, config, moduleFilter, files);
|
|
126
|
+
}
|
|
127
|
+
return files;
|
|
128
|
+
}
|
|
129
|
+
function walkDir(dir, config, moduleFilter, out) {
|
|
130
|
+
let entries;
|
|
131
|
+
try {
|
|
132
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
for (const entry of entries) {
|
|
138
|
+
const fullPath = path.join(dir, entry.name);
|
|
139
|
+
if (entry.isDirectory()) {
|
|
140
|
+
if (entry.name === 'node_modules' || entry.name === '.git' || entry.name === 'bazel-out')
|
|
141
|
+
continue;
|
|
142
|
+
if (moduleFilter) {
|
|
143
|
+
const moduleName = extractModuleName(fullPath, config);
|
|
144
|
+
if (moduleName && moduleName !== moduleFilter)
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
walkDir(fullPath, config, moduleFilter, out);
|
|
148
|
+
}
|
|
149
|
+
else if (entry.name.endsWith('.tsx') && !entry.name.endsWith('.spec.tsx') && !entry.name.endsWith('.test.tsx')) {
|
|
150
|
+
if (!isInTestDir(fullPath)) {
|
|
151
|
+
out.push(fullPath);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function isInTestDir(filePath) {
|
|
157
|
+
const parts = filePath.split(path.sep);
|
|
158
|
+
return parts.includes('test') || parts.includes('__tests__');
|
|
159
|
+
}
|
|
160
|
+
function extractModuleName(filePath, config) {
|
|
161
|
+
for (const root of config.scanRoots) {
|
|
162
|
+
if (filePath === root || filePath.startsWith(root + path.sep)) {
|
|
163
|
+
const relative = path.relative(root, filePath);
|
|
164
|
+
const firstSegment = relative.split(path.sep)[0];
|
|
165
|
+
return firstSegment;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
function isSharedLibFile(filePath, config) {
|
|
171
|
+
return config.sharedLibPaths.some(libPath => filePath === libPath || filePath.startsWith(libPath + path.sep));
|
|
172
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeSimilarity = computeSimilarity;
|
|
4
|
+
exports.jaccardSimilarity = jaccardSimilarity;
|
|
5
|
+
exports.lcsRatio = lcsRatio;
|
|
6
|
+
const VM_WEIGHT = 0.4;
|
|
7
|
+
const RENDER_WEIGHT = 0.35;
|
|
8
|
+
const IMPORT_WEIGHT = 0.25;
|
|
9
|
+
function computeSimilarity(a, b) {
|
|
10
|
+
const vmSim = jaccardSimilarity(a.viewModelProps, b.viewModelProps);
|
|
11
|
+
const renderSim = lcsRatio(a.renderElements, b.renderElements);
|
|
12
|
+
const importSim = jaccardSimilarity(a.importPaths, b.importPaths);
|
|
13
|
+
return VM_WEIGHT * vmSim + RENDER_WEIGHT * renderSim + IMPORT_WEIGHT * importSim;
|
|
14
|
+
}
|
|
15
|
+
function jaccardSimilarity(a, b) {
|
|
16
|
+
if (a.length === 0 && b.length === 0)
|
|
17
|
+
return 1;
|
|
18
|
+
if (a.length === 0 || b.length === 0)
|
|
19
|
+
return 0;
|
|
20
|
+
const setA = new Set(a);
|
|
21
|
+
const setB = new Set(b);
|
|
22
|
+
let intersection = 0;
|
|
23
|
+
for (const item of setA) {
|
|
24
|
+
if (setB.has(item))
|
|
25
|
+
intersection++;
|
|
26
|
+
}
|
|
27
|
+
const union = setA.size + setB.size - intersection;
|
|
28
|
+
return union === 0 ? 0 : intersection / union;
|
|
29
|
+
}
|
|
30
|
+
function lcsRatio(a, b) {
|
|
31
|
+
if (a.length === 0 && b.length === 0)
|
|
32
|
+
return 1;
|
|
33
|
+
if (a.length === 0 || b.length === 0)
|
|
34
|
+
return 0;
|
|
35
|
+
const lcsLen = lcsLength(a, b);
|
|
36
|
+
return (2 * lcsLen) / (a.length + b.length);
|
|
37
|
+
}
|
|
38
|
+
function lcsLength(a, b) {
|
|
39
|
+
const m = a.length;
|
|
40
|
+
const n = b.length;
|
|
41
|
+
let prev = new Array(n + 1).fill(0);
|
|
42
|
+
let curr = new Array(n + 1).fill(0);
|
|
43
|
+
for (let i = 1; i <= m; i++) {
|
|
44
|
+
for (let j = 1; j <= n; j++) {
|
|
45
|
+
if (a[i - 1] === b[j - 1]) {
|
|
46
|
+
curr[j] = prev[j - 1] + 1;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
curr[j] = Math.max(prev[j], curr[j - 1]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
[prev, curr] = [curr, prev];
|
|
53
|
+
curr.fill(0);
|
|
54
|
+
}
|
|
55
|
+
return prev[n];
|
|
56
|
+
}
|
package/dist/core/constants.js
CHANGED
|
@@ -31,7 +31,7 @@ var Architecture;
|
|
|
31
31
|
exports.ALL_ARCHITECTURES = [Architecture.ARMV7, Architecture.ARM64, Architecture.X86_64];
|
|
32
32
|
var TEMPLATE_BASE_PATHS;
|
|
33
33
|
(function (TEMPLATE_BASE_PATHS) {
|
|
34
|
-
TEMPLATE_BASE_PATHS["
|
|
34
|
+
TEMPLATE_BASE_PATHS["MODULE_BAZEL"] = "MODULE.bazel.template";
|
|
35
35
|
TEMPLATE_BASE_PATHS["BAZEL_RC"] = ".bazelrc.template";
|
|
36
36
|
TEMPLATE_BASE_PATHS["BAZEL_VERSION"] = ".bazelversion.template";
|
|
37
37
|
TEMPLATE_BASE_PATHS["USER_CONFIG"] = "config.yaml.template";
|
|
@@ -15,7 +15,7 @@ describe('npm package contents', () => {
|
|
|
15
15
|
});
|
|
16
16
|
it('includes .metadata templates for bootstrap config', () => {
|
|
17
17
|
expect(packedFiles).toContain('.metadata/config.yaml.template');
|
|
18
|
-
expect(packedFiles).toContain('.metadata/
|
|
18
|
+
expect(packedFiles).toContain('.metadata/MODULE.bazel.template');
|
|
19
19
|
});
|
|
20
20
|
it('includes .bootstrap templates for project scaffolding', () => {
|
|
21
21
|
expect(packedFiles).toContain('.bootstrap/');
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPreviewDir = getPreviewDir;
|
|
4
|
+
exports.generatePreviewFiles = generatePreviewFiles;
|
|
5
|
+
exports.findModuleDir = findModuleDir;
|
|
6
|
+
exports.getModuleNameFromImport = getModuleNameFromImport;
|
|
7
|
+
exports.readModuleName = readModuleName;
|
|
8
|
+
exports.isValdiWorkspace = isValdiWorkspace;
|
|
9
|
+
exports.computeModuleLabel = computeModuleLabel;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
12
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
13
|
+
const viewModelParser_1 = require("./viewModelParser");
|
|
14
|
+
const PREVIEW_DIR = 'apps/preview';
|
|
15
|
+
function getPreviewDir(workspaceRoot) {
|
|
16
|
+
return path_1.default.join(workspaceRoot, PREVIEW_DIR);
|
|
17
|
+
}
|
|
18
|
+
function generatePreviewFiles(config) {
|
|
19
|
+
const previewDir = getPreviewDir(config.workspaceRoot);
|
|
20
|
+
fs_1.default.mkdirSync(previewDir, { recursive: true });
|
|
21
|
+
fs_1.default.writeFileSync(path_1.default.join(previewDir, 'PreviewRoot.tsx'), generatePreviewTsx(config));
|
|
22
|
+
fs_1.default.writeFileSync(path_1.default.join(previewDir, 'BUILD.bazel'), generateBuildBazel(config));
|
|
23
|
+
fs_1.default.writeFileSync(path_1.default.join(previewDir, 'tsconfig.json'), generateTsconfig(config));
|
|
24
|
+
}
|
|
25
|
+
function generatePreviewTsx(config) {
|
|
26
|
+
const { viewModel, importPath } = config;
|
|
27
|
+
const { componentName, props } = viewModel;
|
|
28
|
+
const interactiveState = (0, viewModelParser_1.detectInteractiveState)(props);
|
|
29
|
+
const hasState = interactiveState.length > 0;
|
|
30
|
+
const lines = [];
|
|
31
|
+
if (hasState) {
|
|
32
|
+
lines.push("import { StatefulComponent } from 'valdi_core/src/Component';");
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
lines.push("import { Component } from 'valdi_core/src/Component';");
|
|
36
|
+
}
|
|
37
|
+
lines.push("import { Style } from 'valdi_core/src/Style';");
|
|
38
|
+
lines.push("import { View } from 'valdi_tsx/src/NativeTemplateElements';");
|
|
39
|
+
lines.push(`import { ${componentName} } from '${importPath}';`);
|
|
40
|
+
lines.push('');
|
|
41
|
+
lines.push('/**');
|
|
42
|
+
lines.push(' * @ViewModel');
|
|
43
|
+
lines.push(' * @ExportModel');
|
|
44
|
+
lines.push(' */');
|
|
45
|
+
lines.push('export interface ViewModel {}');
|
|
46
|
+
lines.push('');
|
|
47
|
+
lines.push('/**');
|
|
48
|
+
lines.push(' * @Context');
|
|
49
|
+
lines.push(' * @ExportModel');
|
|
50
|
+
lines.push(' */');
|
|
51
|
+
lines.push('export interface ComponentContext {}');
|
|
52
|
+
lines.push('');
|
|
53
|
+
if (hasState) {
|
|
54
|
+
lines.push('interface PreviewState {');
|
|
55
|
+
for (const s of interactiveState) {
|
|
56
|
+
lines.push(` ${s.stateName}: boolean;`);
|
|
57
|
+
}
|
|
58
|
+
lines.push('}');
|
|
59
|
+
lines.push('');
|
|
60
|
+
}
|
|
61
|
+
lines.push('/**');
|
|
62
|
+
lines.push(' * @Component');
|
|
63
|
+
lines.push(' * @ExportModel');
|
|
64
|
+
lines.push(' */');
|
|
65
|
+
if (hasState) {
|
|
66
|
+
lines.push('export class PreviewRoot extends StatefulComponent<ViewModel, PreviewState, ComponentContext> {');
|
|
67
|
+
lines.push(' state: PreviewState = {');
|
|
68
|
+
for (const s of interactiveState) {
|
|
69
|
+
lines.push(` ${s.stateName}: false,`);
|
|
70
|
+
}
|
|
71
|
+
lines.push(' };');
|
|
72
|
+
lines.push('');
|
|
73
|
+
for (const s of interactiveState) {
|
|
74
|
+
lines.push(` private ${s.callbackPropName}Handler = (val: boolean) => this.setState({ ${s.stateName}: val });`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
lines.push('export class PreviewRoot extends Component<ViewModel, ComponentContext> {');
|
|
79
|
+
}
|
|
80
|
+
lines.push('');
|
|
81
|
+
lines.push(' onRender(): void {');
|
|
82
|
+
lines.push(' <view style={styles.container}>');
|
|
83
|
+
lines.push(` <label value="${componentName}" font="system-bold 20" color="#000000" marginBottom={24} />`);
|
|
84
|
+
lines.push(` ${generateComponentJsx(componentName, props, interactiveState)}`);
|
|
85
|
+
lines.push(' </view>;');
|
|
86
|
+
lines.push(' }');
|
|
87
|
+
lines.push('}');
|
|
88
|
+
lines.push('');
|
|
89
|
+
lines.push('const styles = {');
|
|
90
|
+
lines.push(' container: new Style<View>({');
|
|
91
|
+
lines.push(" backgroundColor: '#ffffff',");
|
|
92
|
+
lines.push(" width: '100%',");
|
|
93
|
+
lines.push(" height: '100%',");
|
|
94
|
+
lines.push(" alignItems: 'center',");
|
|
95
|
+
lines.push(" justifyContent: 'center',");
|
|
96
|
+
lines.push(' padding: 24,');
|
|
97
|
+
lines.push(' }),');
|
|
98
|
+
lines.push('};');
|
|
99
|
+
lines.push('');
|
|
100
|
+
return lines.join('\n');
|
|
101
|
+
}
|
|
102
|
+
function generateComponentJsx(componentName, props, interactiveState) {
|
|
103
|
+
const stateMap = new Map();
|
|
104
|
+
for (const s of interactiveState) {
|
|
105
|
+
stateMap.set(s.boolPropName, s);
|
|
106
|
+
stateMap.set(s.callbackPropName, s);
|
|
107
|
+
}
|
|
108
|
+
const requiredProps = props.filter(p => !p.optional);
|
|
109
|
+
if (requiredProps.length === 0) {
|
|
110
|
+
return `<${componentName} />`;
|
|
111
|
+
}
|
|
112
|
+
const propAssignments = [];
|
|
113
|
+
for (const prop of requiredProps) {
|
|
114
|
+
const state = stateMap.get(prop.name);
|
|
115
|
+
if (state && prop.isBoolean) {
|
|
116
|
+
propAssignments.push(`${prop.name}={this.state.${state.stateName}}`);
|
|
117
|
+
}
|
|
118
|
+
else if (state && prop.isCallback) {
|
|
119
|
+
propAssignments.push(`${prop.name}={this.${state.callbackPropName}Handler}`);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
propAssignments.push(`${prop.name}={${(0, viewModelParser_1.sampleValueForType)(prop.typeString)}}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (propAssignments.length <= 2) {
|
|
126
|
+
return `<${componentName} ${propAssignments.join(' ')} />`;
|
|
127
|
+
}
|
|
128
|
+
const indentedProps = propAssignments.map(p => ` ${p}`).join('\n');
|
|
129
|
+
return `<${componentName}\n${indentedProps}\n />`;
|
|
130
|
+
}
|
|
131
|
+
function generateBuildBazel(config) {
|
|
132
|
+
const prefix = config.isValdiRepo ? '' : '@valdi';
|
|
133
|
+
return `load("${prefix}//bzl/valdi:valdi_application.bzl", "valdi_application")
|
|
134
|
+
load("${prefix}//bzl/valdi:valdi_module.bzl", "valdi_module")
|
|
135
|
+
|
|
136
|
+
valdi_module(
|
|
137
|
+
name = "preview",
|
|
138
|
+
srcs = glob([
|
|
139
|
+
"**/*.ts",
|
|
140
|
+
"**/*.tsx",
|
|
141
|
+
]),
|
|
142
|
+
visibility = ["//visibility:public"],
|
|
143
|
+
deps = [
|
|
144
|
+
"${prefix}//src/valdi_modules/src/valdi/valdi_core",
|
|
145
|
+
"${prefix}//src/valdi_modules/src/valdi/valdi_tsx",
|
|
146
|
+
"${config.moduleLabel}",
|
|
147
|
+
],
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
valdi_application(
|
|
151
|
+
name = "preview_app",
|
|
152
|
+
root_component_path = "PreviewRoot@preview/PreviewRoot",
|
|
153
|
+
title = "Valdi Preview",
|
|
154
|
+
deps = [":preview"],
|
|
155
|
+
)
|
|
156
|
+
`;
|
|
157
|
+
}
|
|
158
|
+
function generateTsconfig(config) {
|
|
159
|
+
const previewDir = getPreviewDir(config.workspaceRoot);
|
|
160
|
+
if (config.isValdiRepo) {
|
|
161
|
+
const valdiModulesDir = path_1.default.join(config.workspaceRoot, 'src/valdi_modules/src/valdi');
|
|
162
|
+
const relToValdiModules = path_1.default.relative(previewDir, valdiModulesDir);
|
|
163
|
+
const moduleDir = findModuleDir(config.componentFilePath);
|
|
164
|
+
const moduleName = getModuleNameFromImport(config.importPath);
|
|
165
|
+
const relToModule = path_1.default.relative(previewDir, moduleDir);
|
|
166
|
+
const paths = {
|
|
167
|
+
'preview/*': ['./*'],
|
|
168
|
+
'valdi_tsx/*': [`${relToValdiModules}/valdi_tsx/*`],
|
|
169
|
+
'valdi_core/*': [`${relToValdiModules}/valdi_core/*`],
|
|
170
|
+
'valdi_core': [`${relToValdiModules}/valdi_core/src/tslib.d.ts`],
|
|
171
|
+
};
|
|
172
|
+
paths[`${moduleName}/*`] = [`${relToModule}/*`];
|
|
173
|
+
return JSON.stringify({
|
|
174
|
+
extends: '../../modules/_configs/base.tsconfig.json',
|
|
175
|
+
compilerOptions: {
|
|
176
|
+
types: ['../../modules/types/Long', '../../modules/types/globals'],
|
|
177
|
+
paths,
|
|
178
|
+
},
|
|
179
|
+
}, null, 2) + '\n';
|
|
180
|
+
}
|
|
181
|
+
const baseConfigRel = findBaseConfigRelativePath(config.workspaceRoot, previewDir);
|
|
182
|
+
return JSON.stringify({
|
|
183
|
+
extends: baseConfigRel,
|
|
184
|
+
compilerOptions: {
|
|
185
|
+
paths: {
|
|
186
|
+
'preview/*': ['./*'],
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
}, null, 2) + '\n';
|
|
190
|
+
}
|
|
191
|
+
function findModuleDir(componentFilePath) {
|
|
192
|
+
let dir = path_1.default.dirname(componentFilePath);
|
|
193
|
+
while (dir !== path_1.default.dirname(dir)) {
|
|
194
|
+
const buildFile = path_1.default.join(dir, 'BUILD.bazel');
|
|
195
|
+
if (fs_1.default.existsSync(buildFile)) {
|
|
196
|
+
const content = fs_1.default.readFileSync(buildFile, 'utf8');
|
|
197
|
+
if (/valdi_module\s*\(/.test(content)) {
|
|
198
|
+
return dir;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
dir = path_1.default.dirname(dir);
|
|
202
|
+
}
|
|
203
|
+
throw new Error(`Could not find a BUILD.bazel with valdi_module() above ${componentFilePath}`);
|
|
204
|
+
}
|
|
205
|
+
function getModuleNameFromImport(importPath) {
|
|
206
|
+
return importPath.split('/')[0];
|
|
207
|
+
}
|
|
208
|
+
function readModuleName(moduleDir) {
|
|
209
|
+
const buildContent = fs_1.default.readFileSync(path_1.default.join(moduleDir, 'BUILD.bazel'), 'utf8');
|
|
210
|
+
const nameMatch = buildContent.match(/valdi_module\s*\([^)]*name\s*=\s*"(\w+)"/s);
|
|
211
|
+
if (!nameMatch) {
|
|
212
|
+
throw new Error(`Could not find valdi_module name in ${moduleDir}/BUILD.bazel`);
|
|
213
|
+
}
|
|
214
|
+
return nameMatch[1];
|
|
215
|
+
}
|
|
216
|
+
function isValdiWorkspace(workspaceRoot) {
|
|
217
|
+
const wsFile = path_1.default.join(workspaceRoot, 'WORKSPACE');
|
|
218
|
+
if (!fs_1.default.existsSync(wsFile))
|
|
219
|
+
return false;
|
|
220
|
+
const content = fs_1.default.readFileSync(wsFile, 'utf8');
|
|
221
|
+
return /workspace\s*\(\s*name\s*=\s*"valdi"\s*\)/.test(content);
|
|
222
|
+
}
|
|
223
|
+
function computeModuleLabel(moduleDir, workspaceRoot) {
|
|
224
|
+
const relPath = path_1.default.relative(workspaceRoot, moduleDir);
|
|
225
|
+
const moduleName = readModuleName(moduleDir);
|
|
226
|
+
return `//${relPath}:${moduleName}`;
|
|
227
|
+
}
|
|
228
|
+
function findBaseConfigRelativePath(workspaceRoot, fromDir) {
|
|
229
|
+
const candidates = [
|
|
230
|
+
'valdi_modules/_configs/base.tsconfig.json',
|
|
231
|
+
'modules/_configs/base.tsconfig.json',
|
|
232
|
+
];
|
|
233
|
+
for (const candidate of candidates) {
|
|
234
|
+
const fullPath = path_1.default.join(workspaceRoot, candidate);
|
|
235
|
+
if (fs_1.default.existsSync(fullPath)) {
|
|
236
|
+
return path_1.default.relative(fromDir, fullPath);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return '../../valdi_modules/_configs/base.tsconfig.json';
|
|
240
|
+
}
|