Skip to content

Commit 4f2321d

Browse files
Spencerkibanamachine
Spencer
andauthored
[ToolingLog] remove some manual indent management (#126636)
* [ToolingLog] remove some manual indent management * improve log message * [dev/build] write the newline after a failed step Co-authored-by: Kibana Machine <[email protected]>
1 parent a7deb8d commit 4f2321d

File tree

10 files changed

+156
-166
lines changed

10 files changed

+156
-166
lines changed

packages/kbn-dev-utils/src/tooling_log/tooling_log.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ export class ToolingLog {
6262
* @param delta the number of spaces to increase/decrease the indentation
6363
* @param block a function to run and reset any indentation changes after
6464
*/
65-
public indent<T>(delta = 0, block?: () => Promise<T>) {
65+
public indent(delta: number): undefined;
66+
public indent<T>(delta: number, block: () => Promise<T>): Promise<T>;
67+
public indent<T>(delta: number, block: () => T): T;
68+
public indent<T>(delta = 0, block?: () => T | Promise<T>) {
6669
const originalWidth = this.indentWidth$.getValue();
6770
this.indentWidth$.next(Math.max(originalWidth + delta, 0));
6871
if (!block) {

packages/kbn-es/src/cli_commands/build_snapshots.js

+23-24
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,31 @@ exports.run = async (defaults = {}) => {
4242
for (const license of ['oss', 'trial']) {
4343
for (const platform of ['darwin', 'win32', 'linux']) {
4444
log.info('Building', platform, license === 'trial' ? 'default' : 'oss', 'snapshot');
45-
log.indent(4);
45+
await log.indent(4, async () => {
46+
const snapshotPath = await buildSnapshot({
47+
license,
48+
sourcePath: options.sourcePath,
49+
log,
50+
platform,
51+
});
4652

47-
const snapshotPath = await buildSnapshot({
48-
license,
49-
sourcePath: options.sourcePath,
50-
log,
51-
platform,
52-
});
53-
54-
const filename = basename(snapshotPath);
55-
const outputPath = resolve(outputDir, filename);
56-
const hash = createHash('sha512');
57-
await pipelineAsync(
58-
Fs.createReadStream(snapshotPath),
59-
new Transform({
60-
transform(chunk, _, cb) {
61-
hash.update(chunk);
62-
cb(undefined, chunk);
63-
},
64-
}),
65-
Fs.createWriteStream(outputPath)
66-
);
53+
const filename = basename(snapshotPath);
54+
const outputPath = resolve(outputDir, filename);
55+
const hash = createHash('sha512');
56+
await pipelineAsync(
57+
Fs.createReadStream(snapshotPath),
58+
new Transform({
59+
transform(chunk, _, cb) {
60+
hash.update(chunk);
61+
cb(undefined, chunk);
62+
},
63+
}),
64+
Fs.createWriteStream(outputPath)
65+
);
6766

68-
Fs.writeFileSync(`${outputPath}.sha512`, `${hash.digest('hex')} ${filename}`);
69-
log.success('snapshot and shasum written to', outputPath);
70-
log.indent(-4);
67+
Fs.writeFileSync(`${outputPath}.sha512`, `${hash.digest('hex')} ${filename}`);
68+
log.success('snapshot and shasum written to', outputPath);
69+
});
7170
}
7271
}
7372
};

packages/kbn-es/src/cluster.js

+65-70
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ exports.Cluster = class Cluster {
5959
*/
6060
async installSource(options = {}) {
6161
this._log.info(chalk.bold('Installing from source'));
62-
this._log.indent(4);
63-
64-
const { installPath } = await installSource({ log: this._log, ...options });
65-
66-
this._log.indent(-4);
67-
68-
return { installPath };
62+
return await this._log.indent(4, async () => {
63+
const { installPath } = await installSource({ log: this._log, ...options });
64+
return { installPath };
65+
});
6966
}
7067

7168
/**
@@ -78,16 +75,14 @@ exports.Cluster = class Cluster {
7875
*/
7976
async downloadSnapshot(options = {}) {
8077
this._log.info(chalk.bold('Downloading snapshot'));
81-
this._log.indent(4);
78+
return await this._log.indent(4, async () => {
79+
const { installPath } = await downloadSnapshot({
80+
log: this._log,
81+
...options,
82+
});
8283

83-
const { installPath } = await downloadSnapshot({
84-
log: this._log,
85-
...options,
84+
return { installPath };
8685
});
87-
88-
this._log.indent(-4);
89-
90-
return { installPath };
9186
}
9287

9388
/**
@@ -100,16 +95,14 @@ exports.Cluster = class Cluster {
10095
*/
10196
async installSnapshot(options = {}) {
10297
this._log.info(chalk.bold('Installing from snapshot'));
103-
this._log.indent(4);
98+
return await this._log.indent(4, async () => {
99+
const { installPath } = await installSnapshot({
100+
log: this._log,
101+
...options,
102+
});
104103

105-
const { installPath } = await installSnapshot({
106-
log: this._log,
107-
...options,
104+
return { installPath };
108105
});
109-
110-
this._log.indent(-4);
111-
112-
return { installPath };
113106
}
114107

115108
/**
@@ -122,16 +115,14 @@ exports.Cluster = class Cluster {
122115
*/
123116
async installArchive(path, options = {}) {
124117
this._log.info(chalk.bold('Installing from an archive'));
125-
this._log.indent(4);
118+
return await this._log.indent(4, async () => {
119+
const { installPath } = await installArchive(path, {
120+
log: this._log,
121+
...options,
122+
});
126123

127-
const { installPath } = await installArchive(path, {
128-
log: this._log,
129-
...options,
124+
return { installPath };
130125
});
131-
132-
this._log.indent(-4);
133-
134-
return { installPath };
135126
}
136127

137128
/**
@@ -144,21 +135,19 @@ exports.Cluster = class Cluster {
144135
*/
145136
async extractDataDirectory(installPath, archivePath, extractDirName = 'data') {
146137
this._log.info(chalk.bold(`Extracting data directory`));
147-
this._log.indent(4);
148-
149-
// stripComponents=1 excludes the root directory as that is how our archives are
150-
// structured. This works in our favor as we can explicitly extract into the data dir
151-
const extractPath = path.resolve(installPath, extractDirName);
152-
this._log.info(`Data archive: ${archivePath}`);
153-
this._log.info(`Extract path: ${extractPath}`);
154-
155-
await extract({
156-
archivePath,
157-
targetDir: extractPath,
158-
stripComponents: 1,
138+
await this._log.indent(4, async () => {
139+
// stripComponents=1 excludes the root directory as that is how our archives are
140+
// structured. This works in our favor as we can explicitly extract into the data dir
141+
const extractPath = path.resolve(installPath, extractDirName);
142+
this._log.info(`Data archive: ${archivePath}`);
143+
this._log.info(`Extract path: ${extractPath}`);
144+
145+
await extract({
146+
archivePath,
147+
targetDir: extractPath,
148+
stripComponents: 1,
149+
});
159150
});
160-
161-
this._log.indent(-4);
162151
}
163152

164153
/**
@@ -169,24 +158,27 @@ exports.Cluster = class Cluster {
169158
* @returns {Promise<void>}
170159
*/
171160
async start(installPath, options = {}) {
172-
this._exec(installPath, options);
173-
174-
await Promise.race([
175-
// wait for native realm to be setup and es to be started
176-
Promise.all([
177-
first(this._process.stdout, (data) => {
178-
if (/started/.test(data)) {
179-
return true;
180-
}
181-
}),
182-
this._setupPromise,
183-
]),
161+
// _exec indents and we wait for our own end condition, so reset the indent level to it's current state after we're done waiting
162+
await this._log.indent(0, async () => {
163+
this._exec(installPath, options);
164+
165+
await Promise.race([
166+
// wait for native realm to be setup and es to be started
167+
Promise.all([
168+
first(this._process.stdout, (data) => {
169+
if (/started/.test(data)) {
170+
return true;
171+
}
172+
}),
173+
this._setupPromise,
174+
]),
184175

185-
// await the outcome of the process in case it exits before starting
186-
this._outcome.then(() => {
187-
throw createCliError('ES exited without starting');
188-
}),
189-
]);
176+
// await the outcome of the process in case it exits before starting
177+
this._outcome.then(() => {
178+
throw createCliError('ES exited without starting');
179+
}),
180+
]);
181+
});
190182
}
191183

192184
/**
@@ -197,16 +189,19 @@ exports.Cluster = class Cluster {
197189
* @returns {Promise<void>}
198190
*/
199191
async run(installPath, options = {}) {
200-
this._exec(installPath, options);
192+
// _exec indents and we wait for our own end condition, so reset the indent level to it's current state after we're done waiting
193+
await this._log.indent(0, async () => {
194+
this._exec(installPath, options);
195+
196+
// log native realm setup errors so they aren't uncaught
197+
this._setupPromise.catch((error) => {
198+
this._log.error(error);
199+
this.stop();
200+
});
201201

202-
// log native realm setup errors so they aren't uncaught
203-
this._setupPromise.catch((error) => {
204-
this._log.error(error);
205-
this.stop();
202+
// await the final outcome of the process
203+
await this._outcome;
206204
});
207-
208-
// await the final outcome of the process
209-
await this._outcome;
210205
}
211206

212207
/**

packages/kbn-optimizer/src/log_optimizer_state.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ export function logOptimizerState(log: ToolingLog, config: OptimizerConfig) {
9595

9696
if (state.phase === 'issue') {
9797
log.error(`webpack compile errors`);
98-
log.indent(4);
99-
for (const b of state.compilerStates) {
100-
if (b.type === 'compiler issue') {
101-
log.error(`[${b.bundleId}] build`);
102-
log.indent(4);
103-
log.error(b.failure);
104-
log.indent(-4);
98+
log.indent(4, () => {
99+
for (const b of state.compilerStates) {
100+
if (b.type === 'compiler issue') {
101+
log.error(`[${b.bundleId}] build`);
102+
log.indent(4, () => {
103+
log.error(b.failure);
104+
});
105+
}
105106
}
106-
}
107-
log.indent(-4);
107+
});
108108
return;
109109
}
110110

packages/kbn-plugin-helpers/src/tasks/optimize.ts

+20-21
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,25 @@ export async function optimize({ log, plugin, sourceDir, buildDir }: BuildContex
2323
}
2424

2525
log.info('running @kbn/optimizer');
26-
log.indent(2);
27-
28-
// build bundles into target
29-
const config = OptimizerConfig.create({
30-
repoRoot: REPO_ROOT,
31-
pluginPaths: [sourceDir],
32-
cache: false,
33-
dist: true,
34-
filter: [plugin.manifest.id],
26+
await log.indent(2, async () => {
27+
// build bundles into target
28+
const config = OptimizerConfig.create({
29+
repoRoot: REPO_ROOT,
30+
pluginPaths: [sourceDir],
31+
cache: false,
32+
dist: true,
33+
filter: [plugin.manifest.id],
34+
});
35+
36+
const target = Path.resolve(sourceDir, 'target');
37+
38+
await runOptimizer(config).pipe(logOptimizerState(log, config)).toPromise();
39+
40+
// clean up unnecessary files
41+
Fs.unlinkSync(Path.resolve(target, 'public/metrics.json'));
42+
Fs.unlinkSync(Path.resolve(target, 'public/.kbn-optimizer-cache'));
43+
44+
// move target into buildDir
45+
await asyncRename(target, Path.resolve(buildDir, 'target'));
3546
});
36-
37-
const target = Path.resolve(sourceDir, 'target');
38-
39-
await runOptimizer(config).pipe(logOptimizerState(log, config)).toPromise();
40-
41-
// clean up unnecessary files
42-
Fs.unlinkSync(Path.resolve(target, 'public/metrics.json'));
43-
Fs.unlinkSync(Path.resolve(target, 'public/.kbn-optimizer-cache'));
44-
45-
// move target into buildDir
46-
await asyncRename(target, Path.resolve(buildDir, 'target'));
47-
log.indent(-2);
4847
}

src/core/test_helpers/kbn_server.ts

-6
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,13 @@ export function createTestServers({
229229
writeTo: process.stdout,
230230
});
231231

232-
log.indent(6);
233-
log.info('starting elasticsearch');
234-
log.indent(4);
235-
236232
const es = createTestEsCluster(
237233
defaultsDeep({}, settings.es ?? {}, {
238234
log,
239235
license,
240236
})
241237
);
242238

243-
log.indent(-4);
244-
245239
// Add time for KBN and adding users
246240
adjustTimeout(es.getStartTimeout() + 100000);
247241

0 commit comments

Comments
 (0)