XasCode/terrafile-backend-lib

View on GitHub
src/backend/moduleSources/local.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { startsWith } from './common/startsWith';
import { Path, Status, FetchParams } from '../types';
import type { ModulesKeyType } from './';
import Validate from './common/validate';

function match(source: Path): ModulesKeyType | `` {
  return startsWith(source, `/`) || startsWith(source, `./`) || startsWith(source, `../`) ? `local` : ``;
}

function copyFromLocalDir({ params, dest, fsHelpers }: FetchParams): Status {
  const retVal = {
    success: false,
    contents: null,
    error: `Error copying from local dir`,
  } as Status;
  const src = fsHelpers.getAbsolutePath(params.source).value;
  const dirExists = fsHelpers.checkIfDirExists(src).value;
  if (dirExists) {
    const copyResult = fsHelpers.copyDirAbs(src, dest);
    retVal.success = copyResult.success;
    retVal.contents = [params as [string, Record<string, string>]];
    retVal.error = copyResult.error;
  }
  return retVal;
}

const acceptable = [`comment`, `source`];

const validate = Validate(acceptable);

export default { match, fetch: copyFromLocalDir, validate };