-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Core-users-docs' of https://github.com/newrelic/docs-we…
…bsite into Core-users-docs
- Loading branch information
Showing
46 changed files
with
359 additions
and
30 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,72 @@ | ||
const { | ||
getFilePaths, | ||
getUserInputs, | ||
separateData, | ||
joinData, | ||
addRedirect, | ||
removeRedirect, | ||
} = require('./handlers'); | ||
const fs = require('fs'); | ||
|
||
/** | ||
* Process the results from the add/remove redirects and prints them | ||
* @param {Object} results - Object containing either added/skipped/removed redirects | ||
*/ | ||
const processResults = (results) => { | ||
const addedRedirects = results.filter((file) => file.added); | ||
const skippedRedirects = results.filter((file) => file.skipped); | ||
const removedRedirects = results.filter((file) => file.removed); | ||
|
||
if (addedRedirects.length) { | ||
console.log('(✔️) Added Redirects to the following paths:'); | ||
addedRedirects.forEach((filepath) => console.log(` - ${filepath.added}`)); | ||
} | ||
|
||
if (removedRedirects.length) { | ||
console.log('(✔️) Removed Redirects from the following paths:'); | ||
removedRedirects.forEach((filepath) => | ||
console.log(` - ${filepath.removed}`) | ||
); | ||
} | ||
|
||
if (skippedRedirects.length) { | ||
console.log( | ||
'(✔️) The following files either contain no redirects or already contain the slug:' | ||
); | ||
skippedRedirects.forEach((filepath) => | ||
console.log(` - ${filepath.skipped}`) | ||
); | ||
} | ||
}; | ||
|
||
const main = async () => { | ||
const { action, directory } = getUserInputs(); | ||
const filePaths = getFilePaths(directory); | ||
|
||
if (!filePaths.length) { | ||
console.warn(`<!> No .mdx files found in the directory. Please check: ${directory}`); | ||
return; | ||
} | ||
|
||
const separatedData = filePaths.map(separateData); | ||
|
||
const editedData = separatedData.map((data) => { | ||
if (action === 'add') { | ||
return addRedirect(data); | ||
} | ||
return removeRedirect(data); | ||
}); | ||
|
||
const joinedData = editedData.map(joinData); | ||
|
||
joinedData.forEach(({ data, path }) => { | ||
try { | ||
fs.writeFileSync(path, String(data)); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}); | ||
processResults(editedData); | ||
}; | ||
|
||
main(); |
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,170 @@ | ||
const glob = require('glob'); | ||
const frontmatterGH = require('@github-docs/frontmatter'); | ||
const fs = require('fs'); | ||
|
||
/** | ||
* Adds slashes to beginning and end of directory if missing | ||
* @param {String} directory - The directory to add slashes to | ||
* @returns {String} The directory with slashes added if necessary | ||
*/ | ||
const formatDirectory = (directory) => { | ||
if (!directory.startsWith('/')) { | ||
return `/${directory}`; | ||
} else if (!directory.endsWith('/')) { | ||
return directory.concat('/'); | ||
} | ||
|
||
return directory; | ||
}; | ||
|
||
/** | ||
* Gets slug from path, relative to contents folder | ||
* @param {String} filepath - The full filepath of the file | ||
* @returns {String} The slug of the filepath | ||
*/ | ||
const getSlugFromPath = (filepath) => { | ||
const slug = filepath.match(/content(.*?).mdx/)[1]; | ||
if (!slug) { | ||
console.error( | ||
`<!> Could not find slug from path: ${filepath}, please check filepath` | ||
); | ||
process.exit(1); | ||
} | ||
return slug; | ||
}; | ||
|
||
/** | ||
* Reads a file and separates the content and frontmatter data | ||
* @param {String} path - The full filepath of the file | ||
* @returns {Object} Original path, slug from path, content, frontmatter data | ||
*/ | ||
const separateData = (path) => { | ||
const file = fs.readFileSync(path, 'utf8'); | ||
const slug = getSlugFromPath(path); | ||
const separateData = frontmatterGH(file); | ||
const { data: frontmatterData, content } = separateData; | ||
if (!frontmatterData.redirects) { | ||
frontmatterData.redirects = []; | ||
} | ||
return { path, content, frontmatterData, slug }; | ||
}; | ||
|
||
/** | ||
* Joins the content and frontmatter data | ||
* @param {Object} data - content, frontmatterData, path | ||
* @returns {Object} Combined data and path | ||
*/ | ||
const joinData = ({ content, frontmatterData, path }) => { | ||
const data = frontmatterGH.stringify(content, frontmatterData, { | ||
lineWidth: -1, | ||
}); | ||
|
||
return { path, data }; | ||
}; | ||
|
||
/** | ||
* Adds a redirect if the slug is not already in the redirects array | ||
* @param {Object} data - slug, frontmatterData, rest | ||
* @returns {Object} Edited frontmatterData, whether the redirect was added or skipped | ||
*/ | ||
const addRedirect = ({ frontmatterData, slug, ...rest }) => { | ||
if (frontmatterData.redirects.includes(slug)) { | ||
return { frontmatterData, ...rest, skipped: slug }; | ||
} | ||
return { | ||
frontmatterData: { | ||
...frontmatterData, | ||
redirects: [...frontmatterData.redirects, slug], | ||
}, | ||
...rest, | ||
added: slug, | ||
}; | ||
}; | ||
|
||
/** | ||
* Removes a redirect if the slug is in the redirects array | ||
* @param {Object} data - slug, frontmatterData, rest | ||
* @returns {Object} Edited frontmatterData, whether the redirect was removed or skipped | ||
*/ | ||
const removeRedirect = ({ frontmatterData, slug, ...rest }) => { | ||
if (frontmatterData.redirects.includes(slug)) { | ||
frontmatterData.redirects = frontmatterData.redirects.filter( | ||
(path) => path !== slug | ||
); | ||
if (frontmatterData.redirects.length === 0) { | ||
delete frontmatterData.redirects; | ||
} | ||
return { | ||
frontmatterData, | ||
...rest, | ||
removed: slug, | ||
}; | ||
} | ||
return { frontmatterData, ...rest, skipped: slug }; | ||
}; | ||
|
||
/** | ||
* Globs an array of all mdx files in a directory + sub-directories | ||
* @param {String} directory - The parent directory to search | ||
* @returns {String[]} All mdx files in the directory and sub-directories | ||
*/ | ||
const getFilePaths = (directory) => { | ||
const subDir = directory.length ? directory : '/docs/'; | ||
return glob.sync(`${__dirname}/../../../src/content${subDir}**/*.mdx`); | ||
}; | ||
|
||
/** | ||
* Retrieves the user inputs from command line | ||
* @returns {Object} The directory and action specified by the user | ||
*/ | ||
const getUserInputs = () => { | ||
const userInputs = process.argv.slice(2); | ||
const actionInput = userInputs.find((input) => | ||
['--add', '--remove'].includes(input) | ||
); | ||
|
||
const printExample = () => | ||
console.warn( | ||
'Example: yarn add-remove-redirects --add src/content/docs/apm' | ||
); | ||
|
||
if (!actionInput) { | ||
console.warn( | ||
'<!> No action specified, please append `--add` or `--remove` to the command' | ||
); | ||
printExample(); | ||
process.exit(0); | ||
} | ||
|
||
const action = actionInput.replace('--', ''); | ||
|
||
const directoryInput = userInputs.find((input) => input !== actionInput); | ||
|
||
if (!directoryInput) { | ||
console.warn( | ||
'<!> No directory specified, please include the directory you want to apply changes to' | ||
); | ||
printExample(); | ||
process.exit(0); | ||
} | ||
|
||
const formattedDirectory = formatDirectory(directoryInput); | ||
if (!fs.existsSync(`src/content/${formattedDirectory}`)) { | ||
console.warn( | ||
'<!> The directory does not exist, please check the your path. Remember that you should not include the `src/content` portion of the path.' | ||
); | ||
printExample(); | ||
process.exit(0); | ||
} | ||
|
||
return { action, directory: formattedDirectory }; | ||
}; | ||
|
||
module.exports = { | ||
getFilePaths, | ||
getUserInputs, | ||
separateData, | ||
joinData, | ||
addRedirect, | ||
removeRedirect, | ||
}; |
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
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
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
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
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
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
Oops, something went wrong.