Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: secure clear function, refactor a new utils #31 #32

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/commands/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ import fs from "fs";
import path from "path";

import { logger } from "../utils/logger";
import { isAsiAirDirectoryF } from "../utils/utils";

export const dropThumbnails = (
dir: string,
isRecursiveCall: boolean = false,
) => {
const clear = (dir: string) => {
if (!isAsiAirDirectoryF(dir).isAsiAirDirectory) {
logger.errorThrow("Not an ASIAIR directory!");
}

dropThumbnails(dir);
dropEmptyDirectories(dir);
};

export default clear;

const dropThumbnails = (dir: string, isRecursiveCall: boolean = false) => {
// Default parameter set to current directory
const files = fs.readdirSync(dir);
files.forEach(file => {
Expand All @@ -30,7 +39,7 @@ export const dropThumbnails = (
logger.info("Asiair dump cleaning - Thumbnails deleted ✅");
};

export const dropEmptyDirectories = (dir: string) => {
const dropEmptyDirectories = (dir: string) => {
const isDirEmpty = (directory: string): boolean => {
const files = fs.readdirSync(directory);
if (files.length === 0) {
Expand Down
11 changes: 4 additions & 7 deletions src/commands/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
getFitsFromDirectory,
matchSetFile,
getImageSpecFromSetName,
isAsiAirDirectoryF,
} from "../utils/utils";
import {
FileImageSpec,
Expand Down Expand Up @@ -173,12 +174,8 @@
projectDirectory: string,
): Promise<FileImageSpec[]> => {
// if ASIAIR used, may stores the lights+flats files in either the Autorun or Plan directories.

const autorunDirectory = `${inputDirectory}/Autorun`;
const planDirectory = `${inputDirectory}/Plan`;

const isAsiAirDump =
fs.existsSync(autorunDirectory) || fs.existsSync(planDirectory);
const { isAsiAirDirectory, autorunDirectory, planDirectory } =
isAsiAirDirectoryF(inputDirectory);

const logFiles = (files: unknown[]) => {
logger.info(`Found ${files.length} FITS in input dir ${inputDirectory}.`);
Expand All @@ -188,7 +185,7 @@
logger.errorThrow(`Input directory ${inputDirectory} does not exists.`);
}

if (!isAsiAirDump) {
if (!isAsiAirDirectory) {
const allFiles = getFitsFromDirectory({
directory: inputDirectory,
projectDirectory,
Expand All @@ -205,16 +202,16 @@

const autorunFiles = fs.existsSync(autorunDirectory)
? getFitsFromDirectory({
directory: autorunDirectory,

Check warning on line 205 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 6 spaces but found 8
projectDirectory,

Check warning on line 206 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 6 spaces but found 8
})

Check warning on line 207 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 6
: [];

const planFiles = fs.existsSync(planDirectory)
? getFitsFromDirectory({
directory: planDirectory,

Check warning on line 212 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 6 spaces but found 8
projectDirectory,

Check warning on line 213 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 6 spaces but found 8
})

Check warning on line 214 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 6
: [];

if (autorunFiles.length === 0 && planFiles.length === 0) {
Expand Down Expand Up @@ -474,12 +471,12 @@
for (const lightsFlatsMatch of lightsFlatsMatches) {
const lights = lightsFlatsMatch.isManualMatch
? allLights.filter(
light =>

Check warning on line 474 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 10
light.sequenceId === lightsFlatsMatch.lightSequenceId &&

Check warning on line 475 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 10 spaces but found 12
light.setName === lightsFlatsMatch.lightSetName,
)

Check warning on line 477 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 6 spaces but found 8
: allLights.filter(
light => light.setName === lightsFlatsMatch.lightSetName,

Check warning on line 479 in src/commands/prepare.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 10
);
if (!lights) {
throw new Error(
Expand Down
5 changes: 2 additions & 3 deletions src/poto-siril.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command } from "commander";
import prepare, { PrepareProps } from "./commands/prepare";
import { dropThumbnails, dropEmptyDirectories } from "./commands/clear";
import clear from "./commands/clear";
import { generateScripts } from "./commands/preprocess.generate-scripts";
import { runScripts } from "./commands/preprocess.exec-scripts";
import { POTO_VERSION } from "./utils/const";
Expand All @@ -20,8 +20,7 @@ program
.argument("<path>", "directory to clear")
.allowExcessArguments(false)
.action(directory => {
dropThumbnails(directory);
dropEmptyDirectories(directory);
clear(directory);
});

program
Expand Down
6 changes: 4 additions & 2 deletions src/tests/e2e/__snapshots__/e2e-testing.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E No Darks/Biases matching, multiples sequences should warn if no matching darks and biases 2`] = `
exports[`E2E no Darks/Biases matching, multiples sequences should warn if no matching darks and biases 2`] = `
"{
"generatedAt": "2024-10-15T00:00:00.000Z",
"potoVersion": "poto-siril 0.5.0",
Expand Down Expand Up @@ -543,7 +543,7 @@ exports[`E2E No Darks/Biases matching, multiples sequences should warn if no mat
}"
`;

exports[`E2E No Darks/Biases matching, multiples sequences should warn if no matching darks and biases 3`] = `
exports[`E2E no Darks/Biases matching, multiples sequences should warn if no matching darks and biases 3`] = `
[
"prompt: [
{
Expand Down Expand Up @@ -2004,6 +2004,8 @@ exports[`E2E should be neat 8`] = `
"debug: Deleted: tmp/asiair-dump-1/Autorun/Flat/Flat_1.0ms_Bin1_H_gain100_20240511-094307_-10.5C_0002_thn.jpg",
"debug: Deleted: tmp/asiair-dump-1/Autorun/Flat/Flat_1.0ms_Bin1_H_gain100_20240511-094308_-10.5C_0003_thn.jpg",
"info: Asiair dump cleaning - Thumbnails deleted ✅",
"info: Asiair dump cleaning - Empty dirs deleted ✅",
"info: Asiair dump cleaning - Thumbnails deleted ✅",
"info: Deleted: tmp/asiair-dump-1/empty/empty",
"info: Deleted: tmp/asiair-dump-1/empty",
"info: Asiair dump cleaning - Empty dirs deleted ✅",
Expand Down
14 changes: 10 additions & 4 deletions src/tests/e2e/e2e-testing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import prepare, {
SelectedInputSubDirectoryChoices,
} from "../../commands/prepare";
import { POTO_JSON } from "../../utils/const";
import { dropThumbnails, dropEmptyDirectories } from "../../commands/clear";
import clear from "../../commands/clear";
import { generateScripts } from "../../commands/preprocess.generate-scripts";
import {
getRealDataFromSample,
Expand Down Expand Up @@ -119,7 +119,7 @@ describe("E2E", () => {
});
expect(files.filter(f => f.endsWith("_thn.jpg"))).toHaveLength(3);

dropThumbnails(asiAirDirectory);
clear(asiAirDirectory);

files = fs.readdirSync(asiAirDirectory, {
recursive: true,
Expand All @@ -137,7 +137,7 @@ describe("E2E", () => {
true,
);

dropEmptyDirectories(asiAirDirectory);
clear(asiAirDirectory);

expect(fs.existsSync(path.join(asiAirDirectory, "empty", "empty"))).toBe(
false,
Expand Down Expand Up @@ -447,7 +447,7 @@ describe("E2E", () => {
});
});

describe("No Darks/Biases matching, multiples sequences", () => {
describe("no Darks/Biases matching, multiples sequences", () => {
it("should warn if no matching darks and biases", async () => {
promptMock
.mockResolvedValueOnce({
Expand Down Expand Up @@ -806,4 +806,10 @@ describe("E2E", () => {
expect(logMessages).toContain(logAfterTheCheckDirectory);
});
});

describe("clear function tests", () => {
it("should throw not asiair directory", async () => {
expect(() => clear(bankDirectory)).toThrow("Not an ASIAIR directory!");
});
});
});
2 changes: 1 addition & 1 deletion src/tests/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import fs from "fs";
import { logger } from "../utils/logger";
import { imageType } from "src/utils/types";
import { imageType } from "../utils/types";

const tmpDir = "tmp";

Expand Down
21 changes: 21 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,24 @@ export const getImageSpecFromSetName = (setName: string): ImageSpec => {
gain: Number(setName.split("_")[3].replace("gain", "")),
} as ImageSpec);
};

/**
* Detect if ASIAIR directory structure is present.
*/
export const isAsiAirDirectoryF = (
directory: string,
): {
isAsiAirDirectory: boolean;
autorunDirectory: string;
planDirectory: string;
} => {
const autorunDirectory = `${directory}/Autorun`;
const planDirectory = `${directory}/Plan`;

return {
isAsiAirDirectory:
fs.existsSync(autorunDirectory) || fs.existsSync(planDirectory),
autorunDirectory,
planDirectory,
};
};
Loading