Skip to content

Commit

Permalink
feat: added --debug-tar flag
Browse files Browse the repository at this point in the history
it writes out the intermediate tar file that was used
  • Loading branch information
Dominic Scheirlinck committed Jan 14, 2022
1 parent c4c9085 commit fdd7823
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/commands/upload.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { createWriteStream } from 'fs';
import { pipeline as pipelineCb } from 'stream';
import util from 'util';
import { flags as f } from '@oclif/command';
import debug from 'debug';
import { upload } from '../artifacts/api';
Expand All @@ -7,11 +10,14 @@ import { Artifact } from '../artifacts/model';
import { BaseCommand, BaseFlags } from '../command';
import { produceTarStream } from '../util/tar';

const pipeline = util.promisify(pipelineCb);

const log = debug('monofo:cmd:upload');

interface UploadFlags extends BaseFlags {
'files-from'?: string;
null: boolean;
'debug-tar'?: string;
}

interface UploadArgs {
Expand Down Expand Up @@ -59,6 +65,10 @@ locally cached
dependsOn: ['files-from'],
description: "If given, the list of files is expected to be null-separated (a la find's -print0)",
}),
'debug-tar': f.string({
char: 'T',
description: 'A path to write the uncompressed resulting tar file to (as a debug measure)',
}),
};

static override args = [
Expand Down Expand Up @@ -106,6 +116,12 @@ locally cached

const tarStream = produceTarStream(paths);

if (flags['debug-tar']) {
log(`Writing debug tar to ${flags['debug-tar']}`);
await pipeline(tarStream, createWriteStream(flags['debug-tar']));
log('Finished writing debug tar');
}

log(`Deflating archive`);
await deflator(artifact, tarStream);
log(`Archive deflated at ${args.output}`);
Expand Down
20 changes: 18 additions & 2 deletions src/util/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,26 @@ export function depthSort(paths: string[]): string[] {
.sort((p1, p2) => p1.split(path.sep).length - p2.split(path.sep).length);
}

/**
* number of ch occurances in str without .split() (less allocations)
*/
const charCount = (haystack: string, needleChar: string) =>
_.sumBy(haystack, (x: string) => (x === needleChar ? 1 : 0));

export function produceTarStream(paths: PathsToPack) {
const prefixMatch: string[] = Object.entries(paths)
.filter(([, { recurse }]) => recurse)
.map(([k]) => (k.startsWith('./') ? k.slice(2) : k));
.map(([pathKey]) => {
const toPackPath = pathKey.startsWith('./') ? pathKey.slice(2) : pathKey; // without initial ./ prefix for matching purposes
const depth = charCount(pathKey, path.sep);
return { toPackPath, depth };
})
.sort()
.sort((a, b): number => a.depth - b.depth)
.map(({ toPackPath }) => toPackPath);

// These are sorted so that lower depths occur first in the array
// This is convenient for monorepo setups, where more common dependencies are usually lower in the tree

const exactMatch: Record<string, boolean> = Object.fromEntries(
Object.entries(paths)
Expand All @@ -82,7 +98,7 @@ export function produceTarStream(paths: PathsToPack) {

return pack('.', {
ignore: (name: string): boolean => {
return !(name in exactMatch) && prefixMatch.find((prefix) => name.startsWith(prefix)) === undefined;
return !(name in exactMatch || prefixMatch.find((prefix) => name.startsWith(prefix)));
},
});
}

0 comments on commit fdd7823

Please sign in to comment.