Skip to content

Commit

Permalink
fix: prevent duplicated script tag added
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdmax committed Feb 23, 2019
1 parent c6514e4 commit 8ee87ad
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/spa-github-pages-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,20 @@ const addScriptToIndexHtml = async (baseDir: string, docsFolderName: string): Pr
const scriptContent = await fs.readFile(scriptFilePath, 'utf8')
const $index = cheerio.load(indexHtmlContent)

// Append spa script to head tag
$index('head').append(scriptContent)
if ($index.html().includes(scriptContent)) {
log(
'It seems you have already added the SPA script, you only need one script for index.html',
'info'
)
} else {
// Append spa script to head tag
$index('head').append(scriptContent)

// Over write old index file with updated one
await fs.writeFile(indexHtmlFilePath, $index.html())
// Over write old index file with updated one
await fs.writeFile(indexHtmlFilePath, $index.html())

log('Add script to index.html successfully!!!', 'info')
log('Add script to index.html successfully!!!', 'info')
}
}

const makeGHPageSPAFactory = (
Expand Down
43 changes: 43 additions & 0 deletions test/spa-github-pages-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,49 @@ describe('addScriptToIndexHtml', () => {
})
})

describe('with index.html and spa script existed', () => {
it('adds scripts to index.html only once', async () => {
await addScriptToIndexHtml(baseDir, docsFolderName)
await addScriptToIndexHtml(baseDir, docsFolderName)

const scriptContent = await fs.readFile(path.join(__dirname, '../assets/script.txt'), 'utf8')
const $script = cheerio.load(scriptContent, {
xmlMode: true
})
const scriptStartComment = $script.root()[0].children[0].data
const noteComment = $script.root()[0].children[2].data
const scriptMainContent = $script.root()[0].children[4].children[0].data
const scriptEndComment = $script.root()[0].children[6].data

const copiedIndexHtmlContent = await fs.readFile(
path.join(__dirname, 'docs/index.html'),
'utf8'
)
const $index = cheerio.load(copiedIndexHtmlContent)
const $indexHeadChildren = $index('head')[0].children
const actualScriptStartComment = $indexHeadChildren.filter(
$indexHeadChild => $indexHeadChild.data === scriptStartComment
)
const actualNoteComment = $indexHeadChildren.filter(
$indexHeadChild => $indexHeadChild.data === noteComment
)
const actualScriptMainContent = $indexHeadChildren.filter(
$indexHeadChild =>
$indexHeadChild.children &&
$indexHeadChild.children[0] &&
$indexHeadChild.children[0].data === scriptMainContent
)
const actualScriptEndComment = $indexHeadChildren.filter(
$indexHeadChild => $indexHeadChild.data === scriptEndComment
)

expect(actualScriptStartComment.length).toEqual(1)
expect(actualNoteComment.length).toEqual(1)
expect(actualScriptMainContent.length).toEqual(1)
expect(actualScriptEndComment.length).toEqual(1)
})
})

describe('with index.html not existed', () => {
it('throws error', async () => {
await fs.remove(path.join(__dirname, 'docs/index.html'))
Expand Down

0 comments on commit 8ee87ad

Please sign in to comment.