-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import got from 'got' | ||
import cheerio from 'cheerio' | ||
|
||
const GIT_DOCUMENTATION_URL = 'https://git-scm.com/docs/githooks' | ||
const cheerioObjectToArray = elements => | ||
Array.from(elements).map(element => cheerio(element)) | ||
|
||
async function fetchData() { | ||
const {body: html} = await got(GIT_DOCUMENTATION_URL) | ||
const sections = cheerioObjectToArray(cheerio.load(html)('.sect1')).reduce( | ||
(sections, section) => { | ||
const anchor = section.find('a.anchor') | ||
const parent = anchor.parent() | ||
const id = parent.attr('id').replace(/^_/, '') | ||
|
||
if (anchor.attr('href') !== `#_${id}`) { | ||
return sections | ||
} | ||
|
||
const body = section.find('.sectionbody') | ||
sections[id] = cheerio(body) | ||
return sections | ||
}, | ||
{} | ||
) | ||
|
||
const hooks = cheerioObjectToArray(sections.hooks.find('.sect2')) | ||
.map(section => { | ||
const anchor = section.find('a.anchor').eq(0) | ||
const parent = anchor.parent() | ||
const id = parent.attr('id') | ||
|
||
if (anchor.attr('href') !== `#${id}`) { | ||
return null | ||
} | ||
|
||
const hook = parent.text().trim() | ||
|
||
return { | ||
hook, | ||
description: cheerioObjectToArray(section.find('.paragraph')) | ||
.map(paragraph => paragraph.text()) | ||
.join('\n') | ||
.replace(/\n+/g, '\n') | ||
.trim(), | ||
} | ||
}) | ||
.filter(Boolean) | ||
|
||
return hooks.map(({hook}) => hook) | ||
} | ||
|
||
export default fetchData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,11 @@ | ||
import got from 'got' | ||
import cheerio from 'cheerio' | ||
import writePrettierFile from 'write-prettier-file' | ||
import path from 'path' | ||
import fetch from './fetch' | ||
|
||
const JSON_FILE = path.join(__dirname, '../index.json') | ||
const GIT_DOCUMENTATION_URL = 'https://git-scm.com/docs/githooks' | ||
const cheerioObjectToArray = elements => | ||
Array.from(elements).map(element => cheerio(element)) | ||
|
||
;(async () => { | ||
const {body: html} = await got(GIT_DOCUMENTATION_URL) | ||
const data = await fetch() | ||
|
||
const sections = cheerioObjectToArray(cheerio.load(html)('.sect1')).reduce( | ||
(sections, section) => { | ||
const anchor = section.find('a.anchor') | ||
const parent = anchor.parent() | ||
const id = parent.attr('id').replace(/^_/, '') | ||
|
||
if (anchor.attr('href') !== `#_${id}`) { | ||
return sections | ||
} | ||
|
||
const body = section.find('.sectionbody') | ||
sections[id] = cheerio(body) | ||
return sections | ||
}, | ||
{} | ||
) | ||
|
||
const hooks = cheerioObjectToArray(sections.hooks.find('.sect2')) | ||
.map(section => { | ||
const anchor = section.find('a.anchor').eq(0) | ||
const parent = anchor.parent() | ||
const id = parent.attr('id') | ||
|
||
if (anchor.attr('href') !== `#${id}`) { | ||
return null | ||
} | ||
|
||
const hook = parent.text().trim() | ||
|
||
return { | ||
hook, | ||
description: cheerioObjectToArray(section.find('.paragraph')) | ||
.map(paragraph => paragraph.text()) | ||
.join('\n') | ||
.replace(/\n+/g, '\n') | ||
.trim(), | ||
} | ||
}) | ||
.filter(Boolean) | ||
|
||
writePrettierFile( | ||
JSON_FILE, | ||
JSON.stringify( | ||
hooks.map(({hook}) => hook), | ||
null, | ||
2 | ||
) | ||
) | ||
writePrettierFile(JSON_FILE, JSON.stringify(data, null, 2)) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,33 @@ | ||
const assert = require('assert') | ||
const fs = require('fs') | ||
const hooks = require('.') | ||
import test from 'ava' | ||
import fs from 'fs' | ||
import hooks from './index.json' | ||
import fetch from './scripts/fetch' | ||
|
||
assert.strict.ok(Array.isArray(hooks), 'Git hooks should be an array.') | ||
assert.strict.ok(hooks.length > 0, 'Git hooks should not be empty.') | ||
assert.strict.ok( | ||
hooks.includes('commit-msg'), | ||
`Git hooks should has \`commit-msg\`` | ||
) | ||
test('main', t => { | ||
t.true(Array.isArray(hooks), 'Git hooks should be an array.') | ||
t.true(hooks.length > 0, 'Git hooks should not be empty.') | ||
t.true(hooks.includes('commit-msg'), `Git hooks should has \`commit-msg\`.`) | ||
}) | ||
|
||
const hooksInRepository = [ | ||
...new Set(fs.readdirSync('.git/hooks').filter(file => !file.includes('.'))), | ||
] | ||
test('.git/hooks', t => { | ||
const hooksInRepository = [ | ||
...new Set( | ||
fs.readdirSync('.git/hooks').filter(file => !file.includes('.')) | ||
), | ||
] | ||
const set = new Set(hooks) | ||
|
||
const set = new Set(hooks) | ||
for (const hook of hooksInRepository) { | ||
assert.strict.ok(set.has(hook), `Git hooks should has ${hook}`) | ||
} | ||
for (const hook of hooksInRepository) { | ||
t.true(set.has(hook), `Git hooks should has ${hook}.`) | ||
} | ||
}) | ||
|
||
test('git-scm.com', async t => { | ||
const data = await fetch() | ||
|
||
t.deepEqual( | ||
hooks, | ||
data, | ||
`Git hooks should has be same as data from git-scm.com .` | ||
) | ||
}) |