@snap/valdi 1.0.1
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/.bazelignore +2 -0
- package/.bootstrap/apps/cli_application/AGENTS.md.template +215 -0
- package/.bootstrap/apps/cli_application/BUILD.bazel.template +7 -0
- package/.bootstrap/apps/cli_application/modules/{{MODULE_NAME}}/BUILD.bazel.template +11 -0
- package/.bootstrap/apps/cli_application/modules/{{MODULE_NAME}}/src/index.ts +1 -0
- package/.bootstrap/apps/ui_application/.eslint.rc +27 -0
- package/.bootstrap/apps/ui_application/AGENTS.md.template +202 -0
- package/.bootstrap/apps/ui_application/BUILD.bazel.template +19 -0
- package/.bootstrap/apps/ui_application/modules/{{MODULE_NAME}}/BUILD.bazel.template +21 -0
- package/.bootstrap/apps/ui_application/modules/{{MODULE_NAME}}/res/valdi@3x.png +0 -0
- package/.bootstrap/apps/ui_application/modules/{{MODULE_NAME}}/src/App.tsx +133 -0
- package/.bootstrap/apps/ui_application/package.json +24 -0
- package/.bootstrap/modules/cpp_bridge_module/BUILD.bazel.template +21 -0
- package/.bootstrap/modules/cpp_bridge_module/cpp/CalculatorModuleFactory.cpp.template +29 -0
- package/.bootstrap/modules/cpp_bridge_module/src/Calculator.d.ts +1 -0
- package/.bootstrap/modules/ios_android_bridge_module/BUILD.bazel.template +39 -0
- package/.bootstrap/modules/ios_android_bridge_module/android/CalculatorModuleFactory.kt.template +18 -0
- package/.bootstrap/modules/ios_android_bridge_module/ios/SCCCalculatorModuleFactory.m.template +31 -0
- package/.bootstrap/modules/ios_android_bridge_module/src/Calculator.d.ts +4 -0
- package/.bootstrap/modules/ios_android_view_module/BUILD.bazel.template +41 -0
- package/.bootstrap/modules/ios_android_view_module/android/CircleView.kt.template +42 -0
- package/.bootstrap/modules/ios_android_view_module/android/CircleViewAttributesBinder.kt.template +60 -0
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.h +6 -0
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.m +82 -0
- package/.bootstrap/modules/ios_android_view_module/src/Circle.tsx.template +15 -0
- package/.bootstrap/modules/ui_component/BUILD.bazel.template +34 -0
- package/.bootstrap/modules/ui_component/src/NewModuleComponent.tsx +45 -0
- package/.config/copyconfig.json +10 -0
- package/.editorconfig +21 -0
- package/.eslintignore +1 -0
- package/.eslintrc.js +72 -0
- package/.metadata/.bazelrc.template +91 -0
- package/.metadata/.bazelversion.template +1 -0
- package/.metadata/.eslintrc.js.template +27 -0
- package/.metadata/.gitignore.template +4 -0
- package/.metadata/.prettierrc.json.template +7 -0
- package/.metadata/.watchmanconfig.template +8 -0
- package/.metadata/README.md.template +31 -0
- package/.metadata/WORKSPACE.template +57 -0
- package/.metadata/config.yaml.template +1 -0
- package/.metadata/package.json.template +23 -0
- package/.prettierignore +2 -0
- package/.prettierrc.json +7 -0
- package/README.md +123 -0
- package/dist/commands/bootstrap.js +233 -0
- package/dist/commands/build.js +67 -0
- package/dist/commands/devSetup.js +18 -0
- package/dist/commands/doctor.js +892 -0
- package/dist/commands/export.js +84 -0
- package/dist/commands/hotreload.js +89 -0
- package/dist/commands/install.js +71 -0
- package/dist/commands/lint.js +18 -0
- package/dist/commands/lint_commands/lintCheck.js +31 -0
- package/dist/commands/lint_commands/lintFormat.js +31 -0
- package/dist/commands/log.js +84 -0
- package/dist/commands/newModule.js +208 -0
- package/dist/commands/package.js +66 -0
- package/dist/commands/projectsync.js +316 -0
- package/dist/commands/test.js +54 -0
- package/dist/core/constants.js +76 -0
- package/dist/core/errors.js +12 -0
- package/dist/index.js +22 -0
- package/dist/setup/DevSetupHelper.js +146 -0
- package/dist/setup/linuxSetup.js +31 -0
- package/dist/setup/macOSSetup.js +35 -0
- package/dist/setup/setupEntryPoint.js +56 -0
- package/dist/setup/versions.js +8 -0
- package/dist/utils/ArgumentsResolver.js +23 -0
- package/dist/utils/BazelClient.js +219 -0
- package/dist/utils/Digraph.js +71 -0
- package/dist/utils/LoadingIndicator.js +38 -0
- package/dist/utils/applicationUtils.js +201 -0
- package/dist/utils/buildInfo.js +60 -0
- package/dist/utils/byString.js +6 -0
- package/dist/utils/cliUtils.js +93 -0
- package/dist/utils/copyBootstrapFiles.js +26 -0
- package/dist/utils/deviceUtils.js +72 -0
- package/dist/utils/errorUtils.js +55 -0
- package/dist/utils/fileUtils.js +204 -0
- package/dist/utils/githubUtils.js +8 -0
- package/dist/utils/jsonUtils.js +28 -0
- package/dist/utils/lintUtils.js +149 -0
- package/dist/utils/logUtils.js +8 -0
- package/dist/utils/pathUtils.js +12 -0
- package/dist/utils/stringUtils.js +17 -0
- package/dist/utils/stringUtils.spec.js +18 -0
- package/dist/utils/tempDir.js +26 -0
- package/dist/utils/zipUtils.js +16 -0
- package/package.json +51 -0
- package/prettier.config.js +4 -0
- package/spec/support/jasmine.json +6 -0
- package/src/commands/bootstrap.ts +317 -0
- package/src/commands/build.ts +89 -0
- package/src/commands/devSetup.ts +17 -0
- package/src/commands/doctor.ts +1422 -0
- package/src/commands/export.ts +131 -0
- package/src/commands/hotreload.ts +108 -0
- package/src/commands/install.ts +93 -0
- package/src/commands/lint.ts +15 -0
- package/src/commands/lint_commands/lintCheck.ts +53 -0
- package/src/commands/lint_commands/lintFormat.ts +49 -0
- package/src/commands/log.ts +108 -0
- package/src/commands/newModule.ts +287 -0
- package/src/commands/package.ts +102 -0
- package/src/commands/projectsync.ts +488 -0
- package/src/commands/test.ts +68 -0
- package/src/core/constants.ts +95 -0
- package/src/core/errors.ts +10 -0
- package/src/index.ts +22 -0
- package/src/setup/DevSetupHelper.ts +188 -0
- package/src/setup/linuxSetup.ts +38 -0
- package/src/setup/macOSSetup.ts +42 -0
- package/src/setup/setupEntryPoint.ts +64 -0
- package/src/setup/versions.ts +8 -0
- package/src/utils/ArgumentsResolver.ts +23 -0
- package/src/utils/BazelClient.ts +285 -0
- package/src/utils/Digraph.ts +81 -0
- package/src/utils/LoadingIndicator.ts +43 -0
- package/src/utils/applicationUtils.ts +276 -0
- package/src/utils/buildInfo.ts +99 -0
- package/src/utils/byString.ts +3 -0
- package/src/utils/cliUtils.ts +112 -0
- package/src/utils/copyBootstrapFiles.ts +30 -0
- package/src/utils/deviceUtils.ts +76 -0
- package/src/utils/errorUtils.ts +63 -0
- package/src/utils/fileUtils.ts +257 -0
- package/src/utils/githubUtils.ts +6 -0
- package/src/utils/jsonUtils.ts +30 -0
- package/src/utils/lintUtils.ts +190 -0
- package/src/utils/logUtils.ts +9 -0
- package/src/utils/pathUtils.ts +12 -0
- package/src/utils/stringUtils.spec.ts +19 -0
- package/src/utils/stringUtils.ts +15 -0
- package/src/utils/tempDir.ts +24 -0
- package/src/utils/zipUtils.ts +14 -0
- package/test/ValdiSmokeTest.ts +278 -0
- package/test/helpers/AsyncHelpers.ts +113 -0
- package/test/helpers/CommandHelpers.ts +37 -0
- package/test/helpers/ProjectHelpers.ts +153 -0
- package/test/helpers/StreamHelpers.ts +37 -0
- package/test/helpers/TypeScriptClient.ts +217 -0
- package/tsconfig.dist.json +8 -0
- package/tsconfig.json +42 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
export function makeTempDir(): string {
|
|
7
|
+
return path.join(os.tmpdir(), crypto.randomUUID());
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function withTempDir<T>(receiveTempDir: (tempDir: string) => Promise<T>): Promise<T> {
|
|
11
|
+
const tempDir = makeTempDir();
|
|
12
|
+
|
|
13
|
+
fs.mkdirSync(tempDir);
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
return await receiveTempDir(tempDir);
|
|
17
|
+
} finally {
|
|
18
|
+
try {
|
|
19
|
+
fs.rmdirSync(tempDir);
|
|
20
|
+
} catch {
|
|
21
|
+
// no-op
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import extract from 'extract-zip';
|
|
4
|
+
|
|
5
|
+
export async function decompressTo(inputFilePath: string, outputFilePath: string): Promise<void> {
|
|
6
|
+
const archive = path.resolve(inputFilePath);
|
|
7
|
+
const destination = path.resolve(outputFilePath);
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
await extract(archive, { dir: destination });
|
|
11
|
+
} catch (error: unknown) {
|
|
12
|
+
throw new Error(`Failed to extract “${archive}” → “${destination}”: ${(error as Error).message}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import { mkdir, unlink } from 'node:fs/promises';
|
|
4
|
+
import { basename, join } from 'node:path';
|
|
5
|
+
import { afterEach, beforeEach, describe, it } from 'node:test';
|
|
6
|
+
import { lookForOutput } from './helpers/AsyncHelpers';
|
|
7
|
+
import { run } from './helpers/CommandHelpers';
|
|
8
|
+
import {
|
|
9
|
+
atBeginning,
|
|
10
|
+
createProject,
|
|
11
|
+
findDefinitionForTokenInFile,
|
|
12
|
+
insertLineAfter,
|
|
13
|
+
insertLineBefore,
|
|
14
|
+
lineMatches,
|
|
15
|
+
} from './helpers/ProjectHelpers';
|
|
16
|
+
import { type ProjectConfig } from './helpers/ProjectHelpers';
|
|
17
|
+
import { type TSServerClient, createClient, lineAndOffset } from './helpers/TypeScriptClient';
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
const PROJECT_ROOT = process.env['PROJECT_ROOT']!;
|
|
21
|
+
const PROJECT_NAME = process.env['PROJECT_NAME'] ?? basename(PROJECT_ROOT);
|
|
22
|
+
|
|
23
|
+
// eslint-disable-next-line unicorn/prefer-module
|
|
24
|
+
const runtimeDir = __dirname;
|
|
25
|
+
|
|
26
|
+
const OPEN_SOURCE_DIR = process.env['OPEN_SOURCE_DIR']!;
|
|
27
|
+
|
|
28
|
+
void describe('valdi ui app', () => {
|
|
29
|
+
let client: TSServerClient;
|
|
30
|
+
let project: ProjectConfig;
|
|
31
|
+
|
|
32
|
+
beforeEach(async () => {
|
|
33
|
+
await bootstrap(PROJECT_ROOT, PROJECT_NAME, 'ui_application');
|
|
34
|
+
// Create a tsserver client to interact with tsserver
|
|
35
|
+
client = createClient(runtimeDir);
|
|
36
|
+
// Create the project instance which is a wrapper to find and modify files in valdi modules
|
|
37
|
+
project = createProject(PROJECT_NAME, PROJECT_ROOT);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(async () => {
|
|
41
|
+
// Shuts down the tsserver instance for this test
|
|
42
|
+
await client.disconnect();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
void describe('cli', () => {
|
|
46
|
+
void it('build ios', async () => {
|
|
47
|
+
const task = run('valdi', ['build', 'ios']);
|
|
48
|
+
await lookForOutput({ stderr: [/Build completed successfully, \d+ total actions?/] }, task);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
void it('build macos', async () => {
|
|
52
|
+
const task = run('valdi', ['build', 'macos']);
|
|
53
|
+
await lookForOutput({ stderr: [/Build completed successfully, \d+ total actions?/] }, task);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
void it('build android', { skip: true }, async () => {
|
|
57
|
+
const task = run('valdi', ['build', 'android']);
|
|
58
|
+
await lookForOutput({ stderr: [/Build completed successfully, \d+ total actions?/] }, task);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
void it('valdi doctor', async () => {
|
|
62
|
+
const task = run('valdi', ['doctor']);
|
|
63
|
+
await lookForOutput({ stdout: [
|
|
64
|
+
'Valdi Doctor Report',
|
|
65
|
+
'Node.js installation',
|
|
66
|
+
'Bazel installation',
|
|
67
|
+
'Android installation',
|
|
68
|
+
'iOS development',
|
|
69
|
+
'Java installation',
|
|
70
|
+
'Development tools'
|
|
71
|
+
]}, task)
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Runs `valdi new_module`
|
|
76
|
+
* Updates `App.tsx` to import the module and use it
|
|
77
|
+
* Checks that the tsserver discovers the reference can be found
|
|
78
|
+
*/
|
|
79
|
+
void it('new_module ui_component', async () => {
|
|
80
|
+
const moduleName = 'testNewModule';
|
|
81
|
+
const task = run('valdi', ['new_module', '--skip-checks', '--template', 'ui_component', moduleName]);
|
|
82
|
+
|
|
83
|
+
await lookForOutput({ stdout: ['Success! New module directory:'] }, task);
|
|
84
|
+
|
|
85
|
+
// Add testNewModule bazel dependency for valdi_app
|
|
86
|
+
await project.updateFile(
|
|
87
|
+
'BUILD.bazel',
|
|
88
|
+
'valdi_app',
|
|
89
|
+
insertLineAfter(lineMatches('deps = ['), `\t\t"//modules/${moduleName}",\n`),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
// Insert new import statement for the new module at the top of the file
|
|
93
|
+
await project.updateFile(
|
|
94
|
+
'App.tsx',
|
|
95
|
+
'valdi_app',
|
|
96
|
+
insertLineBefore(() => true, `import { NewModuleComponent } from '${moduleName}/src/NewModuleComponent';\n`),
|
|
97
|
+
);
|
|
98
|
+
// Try to use the component in onRender
|
|
99
|
+
await project.updateFile(
|
|
100
|
+
'App.tsx',
|
|
101
|
+
'valdi_app',
|
|
102
|
+
insertLineBefore(lineMatches('</view>;'), '<NewModuleComponent />\n'),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
await run('valdi', ['projectsync'], { pipeOutput: true }).wait();
|
|
106
|
+
|
|
107
|
+
// We updated the file on the filesystem, the ts client needs to know to refresh the file
|
|
108
|
+
const file = await project.findFile('App.tsx', 'valdi_app');
|
|
109
|
+
await client.command('open', { file });
|
|
110
|
+
|
|
111
|
+
// Throws if it cannot find a valid TypeScript code reference
|
|
112
|
+
await findDefinitionForTokenInFile({ project, client }, 'NewModuleComponent />', 'App.tsx', 'valdi_app');
|
|
113
|
+
|
|
114
|
+
await client.command('close', { file });
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
void describe('typescript', () => {
|
|
119
|
+
void it('finds project references', async () => {
|
|
120
|
+
await client.command('open', { file: await project.findFile('App.tsx', 'valdi_app') });
|
|
121
|
+
|
|
122
|
+
await Promise.all(
|
|
123
|
+
['StatefulComponent', 'Label', 'Device', 'view>', 'style='].map(token =>
|
|
124
|
+
findDefinitionForTokenInFile({ client, project }, token, 'App.tsx', 'valdi_app'),
|
|
125
|
+
),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
await client.command('close', { file: await project.findFile('App.tsx', 'valdi_app') });
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Check for VSCode completions when typing "<l" inside the App `onRender` method.
|
|
133
|
+
*
|
|
134
|
+
* It should offer `<label` and `<layout`.
|
|
135
|
+
*/
|
|
136
|
+
void it('finds code completions', async () => {
|
|
137
|
+
const file = await project.findFile('App.tsx', 'valdi_app');
|
|
138
|
+
await client.command('open', { file });
|
|
139
|
+
await client.command('reload', { file });
|
|
140
|
+
|
|
141
|
+
const appSource = await project.findFile('App.tsx');
|
|
142
|
+
|
|
143
|
+
// Look for the whole definition block of onRender by asking tsserver for the definition
|
|
144
|
+
const view = await lineAndOffset('onRender', appSource);
|
|
145
|
+
const onRenderDef = (await client.command('definition', { file: appSource, ...view })) as {
|
|
146
|
+
body?: { contextEnd: { line: number; offset: number } }[];
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const defInfo = onRenderDef.body![0]!;
|
|
150
|
+
|
|
151
|
+
// Insert the '<' character before the ending "}" of onRender
|
|
152
|
+
const insertionLocation = {
|
|
153
|
+
file: appSource,
|
|
154
|
+
line: defInfo.contextEnd.line,
|
|
155
|
+
endLine: defInfo.contextEnd.line,
|
|
156
|
+
offset: defInfo.contextEnd.offset - 2,
|
|
157
|
+
endOffset: defInfo.contextEnd.offset,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// Tell tsserver to update its representatin of the file
|
|
161
|
+
await client.command('change', { insertString: '<l', ...insertionLocation });
|
|
162
|
+
|
|
163
|
+
// Ask for the completion info at the place the '<l' was inserted
|
|
164
|
+
const info = (await client.command('completionInfo', {
|
|
165
|
+
file: appSource,
|
|
166
|
+
...defInfo.contextEnd,
|
|
167
|
+
offset: defInfo.contextEnd.offset,
|
|
168
|
+
prefix: 'l',
|
|
169
|
+
})) as { body?: { entries: { name: string }[] } };
|
|
170
|
+
|
|
171
|
+
// We expect certain items to be available as completions
|
|
172
|
+
const completionsExpected = ['label', 'layout'];
|
|
173
|
+
const missing = completionsExpected.filter(
|
|
174
|
+
item => info.body!.entries.some(({ name }) => name === item) === undefined,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
if (missing.length > 0) {
|
|
178
|
+
throw new Error('missing completions: ' + missing.join(', '));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
await client.command('close', { file });
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
void it('has localized strings', async () => {
|
|
185
|
+
await project.updateFile(
|
|
186
|
+
'BUILD.bazel',
|
|
187
|
+
'valdi_app',
|
|
188
|
+
insertLineBefore(lineMatches('visibility = ["//visibility:public"],'), ' strings_dir = "strings",\n'),
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const strings = {
|
|
192
|
+
l10nsvc: 'sourcefile',
|
|
193
|
+
welcome: {
|
|
194
|
+
defaultMessage: 'Hello World',
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
await project.createFile('strings/strings-en.json', 'valdi_app', JSON.stringify(strings, null, ' '));
|
|
199
|
+
await run('valdi', ['projectsync'], { pipeOutput: true }).wait();
|
|
200
|
+
|
|
201
|
+
await project.updateFile(
|
|
202
|
+
'App.tsx',
|
|
203
|
+
'valdi_app',
|
|
204
|
+
insertLineBefore(atBeginning, "import strings from './Strings'\n"),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const appFile = await project.findFile('App.tsx', 'valdi_app');
|
|
208
|
+
await client.command('open', { file: appFile });
|
|
209
|
+
const [pathDescriptor] = await findDefinitionForTokenInFile(
|
|
210
|
+
{ project, client },
|
|
211
|
+
'strings',
|
|
212
|
+
'App.tsx',
|
|
213
|
+
'valdi_app',
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
assert.match(pathDescriptor, /\/generated_ts\/valdi_app\/src\/Strings\.d\.ts:\d+:\d+/);
|
|
217
|
+
|
|
218
|
+
await client.command('close', { file: appFile });
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
void describe('valdi cli app', () => {
|
|
224
|
+
const CLI_PROJECT_ROOT = join(PROJECT_ROOT, 'valdi_cli_app');
|
|
225
|
+
const CLI_PROJECT_NAME = basename(CLI_PROJECT_ROOT);
|
|
226
|
+
|
|
227
|
+
beforeEach(async () => {
|
|
228
|
+
await bootstrap(CLI_PROJECT_ROOT, CLI_PROJECT_NAME, 'cli_application');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
void it('install cli', async () => {
|
|
232
|
+
const task = run('valdi', ['install', 'cli'], { pipeOutput: true });
|
|
233
|
+
await lookForOutput(
|
|
234
|
+
{
|
|
235
|
+
// means the bazel build was succesful
|
|
236
|
+
stderr: [/Build completed successfully, \d+ total actions?/],
|
|
237
|
+
// The default bootstrapped app logs "Hello world!"
|
|
238
|
+
stdout: ['Hello world!'],
|
|
239
|
+
},
|
|
240
|
+
task,
|
|
241
|
+
);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Sets up a valdi project for tests
|
|
247
|
+
*
|
|
248
|
+
* - Delete's the existing directory (if any)
|
|
249
|
+
* - Re-creates the directory
|
|
250
|
+
* - Changes the currenty working directory to the new directory
|
|
251
|
+
* - Runs `valdi bootstrap` linked to the OPEN_SOURCE_DIR for `@@valdi`
|
|
252
|
+
* - Runs `valdi projectsync`
|
|
253
|
+
*/
|
|
254
|
+
async function bootstrap(
|
|
255
|
+
projectRootPath: string,
|
|
256
|
+
projectName: string,
|
|
257
|
+
applicationType: 'ui_application' | 'cli_application',
|
|
258
|
+
) {
|
|
259
|
+
// 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');
|
|
263
|
+
});
|
|
264
|
+
await mkdir(projectRootPath, { recursive: true });
|
|
265
|
+
|
|
266
|
+
// Run all commands from within the projectRootPath
|
|
267
|
+
process.chdir(projectRootPath);
|
|
268
|
+
|
|
269
|
+
await run(
|
|
270
|
+
'valdi',
|
|
271
|
+
['bootstrap', '-y', `-n=${projectName}`, `-t=${applicationType}`, `-l=${OPEN_SOURCE_DIR}`, '-p', '--with-cleanup'],
|
|
272
|
+
{
|
|
273
|
+
pipeOutput: true,
|
|
274
|
+
},
|
|
275
|
+
).wait();
|
|
276
|
+
|
|
277
|
+
await run('valdi', ['projectsync'], { pipeOutput: true }).wait();
|
|
278
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { SubCommand } from './CommandHelpers';
|
|
2
|
+
import { onEachLine } from './StreamHelpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Create a Promise that rejects after ms milliseconds from the last time it is "touched".
|
|
6
|
+
*
|
|
7
|
+
* Returns a promise, a function to reset the timeout, and a function to clear the timeout.
|
|
8
|
+
*
|
|
9
|
+
* The returned promise will never resolve.
|
|
10
|
+
*/
|
|
11
|
+
export function timeoutOnIdle(ms = 30_000): [timeout: Promise<never>, touch: () => void, clear: () => void] {
|
|
12
|
+
let timer: NodeJS.Timeout;
|
|
13
|
+
let resetTimer: () => void;
|
|
14
|
+
let clearTimer: () => void;
|
|
15
|
+
let complete = false;
|
|
16
|
+
|
|
17
|
+
const timeout = new Promise<never>((_resolve, reject) => {
|
|
18
|
+
clearTimer = () => {
|
|
19
|
+
complete = true;
|
|
20
|
+
clearTimeout(timer);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
resetTimer = () => {
|
|
24
|
+
clearTimeout(timer);
|
|
25
|
+
if (complete) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
timer = setTimeout(() => {
|
|
29
|
+
complete = true;
|
|
30
|
+
reject(new Error('timeout'));
|
|
31
|
+
}, ms);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
resetTimer();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return [timeout, () => resetTimer(), () => clearTimer()];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Given a task created with CommandHelpers.run, watches the STDOUT and/or STDERR
|
|
42
|
+
* for the given strings or expressions.
|
|
43
|
+
*
|
|
44
|
+
* Will timeeout if there's no output after 30 seconds.
|
|
45
|
+
*
|
|
46
|
+
* Rejects if there are queries remaning after the
|
|
47
|
+
*/
|
|
48
|
+
export function lookForOutput(
|
|
49
|
+
{ stdout = [], stderr = [] }: { stdout?: (string | RegExp)[]; stderr?: (string | RegExp)[] },
|
|
50
|
+
subcommand: SubCommand,
|
|
51
|
+
): Promise<true> {
|
|
52
|
+
const [idleTimer, touch, complete] = timeoutOnIdle();
|
|
53
|
+
|
|
54
|
+
const monitor = (requiredQueries: (string | RegExp)[]) => {
|
|
55
|
+
let queries = requiredQueries.slice();
|
|
56
|
+
let onDone: (value: true) => void, onError: (error: Error) => void;
|
|
57
|
+
|
|
58
|
+
const promise = new Promise((resolve, reject) => {
|
|
59
|
+
onDone = resolve;
|
|
60
|
+
onError = reject;
|
|
61
|
+
|
|
62
|
+
// If queries are empty to start with resolve immediately
|
|
63
|
+
if (queries.length === 0) {
|
|
64
|
+
resolve(true);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const isDone = () => queries.length === 0;
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
promise,
|
|
72
|
+
isDone,
|
|
73
|
+
onEachLine: onEachLine(line => {
|
|
74
|
+
touch();
|
|
75
|
+
queries = queries.filter((query): boolean => {
|
|
76
|
+
if (typeof query === 'string') {
|
|
77
|
+
return !line.toString().includes(query);
|
|
78
|
+
}
|
|
79
|
+
if (query instanceof RegExp) {
|
|
80
|
+
return !query.test(line.toString('utf8'));
|
|
81
|
+
}
|
|
82
|
+
onError(new Error('invalid query'));
|
|
83
|
+
return false;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (isDone()) {
|
|
87
|
+
onDone(true);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
}),
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const stdoutMonitor = monitor(stdout);
|
|
95
|
+
const stderrMonitor = monitor(stderr);
|
|
96
|
+
|
|
97
|
+
subcommand.process.stdout.compose(stdoutMonitor.onEachLine).pipe(process.stdout);
|
|
98
|
+
subcommand.process.stderr.compose(stderrMonitor.onEachLine).pipe(process.stderr);
|
|
99
|
+
|
|
100
|
+
return Promise.race([
|
|
101
|
+
idleTimer,
|
|
102
|
+
subcommand
|
|
103
|
+
.wait()
|
|
104
|
+
.then((): true => {
|
|
105
|
+
const allFound = [stderrMonitor, stderrMonitor].every(monitor => monitor.isDone());
|
|
106
|
+
if (allFound) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
throw new Error('Not all queries found: ' + [stdout, stderr].map(q => q.toString()).join('\n - '));
|
|
110
|
+
})
|
|
111
|
+
.finally(() => complete()),
|
|
112
|
+
]);
|
|
113
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type ChildProcessWithoutNullStreams, spawn } from 'child_process';
|
|
2
|
+
|
|
3
|
+
export interface SubCommand {
|
|
4
|
+
process: ChildProcessWithoutNullStreams;
|
|
5
|
+
/**
|
|
6
|
+
* Resolvess when the command exits
|
|
7
|
+
*/
|
|
8
|
+
wait: () => Promise<number>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const run = (
|
|
12
|
+
command: string,
|
|
13
|
+
args: string[] = [],
|
|
14
|
+
{ pipeOutput = false, cwd }: { pipeOutput?: boolean; cwd?: string } = {},
|
|
15
|
+
): SubCommand => {
|
|
16
|
+
const child = spawn(command, args, { detached: true, stdio: 'pipe', cwd });
|
|
17
|
+
|
|
18
|
+
if (pipeOutput) {
|
|
19
|
+
child.stderr.pipe(process.stderr);
|
|
20
|
+
child.stdout.pipe(process.stdout);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const complete = new Promise<number>((resolve, reject) => {
|
|
24
|
+
child.on('close', exitCode => {
|
|
25
|
+
if (exitCode !== null && exitCode !== 0) {
|
|
26
|
+
reject(new Error(command + ' ' + args.join(' ') + ' failed with code ' + exitCode.toString()));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
resolve(exitCode ?? 0);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
process: child,
|
|
35
|
+
wait: () => complete,
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { mkdir, readFile, writeFile } from 'fs/promises';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { eachLine } from './StreamHelpers';
|
|
5
|
+
import type { FilePosition, TSServerClient } from './TypeScriptClient';
|
|
6
|
+
import { lineAndOffset } from './TypeScriptClient';
|
|
7
|
+
|
|
8
|
+
type FileUpdater = (contents: Buffer) => Promise<Buffer>;
|
|
9
|
+
|
|
10
|
+
export interface ProjectConfig {
|
|
11
|
+
rootPath: string;
|
|
12
|
+
name: string;
|
|
13
|
+
findFile: (fileName: string, withinModule?: string) => Promise<string>;
|
|
14
|
+
updateFile: (fileName: string, withinModule: string | undefined, change: FileUpdater) => Promise<void>;
|
|
15
|
+
createFile: (
|
|
16
|
+
fileName: string,
|
|
17
|
+
withinModule: string | undefined,
|
|
18
|
+
source: Parameters<typeof writeFile>[1],
|
|
19
|
+
) => Promise<string>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface TaskContext {
|
|
23
|
+
project: ProjectConfig;
|
|
24
|
+
client: TSServerClient;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createProject(name: string, rootPath: string): ProjectConfig {
|
|
28
|
+
const findFile: ProjectConfig['findFile'] = (fileName, withinModule) => {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const buffers: Buffer[] = [];
|
|
31
|
+
|
|
32
|
+
const errorBuffers: Buffer[] = [];
|
|
33
|
+
const directory = path.join(...['.', 'modules'].concat(withinModule ?? []));
|
|
34
|
+
|
|
35
|
+
const child = spawn('find', [directory]);
|
|
36
|
+
child.stdout.on('data', (chunk: Buffer) => {
|
|
37
|
+
buffers.push(chunk);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
child.stderr.on('data', (chunk: Buffer) => {
|
|
41
|
+
errorBuffers.push(chunk);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
child.on('close', code => {
|
|
45
|
+
if (!!code && code > 0) {
|
|
46
|
+
reject(new Error('exited with error code: ' + code.toString()));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const output = Buffer.concat(buffers).toString('utf8');
|
|
50
|
+
|
|
51
|
+
const files = output.trim().split('\n');
|
|
52
|
+
|
|
53
|
+
const found = files.find(file => file.endsWith(`/${fileName}`));
|
|
54
|
+
|
|
55
|
+
if (!found) {
|
|
56
|
+
reject(new Error('File not found: ' + fileName));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
resolve(path.join(rootPath, found));
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
name,
|
|
66
|
+
rootPath,
|
|
67
|
+
findFile,
|
|
68
|
+
updateFile: async (file, withinModule, updater) => {
|
|
69
|
+
const path = await findFile(file, withinModule);
|
|
70
|
+
const contents = await readFile(path);
|
|
71
|
+
const updated = await updater(contents);
|
|
72
|
+
await writeFile(path, updated);
|
|
73
|
+
},
|
|
74
|
+
createFile: async (filePath, withinModule, source) => {
|
|
75
|
+
const fullPath = path.resolve(path.join(rootPath, withinModule ? `modules/${withinModule}` : '.', filePath));
|
|
76
|
+
await mkdir(path.dirname(fullPath), { recursive: true });
|
|
77
|
+
await writeFile(fullPath, source);
|
|
78
|
+
return fullPath;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
type LineMatcher = (line: string) => boolean;
|
|
84
|
+
|
|
85
|
+
export function lineMatches(search: string | RegExp): LineMatcher {
|
|
86
|
+
return line => {
|
|
87
|
+
if (typeof search === 'string') {
|
|
88
|
+
return line.toString().includes(search);
|
|
89
|
+
}
|
|
90
|
+
return search.test(line);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const atBeginning: LineMatcher = () => true;
|
|
95
|
+
|
|
96
|
+
export function insertLine(position: 'before' | 'after', search: LineMatcher, content: string | Buffer): FileUpdater {
|
|
97
|
+
const match = (content: Buffer | string): boolean => search(content.toString());
|
|
98
|
+
return contents => {
|
|
99
|
+
let found = false;
|
|
100
|
+
let buffer = Buffer.from([]);
|
|
101
|
+
for (const line of eachLine(contents)) {
|
|
102
|
+
const bufferLine = typeof line === 'string' ? Buffer.from(line) : line;
|
|
103
|
+
if (position === 'after') {
|
|
104
|
+
buffer = Buffer.concat([buffer, bufferLine]);
|
|
105
|
+
}
|
|
106
|
+
if (!found && match(line)) {
|
|
107
|
+
found = true;
|
|
108
|
+
buffer = Buffer.concat([buffer, typeof content === 'string' ? Buffer.from(content) : content]);
|
|
109
|
+
}
|
|
110
|
+
if (position === 'before') {
|
|
111
|
+
buffer = Buffer.concat([buffer, bufferLine]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return Promise.resolve(buffer);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function insertLineBefore(search: LineMatcher, content: string | Buffer): FileUpdater {
|
|
119
|
+
return insertLine('before', search, content);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function insertLineAfter(search: LineMatcher, content: string | Buffer): FileUpdater {
|
|
123
|
+
return insertLine('after', search, content);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function findDefinitionForTokenInFile(
|
|
127
|
+
context: TaskContext,
|
|
128
|
+
token: string,
|
|
129
|
+
file: string,
|
|
130
|
+
valdiModuleName?: string,
|
|
131
|
+
): Promise<[pathDescriptor: string, position: FilePosition, location: { file: string }, token: string]> {
|
|
132
|
+
const appSource = await context.project.findFile(file, valdiModuleName);
|
|
133
|
+
const position = await lineAndOffset(token, appSource);
|
|
134
|
+
const definition = await context.client.command('definition', {
|
|
135
|
+
file: appSource,
|
|
136
|
+
line: position.line,
|
|
137
|
+
offset: position.offset + 1,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
if ('success' in definition && !definition['success']) {
|
|
141
|
+
console.error(definition['message']);
|
|
142
|
+
throw new Error('failed to get typeDefinition');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const body = definition['body'] as { file: string; start: { line: number; offset: number } }[];
|
|
146
|
+
const location = body.at(0);
|
|
147
|
+
|
|
148
|
+
if (!location) {
|
|
149
|
+
throw new Error('No library definitions found for: ' + token + ' at ' + JSON.stringify(position));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return [`${location.file}:${location.start.line}:${location.start.offset}`, position, location, token];
|
|
153
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export function* eachLine(chunkOrString: Buffer | string): Generator<Buffer | string> {
|
|
2
|
+
let position = 0;
|
|
3
|
+
const chunk = typeof chunkOrString === 'string' ? Buffer.from(chunkOrString) : chunkOrString;
|
|
4
|
+
while (position < chunk.length) {
|
|
5
|
+
const newline = chunk.indexOf('\n', position);
|
|
6
|
+
if (newline === -1) {
|
|
7
|
+
yield chunk.subarray(position).toString('utf8') + '\n';
|
|
8
|
+
break;
|
|
9
|
+
}
|
|
10
|
+
yield chunk.subarray(position, newline).toString('utf8') + `\n`;
|
|
11
|
+
position = newline + 1;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function* pipeEachLine(source: AsyncGenerator<Buffer | string>): AsyncGenerator<Buffer | string> {
|
|
16
|
+
for await (const chunk of source) {
|
|
17
|
+
for (const line of eachLine(chunk)) {
|
|
18
|
+
yield line;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Reads the input of source and calls `fn` for each line as a side-effect.
|
|
25
|
+
*
|
|
26
|
+
* The input/output is not changed and cannot be mutated by the callback.
|
|
27
|
+
*/
|
|
28
|
+
export function onEachLine(
|
|
29
|
+
fn: (line: Buffer) => void,
|
|
30
|
+
): (source: AsyncGenerator<Buffer | string>) => AsyncGenerator<string | Buffer> {
|
|
31
|
+
return async function* (source) {
|
|
32
|
+
for await (const line of pipeEachLine(source)) {
|
|
33
|
+
fn(typeof line === 'string' ? Buffer.from(line) : line);
|
|
34
|
+
yield line;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|