Skip to content

Commit

Permalink
feat(gatsby-source-filesystem): remove slash (#14372)
Browse files Browse the repository at this point in the history
* remove slash

* remove unnecessary semicolon && add more test

* add test for extended length paths
  • Loading branch information
wucong authored and freiksenet committed Jun 10, 2019
1 parent fcbc66a commit 1d9ba86
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 0 additions & 1 deletion packages/gatsby-source-filesystem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"pretty-bytes": "^4.0.2",
"progress": "^1.1.8",
"read-chunk": "^3.0.0",
"slash": "^1.0.0",
"valid-url": "^1.0.9",
"xstate": "^3.1.0"
},
Expand Down
19 changes: 18 additions & 1 deletion packages/gatsby-source-filesystem/src/__tests__/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getRemoteFileExtension, getRemoteFileName } = require(`../utils`)
const { getRemoteFileExtension, getRemoteFileName, slash } = require(`../utils`)

describe(`create remote file node`, () => {
it(`can correctly retrieve file name and extensions`, () => {
Expand All @@ -22,3 +22,20 @@ describe(`create remote file node`, () => {
})
})
})

describe(`slash path`, () => {
it(`can correctly slash path`, () => {
;[
[`foo\\bar`, `foo/bar`],
[`foo/bar`, `foo/bar`],
[`foo\\中文`, `foo/中文`],
[`foo/中文`, `foo/中文`],
].forEach(([path, expectRes]) => {
expect(slash(path)).toBe(expectRes)
})
})
it(`does not modify extended length paths`, () => {
const extended = `\\\\?\\some\\path`
expect(slash(extended)).toBe(extended)
})
})
2 changes: 1 addition & 1 deletion packages/gatsby-source-filesystem/src/create-file-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const slash = require(`slash`)
const { slash } = require(`./utils`)
const path = require(`path`)
const fs = require(`fs-extra`)
const mime = require(`mime`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-filesystem/src/create-file-path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require(`path`)
const slash = require(`slash`)
const { slash } = require(`./utils`)

function findFileNode({ node, getNode }) {
// Find the file node.
Expand Down
19 changes: 19 additions & 0 deletions packages/gatsby-source-filesystem/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,22 @@ export function getRemoteFileExtension(url) {
export function getRemoteFileName(url) {
return getParsedPath(url).name
}

/**
* slash
* --
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
*
*
* @param {String} path
* @return {String} slashed path
*/
export function slash(path) {
const isExtendedLengthPath = /^\\\\\?\\/.test(path)

if (isExtendedLengthPath) {
return path
}

return path.replace(/\\/g, `/`)
}

0 comments on commit 1d9ba86

Please sign in to comment.