Skip to content

Commit

Permalink
fix: reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan committed Sep 25, 2021
1 parent 74b51f9 commit b6efbd9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
7 changes: 4 additions & 3 deletions .eslintrc
Expand Up @@ -31,11 +31,12 @@
},
"rules": {
"arrow-parens": ["error", "always"],
"no-undefined": "error",
"newline-per-chained-call": "off",
"import/prefer-default-export": ["off"],
"complexity": ["warn", 4],
"import/extensions": ["error", { "ts": "never" }],
"import/prefer-default-export": ["off"],
"newline-per-chained-call": "off",
"no-restricted-syntax": ["off", "ForOfStatement"],
"no-undefined": "error",
"@typescript-eslint/explicit-function-return-type": ["off"],
"@typescript-eslint/explicit-member-accessibility": [
"error",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -3,7 +3,7 @@
## Flexible path builder and walker

[![Build Status](https://travis-ci.com/jaspenlind/flexi-path.svg?branch=master)](https://travis-ci.com/jaspenlind/flexi-path)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/d53c318f91a54f49822d30d9974c1003)](https://www.codacy.com/manual/jaspenlind/flexi-path?utm_source=github.com&utm_medium=referral&utm_content=jaspenlind/flexi-path&utm_campaign=Badge_Grade)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/d53c318f91a54f49822d30d9974c1003)](https://www.codacy.com/gh/jaspenlind/flexi-path/dashboard?utm_source=github.com&utm_medium=referral&utm_content=jaspenlind/flexi-path&utm_campaign=Badge_Grade)
![GitHub top language](https://img.shields.io/github/languages/top/jaspenlind/flexi-path)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![Coverage Status](https://coveralls.io/repos/jaspenlind/flexi-path/badge.svg?branch=master)](https://coveralls.io/r/jaspenlind/flexi-path?branch=master)
Expand Down
17 changes: 12 additions & 5 deletions src/lib/path/meta/type.ts
Expand Up @@ -6,6 +6,12 @@ import { exists, isValid, stats } from ".";

export { PathType } from "../../../types";

/**
* @ignore
*/
const mightBeFile = (path: string): boolean =>
parse(path).ext !== "" || (!path.endsWith(constants.sep) && parent(path)().path !== constants.empty);

/**
* @ignore
*/
Expand All @@ -14,8 +20,7 @@ const guessType = (path: string): PathType => {
return PathType.Unknown;
}

const maybeFile =
parse(path).ext !== "" || (!path.endsWith(constants.sep) && parent(path)().path !== constants.empty);
const maybeFile = mightBeFile(path);

return maybeFile ? PathType.File : PathType.Directory;
};
Expand All @@ -34,9 +39,11 @@ const type = (path: string): PathType => {

const stat = stats(path);

return (
(stat && stat.isDirectory() && PathType.Directory) || (stat && stat.isFile() && PathType.File) || PathType.Unknown
);
if (!stat) {
return PathType.Unknown;
}

return stat.isDirectory() ? PathType.Directory : PathType.File;
};

export default type;
70 changes: 48 additions & 22 deletions src/lib/path/write.ts
Expand Up @@ -6,6 +6,49 @@ import { FlexiPath, PathType } from "../../types";
import { exists, parent, type } from ".";
import { create, WriteOptions } from "../../models/writeOptions";

/**
* @ignore
*/
const createDirectory = (path: string): void => {
if (!exists(path)) {
shell.mkdir("-p", path);
}
};

const createFile = (path: string, options: WriteOptions, content?: string | NodeJS.ArrayBufferView): void => {
const directory = parent(path)();

if (!directory) {
return;
}

createDirectory(directory.path);

if (content) {
writeFileSync(path, content, { encoding: options.encoding });
} else {
shell.touch(path);
}
};

/**
* @ignore
*/
const ensureValidType = (pathType: PathType): void => {
if (pathType === PathType.Unknown) {
throw new Error("Path is not valid or type cannot be determined");
}
};

/**
* @ignore
*/
const ensureNotExists = (path: string, options: WriteOptions): void => {
if (!options.overwrite && exists(path)) {
throw new Error("Path already exists");
}
};

/**
* Writes the current `path` to disk if possible
* @category path
Expand All @@ -18,32 +61,15 @@ const write = (
options?: Partial<WriteOptions>
): FlexiPath => {
const parsedType = type(path);
const allOptions = create(options);

if (parsedType === PathType.Unknown) {
throw new Error("Path is not valid or type cannot be determined");
}

const { encoding, overwrite } = create(options);

if (!overwrite && exists(path)) {
throw new Error("Path already exists");
}
ensureValidType(parsedType);
ensureNotExists(path, allOptions);

if (parsedType === PathType.Directory) {
shell.mkdir("-p", path);
createDirectory(path);
} else {
const parsedParent = parent(path)();
if (parsedParent) {
if (!exists(parsedParent.path)) {
shell.mkdir("-p", parsedParent.path);
}

if (content) {
writeFileSync(path, content, { encoding });
} else {
shell.touch(path);
}
}
createFile(path, allOptions, content);
}

return flexi.path(path);
Expand Down

0 comments on commit b6efbd9

Please sign in to comment.