Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ jest.mock(`fs`, () => {
existsSync: jest.fn(),
readdirSync: jest.fn(),
readFileSync: jest.fn(),
lstatSync: jest.fn(),
};
});

Expand All @@ -28,13 +29,19 @@ describe('gatsby-remark-embedded-codesandbox', () => {
fs.existsSync.mockReset();
fs.readdirSync.mockReset();
fs.readFileSync.mockReset();
fs.lstatSync.mockReset();

fs.existsSync.mockReturnValue(true);
fs.readdirSync.mockReturnValue(['index.html', 'index.js', 'package.json']);
fs.readFileSync
.mockReturnValueOnce('<html><body></body></html>')
.mockReturnValueOnce('const foo = "bar";')
.mockReturnValueOnce('{ "name": "example" }');
fs.lstatSync.mockReturnValue({
isDirectory: function() {
return false;
},
});
});

it(`generates an embedded sandbox for the specified example folder`, async () => {
Expand Down
23 changes: 19 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ const compress = string =>
.replace(/\//g, `_`) // Convert '/' to '_'
.replace(/=+$/, ``); // Remove ending '='

const getFiles = (baseDir, dir, files) => {
fs.readdirSync(dir).forEach(function(file) {
var subDir = path.join(dir, file);
if (fs.lstatSync(subDir).isDirectory()) {
getFiles(baseDir, subDir, files);
} else {
let filePath = path.join(dir, file);
files.push(filePath.replace(`${baseDir}/`, ''));
}
});
};
Comment on lines +24 to +34
Copy link
Owner

Choose a reason for hiding this comment

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

I was thinking something like this (haven't tested it tho!)

const getFiles = (dir) => {
  const files = [];
  fs.readdirSync(dir).forEach(function(file) {
    const subDir = path.join(dir, file);
    if (fs.lstatSync(subDir).isDirectory()) {
	  files.push(...getFiles(subDir));
    } else {
      files.push(path.join(dir, file));
    } 
  });

  return files;
};


module.exports = (
{ markdownAST },
{
Expand All @@ -46,18 +58,21 @@ module.exports = (

const getFilesList = directory => {
let packageJsonFound = false;
const folderFiles = fs.readdirSync(directory);
const sandboxFiles = folderFiles
const folderFiles = [];
const sandboxFiles = [];
getFiles(directory, directory, folderFiles);

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);
const content = fs.readFileSync(fullFilePath, 'utf-8');
return {
sandboxFiles.push({
name: file,
content,
};
});
});
Comment on lines -49 to 76
Copy link
Owner

Choose a reason for hiding this comment

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

And then you can just do

    const sandboxFiles = getFiles(directory)
      // 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);
        const content = fs.readFileSync(fullFilePath, 'utf-8');
        return {
          name: file,
          content,
        };
      });


let workingDir = directory;
Expand Down