Skip to content

Commit

Permalink
support no gitignore file, closes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
nklayman committed Jul 21, 2018
1 parent 4100848 commit 0730bc7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
57 changes: 57 additions & 0 deletions __tests__/generator.spec.js
@@ -0,0 +1,57 @@
const generator = require('../generator')
const fs = require('fs')
jest.mock('fs')

// Mock the generator api
let completionCb
const mockApi = {
render: jest.fn(),
onCreateComplete: jest.fn(cb => {
completionCb = cb
}),
extendPackage: jest.fn(),
// Make sure api.resolve(path) is used
resolve: jest.fn(path => 'apiResolve_' + path),
hasPlugin: jest.fn()
}
beforeEach(() => {
// Reset mock status
jest.clearAllMocks()
})

test('extends gitignore if it exists', () => {
fs.readFileSync.mockImplementation((path, encoding) => {
// Check that utf8 encoding is set
expect(encoding).toBe('utf8')
// return mock content
return 'existing_content'
})
// Mock existence of .gitignore
fs.existsSync = jest.fn(path => path === 'apiResolve_./.gitignore')
// Run the generator with mock api
generator(mockApi)
// Run the onCreateComplete callback
completionCb()
// New .gitignore should have been written
expect(fs.writeFileSync).toBeCalledWith(
'apiResolve_./.gitignore',
'existing_content\n#Electron-builder output\n/dist_electron'
)
})
test("doesn't modify .gitignore if it doesn't exist", () => {
// Mock lack of .gitignore
fs.existsSync = jest.fn(path => !(path === 'apiResolve_./.gitignore'))
// Run the generator with mock api
generator(mockApi)
// Run the onCreateComplete callback
completionCb()
// New .gitignore should not have been read from or written
expect(fs.writeFileSync).not.toBeCalledWith(
'apiResolve_./.gitignore',
'existing_content\n#Electron-builder output\n/dist_electron'
)
expect(fs.readFileSync).not.toBeCalledWith('apiResolve_./.gitignore', 'utf8')
// Only index should have been read/written
expect(fs.writeFileSync).toHaveBeenCalledTimes(1)
expect(fs.readFileSync).toHaveBeenCalledTimes(1)
})
13 changes: 8 additions & 5 deletions generator/index.js
Expand Up @@ -5,17 +5,20 @@ module.exports = api => {
api.onCreateComplete(() => {
// Read existing index.html and .gitignore
let index = fs.readFileSync(api.resolve('./public/index.html'), 'utf8')
let gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')
// Add base element inside <head> tag
index = index.replace(
/^\s*?<head.*?>\s*?$/m,
`<head>\n <% if (BASE_URL === './') { %><base href="app://./" /><% } %>`
)
// Add /dist_electron to gitignore
gitignore = gitignore + '\n#Electron-builder output\n/dist_electron'
// Write updated files
// Write updated index.html
fs.writeFileSync(api.resolve('./public/index.html'), index)
fs.writeFileSync(api.resolve('./.gitignore'), gitignore)
// Update .gitignore if it exists
if (fs.existsSync(api.resolve('./.gitignore'))) {
let gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')
// Add /dist_electron to gitignore
gitignore = gitignore + '\n#Electron-builder output\n/dist_electron'
fs.writeFileSync(api.resolve('./.gitignore'), gitignore)
}
if (api.hasPlugin('typescript')) {
let background
if (fs.existsSync(api.resolve('./src/background.js'))) {
Expand Down

0 comments on commit 0730bc7

Please sign in to comment.