-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
f41fa81
commit 2d0d59d
Showing
2 changed files
with
195 additions
and
91 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 |
---|---|---|
@@ -1,71 +1,122 @@ | ||
const core = require('@actions/core'); | ||
const github = require('@actions/github'); | ||
const fs = require('fs'); | ||
const algoliasearch = require('algoliasearch'); | ||
const crypto = require('crypto'); | ||
const core = require("@actions/core"); | ||
const github = require("@actions/github"); | ||
const fs = require("fs"); | ||
const algoliasearch = require("algoliasearch"); | ||
const crypto = require("crypto"); | ||
|
||
function extractMetadata(content, metadataName) { | ||
const regex = new RegExp(`---\\s*[\\s\\S]*?${metadataName}:(\\s*.*?)\\s*\\n\\s*[\\s\\S]*?^---`, 'gm'); | ||
const match = content.match(regex); | ||
const regex = new RegExp( | ||
`---\\s*[\\s\\S]*?${metadataName}:(\\s*.*?)\\s*\\n\\s*[\\s\\S]*?^---`, | ||
"gm" | ||
); | ||
const match = content.match(regex); | ||
|
||
const res = regex.exec(content) | ||
const res = regex.exec(content); | ||
|
||
if (res && res[1]) { | ||
return res[1].trim(); | ||
} else { | ||
return null; | ||
} | ||
if (res && res[1]) { | ||
return res[1].trim(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
function generateObjectID(file) { | ||
return crypto.createHash('md5').update("documentation_" + file).digest('hex'); | ||
return crypto | ||
.createHash("md5") | ||
.update("documentation_" + file) | ||
.digest("hex"); | ||
} | ||
|
||
function getModifiedFiled() { | ||
const addedFiles = JSON.parse(process.env.ADDED_FILES); | ||
const deletedFiles = JSON.parse(process.env.DELETED_FILES); | ||
const modifiedFiles = JSON.parse(process.env.MODIFIED_FILES); | ||
|
||
return { | ||
filesToIndex: [...addedFiles, ...modifiedFiles].filter( | ||
(file) => file.endsWith(".md") && !file.startsWith("_partials/") | ||
), | ||
deletedFiles, | ||
}; | ||
} | ||
|
||
function getAllFiles() { | ||
const rootDir = process.env.GITHUB_WORKSPACE; | ||
|
||
const walkSync = (dir, filelist = []) => { | ||
fs.readdirSync(dir).forEach((file) => { | ||
const filePath = `${dir}/${file}`; | ||
if (fs.statSync(filePath).isDirectory()) { | ||
filelist = walkSync(filePath, filelist); | ||
} else { | ||
if (filePath.endsWith(".md") && filePath.indexOf("_partials/") === -1) { | ||
filelist = [...filelist, filePath]; | ||
} | ||
} | ||
}); | ||
return filelist; | ||
}; | ||
|
||
return { | ||
filesToIndex: walkSync(rootDir), | ||
deletedFiles: [], | ||
}; | ||
} | ||
|
||
try { | ||
const addedFiles = JSON.parse(process.env.ADDED_FILES); | ||
const deletedFiles = JSON.parse(process.env.DELETED_FILES); | ||
const modifiedFiles = JSON.parse(process.env.MODIFIED_FILES); | ||
const algoliaIndex = process.env.ALGOLIA_INDEX; | ||
const algoliaAppId = process.env.ALGOLIA_APPLICATION_ID; | ||
const algoliaApiKey = process.env.ALGOLIA_API_KEY; | ||
const algoliaIndex = process.env.ALGOLIA_INDEX; | ||
const algoliaAppId = process.env.ALGOLIA_APPLICATION_ID; | ||
const algoliaApiKey = process.env.ALGOLIA_API_KEY; | ||
|
||
console.log(process.env); | ||
console.log(process.env); | ||
|
||
if (!algoliaIndex || !algoliaAppId || !algoliaApiKey) { | ||
throw new Error("Missing Algolia configuration") | ||
} | ||
if (!algoliaIndex || !algoliaAppId || !algoliaApiKey) { | ||
throw new Error("Missing Algolia configuration"); | ||
} | ||
|
||
const client = algoliasearch(algoliaAppId, algoliaApiKey) | ||
const client = algoliasearch(algoliaAppId, algoliaApiKey); | ||
|
||
const index = client.initIndex(algoliaIndex) | ||
const index = client.initIndex(algoliaIndex); | ||
|
||
const filesToIndex = [...addedFiles, ...modifiedFiles].filter(file => | ||
file.endsWith('.md') && !file.startsWith('_partials/') | ||
); | ||
const { filesToIndex, deletedFiles } = | ||
process.env.MODE === "reindexation" ? getAllFiles() : getModifiedFiled(); | ||
|
||
for (const file of filesToIndex) { | ||
const content = fs.readFileSync(file, 'utf8'); | ||
console.log(filesToIndex); | ||
|
||
const title = extractMetadata(content, 'title'); | ||
const description = extractMetadata(content, 'description'); | ||
for (const file of filesToIndex) { | ||
const content = fs.readFileSync(file, "utf8"); | ||
|
||
if (title) { | ||
let slug = file.replace('.md', '') | ||
if (slug.endsWith('/README')) { | ||
slug = slug.replace('/README', ''); | ||
} | ||
const record = { objectID: generateObjectID(file), type: "documentation", preview: description, title: title, content: [content], slug } | ||
index.saveObject(record).wait() | ||
} else { | ||
console.log(`No title found for ${file}. Skipping...`); | ||
} | ||
} | ||
const title = extractMetadata(content, "title"); | ||
const description = extractMetadata(content, "description"); | ||
|
||
for (const file of deletedFiles) { | ||
index.deleteObject(generateObjectID(file)).wait() | ||
if (title) { | ||
let slug = file.replace(".md", ""); | ||
if (slug.endsWith("/README")) { | ||
slug = slug.replace("/README", ""); | ||
} | ||
const record = { | ||
objectID: generateObjectID(file), | ||
type: "documentation", | ||
preview: description, | ||
title: title, | ||
content: [content], | ||
slug, | ||
}; | ||
try { | ||
index.saveObject(record).wait(); | ||
} catch (error) { | ||
console.log(`Error while indexing ${file}: ${error.message}`); | ||
} | ||
} else { | ||
console.log(`No title found for ${file}. Skipping...`); | ||
} | ||
} | ||
|
||
for (const file of deletedFiles) { | ||
index.deleteObject(generateObjectID(file)).wait(); | ||
} | ||
|
||
core.setOutput("response", "ok"); | ||
core.setOutput("response", "ok"); | ||
} catch (error) { | ||
core.setFailed(error.message); | ||
core.setFailed(error.message); | ||
} |