Skip to content

Commit

Permalink
fix: add script to the top of the index.html's head tag
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdmax committed Feb 24, 2019
1 parent e583c58 commit 35d55d0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/spa-github-pages-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const addScriptToIndexHtml = async (baseDir: string, docsFolderName: string): Pr
)
} else {
// Append spa script to head tag
$index('head').append(scriptContent)
$index('head').prepend(scriptContent)

// Over write old index file with updated one
await fs.writeFile(indexHtmlFilePath, $index.html())
Expand Down
14 changes: 6 additions & 8 deletions test/spa-github-pages-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('addScriptToIndexHtml', () => {
const docsFolderName = 'docs'

describe('with index.html existed', () => {
it('adds scripts to index.html', async () => {
it("adds script to the top of the index.html's head tag", async () => {
await addScriptToIndexHtml(baseDir, docsFolderName)

const scriptContent = await fs.readFile(path.join(__dirname, '../assets/script.txt'), 'utf8')
Expand All @@ -182,12 +182,10 @@ describe('addScriptToIndexHtml', () => {
'utf8'
)
const $index = cheerio.load(copiedIndexHtmlContent)
const childrenLength = $index('head')[0].children.length
const actualScriptStartComment = $index('head')[0].children[childrenLength - 8].data
const actualNoteComment = $index('head')[0].children[childrenLength - 6].data
const actualScriptMainContent = $index('head')[0].children[childrenLength - 4].children[0]
.data
const actualScriptEndComment = $index('head')[0].children[childrenLength - 2].data
const actualScriptStartComment = $index('head')[0].children[0].data
const actualNoteComment = $index('head')[0].children[2].data
const actualScriptMainContent = $index('head')[0].children[4].children[0].data
const actualScriptEndComment = $index('head')[0].children[6].data

expect(actualScriptStartComment).toEqual(expectedScriptStartComment)
expect(actualNoteComment).toEqual(expectedNoteComment)
Expand All @@ -197,7 +195,7 @@ describe('addScriptToIndexHtml', () => {
})

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

Expand Down

0 comments on commit 35d55d0

Please sign in to comment.