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

refactor(is-file): Convert to TypeScript #23093

Merged
merged 11 commits into from
Jun 30, 2020
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/infer/add-inferred-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { GraphQLList } = require(`graphql`)
const invariant = require(`invariant`)
const report = require(`gatsby-cli/lib/reporter`)

const { isFile } = require(`./is-file`)
import { isFile } from "./is-file"
const { isDate } = require(`../types/date`)
const { addDerivedType } = require(`../types/derived-types`)
import { is32BitInteger } from "../../utils/is-32-bit-integer"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,68 @@
const path = require(`path`)
const { slash } = require(`gatsby-core-utils`)
const mime = require(`mime`)
const isRelative = require(`is-relative`)
const isRelativeUrl = require(`is-relative-url`)
import path from "path"
import { slash } from "gatsby-core-utils"
chooban marked this conversation as resolved.
Show resolved Hide resolved
import mime from "mime"
import isRelative from "is-relative"
import isRelativeUrl from "is-relative-url"
import { getValueAt } from "../../utils/get-value-at"
const { getNode, getNodesByType } = require(`../../redux/nodes`)
import { getNode, getNodesByType } from "../../redux/nodes"
import { IGatsbyNode } from "../../redux/types"

const isFile = (fieldPath, relativePath) => {
const filePath = getFilePath(fieldPath, relativePath)
if (!filePath) return false
const filePathExists = getNodesByType(`File`).some(
node => node.absolutePath === filePath
)
return filePathExists
}

module.exports = {
isFile,
}

const getFirstValueAt = (node, selector) => {
const getFirstValueAt = (
node: IGatsbyNode,
selector: string | string[]
): string => {
let value = getValueAt(node, selector)
while (Array.isArray(value)) {
value = value[0]
}
return value
}

const getFilePath = (fieldPath, relativePath) => {
const withBaseDir = (dir: string) => (p: string): string =>
path.posix.join(dir, slash(p))

const findAncestorNode = (
childNode: IGatsbyNode,
predicate: (n: IGatsbyNode) => boolean
): IGatsbyNode | null => {
let node: IGatsbyNode | undefined = childNode
do {
if (predicate(node)) {
return node
}
node = getNode(node.parent)
} while (node !== undefined)
return null
}

const getBaseDir = (node: IGatsbyNode): string | null => {
if (node) {
const { dir } = findAncestorNode(
node,
node => node.internal.type === `File`
) || { dir: `` }
pvdz marked this conversation as resolved.
Show resolved Hide resolved
return typeof dir === `string` ? dir : null
}
return null
}

const getAbsolutePath = (
node: IGatsbyNode,
relativePath: string
): string | string[] | null => {
const dir = getBaseDir(node)
const withDir = withBaseDir(dir ?? ``)
return dir
? Array.isArray(relativePath)
? relativePath.map(withDir)
: withDir(relativePath)
: null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this check is relevant anymore, possibly ever. getAbsolutePath is called from getFilePath, and the relativePath parameter is a value return from slash in gatsby-core-utils which is defined as returning a string. As such, I think that Array.isArray will always return false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at some test coverage data for the original JS file suggests that if it is possible to be an array, it's at least untested.

Screenshot 2020-04-17 at 08 31 35

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right. Let's try not to make any more semantic changes in TS convert PR's than we have to. Then in a followup diff refactor the code because it looks to me like this file can use a little cleanup and modernization.

}

const getFilePath = (
fieldPath: string,
relativePath: string
): string | string[] | null => {
const [typeName, ...selector] = Array.isArray(fieldPath)
? fieldPath
: fieldPath.split(`.`)
Expand All @@ -52,33 +87,11 @@ const getFilePath = (fieldPath, relativePath) => {
return node ? getAbsolutePath(node, normalizedPath) : null
}

const getAbsolutePath = (node, relativePath) => {
const dir = getBaseDir(node)
const withDir = withBaseDir(dir)
return dir
? Array.isArray(relativePath)
? relativePath.map(withDir)
: withDir(relativePath)
: null
}

const getBaseDir = node => {
if (node) {
const { dir } =
findAncestorNode(node, node => node.internal.type === `File`) || {}
return dir
}
return null
}

const withBaseDir = dir => p => path.posix.join(dir, slash(p))

const findAncestorNode = (childNode, predicate) => {
let node = childNode
do {
if (predicate(node)) {
return node
}
} while ((node = node.parent && getNode(node.parent)))
return null
export const isFile = (fieldPath: string, relativePath: string): boolean => {
const filePath = getFilePath(fieldPath, relativePath)
if (!filePath) return false
const filePathExists = getNodesByType(`File`).some(
node => node.absolutePath === filePath
)
return filePathExists
}