mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Restructure to monorepo (#6907)
* wip: restructure to monorepo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor create-release-pr to a package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * github workflow fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix typo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * add webpack-env types to core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix github workflows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor/fix integration tests Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * lint fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * yarn run dev Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * eslint settings for vscode Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * move templates to right package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * open-lens build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * integration test fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix nx task dependencies Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * use bash shell for unit tests in test workflow Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix test:unit for windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix build:app on windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove ELECTRON_BUILDER_EXTRA_ARGS Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * sync src/ from master Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove Makefile from core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
1 parent
0066ca4ea2
commit
2657df2293
3321 files changed
+28699
-11447
No files matched your search
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type AccessPath = (path: string, mode?: number) => Promise<boolean>;
|
||||
|
||||
const accessPathInjectable = getInjectable({
|
||||
id: "access-path",
|
||||
instantiate: (di): AccessPath => {
|
||||
const { access } = di.inject(fsInjectable);
|
||||
|
||||
return async (path, mode) => {
|
||||
try {
|
||||
await access(path, mode);
|
||||
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default accessPathInjectable;
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getGlobalOverride } from "../test-utils/get-global-override";
|
||||
import copyInjectable from "./copy.injectable";
|
||||
|
||||
export default getGlobalOverride(copyInjectable, () => async () => {
|
||||
throw new Error("tried to copy filepaths without override");
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { CopyOptions } from "fs-extra";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type Copy = (src: string, dest: string, options?: CopyOptions | undefined) => Promise<void>;
|
||||
|
||||
const copyInjectable = getInjectable({
|
||||
id: "copy",
|
||||
instantiate: (di): Copy => di.inject(fsInjectable).copy,
|
||||
});
|
||||
|
||||
export default copyInjectable;
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { ReadStream } from "fs";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export interface CreateReadStreamOptions {
|
||||
mode?: number;
|
||||
end?: number | undefined;
|
||||
flags?: string | undefined;
|
||||
encoding?: BufferEncoding | undefined;
|
||||
autoClose?: boolean | undefined;
|
||||
/**
|
||||
* @default false
|
||||
*/
|
||||
emitClose?: boolean | undefined;
|
||||
start?: number | undefined;
|
||||
highWaterMark?: number | undefined;
|
||||
}
|
||||
|
||||
export type CreateReadFileStream = (filePath: string, options?: CreateReadStreamOptions) => ReadStream;
|
||||
|
||||
const createReadFileStreamInjectable = getInjectable({
|
||||
id: "create-read-file-stream",
|
||||
instantiate: (di): CreateReadFileStream => di.inject(fsInjectable).createReadStream,
|
||||
});
|
||||
|
||||
export default createReadFileStreamInjectable;
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type EnsureDirectory = (dirPath: string) => Promise<void>;
|
||||
|
||||
const ensureDirInjectable = getInjectable({
|
||||
id: "ensure-dir",
|
||||
|
||||
// TODO: Remove usages of ensureDir from business logic.
|
||||
// TODO: Read, Write, Watch etc. operations should do this internally.
|
||||
instantiate: (di): EnsureDirectory => di.inject(fsInjectable).ensureDir,
|
||||
});
|
||||
|
||||
export default ensureDirInjectable;
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getGlobalOverrideForFunction } from "../test-utils/get-global-override-for-function";
|
||||
import execFileInjectable from "./exec-file.injectable";
|
||||
|
||||
export default getGlobalOverrideForFunction(execFileInjectable);
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { ExecFileException, ExecFileOptions } from "child_process";
|
||||
import { execFile } from "child_process";
|
||||
import type { AsyncResult } from "../utils/async-result";
|
||||
|
||||
export type ExecFileError = ExecFileException & { stderr: string };
|
||||
|
||||
export interface ExecFile {
|
||||
(filePath: string): Promise<AsyncResult<string, ExecFileError>>;
|
||||
(filePath: string, argsOrOptions: string[] | ExecFileOptions): Promise<AsyncResult<string, ExecFileError>>;
|
||||
(filePath: string, args: string[], options: ExecFileOptions): Promise<AsyncResult<string, ExecFileError>>;
|
||||
}
|
||||
|
||||
const execFileInjectable = getInjectable({
|
||||
id: "exec-file",
|
||||
|
||||
instantiate: (): ExecFile => {
|
||||
return (filePath: string, argsOrOptions?: string[] | ExecFileOptions, maybeOptions?: ExecFileOptions) => {
|
||||
const { args, options } = (() => {
|
||||
if (Array.isArray(argsOrOptions)) {
|
||||
return {
|
||||
args: argsOrOptions,
|
||||
options: maybeOptions ?? {},
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
args: [],
|
||||
options: argsOrOptions ?? {},
|
||||
};
|
||||
}
|
||||
})();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
execFile(filePath, args, options, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
resolve({
|
||||
callWasSuccessful: false,
|
||||
error: Object.assign(error, { stderr }),
|
||||
});
|
||||
} else {
|
||||
resolve({
|
||||
callWasSuccessful: true,
|
||||
response: stdout,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
},
|
||||
|
||||
causesSideEffects: true,
|
||||
});
|
||||
|
||||
export default execFileInjectable;
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getGlobalOverride } from "../test-utils/get-global-override";
|
||||
import extractTarInjectable from "./extract-tar.injectable";
|
||||
|
||||
export default getGlobalOverride(extractTarInjectable, () => async () => {
|
||||
throw new Error("tried to extract a tar file without override");
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { ExtractOptions } from "tar";
|
||||
import { extract } from "tar";
|
||||
import getDirnameOfPathInjectable from "../path/get-dirname.injectable";
|
||||
|
||||
export type ExtractTar = (filePath: string, opts?: ExtractOptions) => Promise<void>;
|
||||
|
||||
const extractTarInjectable = getInjectable({
|
||||
id: "extract-tar",
|
||||
instantiate: (di): ExtractTar => {
|
||||
const getDirnameOfPath = di.inject(getDirnameOfPathInjectable);
|
||||
|
||||
return (filePath, opts = {}) => extract({
|
||||
file: filePath,
|
||||
cwd: getDirnameOfPath(filePath),
|
||||
...opts,
|
||||
});
|
||||
},
|
||||
causesSideEffects: true,
|
||||
});
|
||||
|
||||
export default extractTarInjectable;
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { ReadOptions } from "fs-extra";
|
||||
import fse from "fs-extra";
|
||||
|
||||
/**
|
||||
* NOTE: Add corrisponding a corrisponding override of this injecable in `src/test-utils/override-fs-with-fakes.ts`
|
||||
*/
|
||||
const fsInjectable = getInjectable({
|
||||
id: "fs",
|
||||
instantiate: () => {
|
||||
const {
|
||||
promises: {
|
||||
readFile,
|
||||
writeFile,
|
||||
readdir,
|
||||
lstat,
|
||||
rm,
|
||||
access,
|
||||
stat,
|
||||
},
|
||||
ensureDir,
|
||||
ensureDirSync,
|
||||
readFileSync,
|
||||
readJson,
|
||||
writeJson,
|
||||
readJsonSync,
|
||||
writeFileSync,
|
||||
writeJsonSync,
|
||||
pathExistsSync,
|
||||
pathExists,
|
||||
copy,
|
||||
createReadStream,
|
||||
} = fse;
|
||||
|
||||
return {
|
||||
readFile,
|
||||
readJson: readJson as (file: string, options?: ReadOptions | BufferEncoding) => Promise<any>,
|
||||
writeFile,
|
||||
writeJson,
|
||||
pathExists,
|
||||
readdir,
|
||||
readFileSync,
|
||||
readJsonSync,
|
||||
writeFileSync,
|
||||
writeJsonSync,
|
||||
pathExistsSync,
|
||||
lstat,
|
||||
rm,
|
||||
access,
|
||||
copy: copy as (src: string, dest: string, options?: fse.CopyOptions) => Promise<void>,
|
||||
ensureDir: ensureDir as (path: string, options?: number | fse.EnsureOptions ) => Promise<void>,
|
||||
ensureDirSync,
|
||||
createReadStream,
|
||||
stat,
|
||||
};
|
||||
},
|
||||
causesSideEffects: true,
|
||||
});
|
||||
|
||||
export default fsInjectable;
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getGlobalOverride } from "../test-utils/get-global-override";
|
||||
import lstatInjectable from "./lstat.injectable";
|
||||
|
||||
export default getGlobalOverride(lstatInjectable, () => async () => {
|
||||
throw new Error("tried to lstat a filepath without override");
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { Stats } from "fs";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type LStat = (path: string) => Promise<Stats>;
|
||||
|
||||
const lstatInjectable = getInjectable({
|
||||
id: "lstat",
|
||||
instantiate: (di): LStat => di.inject(fsInjectable).lstat,
|
||||
});
|
||||
|
||||
export default lstatInjectable;
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
const pathExistsSyncInjectable = getInjectable({
|
||||
id: "path-exists-sync",
|
||||
instantiate: (di) => di.inject(fsInjectable).pathExistsSync,
|
||||
});
|
||||
|
||||
export default pathExistsSyncInjectable;
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type PathExists = (path: string) => Promise<boolean>;
|
||||
|
||||
const pathExistsInjectable = getInjectable({
|
||||
id: "path-exists",
|
||||
instantiate: (di): PathExists => di.inject(fsInjectable).pathExists,
|
||||
});
|
||||
|
||||
export default pathExistsInjectable;
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getGlobalOverride } from "../test-utils/get-global-override";
|
||||
import readDirectoryInjectable from "./read-directory.injectable";
|
||||
|
||||
export default getGlobalOverride(readDirectoryInjectable, () => async () => {
|
||||
throw new Error("tried to read a directory's content without override");
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { Dirent } from "fs";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export interface ReadDirectory {
|
||||
(
|
||||
path: string,
|
||||
options: "buffer" | { encoding: "buffer"; withFileTypes?: false | undefined }
|
||||
): Promise<Buffer[]>;
|
||||
(
|
||||
path: string,
|
||||
options?:
|
||||
| { encoding: BufferEncoding; withFileTypes?: false | undefined }
|
||||
| BufferEncoding
|
||||
): Promise<string[]>;
|
||||
(
|
||||
path: string,
|
||||
options?: { encoding?: BufferEncoding; withFileTypes?: false | undefined },
|
||||
): Promise<string[] | Buffer[]>;
|
||||
(
|
||||
path: string,
|
||||
options: { encoding?: BufferEncoding; withFileTypes: true },
|
||||
): Promise<Dirent[]>;
|
||||
}
|
||||
|
||||
const readDirectoryInjectable = getInjectable({
|
||||
id: "read-directory",
|
||||
instantiate: (di): ReadDirectory => di.inject(fsInjectable).readdir,
|
||||
});
|
||||
|
||||
export default readDirectoryInjectable;
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type ReadFileBufferSync = (filePath: string) => Buffer;
|
||||
|
||||
const readFileBufferSyncInjectable = getInjectable({
|
||||
id: "read-file-buffer-sync",
|
||||
instantiate: (di): ReadFileBufferSync => {
|
||||
const { readFileSync } = di.inject(fsInjectable);
|
||||
|
||||
return (filePath) => readFileSync(filePath);
|
||||
},
|
||||
});
|
||||
|
||||
export default readFileBufferSyncInjectable;
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
const readFileBufferInjectable = getInjectable({
|
||||
id: "read-file-buffer",
|
||||
|
||||
instantiate: (di) => (filePath: string) =>
|
||||
di.inject(fsInjectable).readFile(filePath),
|
||||
});
|
||||
|
||||
export default readFileBufferInjectable;
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type ReadFileSync = (filePath: string) => string;
|
||||
|
||||
const readFileSyncInjectable = getInjectable({
|
||||
id: "read-file-sync",
|
||||
instantiate: (di): ReadFileSync => {
|
||||
const { readFileSync } = di.inject(fsInjectable);
|
||||
|
||||
return (filePath) => readFileSync(filePath, "utf-8");
|
||||
},
|
||||
});
|
||||
|
||||
export default readFileSyncInjectable;
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type ReadFile = (filePath: string) => Promise<string>;
|
||||
|
||||
const readFileInjectable = getInjectable({
|
||||
id: "read-file",
|
||||
|
||||
instantiate: (di): ReadFile => {
|
||||
const { readFile } = di.inject(fsInjectable);
|
||||
|
||||
return (filePath) => readFile(filePath, "utf-8");
|
||||
},
|
||||
});
|
||||
|
||||
export default readFileInjectable;
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { JsonValue } from "type-fest";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type ReadJson = (filePath: string) => Promise<JsonValue>;
|
||||
|
||||
const readJsonFileInjectable = getInjectable({
|
||||
id: "read-json-file",
|
||||
instantiate: (di): ReadJson => di.inject(fsInjectable).readJson,
|
||||
});
|
||||
|
||||
export default readJsonFileInjectable;
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
const readJsonSyncInjectable = getInjectable({
|
||||
id: "read-json-sync",
|
||||
instantiate: (di) => di.inject(fsInjectable).readJsonSync,
|
||||
});
|
||||
|
||||
export default readJsonSyncInjectable;
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import readFileInjectable from "./read-file.injectable";
|
||||
import yaml from "js-yaml";
|
||||
|
||||
export type ReadYamlFile = (filePath: string) => Promise<unknown>;
|
||||
|
||||
const readYamlFileInjectable = getInjectable({
|
||||
id: "read-yaml-file",
|
||||
|
||||
instantiate: (di): ReadYamlFile => {
|
||||
const readFile = di.inject(readFileInjectable);
|
||||
|
||||
return async (filePath: string) => {
|
||||
const contents = await readFile(filePath);
|
||||
|
||||
return yaml.load(contents);
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default readYamlFileInjectable;
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getGlobalOverride } from "../test-utils/get-global-override";
|
||||
import removePathInjectable from "./remove.injectable";
|
||||
|
||||
export default getGlobalOverride(removePathInjectable, () => async () => {
|
||||
throw new Error("tried to remove path without override");
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type RemovePath = (filePath: string) => Promise<void>;
|
||||
|
||||
const removePathInjectable = getInjectable({
|
||||
id: "remove-path",
|
||||
instantiate: (di): RemovePath => {
|
||||
const { rm } = di.inject(fsInjectable);
|
||||
|
||||
return (filePath) => rm(filePath, { force: true, recursive: true });
|
||||
},
|
||||
});
|
||||
|
||||
export default removePathInjectable;
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { Stats } from "fs";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type Stat = (path: string) => Promise<Stats>;
|
||||
|
||||
const statInjectable = getInjectable({
|
||||
id: "stat",
|
||||
instantiate: (di): Stat => di.inject(fsInjectable).stat,
|
||||
});
|
||||
|
||||
export default statInjectable;
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { AsyncResult } from "../utils/async-result";
|
||||
import { isErrnoException } from "../utils";
|
||||
import type { Stats } from "fs-extra";
|
||||
import { lowerFirst } from "lodash/fp";
|
||||
import statInjectable from "./stat.injectable";
|
||||
|
||||
export type ValidateDirectory = (path: string) => Promise<AsyncResult<undefined>>;
|
||||
|
||||
function getUserReadableFileType(stats: Stats): string {
|
||||
if (stats.isFile()) {
|
||||
return "a file";
|
||||
}
|
||||
|
||||
if (stats.isFIFO()) {
|
||||
return "a pipe";
|
||||
}
|
||||
|
||||
if (stats.isSocket()) {
|
||||
return "a socket";
|
||||
}
|
||||
|
||||
if (stats.isBlockDevice()) {
|
||||
return "a block device";
|
||||
}
|
||||
|
||||
if (stats.isCharacterDevice()) {
|
||||
return "a character device";
|
||||
}
|
||||
|
||||
return "an unknown file type";
|
||||
}
|
||||
|
||||
const validateDirectoryInjectable = getInjectable({
|
||||
id: "validate-directory",
|
||||
|
||||
instantiate: (di): ValidateDirectory => {
|
||||
const stat = di.inject(statInjectable);
|
||||
|
||||
return async (path) => {
|
||||
try {
|
||||
const stats = await stat(path);
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
return { callWasSuccessful: true, response: undefined };
|
||||
}
|
||||
|
||||
return { callWasSuccessful: false, error: `the provided path is ${getUserReadableFileType(stats)} and not a directory.` };
|
||||
} catch (error) {
|
||||
if (!isErrnoException(error)) {
|
||||
return { callWasSuccessful: false, error: "of an unknown error, please try again." };
|
||||
}
|
||||
|
||||
const humanReadableErrors: Record<string, string> = {
|
||||
ENOENT: "the provided path does not exist.",
|
||||
EACCES: "search permissions is denied for one of the directories in the prefix of the provided path.",
|
||||
ELOOP: "the provided path is a sym-link which points to a chain of sym-links that is too long to resolve. Perhaps it is cyclic.",
|
||||
ENAMETOOLONG: "the pathname is too long to be used.",
|
||||
ENOTDIR: "a prefix of the provided path is not a directory.",
|
||||
};
|
||||
|
||||
const humanReadableError = error.code
|
||||
? humanReadableErrors[error.code]
|
||||
: lowerFirst(String(error));
|
||||
|
||||
return { callWasSuccessful: false, error: humanReadableError };
|
||||
}
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default validateDirectoryInjectable;
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getGlobalOverride } from "../../test-utils/get-global-override";
|
||||
import watchInjectable from "./watch.injectable";
|
||||
|
||||
export default getGlobalOverride(watchInjectable, () => () => {
|
||||
throw new Error("Tried to call file system watch without explicit override");
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { watch } from "chokidar";
|
||||
import type { Stats } from "fs";
|
||||
import type TypedEventEmitter from "typed-emitter";
|
||||
import type { SingleOrMany } from "../../utils";
|
||||
|
||||
export interface AlwaysStatWatcherEvents {
|
||||
add: (path: string, stats: Stats) => void;
|
||||
addDir: (path: string, stats: Stats) => void;
|
||||
change: (path: string, stats: Stats) => void;
|
||||
}
|
||||
|
||||
export interface MaybeStatWatcherEvents {
|
||||
add: (path: string, stats?: Stats) => void;
|
||||
addDir: (path: string, stats?: Stats) => void;
|
||||
change: (path: string, stats?: Stats) => void;
|
||||
}
|
||||
|
||||
export type WatcherEvents<AlwaysStat extends boolean> = BaseWatcherEvents
|
||||
& (
|
||||
AlwaysStat extends true
|
||||
? AlwaysStatWatcherEvents
|
||||
: MaybeStatWatcherEvents
|
||||
);
|
||||
|
||||
export interface BaseWatcherEvents {
|
||||
error: (error: Error) => void;
|
||||
ready: () => void;
|
||||
unlink: (path: string) => void;
|
||||
unlinkDir: (path: string) => void;
|
||||
}
|
||||
|
||||
export interface Watcher<AlwaysStat extends boolean> extends TypedEventEmitter<WatcherEvents<AlwaysStat>> {
|
||||
close: () => Promise<void>;
|
||||
}
|
||||
|
||||
export type WatcherOptions<AlwaysStat extends boolean> = {
|
||||
/**
|
||||
* Indicates whether the process should continue to run as long as files are being watched. If
|
||||
* set to `false` when using `fsevents` to watch, no more events will be emitted after `ready`,
|
||||
* even if the process continues to run.
|
||||
*/
|
||||
persistent?: boolean;
|
||||
|
||||
/**
|
||||
* ([anymatch](https://github.com/micromatch/anymatch)-compatible definition) Defines files/paths to
|
||||
* be ignored. The whole relative or absolute path is tested, not just filename. If a function
|
||||
* with two arguments is provided, it gets called twice per path - once with a single argument
|
||||
* (the path), second time with two arguments (the path and the
|
||||
* [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object of that path).
|
||||
*/
|
||||
ignored?: SingleOrMany<string | RegExp | ((path: string) => boolean)>;
|
||||
|
||||
/**
|
||||
* If set to `false` then `add`/`addDir` events are also emitted for matching paths while
|
||||
* instantiating the watching as chokidar discovers these file paths (before the `ready` event).
|
||||
*/
|
||||
ignoreInitial?: boolean;
|
||||
|
||||
/**
|
||||
* When `false`, only the symlinks themselves will be watched for changes instead of following
|
||||
* the link references and bubbling events through the link's path.
|
||||
*/
|
||||
followSymlinks?: boolean;
|
||||
|
||||
/**
|
||||
* The base directory from which watch `paths` are to be derived. Paths emitted with events will
|
||||
* be relative to this.
|
||||
*/
|
||||
cwd?: string;
|
||||
|
||||
/**
|
||||
* If set to true then the strings passed to .watch() and .add() are treated as literal path
|
||||
* names, even if they look like globs. Default: false.
|
||||
*/
|
||||
disableGlobbing?: boolean;
|
||||
|
||||
/**
|
||||
* Whether to use fs.watchFile (backed by polling), or fs.watch. If polling leads to high CPU
|
||||
* utilization, consider setting this to `false`. It is typically necessary to **set this to
|
||||
* `true` to successfully watch files over a network**, and it may be necessary to successfully
|
||||
* watch files in other non-standard situations. Setting to `true` explicitly on OS X overrides
|
||||
* the `useFsEvents` default.
|
||||
*/
|
||||
usePolling?: boolean;
|
||||
|
||||
/**
|
||||
* Whether to use the `fsevents` watching interface if available. When set to `true` explicitly
|
||||
* and `fsevents` is available this supercedes the `usePolling` setting. When set to `false` on
|
||||
* OS X, `usePolling: true` becomes the default.
|
||||
*/
|
||||
useFsEvents?: boolean;
|
||||
|
||||
/**
|
||||
* If set, limits how many levels of subdirectories will be traversed.
|
||||
*/
|
||||
depth?: number;
|
||||
|
||||
/**
|
||||
* Interval of file system polling.
|
||||
*/
|
||||
interval?: number;
|
||||
|
||||
/**
|
||||
* Interval of file system polling for binary files. ([see list of binary extensions](https://gi
|
||||
* thub.com/sindresorhus/binary-extensions/blob/master/binary-extensions.json))
|
||||
*/
|
||||
binaryInterval?: number;
|
||||
|
||||
/**
|
||||
* Indicates whether to watch files that don't have read permissions if possible. If watching
|
||||
* fails due to `EPERM` or `EACCES` with this set to `true`, the errors will be suppressed
|
||||
* silently.
|
||||
*/
|
||||
ignorePermissionErrors?: boolean;
|
||||
|
||||
/**
|
||||
* `true` if `useFsEvents` and `usePolling` are `false`). Automatically filters out artifacts
|
||||
* that occur when using editors that use "atomic writes" instead of writing directly to the
|
||||
* source file. If a file is re-added within 100 ms of being deleted, Chokidar emits a `change`
|
||||
* event rather than `unlink` then `add`. If the default of 100 ms does not work well for you,
|
||||
* you can override it by setting `atomic` to a custom value, in milliseconds.
|
||||
*/
|
||||
atomic?: boolean | number;
|
||||
|
||||
/**
|
||||
* can be set to an object in order to adjust timing params:
|
||||
*/
|
||||
awaitWriteFinish?: AwaitWriteFinishOptions | boolean;
|
||||
} & (AlwaysStat extends true
|
||||
? {
|
||||
alwaysStat: true;
|
||||
}
|
||||
: {
|
||||
alwaysStat?: false;
|
||||
}
|
||||
);
|
||||
|
||||
export interface AwaitWriteFinishOptions {
|
||||
/**
|
||||
* Amount of time in milliseconds for a file size to remain constant before emitting its event.
|
||||
*/
|
||||
stabilityThreshold?: number;
|
||||
|
||||
/**
|
||||
* File size polling interval.
|
||||
*/
|
||||
pollInterval?: number;
|
||||
}
|
||||
|
||||
export type Watch = <AlwaysStat extends boolean = false>(path: string, options?: WatcherOptions<AlwaysStat>) => Watcher<AlwaysStat>;
|
||||
|
||||
// TODO: Introduce wrapper to allow simpler API
|
||||
const watchInjectable = getInjectable({
|
||||
id: "watch",
|
||||
instantiate: () => watch as Watch,
|
||||
causesSideEffects: true,
|
||||
});
|
||||
|
||||
export default watchInjectable;
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import getDirnameOfPathInjectable from "../path/get-dirname.injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type WriteBufferSync = (filePath: string, contents: Buffer) => void;
|
||||
|
||||
const writeBufferSyncInjectable = getInjectable({
|
||||
id: "write-buffer-sync",
|
||||
instantiate: (di): WriteBufferSync => {
|
||||
const {
|
||||
writeFileSync,
|
||||
ensureDirSync,
|
||||
} = di.inject(fsInjectable);
|
||||
const getDirnameOfPath = di.inject(getDirnameOfPathInjectable);
|
||||
|
||||
return (filePath, contents) => {
|
||||
ensureDirSync(getDirnameOfPath(filePath), {
|
||||
mode: 0o755,
|
||||
});
|
||||
writeFileSync(filePath, contents);
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default writeBufferSyncInjectable;
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import getDirnameOfPathInjectable from "../path/get-dirname.injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type WriteFileSync = (filePath: string, contents: string) => void;
|
||||
|
||||
const writeFileSyncInjectable = getInjectable({
|
||||
id: "write-file-sync",
|
||||
instantiate: (di): WriteFileSync => {
|
||||
const {
|
||||
writeFileSync,
|
||||
ensureDirSync,
|
||||
} = di.inject(fsInjectable);
|
||||
const getDirnameOfPath = di.inject(getDirnameOfPathInjectable);
|
||||
|
||||
return (filePath, contents) => {
|
||||
ensureDirSync(getDirnameOfPath(filePath), {
|
||||
mode: 0o755,
|
||||
});
|
||||
writeFileSync(filePath, contents);
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default writeFileSyncInjectable;
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getGlobalOverride } from "../test-utils/get-global-override";
|
||||
import writeFileInjectable from "./write-file.injectable";
|
||||
|
||||
export default getGlobalOverride(writeFileInjectable, () => async () => {
|
||||
throw new Error("tried to write file without override");
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { WriteFileOptions } from "fs-extra";
|
||||
import getDirnameOfPathInjectable from "../path/get-dirname.injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type WriteFile = (filePath: string, content: string | Buffer, opts?: WriteFileOptions) => Promise<void>;
|
||||
|
||||
const writeFileInjectable = getInjectable({
|
||||
id: "write-file",
|
||||
|
||||
instantiate: (di): WriteFile => {
|
||||
const { writeFile, ensureDir } = di.inject(fsInjectable);
|
||||
const getDirnameOfPath = di.inject(getDirnameOfPathInjectable);
|
||||
|
||||
return async (filePath, content, opts = {}) => {
|
||||
await ensureDir(getDirnameOfPath(filePath), {
|
||||
mode: 0o755,
|
||||
...opts,
|
||||
});
|
||||
|
||||
const { encoding = "utf-8", ...options } = opts;
|
||||
|
||||
await writeFile(filePath, content, {
|
||||
encoding: encoding as BufferEncoding,
|
||||
...options,
|
||||
});
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default writeFileInjectable;
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import getDirnameOfPathInjectable from "../path/get-dirname.injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type WriteJson = (filePath: string, contents: unknown) => Promise<void>;
|
||||
|
||||
const writeJsonFileInjectable = getInjectable({
|
||||
id: "write-json-file",
|
||||
|
||||
instantiate: (di): WriteJson => {
|
||||
const { writeJson, ensureDir } = di.inject(fsInjectable);
|
||||
const getDirnameOfPath = di.inject(getDirnameOfPathInjectable);
|
||||
|
||||
return async (filePath, content) => {
|
||||
await ensureDir(getDirnameOfPath(filePath), { mode: 0o755 });
|
||||
|
||||
await writeJson(filePath, content, {
|
||||
encoding: "utf-8",
|
||||
spaces: 2,
|
||||
});
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default writeJsonFileInjectable;
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import getDirnameOfPathInjectable from "../path/get-dirname.injectable";
|
||||
import fsInjectable from "./fs.injectable";
|
||||
|
||||
export type WriteJsonSync = (filePath: string, contents: unknown) => void;
|
||||
|
||||
const writeJsonSyncInjectable = getInjectable({
|
||||
id: "write-json-sync",
|
||||
instantiate: (di): WriteJsonSync => {
|
||||
const {
|
||||
writeJsonSync,
|
||||
ensureDirSync,
|
||||
} = di.inject(fsInjectable);
|
||||
const getDirnameOfPath = di.inject(getDirnameOfPathInjectable);
|
||||
|
||||
return (filePath, content) => {
|
||||
ensureDirSync(getDirnameOfPath(filePath), { mode: 0o755 });
|
||||
|
||||
writeJsonSync(filePath, content, {
|
||||
encoding: "utf-8",
|
||||
spaces: 2,
|
||||
});
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default writeJsonSyncInjectable;
|
||||
Reference in new issue
Block a user