kibana/x-pack/plugins/integration_assistant/server/util/files.ts
Sergi Massaneda 3dd2034b27
[Integration AutoImport] Use kibana data directory as integration build working dir (#188661)
## Summary

This PR changes the working directory to build the integration Zip
package from `/tmp` to the Kibana data directory using the `@kbn/utils`
library.
It also removes the working directory when the integration zip build
finishes, to keep the house clean.

This change is necessary to prevent the ENOENT error from happening in
serverless environments when creating the working directory.

Before:


![serverless_error](https://github.com/user-attachments/assets/f2a89464-d9ed-4eee-a26f-fce300133e8a)

After:


![serverless_success](https://github.com/user-attachments/assets/58ceb2fd-9121-4242-a5f9-0b504ab5e991)

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2024-07-19 12:31:26 +02:00

51 lines
1.4 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { cpSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync, rmSync } from 'fs';
import { dirname } from 'path';
export function existsSync(path: string): boolean {
try {
statSync(path);
return true;
} catch (error) {
if (error.code === 'ENOENT') {
return false;
} else {
throw error;
}
}
}
export function ensureDirSync(dirPath: string): void {
const exists = existsSync(dirPath);
if (!exists) {
mkdirSync(dirPath, { recursive: true });
}
}
export function createSync(path: string, content: string | Buffer): void {
writeFileSync(path, content, { encoding: 'utf-8' });
}
export function copySync(source: string, destination: string): void {
// Ensure the destination directory exists
mkdirSync(dirname(destination), { recursive: true });
cpSync(source, destination, { recursive: true });
}
export function listDirSync(path: string): string[] {
return readdirSync(path);
}
export function readSync(path: string): string {
return readFileSync(path, { encoding: 'utf-8' });
}
export function removeDirSync(path: string): void {
rmSync(path, { recursive: true, force: true });
}