Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async function compressFiles(_ctx: Context, config: DeploymentConfig) {

try {
await makeDirectory(config.archivePath);
await zipDirectory(config.projectDir, config.archivePath, config.project.userConfig.deploy?.exclude ?? []);
await zipDirectory(config.projectDir, config.archivePath, config.project.userConfig.deploy?.exclude ?? [], config.project.userConfig.deploy?.include ?? []);

const sizeMb = statSync(config.archivePath).size / 1024 / 1024;
const sizeMbString = sizeMb.toFixed(2);
Expand Down
22 changes: 22 additions & 0 deletions src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readdirSync, statSync } from "node:fs";
import { join } from "node:path";

export function getAllFiles(dirPath: string, basePath = ""): string[] {
let results: string[] = [];
const list = readdirSync(dirPath);

list.forEach((file) => {
const filePath = join(dirPath, file);
const stat = statSync(filePath);

const relativePath = basePath ? join(basePath, file) : file;

if (stat && stat.isDirectory()) {
results = results.concat(getAllFiles(filePath, relativePath));
} else {
results.push(relativePath);
}
});

return results;
}
39 changes: 25 additions & 14 deletions src/utils/zip.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { createWriteStream } from "node:fs";
import archiver from "archiver";
import { minimatch } from "minimatch";
import { getAllFiles } from "./files";
import { join } from "node:path";

const DEFAULT_IGNORE = ["node_modules", ".git"];

/**
* Zip a directory.
*/
export function zipDirectory(sourceDir: string, outputPath: string, ignore: string[]): Promise<void> {
export function zipDirectory(sourceDir: string, outputPath: string, ignore: string[], include: string[]): Promise<void> {
return new Promise((resolve, reject) => {
const output = createWriteStream(outputPath);
const archive = archiver("zip", { zlib: { level: 9 } });
Expand All @@ -20,24 +23,32 @@ export function zipDirectory(sourceDir: string, outputPath: string, ignore: stri
archive.on("error", reject);
archive.pipe(output);

archive.glob("**/*", {
cwd: sourceDir,
ignore: allIgnore.flatMap(p => {
if (p.includes("/") || p.includes("**")) return [p];
if (p.startsWith("*.")) return [`**/${p}`];
return [`**/${p}`, `**/${p}/**`, `${p}`, `${p}/**`];
}),
dot: true,
});
const allFiles = getAllFiles(sourceDir, "");

for (const filePath of allFiles) {
const isIgnored = allIgnore.some(pattern =>
minimatch(filePath, pattern, { dot: true, matchBase: true }) ||
minimatch(filePath + "/**", pattern, { dot: true })
);

if (isIgnored) continue;

if (include.length > 0) {
const isIncluded = include.some(pattern =>
minimatch(filePath, pattern, { dot: true, matchBase: true })
);

if (!isIncluded) continue;
}

const fullPath = join(sourceDir, filePath);
archive.file(fullPath, { name: filePath });
}

archive.finalize();
});
}

// Paths patterns utils

import { minimatch } from "minimatch";

export function getDeletableFiles(rootFiles: string[], exclude: string[]) {
const toDelete = rootFiles.filter(item => {
const shouldIgnore = exclude.some(
Expand Down