backend-common: make ReadTreeResponse.dir() accept an options object instead

This commit is contained in:
Patrik Oldsberg
2020-11-12 12:31:23 +01:00
parent 73231427e1
commit 5bc30d262f
3 changed files with 22 additions and 5 deletions
@@ -293,7 +293,7 @@ describe('GithubUrlReader', () => {
);
mockfs();
const directory = await response.dir('/tmp/fs');
const directory = await response.dir({ targetDir: '/tmp/fs' });
const writtenToDirectory = fs.existsSync(directory);
const paths = await recursive(directory);
@@ -18,7 +18,13 @@ import { Config } from '@backstage/config';
import parseGitUri from 'git-url-parse';
import fetch from 'cross-fetch';
import { NotFoundError } from '../errors';
import { ReaderFactory, ReadTreeResponse, UrlReader, File } from './types';
import {
ReaderFactory,
ReadTreeResponse,
UrlReader,
File,
ReadTreeResponseDirOptions,
} from './types';
import tar from 'tar';
import fs from 'fs-extra';
import concatStream from 'concat-stream';
@@ -294,9 +300,10 @@ export class GithubUrlReader implements UrlReader {
resolve(Buffer.from('Archive is not yet implemented')),
);
},
dir: (outDir: string | undefined) => {
dir: (options?: ReadTreeResponseDirOptions) => {
const targetDirectory =
outDir || fs.mkdtempSync(path.join(os.tmpdir(), 'backstage-'));
options?.targetDir ||
fs.mkdtempSync(path.join(os.tmpdir(), 'backstage-'));
return new Promise((res, rej) => {
Promise.all(
+11 -1
View File
@@ -17,6 +17,11 @@
import { Logger } from 'winston';
import { Config } from '@backstage/config';
export type ReadTreeOptions = {
/** A filter that can be used to select which files should be extracted. By default all files are extracted */
filter?(path: string): boolean;
};
/**
* A generic interface for fetching plain data from URLs.
*/
@@ -48,8 +53,13 @@ export type File = {
content(): Promise<Buffer>;
};
export type ReadTreeResponseDirOptions = {
/** The directory to write files to. Defaults to the OS tmpdir or `backend.workingDirectory` if set in config */
targetDir?: string;
};
export type ReadTreeResponse = {
files(): Promise<File[]>;
archive(): Promise<Buffer>;
dir(outDir?: string): Promise<string>;
dir(options?: ReadTreeResponseDirOptions): Promise<string>;
};