Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove is-directory, use fs module directly #1897

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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