From 889041fc8a8db9b6b25462c18c957904c20be8c9 Mon Sep 17 00:00:00 2001 From: Guy Thomas Date: Thu, 18 Mar 2021 17:41:08 -0500 Subject: [PATCH 1/4] recursivley create files --- src/__tests__/index.js | 3 +++ src/index.js | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 6ee4faf..f47b38f 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -3,6 +3,9 @@ jest.mock(`fs`, () => { existsSync: jest.fn(), readdirSync: jest.fn(), readFileSync: jest.fn(), + statSync: jest.fn(() => ({ + isDirectory: jest.fn(() => false) + })) }; }); diff --git a/src/index.js b/src/index.js index f8fb78c..6dcfa0c 100644 --- a/src/index.js +++ b/src/index.js @@ -21,6 +21,16 @@ const compress = string => .replace(/\//g, `_`) // Convert '/' to '_' .replace(/=+$/, ``); // Remove ending '=' +const getAllFiles = dirPath => + fs.readdirSync(dirPath).reduce((acc, file) => { + const relativePath = dirPath + '/' + file; + const isDirectory = fs.statSync(relativePath).isDirectory(); + // Removes the dirPath from start of the string + const fileNameForSandbox = relativePath.replace(new RegExp(`^${dirPath}/`), "") + const additions = isDirectory ? getAllFiles(relativePath) : [fileNameForSandbox]; + return [...acc, ...additions]; + }, []); + module.exports = ( { markdownAST }, { @@ -46,16 +56,16 @@ module.exports = ( const getFilesList = directory => { let packageJsonFound = false; - const folderFiles = fs.readdirSync(directory); + const folderFiles = getAllFiles(directory); const sandboxFiles = folderFiles // we ignore the package.json file as it will // be handled separately - .filter(file => file !== 'package.json') - .map(file => { - const fullFilePath = path.resolve(directory, file); + .filter(file => !file.includes('package.json')) + .map(relativePath => { + const fullFilePath = path.resolve(__dirname, directory, relativePath); const content = fs.readFileSync(fullFilePath, 'utf-8'); return { - name: file, + name: relativePath, content, }; }); @@ -92,14 +102,13 @@ module.exports = ( }; const getPackageJsonFile = fileList => { - const found = fileList.filter(name => name === 'package.json'); - return found.length > null; + return fileList.find(file => file.includes('package.json')); }; const createParams = files => { const filesObj = files.reduce((prev, current) => { // parse package.json first - if (current.name === 'package.json') { + if (current.name.includes('package.json')) { prev[current.name] = { content: JSON.parse(current.content) }; } else { prev[current.name] = { content: current.content }; @@ -109,7 +118,6 @@ module.exports = ( const params = { files: filesObj, }; - return compress(JSON.stringify(params)); }; From 33ae47d931f7d4881d99555874a4509342823251 Mon Sep 17 00:00:00 2001 From: Guy Thomas Date: Thu, 18 Mar 2021 20:25:34 -0500 Subject: [PATCH 2/4] exclude node_modules --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index 6dcfa0c..305bad5 100644 --- a/src/index.js +++ b/src/index.js @@ -23,6 +23,7 @@ const compress = string => const getAllFiles = dirPath => fs.readdirSync(dirPath).reduce((acc, file) => { + if (file === 'node_modules') return acc; const relativePath = dirPath + '/' + file; const isDirectory = fs.statSync(relativePath).isDirectory(); // Removes the dirPath from start of the string From fac8a4d834e61c2d94a5c4a47b6345a2785a7b98 Mon Sep 17 00:00:00 2001 From: Guy Thomas Date: Thu, 18 Mar 2021 20:33:56 -0500 Subject: [PATCH 3/4] dont remove base path until the end --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 305bad5..8774504 100644 --- a/src/index.js +++ b/src/index.js @@ -26,9 +26,7 @@ const getAllFiles = dirPath => if (file === 'node_modules') return acc; const relativePath = dirPath + '/' + file; const isDirectory = fs.statSync(relativePath).isDirectory(); - // Removes the dirPath from start of the string - const fileNameForSandbox = relativePath.replace(new RegExp(`^${dirPath}/`), "") - const additions = isDirectory ? getAllFiles(relativePath) : [fileNameForSandbox]; + const additions = isDirectory ? getAllFiles(relativePath) : [relativePath]; return [...acc, ...additions]; }, []); @@ -58,7 +56,9 @@ module.exports = ( const getFilesList = directory => { let packageJsonFound = false; const folderFiles = getAllFiles(directory); + const basePathRE = new RegExp(`^${directory}/`); const sandboxFiles = folderFiles + .map(file => file.replace(basePathRE, '')) // we ignore the package.json file as it will // be handled separately .filter(file => !file.includes('package.json')) From ce63eb0dcec56284688abfbebb32bb06c1585e36 Mon Sep 17 00:00:00 2001 From: Guy Thomas Date: Fri, 19 Mar 2021 08:41:30 -0500 Subject: [PATCH 4/4] allow ignoring of file / folder names --- README.md | 9 +++++++++ package.json | 10 +++++----- src/index.js | 27 +++++++++++++++++++-------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4b93155..2bc080c 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,15 @@ This is than passed to the (awesome) [define api](https://codesandbox.io/docs/im // Customise the embedding iframe given the generated url // default: getIframe: url => `` + + + // Customise the ignored file / folder names + // default: + ignoredFiles: [ + 'node_modules', + 'yarn.lock', + 'package-lock.json' + ] } } ] diff --git a/package.json b/package.json index d2066e1..80e5029 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { - "name": "gatsby-remark-embedded-codesandbox", - "version": "1.2.0", - "description": "Gatsby Remark plugin for embedding Codesandbox given a folder of files", + "name": "@guy/gatsby-remark-embedded-codesandbox", + "version": "1.0.0", + "description": "Gatsby Remark plugin for embedding Codesandbox given a recursive folder of files", "main": "index.js", - "repository": "https://github.com/elboman/gatsby-remark-embedded-codesandbox", + "repository": "https://github.com/guyathomas/gatsby-remark-embedded-codesandbox", "author": "Marco Botto ", "bugs": { - "url": "https://github.com/elboman/gatsby-remark-embedded-codesandbox/issues" + "url": "https://github.com/guyathomas/gatsby-remark-embedded-codesandbox/issues" }, "keywords": [ "gatsby", diff --git a/src/index.js b/src/index.js index 8774504..69c270e 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,12 @@ const DEFAULT_EMBED_OPTIONS = { const DEFAULT_GET_IFRAME = url => ``; +const DEFAULT_IGNORED_FILES = [ + 'node_modules', + 'package-lock.json', + 'yarn.lock' +] + // Matches compression used in Babel and CodeSandbox REPLs // https://github.com/babel/website/blob/master/js/repl/UriUtils.js const compress = string => @@ -21,14 +27,7 @@ const compress = string => .replace(/\//g, `_`) // Convert '/' to '_' .replace(/=+$/, ``); // Remove ending '=' -const getAllFiles = dirPath => - fs.readdirSync(dirPath).reduce((acc, file) => { - if (file === 'node_modules') return acc; - const relativePath = dirPath + '/' + file; - const isDirectory = fs.statSync(relativePath).isDirectory(); - const additions = isDirectory ? getAllFiles(relativePath) : [relativePath]; - return [...acc, ...additions]; - }, []); + module.exports = ( { markdownAST }, @@ -37,6 +36,7 @@ module.exports = ( protocol = DEFAULT_PROTOCOL, embedOptions = DEFAULT_EMBED_OPTIONS, getIframe = DEFAULT_GET_IFRAME, + ignoredFiles = DEFAULT_IGNORED_FILES, } ) => { if (!rootDirectory) { @@ -47,12 +47,23 @@ module.exports = ( rootDirectory += '/'; } + const ignoredFilesSet = new Set(ignoredFiles) + const getDirectoryPath = url => { let directoryPath = url.replace(protocol, ''); const fullPath = path.join(rootDirectory, directoryPath); return normalizePath(fullPath); }; + const getAllFiles = (dirPath) => + fs.readdirSync(dirPath).reduce((acc, file) => { + if (ignoredFilesSet.has(file)) return acc; + const relativePath = dirPath + '/' + file; + const isDirectory = fs.statSync(relativePath).isDirectory(); + const additions = isDirectory ? getAllFiles(relativePath) : [relativePath]; + return [...acc, ...additions]; + }, []); + const getFilesList = directory => { let packageJsonFound = false; const folderFiles = getAllFiles(directory);