mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-04-02 05:23:21 +00:00
feat: Utils to do bindLan and have ipv4 and ipv6 if need be
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
import { GenericManifest } from "../manifest/ManifestTypes";
|
||||
import * as T from "../types";
|
||||
import { GenericManifest } from "../manifest/ManifestTypes"
|
||||
import * as T from "../types"
|
||||
|
||||
export type BACKUP = "BACKUP";
|
||||
export type BACKUP = "BACKUP"
|
||||
export const DEFAULT_OPTIONS: T.BackupOptions = {
|
||||
delete: true,
|
||||
force: true,
|
||||
ignoreExisting: false,
|
||||
exclude: [],
|
||||
};
|
||||
}
|
||||
type BackupSet<Volumes extends string> = {
|
||||
srcPath: string;
|
||||
srcVolume: Volumes | BACKUP;
|
||||
dstPath: string;
|
||||
dstVolume: Volumes | BACKUP;
|
||||
options?: Partial<T.BackupOptions>;
|
||||
};
|
||||
srcPath: string
|
||||
srcVolume: Volumes | BACKUP
|
||||
dstPath: string
|
||||
dstVolume: Volumes | BACKUP
|
||||
options?: Partial<T.BackupOptions>
|
||||
}
|
||||
/**
|
||||
* This utility simplifies the volume backup process.
|
||||
* ```ts
|
||||
@@ -38,7 +38,7 @@ type BackupSet<Volumes extends string> = {
|
||||
* ```
|
||||
*/
|
||||
export class Backups<M extends GenericManifest> {
|
||||
static BACKUP: BACKUP = "BACKUP";
|
||||
static BACKUP: BACKUP = "BACKUP"
|
||||
|
||||
constructor(
|
||||
private options = DEFAULT_OPTIONS,
|
||||
@@ -54,24 +54,24 @@ export class Backups<M extends GenericManifest> {
|
||||
dstPath: `./${srcVolume}/`,
|
||||
dstVolume: Backups.BACKUP,
|
||||
})),
|
||||
);
|
||||
)
|
||||
}
|
||||
static addSets<M extends GenericManifest = never>(
|
||||
...options: BackupSet<keyof M["volumes"] & string>[]
|
||||
) {
|
||||
return new Backups().addSets(...options);
|
||||
return new Backups().addSets(...options)
|
||||
}
|
||||
static with_options<M extends GenericManifest = never>(
|
||||
options?: Partial<T.BackupOptions>,
|
||||
) {
|
||||
return new Backups({ ...DEFAULT_OPTIONS, ...options });
|
||||
return new Backups({ ...DEFAULT_OPTIONS, ...options })
|
||||
}
|
||||
set_options(options?: Partial<T.BackupOptions>) {
|
||||
this.options = {
|
||||
...this.options,
|
||||
...options,
|
||||
};
|
||||
return this;
|
||||
}
|
||||
return this
|
||||
}
|
||||
volumes(...volumeNames: Array<keyof M["volumes"] & string>) {
|
||||
return this.addSets(
|
||||
@@ -81,13 +81,13 @@ export class Backups<M extends GenericManifest> {
|
||||
dstPath: `./${srcVolume}/`,
|
||||
dstVolume: Backups.BACKUP,
|
||||
})),
|
||||
);
|
||||
)
|
||||
}
|
||||
addSets(...options: BackupSet<keyof M["volumes"] & string>[]) {
|
||||
options.forEach((x) =>
|
||||
this.backupSet.push({ ...x, options: { ...this.options, ...x.options } }),
|
||||
);
|
||||
return this;
|
||||
)
|
||||
return this
|
||||
}
|
||||
build() {
|
||||
const createBackup: T.ExpectedExports.createBackup = async ({
|
||||
@@ -100,16 +100,16 @@ export class Backups<M extends GenericManifest> {
|
||||
path: ".",
|
||||
})
|
||||
.catch(() => [])
|
||||
).map((x) => `${x}`);
|
||||
).map((x) => `${x}`)
|
||||
const backupPaths = this.backupSet
|
||||
.filter((x) => x.dstVolume === Backups.BACKUP)
|
||||
.map((x) => x.dstPath)
|
||||
.map((x) => x.replace(/\.\/([^]*)\//, "$1"));
|
||||
.map((x) => x.replace(/\.\/([^]*)\//, "$1"))
|
||||
const filteredItems = previousItems.filter(
|
||||
(x) => backupPaths.indexOf(x) === -1,
|
||||
);
|
||||
)
|
||||
for (const itemToRemove of filteredItems) {
|
||||
effects.error(`Trying to remove ${itemToRemove}`);
|
||||
effects.console.error(`Trying to remove ${itemToRemove}`)
|
||||
await effects
|
||||
.removeDir({
|
||||
volumeId: Backups.BACKUP,
|
||||
@@ -122,15 +122,17 @@ export class Backups<M extends GenericManifest> {
|
||||
}),
|
||||
)
|
||||
.catch(() => {
|
||||
effects.warn(`Failed to remove ${itemToRemove} from backup volume`);
|
||||
});
|
||||
effects.console.warn(
|
||||
`Failed to remove ${itemToRemove} from backup volume`,
|
||||
)
|
||||
})
|
||||
}
|
||||
for (const item of this.backupSet) {
|
||||
if (notEmptyPath(item.dstPath)) {
|
||||
await effects.createDir({
|
||||
volumeId: item.dstVolume,
|
||||
path: item.dstPath,
|
||||
});
|
||||
})
|
||||
}
|
||||
await effects
|
||||
.runRsync({
|
||||
@@ -140,10 +142,10 @@ export class Backups<M extends GenericManifest> {
|
||||
...item.options,
|
||||
},
|
||||
})
|
||||
.wait();
|
||||
.wait()
|
||||
}
|
||||
return;
|
||||
};
|
||||
return
|
||||
}
|
||||
const restoreBackup: T.ExpectedExports.restoreBackup = async ({
|
||||
effects,
|
||||
}) => {
|
||||
@@ -152,7 +154,7 @@ export class Backups<M extends GenericManifest> {
|
||||
await effects.createDir({
|
||||
volumeId: item.srcVolume,
|
||||
path: item.srcPath,
|
||||
});
|
||||
})
|
||||
}
|
||||
await effects
|
||||
.runRsync({
|
||||
@@ -165,13 +167,13 @@ export class Backups<M extends GenericManifest> {
|
||||
srcPath: item.dstPath,
|
||||
dstPath: item.srcPath,
|
||||
})
|
||||
.wait();
|
||||
.wait()
|
||||
}
|
||||
return;
|
||||
};
|
||||
return { createBackup, restoreBackup };
|
||||
return
|
||||
}
|
||||
return { createBackup, restoreBackup }
|
||||
}
|
||||
}
|
||||
function notEmptyPath(file: string) {
|
||||
return ["", ".", "./"].indexOf(file) === -1;
|
||||
return ["", ".", "./"].indexOf(file) === -1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user