Skip to content

Commit

Permalink
Fix copying of public folder to target folder during post-build (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis committed Apr 19, 2023
1 parent c6ccc45 commit 4d0b640
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-kids-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sku': patch
---

Fix a bug that could occur when running `sku build` or `sku build-ssr` that caused the copying of assets from the `public` folder to the `target` folder to fail
Binary file added fixtures/ssr-hello-world/assets/foo/bar/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/ssr-hello-world/assets/foo/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/ssr-hello-world/assets/logo2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions fixtures/ssr-hello-world/sku-build.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
port: 8000,
serverPort: 8001,
target: 'dist-build',
public: 'assets',
cspEnabled: true,
cspExtraScriptSrcHosts: ['https://some-cdn.com'],
};
11 changes: 4 additions & 7 deletions packages/sku/lib/buildFileUtils.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
const path = require('path');
const { copyFile, mkdir } = require('fs/promises');
const fs = require('fs/promises');
const { rimraf } = require('rimraf');

const { paths } = require('../context');
const exists = require('./exists');
const copyDirContents = require('./copyDirContents');

const cleanTargetDirectory = () => rimraf(`${paths.target}/*`, { glob: true });

const copyPublicFiles = async () => {
if (await exists(paths.public)) {
console.log(`Copying ${paths.public} to ${paths.target}`);

await copyFile(paths.public, paths.target, {
dereference: true,
});
await copyDirContents(path.join(paths.public), path.join(paths.target));
}
};

const ensureTargetDirectory = async () => {
await mkdir(paths.target, { recursive: true });
await fs.mkdir(paths.target, { recursive: true });
};

const cleanRenderJs = async () => {
Expand Down
46 changes: 46 additions & 0 deletions packages/sku/lib/copyDirContents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const path = require('path');
const fs = require('fs/promises');
const exists = require('./exists');

/**
* Recursively copy the contents of a directory into another directory
* If the desitnation directory does not already exist, it will be created
*
* @typedef {import('fs').PathLike} PathLike
* @param {string} srcPath The src path of a
* @param {string} destPath A path to a file or directory
*/
const copyDirContents = async (srcPath, destPath) => {
const srcStat = await fs.stat(srcPath);
if (!srcStat.isDirectory()) {
throw new Error(`Source ${srcPath} is not a directory`);
}

if (await exists(destPath)) {
const destStat = await fs.stat(destPath);
if (!destStat.isDirectory()) {
throw new Error(`Destination ${destPath} is not a directory`);
}
} else {
await fs.mkdir(destPath);
}

const srcItems = await fs.readdir(srcPath);

for (const srcItem of srcItems) {
const srcItemPath = path.resolve(srcPath, srcItem);
const srcItemStat = await fs.stat(srcItemPath);

const destItemPath = path.resolve(destPath, srcItem);

if (srcItemStat.isFile()) {
// destItemPath will also be a file path, not a folder
await fs.copyFile(srcItemPath, destItemPath);
} else if (srcItemStat.isDirectory()) {
// recursively copy src item directory contents
await copyDirContents(srcItemPath, destItemPath);
}
}
};

module.exports = copyDirContents;
16 changes: 16 additions & 0 deletions tests/ssr-hello-world.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,21 @@ describe('ssr-hello-world', () => {
expect(snapshot).toMatchSnapshot();
});
});

it('should copy all public assets to the target folder', async () => {
const files = await fs.readdir(path.join(appDir, 'dist-build'));
expect(files).toContain('logo.png');
expect(files).toContain('logo2.png');
expect(files).toContain('foo');

const fooFiles = await fs.readdir(path.join(appDir, 'dist-build/foo'));
expect(fooFiles).toContain('logo.png');
expect(fooFiles).toContain('bar');

const barFiles = await fs.readdir(
path.join(appDir, 'dist-build/foo/bar'),
);
expect(barFiles).toContain('logo.png');
});
});
});

0 comments on commit 4d0b640

Please sign in to comment.