Skip to content

Commit 8d1fc8e

Browse files
committed
fix: throw CLIError on write failures in rewriteAssets and rewriteTemplates
Align error handling with rewriteManifest instead of silently swallowing errors.
1 parent 7a8cc3d commit 8d1fc8e

4 files changed

Lines changed: 19 additions & 30 deletions

File tree

packages/zcli-themes/src/lib/rewriteAssets.test.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,38 +60,27 @@ describe('rewriteAssets', () => {
6060
expect(writeFileSyncStub.callCount).to.equal(0)
6161
})
6262

63-
it('ignores write errors', () => {
63+
it('throws an error when file cannot be written', () => {
6464
sinon.stub(fs, 'mkdirSync')
6565
const writeFileSyncStub = sinon.stub(fs, 'writeFileSync')
6666

67-
writeFileSyncStub.onFirstCall().throws(new Error('Permission denied'))
68-
writeFileSyncStub.onSecondCall().returns(undefined)
67+
writeFileSyncStub.throws(new Error('Permission denied'))
6968

7069
const jsContent = Buffer.from('first').toString('base64')
71-
const jsContent2 = Buffer.from('second').toString('base64')
7270

7371
expect(() => {
74-
rewriteAssets('theme/path', {
75-
'a.js': jsContent,
76-
'b.js': jsContent2
77-
})
78-
}).to.not.throw()
79-
80-
expect(writeFileSyncStub.callCount).to.equal(2)
72+
rewriteAssets('theme/path', { 'a.js': jsContent })
73+
}).to.throw('Failed to write asset file: theme/path/assets/a.js')
8174
})
8275

83-
it('ignores mkdir errors', () => {
84-
const mkdirSyncStub = sinon.stub(fs, 'mkdirSync')
85-
const writeFileSyncStub = sinon.stub(fs, 'writeFileSync')
86-
87-
mkdirSyncStub.throws(new Error('Permission denied'))
76+
it('throws an error when assets directory cannot be created', () => {
77+
sinon.stub(fs, 'mkdirSync').throws(new Error('Permission denied'))
78+
sinon.stub(fs, 'writeFileSync')
8879

8980
const jsContent = Buffer.from('hello').toString('base64')
9081

9182
expect(() => {
9283
rewriteAssets('theme/path', { 'a.js': jsContent })
93-
}).to.not.throw()
94-
95-
expect(writeFileSyncStub.callCount).to.equal(1)
84+
}).to.throw('Failed to create assets directory: theme/path/assets')
9685
})
9786
})
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { CLIError } from '@oclif/core/lib/errors'
12
import * as fs from 'fs'
3+
import * as chalk from 'chalk'
24

35
export default function rewriteAssets (themePath: string, assets: Record<string, string>) {
46
const assetsDir = `${themePath}/assets`
57

68
try {
79
fs.mkdirSync(assetsDir, { recursive: true })
810
} catch (error) {
9-
// Ignore errors if directory can't be created
11+
throw new CLIError(chalk.red(`Failed to create assets directory: ${assetsDir}`))
1012
}
1113

1214
for (const [filename, base64Content] of Object.entries(assets)) {
@@ -15,7 +17,7 @@ export default function rewriteAssets (themePath: string, assets: Record<string,
1517
try {
1618
fs.writeFileSync(filePath, Buffer.from(base64Content, 'base64'))
1719
} catch (error) {
18-
// Ignore errors if file can't be written
20+
throw new CLIError(chalk.red(`Failed to write asset file: ${filePath}`))
1921
}
2022
}
2123
}

packages/zcli-themes/src/lib/rewriteTemplates.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,18 @@ describe('rewriteTemplates', () => {
3434
])
3535
})
3636

37-
it('ignores write errors', () => {
37+
it('throws an error when file cannot be written', () => {
3838
const writeFileSyncStub = sinon.stub(fs, 'writeFileSync')
3939

40-
writeFileSyncStub.onFirstCall().throws(new Error('Permission denied'))
41-
writeFileSyncStub.onSecondCall().returns(undefined)
40+
writeFileSyncStub.throws(new Error('Permission denied'))
4241

4342
const templates = {
44-
home_page: '<h1>Updated Home</h1>',
45-
article_page: '<h1>Updated Article</h1>'
43+
home_page: '<h1>Updated Home</h1>'
4644
}
4745

4846
expect(() => {
4947
rewriteTemplates('theme/path', templates)
50-
}).to.not.throw()
51-
52-
expect(writeFileSyncStub.callCount).to.equal(2)
48+
}).to.throw('Failed to write template file: theme/path/templates/home_page.hbs')
5349
})
5450

5551
it('handles empty templates object', () => {

packages/zcli-themes/src/lib/rewriteTemplates.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { CLIError } from '@oclif/core/lib/errors'
12
import * as fs from 'fs'
3+
import * as chalk from 'chalk'
24

35
export default function rewriteTemplates (themePath: string, templates: Record<string, string>) {
46
for (const [identifier, content] of Object.entries(templates)) {
@@ -8,7 +10,7 @@ export default function rewriteTemplates (themePath: string, templates: Record<s
810
try {
911
fs.writeFileSync(filePath, content)
1012
} catch (error) {
11-
// Ignore errors if file doesn't exist or can't be written
13+
throw new CLIError(chalk.red(`Failed to write template file: ${filePath}`))
1214
}
1315
}
1416
}

0 commit comments

Comments
 (0)