Skip to content
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
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"env-paths": "3.0.0",
"envfile": "7.1.0",
"fs-extra": "11.3.0",
"got": "14.4.6",
"joi": "17.13.3",
"kleur": "4.1.5",
"prompts": "2.4.2"
Expand Down
71 changes: 68 additions & 3 deletions packages/core/source/commands/config-publish-custom.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Commands, Contracts, Identifiers, Services } from "@mainsail/cli";
import { inject, injectable, postConstruct } from "@mainsail/container";
import { http } from "@mainsail/utils";
import { existsSync, writeFileSync } from "fs";
import { createWriteStream, existsSync, writeFileSync } from "fs";
import { ensureDirSync, removeSync } from "fs-extra/esm";
import got from "got";
import Joi from "joi";
import { join } from "path";
import stream from "stream";
import { promisify } from "util";

const ENV = `MAINSAIL_LOG_LEVEL=info
MAINSAIL_LOG_LEVEL_FILE=debug`;
Expand Down Expand Up @@ -37,6 +40,7 @@
.setFlag("app", "The link to the app.json file.", Joi.string().uri())
.setFlag("peers", "The link to the peers.json file.", Joi.string().uri())
.setFlag("crypto", "The link to the app.json file.", Joi.string().uri())
.setFlag("snapshot", "The link to the <snapshot>.compressed file.", Joi.string().uri())
.setFlag("reset", "Using the --reset flag will remove existing configuration.", Joi.boolean())
.setFlag("overwrite", "Using the --overwrite will overwrite existing configuration.", Joi.boolean());
}
Expand Down Expand Up @@ -143,12 +147,73 @@
},
title: "Publish crypto (crypto.json)",
},
{
skip: () => {
if (!flags.snapshot) {
return true;
}

if (
existsSync(`${configDestination}/snapshot/${this.#getFileName(flags.snapshot)}`) &&
!flags.overwrite
) {
return true;
}

return false;
},
task: async () => {
const snapshotDirectory = join(configDestination, "snapshot");
ensureDirSync(snapshotDirectory);

await this.#downloadFile(
flags.snapshot,
join(snapshotDirectory, this.#verifyFileName(this.#getFileName(flags.snapshot))),
);
},
title: "Publish snapshot (<hash>.compressed)",
},

Check warning on line 175 in packages/core/source/commands/config-publish-custom.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/source/commands/config-publish-custom.ts#L150-L175

Added lines #L150 - L175 were not covered by tests
]);
}

async #getFile(url: string): Promise<string> {
const { data } = await http.get(url);
try {
const { data } = await http.get(url);
return data;
} catch (error) {
console.error(`Error fetching file from ${url}:`, error);

throw new Error(
`Failed to fetch file from ${url}: ${error instanceof Error ? error.message : String(error)}`,
);
}
}

Check warning on line 190 in packages/core/source/commands/config-publish-custom.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/source/commands/config-publish-custom.ts#L180-L190

Added lines #L180 - L190 were not covered by tests

#getFileName(url: string): string {
const parts = url.split("/");
const fileName = parts.at(-1);

if (!fileName) {
throw new Error("Invalid URL provided, cannot extract file name.");
}

return fileName;
}

Check warning on line 201 in packages/core/source/commands/config-publish-custom.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/source/commands/config-publish-custom.ts#L193-L201

Added lines #L193 - L201 were not covered by tests

#verifyFileName(fileName: string): string {
const validFileName = Joi.string()
.regex(/^[a-z0-9]+\.(compressed)$/)
.required();
const { error } = validFileName.validate(fileName);

if (error) {
throw new Error(`Invalid file name: ${fileName}. Expected format: <hash>.compressed.`);
}

return fileName;
}

Check warning on line 214 in packages/core/source/commands/config-publish-custom.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/source/commands/config-publish-custom.ts#L204-L214

Added lines #L204 - L214 were not covered by tests

return data;
async #downloadFile(source: string, destination: string): Promise<void> {
await promisify(stream.pipeline)(got.stream(source), createWriteStream(destination));

Check warning on line 217 in packages/core/source/commands/config-publish-custom.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/source/commands/config-publish-custom.ts#L217

Added line #L217 was not covered by tests
}
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading