Skip to content

Commit

Permalink
resolve file system package from list
Browse files Browse the repository at this point in the history
  • Loading branch information
rafawalter committed Nov 25, 2020
1 parent 9103967 commit 36054ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
19 changes: 18 additions & 1 deletion src/lib/repository/file_system_repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ Deno.test('should list FileSystemPackages', () => {
packages.forEach(pkg => assert(pkg instanceof FileSystemPackage))
})

Deno.test('should resolve package by name', () => {
const repo = getTestRepo()

const pkg = repo.resolvePackage('amazingYml')

assert(pkg instanceof FileSystemPackage)
assertEquals(pkg.name, 'amazingYml')
})

Deno.test('should resolve package that does not exists as undefined', () => {
const repo = getTestRepo()

const pkg = repo.resolvePackage('--this-package-does-not-exist--')

assertEquals(pkg, undefined)
})

function getTestRepo(rootDir = './testdata/testRepo') {
return new FileSystemRepository(new Config([]), rootDir)
}
}
17 changes: 12 additions & 5 deletions src/lib/repository/file_system_repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import {existsSync, ExpandGlobOptions, expandGlobSync} from "https://deno.land/s
import Repository from './repository.ts'
import FileSystemPackage from '../package/file_system_package.ts'
import Config from '../config.ts';
import Package from "../package/package.ts";

export default class FileSystemRepository implements Repository {
packages: Array<FileSystemPackage> = [];

constructor(private config: Config, private rootDir: string) {
log.debug(`FSRepo: Root=${this.rootDir}`);

Expand All @@ -22,7 +23,8 @@ export default class FileSystemRepository implements Repository {
return undefined;
}

let pkg = this.readPackageInDir(packageName, this.rootDir);
// let pkg = this.readPackageInDir(packageName, this.rootDir);
const pkg = this.readPackageFromList(packageName);
if (pkg) {
log.debug(`FSRepo: ${packageName} => ${pkg.toString()}`);
}
Expand Down Expand Up @@ -75,12 +77,12 @@ export default class FileSystemRepository implements Repository {
yamlFile,
yamlStr,
this);

return pkg;
}

packages: Array<Package> = [];

listPackages(rootDirOnly?:boolean) {
listPackages(rootDirOnly?: boolean): Array<FileSystemPackage> {
if (!existsSync(`${this.rootDir}`)) {
log.debug(`# listPackages: rootDir not found ${this.rootDir}`)
return [];
Expand All @@ -94,7 +96,7 @@ export default class FileSystemRepository implements Repository {
}
log.debug(`# listPackages: ${packagesGlob} ${JSON.stringify(globOptions)}`)
const packageFiles = expandGlobSync(packagesGlob, globOptions)
const packages: Array<Package> = []
const packages: Array<FileSystemPackage> = [];
for (const file of packageFiles) {
log.debug(`## listPackages: file ${JSON.stringify(file)}`)
const packageName = file.name.replace(/\.levain\.ya?ml/, '')
Expand All @@ -107,4 +109,9 @@ export default class FileSystemRepository implements Repository {
log.debug(`# listPackages: added ${packages.length} packages`)
return packages;
}

private readPackageFromList(packageName: string): FileSystemPackage | undefined {
return this.packages
.find(pkg => pkg.name == packageName);
}
}

0 comments on commit 36054ad

Please sign in to comment.