Skip to content

Commit

Permalink
fix: Remove is-directory, use fs module directly (#1897)
Browse files Browse the repository at this point in the history
Remove is-directory that is no longer maintained and replace it with direct calls of the fs module. It does exactly the same thing as is-directory with a bonus of having types built-in.
  • Loading branch information
visualfanatic committed Jan 10, 2022
1 parent 0a477a6 commit 77a2a2e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -32,8 +32,8 @@
"@vxna/mini-html-webpack-template": "^2.0.0",
"acorn": "^6.4.1",
"acorn-jsx": "^5.1.0",
"ast-types": "~0.14.2",
"assert": "1.5.0",
"ast-types": "~0.14.2",
"buble": "0.20.0",
"clean-webpack-plugin": "^3.0.0",
"clipboard-copy": "^3.1.0",
Expand All @@ -53,7 +53,6 @@
"glob": "^7.1.5",
"glogg": "^1.0.2",
"hash-sum": "^2.0.0",
"is-directory": "^0.3.1",
"javascript-stringify": "^2.0.0",
"jss": "^10.0.0",
"jss-plugin-camel-case": "^10.0.0",
Expand Down
22 changes: 16 additions & 6 deletions src/scripts/utils/sanitizeConfig.ts
@@ -1,6 +1,5 @@
import fs from 'fs';
import path from 'path';
import * as isDirectory from 'is-directory';
import castArray from 'lodash/castArray';
import isBoolean from 'lodash/isBoolean';
import isFunction from 'lodash/isFunction';
Expand Down Expand Up @@ -33,9 +32,20 @@ const typeCheckers: Record<string, (untypedObject: unknown) => boolean> = {
};

const typesList = (types: string[]) => listify(types, { finalWord: 'or' });
const shouldBeFile = (types: string[]) => types.some(type => type.includes('file'));
const shouldBeDirectory = (types: string[]) => types.some(type => type.includes('directory'));
const shouldExist = (types: string[]) => types.some(type => type.includes('existing'));
const shouldBeFile = (types: string[]) => types.some((type) => type.includes('file'));
const shouldBeDirectory = (types: string[]) => types.some((type) => type.includes('directory'));
const shouldExist = (types: string[]) => types.some((type) => type.includes('existing'));

function isDirectory(path: string): boolean {
try {
return fs.lstatSync(path).isDirectory();
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
return false;
}
}

/**
* Validates and normalizes config.
Expand Down Expand Up @@ -106,7 +116,7 @@ export default function sanitizeConfig<T extends Record<string, any>>(
const types = castArray(props.type);

// Check type
const hasRightType = types.some(type => {
const hasRightType = types.some((type) => {
if (!typeCheckers[type]) {
throw new StyleguidistError(
`Wrong type ${kleur.bold(type)} specified for ${kleur.bold(key)} in schema.`
Expand Down Expand Up @@ -146,7 +156,7 @@ ${stringify(example)}`
key
);
}
if (shouldBeDirectory(types) && !isDirectory.sync(value)) {
if (shouldBeDirectory(types) && !isDirectory(value)) {
throw new StyleguidistError(
`A directory specified in ${kleur.bold(key)} config option does not exist:\n${value}`,
key
Expand Down
7 changes: 0 additions & 7 deletions src/typings/dependencies/is-directory.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/typings/index.ts
Expand Up @@ -11,7 +11,6 @@ import './dependencies/glogg';
import './dependencies/mini-html-webpack-template';
import './dependencies/stripHtmlComments';
import './dependencies/deepfreeze';
import './dependencies/is-directory';
import './dependencies/q-i';
import './dependencies/to-ast';

Expand Down

0 comments on commit 77a2a2e

Please sign in to comment.