-
Notifications
You must be signed in to change notification settings - Fork 33
feat: write migrated assets to theme during themes:migrate #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import * as sinon from 'sinon' | ||
| import * as fs from 'fs' | ||
| import { expect } from '@oclif/test' | ||
| import rewriteAssets from './rewriteAssets' | ||
|
|
||
| describe('rewriteAssets', () => { | ||
| beforeEach(() => { | ||
| sinon.restore() | ||
| }) | ||
|
|
||
| it('decodes base64 content and writes assets to the correct file paths', () => { | ||
| const mkdirSyncStub = sinon.stub(fs, 'mkdirSync') | ||
| const writeFileSyncStub = sinon.stub(fs, 'writeFileSync') | ||
|
|
||
| const jsContent = Buffer.from('console.log("hello")').toString('base64') | ||
|
|
||
| rewriteAssets('theme/path', { | ||
| 'script.js': jsContent | ||
| }) | ||
|
|
||
| expect(mkdirSyncStub.calledOnce).to.equal(true) | ||
| expect(mkdirSyncStub.firstCall.args[1]).to.deep.equal({ recursive: true }) | ||
|
|
||
| expect(writeFileSyncStub.callCount).to.equal(1) | ||
| expect(writeFileSyncStub.firstCall.args[0]).to.equal('theme/path/assets/script.js') | ||
| expect(Buffer.compare( | ||
| writeFileSyncStub.firstCall.args[1] as Buffer, | ||
| Buffer.from('console.log("hello")') | ||
| )).to.equal(0) | ||
| }) | ||
|
|
||
| it('handles multiple file types', () => { | ||
| sinon.stub(fs, 'mkdirSync') | ||
| const writeFileSyncStub = sinon.stub(fs, 'writeFileSync') | ||
|
|
||
| const jsContent = Buffer.from('var a = 1;').toString('base64') | ||
| const pngContent = Buffer.from([0x89, 0x50, 0x4e, 0x47]).toString('base64') | ||
|
|
||
| rewriteAssets('theme/path', { | ||
| 'app.js': jsContent, | ||
| 'logo.png': pngContent | ||
| }) | ||
|
|
||
| expect(writeFileSyncStub.callCount).to.equal(2) | ||
| expect(writeFileSyncStub.firstCall.args[0]).to.equal('theme/path/assets/app.js') | ||
| expect(writeFileSyncStub.secondCall.args[0]).to.equal('theme/path/assets/logo.png') | ||
|
|
||
| expect(Buffer.compare( | ||
| writeFileSyncStub.secondCall.args[1] as Buffer, | ||
| Buffer.from([0x89, 0x50, 0x4e, 0x47]) | ||
| )).to.equal(0) | ||
| }) | ||
|
|
||
| it('handles empty assets object', () => { | ||
| sinon.stub(fs, 'mkdirSync') | ||
| const writeFileSyncStub = sinon.stub(fs, 'writeFileSync') | ||
|
|
||
| rewriteAssets('theme/path', {}) | ||
|
|
||
| expect(writeFileSyncStub.callCount).to.equal(0) | ||
| }) | ||
|
|
||
| it('throws an error when file cannot be written', () => { | ||
| sinon.stub(fs, 'mkdirSync') | ||
| const writeFileSyncStub = sinon.stub(fs, 'writeFileSync') | ||
|
|
||
| writeFileSyncStub.throws(new Error('Permission denied')) | ||
|
|
||
| const jsContent = Buffer.from('first').toString('base64') | ||
|
|
||
| expect(() => { | ||
| rewriteAssets('theme/path', { 'a.js': jsContent }) | ||
| }).to.throw('Failed to write asset file: theme/path/assets/a.js') | ||
| }) | ||
|
|
||
| it('throws an error when assets directory cannot be created', () => { | ||
| sinon.stub(fs, 'mkdirSync').throws(new Error('Permission denied')) | ||
| sinon.stub(fs, 'writeFileSync') | ||
|
|
||
| const jsContent = Buffer.from('hello').toString('base64') | ||
|
|
||
| expect(() => { | ||
| rewriteAssets('theme/path', { 'a.js': jsContent }) | ||
| }).to.throw('Failed to create assets directory: theme/path/assets') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { CLIError } from '@oclif/core/lib/errors' | ||
| import * as fs from 'fs' | ||
| import * as chalk from 'chalk' | ||
|
|
||
| export default function rewriteAssets (themePath: string, assets: Record<string, string>) { | ||
| const assetsDir = `${themePath}/assets` | ||
|
luis-almeida marked this conversation as resolved.
|
||
|
|
||
| try { | ||
| fs.mkdirSync(assetsDir, { recursive: true }) | ||
| } catch (error) { | ||
| throw new CLIError(chalk.red(`Failed to create assets directory: ${assetsDir}`)) | ||
| } | ||
|
|
||
| for (const [filename, base64Content] of Object.entries(assets)) { | ||
| const filePath = `${assetsDir}/${filename}` | ||
|
luis-almeida marked this conversation as resolved.
|
||
|
|
||
| try { | ||
| fs.writeFileSync(filePath, Buffer.from(base64Content, 'base64')) | ||
| } catch (error) { | ||
| throw new CLIError(chalk.red(`Failed to write asset file: ${filePath}`)) | ||
|
luis-almeida marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.