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
8 changes: 1 addition & 7 deletions src/commands/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@ export default <ICommand<CommandArgs>>{
async run(core, args) {
const spinner = core.print.spin("Searching files...");

const files = await core.fs.glob(args.glob);

core.print.debug("Found %o files: $o", files.length, files);

spinner.start(`Adding ${files.length} files...`);

const zipper: IZip = new Zip();

await zipper.appendFiles(files, core.workspaceFolder);
await zipper.glob(args.glob, core.workspaceFolder);

core.print.debug("Added %o files", zipper.fileCount);

Expand Down
7 changes: 5 additions & 2 deletions src/interfaces/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export interface IFileSystem {
*/
exists(path: string, cwd?: string): boolean

glob(pattern: string | string[]): Promise<string[]>
/**
* @param cwd default `process.cwd()`
*/
glob(pattern: string | string[], cwd?: string): Promise<string[]>

readdir(path: string, recursive?: boolean): Promise<string[]>
readdir(path: string, options: FileSystemReadDirWithFileTypesOptions): Promise<Dirent[]>
Expand All @@ -41,5 +44,5 @@ export interface IFileSystem {
/**
* @param cwd default `process.cwd()`
*/
zipGenerator(glob: string | string[], cwd?: string): AsyncGenerator<Buffer, void, unknown>
zipIterate(glob: string | string[], cwd?: string): AsyncGenerator<Buffer, void, unknown>
}
4 changes: 4 additions & 0 deletions src/interfaces/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export interface IZip {
* @param cwd default `process.cwd()`
*/
appendFiles(files: string[], cwd?: string): Promise<void>
/**
* @param cwd default `process.cwd()`
*/
glob(pattern: string | string[], cwd?: string): Promise<void>
getBuffer(): Promise<Buffer>
writeZip(targetFileName?: string): Promise<boolean>
}
18 changes: 10 additions & 8 deletions src/structures/filesystem/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Ignore from "./ignore";

export default class FileSystem implements IFileSystem {
constructor(
readonly core: Core,
protected readonly core: Core,
) { }

asAbsolutePath(path: string, cwd: string = this.core.workspaceFolder): string {
Expand All @@ -27,16 +27,18 @@ export default class FileSystem implements IFileSystem {
return existsSync(join(cwd, path));
}

async glob(pattern: string | string[]): Promise<string[]> {
async glob(pattern: string | string[], cwd: string = this.core.workspaceFolder): Promise<string[]> {
const ignoreModule = new Ignore();
const ignoreFiles = await ignoreModule.findIgnoreFiles();
const ignoreFiles = await ignoreModule.findIgnoreFiles(cwd);
const ignore = await ignoreModule.resolveIgnoreFiles(ignoreFiles);

const windowsPathsNoEscape = type() === "Windows_NT";

return glob(pattern, {
nodir: true,
dot: true,
ignore,
windowsPathsNoEscape: type() === "Windows_NT",
nodir: true,
windowsPathsNoEscape,
});
}

Expand All @@ -58,11 +60,11 @@ export default class FileSystem implements IFileSystem {
}

async zip(glob: string | string[], cwd: string = this.core.workspaceFolder) {
return Buffer.concat(await Array.fromAsync(this.zipGenerator(glob, cwd)));
return Buffer.concat(await Array.fromAsync(this.zipIterate(glob, cwd)));
}

zipGenerator(glob: string | string[], cwd?: string): AsyncGenerator<Buffer>
async* zipGenerator(glob: string | string[], cwd: string = this.core.workspaceFolder) {
zipIterate(glob: string | string[], cwd?: string): AsyncGenerator<Buffer>
async* zipIterate(glob: string | string[], cwd: string = this.core.workspaceFolder) {
if (Array.isArray(glob)) glob = glob.join(" ");

const encoding = "buffer";
Expand Down
3 changes: 2 additions & 1 deletion src/structures/filesystem/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ export default class Ignore {
return patterns;
}

async findIgnoreFiles() {
async findIgnoreFiles(cwd: string = process.cwd()) {
const path = joinWithRoot(IGNORE_FILENAME);

const patterns = await this.#getGlobfiedIgnore(path);

const files = await glob(`**/${IGNORE_FILENAME}`, {
cwd,
dot: true,
ignore: patterns,
nodir: true,
Expand Down
30 changes: 30 additions & 0 deletions src/structures/filesystem/zip.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import AdmZip from "adm-zip";
import { readFile, stat } from "fs/promises";
import { globIterate } from "glob";
import { type } from "os";
import { join } from "path";
import { type IZip } from "../../interfaces/zip";
import Ignore from "./ignore";

export default class Zip implements IZip {
declare readonly zip: AdmZip;
Expand Down Expand Up @@ -34,6 +37,33 @@ export default class Zip implements IZip {
}
}

async glob(pattern: string | string[], cwd: string = process.cwd()) {
const ignoreModule = new Ignore();
const ignoreFiles = await ignoreModule.findIgnoreFiles(cwd);
const ignore = await ignoreModule.resolveIgnoreFiles(ignoreFiles);

const windowsPathsNoEscape = type() === "Windows_NT";

const globIterator = globIterate(pattern, {
cwd,
dot: true,
ignore,
nodir: true,
windowsPathsNoEscape,
withFileTypes: true,
});

for await (const file of globIterator) {
const filePath = file.fullpath();

const buffer = await readFile(filePath);

const fileRelativeName = file.relative();

this.zip.addFile(fileRelativeName, buffer);
}
}

getBuffer() {
return this.zip.toBufferPromise();
}
Expand Down
Loading