Skip to content

Commit 6ac415d

Browse files
committed
refactor(themes): build paths in rewriteJsAndCss with path.join
Matches the read side in migrate.ts and addresses a review nit. Tests follow the same pattern so they stay portable on Windows.
1 parent 4b5b97c commit 6ac415d

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as sinon from 'sinon'
22
import * as fs from 'fs'
3+
import * as path from 'path'
34
import { expect } from '@oclif/test'
45
import rewriteJsAndCss from './rewriteJsAndCss'
56

@@ -17,27 +18,29 @@ describe('rewriteJsAndCss', () => {
1718
})
1819

1920
expect(writeFileSyncStub.callCount).to.equal(2)
20-
expect(writeFileSyncStub.firstCall.args[0]).to.equal('theme/path/style.css')
21+
expect(writeFileSyncStub.firstCall.args[0]).to.equal(path.join('theme/path', 'style.css'))
2122
expect(writeFileSyncStub.firstCall.args[1]).to.equal('body { color: red; }')
22-
expect(writeFileSyncStub.secondCall.args[0]).to.equal('theme/path/script.js')
23+
expect(writeFileSyncStub.secondCall.args[0]).to.equal(path.join('theme/path', 'script.js'))
2324
expect(writeFileSyncStub.secondCall.args[1]).to.equal('console.log("hi")')
2425
})
2526

2627
it('throws if style.css cannot be written', () => {
28+
const cssPath = path.join('theme/path', 'style.css')
2729
const writeFileSyncStub = sinon.stub(fs, 'writeFileSync')
28-
writeFileSyncStub.withArgs('theme/path/style.css').throws(new Error('Permission denied'))
30+
writeFileSyncStub.withArgs(cssPath).throws(new Error('Permission denied'))
2931

3032
expect(() => {
3133
rewriteJsAndCss('theme/path', { css: 'a', js: 'b' })
32-
}).to.throw('Failed to write file: theme/path/style.css')
34+
}).to.throw(`Failed to write file: ${cssPath}`)
3335
})
3436

3537
it('throws if script.js cannot be written', () => {
38+
const jsPath = path.join('theme/path', 'script.js')
3639
const writeFileSyncStub = sinon.stub(fs, 'writeFileSync')
37-
writeFileSyncStub.withArgs('theme/path/script.js').throws(new Error('Permission denied'))
40+
writeFileSyncStub.withArgs(jsPath).throws(new Error('Permission denied'))
3841

3942
expect(() => {
4043
rewriteJsAndCss('theme/path', { css: 'a', js: 'b' })
41-
}).to.throw('Failed to write file: theme/path/script.js')
44+
}).to.throw(`Failed to write file: ${jsPath}`)
4245
})
4346
})

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { CLIError } from '@oclif/core/lib/errors'
22
import * as fs from 'fs'
3+
import * as path from 'path'
34
import * as chalk from 'chalk'
45

56
export default function rewriteJsAndCss (
67
themePath: string,
78
{ css, js }: { css: string; js: string }
89
) {
9-
const cssPath = `${themePath}/style.css`
10-
const jsPath = `${themePath}/script.js`
10+
const cssPath = path.join(themePath, 'style.css')
11+
const jsPath = path.join(themePath, 'script.js')
1112

1213
try {
1314
fs.writeFileSync(cssPath, css)

0 commit comments

Comments
 (0)