Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
refactor: wrap glob and minimatch to support windows paths
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Jun 3, 2022
1 parent a907ad8 commit a5606b3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import mergeOptions from 'merge-options'
import minimatch from 'minimatch'

import { FunctionSource } from './function.js'
import type { NodeBundlerName } from './runtimes/node/bundlers/index.js'
import type { NodeVersionString } from './runtimes/node/index.js'
import { minimatch } from './utils/matching.js'

export interface FunctionConfig {
externalNodeModules?: string[]
Expand Down
6 changes: 2 additions & 4 deletions src/runtimes/node/bundlers/nft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { basename, dirname, join, normalize, resolve } from 'path'

import { nodeFileTrace } from '@vercel/nft'
import resolveDependency from '@vercel/nft/out/resolve-dependency.js'
import minimatch from 'minimatch'
import unixify from 'unixify'

import type { FunctionConfig } from '../../../../config.js'
import { FeatureFlags } from '../../../../feature_flags.js'
import { cachedReadFile, FsCache } from '../../../../utils/fs.js'
import { minimatch } from '../../../../utils/matching.js'
import { getBasePath } from '../../utils/base_path.js'
import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files.js'
import type { GetSrcFilesFunction, BundleFunction } from '../index.js'
Expand Down Expand Up @@ -64,8 +63,7 @@ const bundle: BundleFunction = async ({
}

const ignoreFunction = (path: string) => {
const normalizedPath = unixify(path)
const shouldIgnore = ignore.some((expression) => minimatch(normalizedPath, expression))
const shouldIgnore = ignore.some((expression) => minimatch(path, expression))

return shouldIgnore
}
Expand Down
8 changes: 2 additions & 6 deletions src/runtimes/node/bundlers/zisi/published.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { promisify } from 'util'

import glob from 'glob'

const pGlob = promisify(glob)
import { glob } from '../../../../utils/matching'

// We use all the files published by the Node.js except some that are not needed
export const getPublishedFiles = async function (modulePath: string): Promise<string[]> {
const ignore = getIgnoredFiles(modulePath)
const publishedFiles = await pGlob(`${modulePath}/**`, {
const publishedFiles = await glob(`${modulePath}/**`, {
ignore,
nodir: true,
absolute: true,
Expand Down
9 changes: 3 additions & 6 deletions src/runtimes/node/bundlers/zisi/tree_files.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Stats } from 'fs'
import { promisify } from 'util'
import type { Stats } from 'fs'

import glob from 'glob'

const pGlob = promisify(glob)
import { glob } from '../../../../utils/matching'

// When using a directory, we include all its descendants except `node_modules`
export const getTreeFiles = async function (srcPath: string, stat: Stats): Promise<string[]> {
if (!stat.isDirectory()) {
return [srcPath]
}

return await pGlob(`${srcPath}/**`, {
return await glob(`${srcPath}/**`, {
ignore: `${srcPath}/**/node_modules/**`,
nodir: true,
absolute: true,
Expand Down
12 changes: 3 additions & 9 deletions src/runtimes/node/utils/included_files.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { normalize, resolve } from 'path'
import { promisify } from 'util'

import glob from 'glob'
import minimatch from 'minimatch'
import unixify from 'unixify'

const pGlob = promisify(glob)
import { minimatch, glob } from '../../../utils/matching'

// Returns the subset of `paths` that don't match any of the glob expressions
// from `exclude`.
Expand Down Expand Up @@ -34,8 +29,7 @@ export const getPathsOfIncludedFiles = async (
const { include, excludePatterns } = includedFiles.reduce<{ include: string[]; excludePatterns: string[] }>(
(acc, path) => {
if (path.startsWith('!')) {
// convert to unix paths, as minimatch does not support windows paths in patterns
const excludePattern = unixify(resolve(basePath, path.slice(1)))
const excludePattern = resolve(basePath, path.slice(1))

return {
include: acc.include,
Expand All @@ -52,7 +46,7 @@ export const getPathsOfIncludedFiles = async (
)
const pathGroups = await Promise.all(
include.map((expression) =>
pGlob(expression, { absolute: true, cwd: basePath, ignore: excludePatterns, nodir: true }),
glob(expression, { absolute: true, cwd: basePath, ignore: excludePatterns, nodir: true }),
),
)

Expand Down
27 changes: 27 additions & 0 deletions src/utils/matching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { promisify } from 'util'

import globFunction from 'glob'
import minimatchFunction from 'minimatch'
import unixify from 'unixify'

const pGlob = promisify(globFunction)

/**
* Both glob and minimatch only support unix style slashes in patterns
* For this reason we wrap them and ensure all patters are always unixified
*/

export const glob = function (pattern: string, options: globFunction.IOptions): Promise<string[]> {
let normalizedIgnore
if (options.ignore) {
normalizedIgnore =
typeof options.ignore === 'string'
? unixify(options.ignore)
: options.ignore.map((expression) => unixify(expression))
}
return pGlob(unixify(pattern), { ...options, ignore: normalizedIgnore })
}

export const minimatch = function (target: string, pattern: string, options?: minimatchFunction.IOptions): boolean {
return minimatchFunction(target, unixify(pattern), options)
}

0 comments on commit a5606b3

Please sign in to comment.