Skip to content

Commit 0730bc7

Browse files
committed
support no gitignore file, closes #36
1 parent 4100848 commit 0730bc7

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

__tests__/generator.spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const generator = require('../generator')
2+
const fs = require('fs')
3+
jest.mock('fs')
4+
5+
// Mock the generator api
6+
let completionCb
7+
const mockApi = {
8+
render: jest.fn(),
9+
onCreateComplete: jest.fn(cb => {
10+
completionCb = cb
11+
}),
12+
extendPackage: jest.fn(),
13+
// Make sure api.resolve(path) is used
14+
resolve: jest.fn(path => 'apiResolve_' + path),
15+
hasPlugin: jest.fn()
16+
}
17+
beforeEach(() => {
18+
// Reset mock status
19+
jest.clearAllMocks()
20+
})
21+
22+
test('extends gitignore if it exists', () => {
23+
fs.readFileSync.mockImplementation((path, encoding) => {
24+
// Check that utf8 encoding is set
25+
expect(encoding).toBe('utf8')
26+
// return mock content
27+
return 'existing_content'
28+
})
29+
// Mock existence of .gitignore
30+
fs.existsSync = jest.fn(path => path === 'apiResolve_./.gitignore')
31+
// Run the generator with mock api
32+
generator(mockApi)
33+
// Run the onCreateComplete callback
34+
completionCb()
35+
// New .gitignore should have been written
36+
expect(fs.writeFileSync).toBeCalledWith(
37+
'apiResolve_./.gitignore',
38+
'existing_content\n#Electron-builder output\n/dist_electron'
39+
)
40+
})
41+
test("doesn't modify .gitignore if it doesn't exist", () => {
42+
// Mock lack of .gitignore
43+
fs.existsSync = jest.fn(path => !(path === 'apiResolve_./.gitignore'))
44+
// Run the generator with mock api
45+
generator(mockApi)
46+
// Run the onCreateComplete callback
47+
completionCb()
48+
// New .gitignore should not have been read from or written
49+
expect(fs.writeFileSync).not.toBeCalledWith(
50+
'apiResolve_./.gitignore',
51+
'existing_content\n#Electron-builder output\n/dist_electron'
52+
)
53+
expect(fs.readFileSync).not.toBeCalledWith('apiResolve_./.gitignore', 'utf8')
54+
// Only index should have been read/written
55+
expect(fs.writeFileSync).toHaveBeenCalledTimes(1)
56+
expect(fs.readFileSync).toHaveBeenCalledTimes(1)
57+
})

generator/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ module.exports = api => {
55
api.onCreateComplete(() => {
66
// Read existing index.html and .gitignore
77
let index = fs.readFileSync(api.resolve('./public/index.html'), 'utf8')
8-
let gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')
98
// Add base element inside <head> tag
109
index = index.replace(
1110
/^\s*?<head.*?>\s*?$/m,
1211
`<head>\n <% if (BASE_URL === './') { %><base href="app://./" /><% } %>`
1312
)
14-
// Add /dist_electron to gitignore
15-
gitignore = gitignore + '\n#Electron-builder output\n/dist_electron'
16-
// Write updated files
13+
// Write updated index.html
1714
fs.writeFileSync(api.resolve('./public/index.html'), index)
18-
fs.writeFileSync(api.resolve('./.gitignore'), gitignore)
15+
// Update .gitignore if it exists
16+
if (fs.existsSync(api.resolve('./.gitignore'))) {
17+
let gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')
18+
// Add /dist_electron to gitignore
19+
gitignore = gitignore + '\n#Electron-builder output\n/dist_electron'
20+
fs.writeFileSync(api.resolve('./.gitignore'), gitignore)
21+
}
1922
if (api.hasPlugin('typescript')) {
2023
let background
2124
if (fs.existsSync(api.resolve('./src/background.js'))) {

0 commit comments

Comments
 (0)