Skip to content

Commit

Permalink
fix: remove usage of readFileAsync (github#28053)
Browse files Browse the repository at this point in the history
* fix: remove usage of readFileAsync

* fix: use same import style

* fix: update tests

Co-authored-by: Peter Bengtsson <[email protected]>
  • Loading branch information
mikesurowiec and peterbe authored Jun 1, 2022
1 parent 35b5427 commit b4608a8
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 63 deletions.
5 changes: 2 additions & 3 deletions lib/all-products.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import fs from 'fs/promises'
import path from 'path'
import readFileAsync from './readfile-async.js'
import frontmatter from './read-frontmatter.js'
import getApplicableVersions from './get-applicable-versions.js'
import removeFPTFromPath from './remove-fpt-from-path.js'

// Both internal and external products are specified in content/index.md
const homepage = path.posix.join(process.cwd(), 'content/index.md')
const { data } = frontmatter(await readFileAsync(homepage, 'utf8'))
const { data } = frontmatter(await fs.readFile(homepage, 'utf8'))

export const productIds = data.children
export const productGroups = []
Expand All @@ -27,7 +26,7 @@ for (const productId of productIds) {
}

const toc = path.posix.join(dir, 'index.md')
const { data } = frontmatter(await readFileAsync(toc, 'utf8'))
const { data } = frontmatter(await fs.readFile(toc, 'utf8'))
const applicableVersions = getApplicableVersions(data.versions, toc)
const href = removeFPTFromPath(path.posix.join('/', applicableVersions[0], productId))

Expand Down
4 changes: 2 additions & 2 deletions lib/check-node-version.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs/promises'
import semver from 'semver'
import path from 'path'
import readFileAsync from './readfile-async.js'

const packageFile = JSON.parse(await readFileAsync(path.join(process.cwd(), './package.json')))
const packageFile = JSON.parse(await fs.readFile(path.join(process.cwd(), './package.json')))
const { engines } = packageFile

/* istanbul ignore next */
Expand Down
7 changes: 4 additions & 3 deletions lib/enterprise-server-releases.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import versionSatisfiesRange from './version-satisfies-range.js'
import path from 'path'
import readFileAsync from './readfile-async.js'
import fs from 'fs/promises'

import versionSatisfiesRange from './version-satisfies-range.js'

export const dates = JSON.parse(
await readFileAsync(path.join(process.cwd(), './lib/enterprise-dates.json'))
await fs.readFile(path.join(process.cwd(), './lib/enterprise-dates.json'))
)

// GHES Release Lifecycle Dates:
Expand Down
5 changes: 3 additions & 2 deletions lib/read-file-contents.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import readFileAsync from './readfile-async.js'
import fs from 'fs/promises'

import encodeBracketedParentheses from './encode-bracketed-parentheses.js'
import fm from './frontmatter.js'

/**
* Read only the frontmatter from file
*/
export default async function fmfromf(filepath) {
let fileContent = await readFileAsync(filepath, 'utf8')
let fileContent = await fs.readFile(filepath, 'utf8')

fileContent = encodeBracketedParentheses(fileContent)

Expand Down
6 changes: 0 additions & 6 deletions lib/readfile-async.js

This file was deleted.

6 changes: 3 additions & 3 deletions lib/search/lunr-search.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { fileURLToPath } from 'url'
import path from 'path'
import lunr from 'lunr'
import fs from 'fs/promises'
import lunrStemmerSupport from 'lunr-languages/lunr.stemmer.support.js'
import tinyseg from 'lunr-languages/tinyseg.js'
import lunrJa from 'lunr-languages/lunr.ja.js'
import lunrEs from 'lunr-languages/lunr.es.js'
import lunrPt from 'lunr-languages/lunr.pt.js'
import { get } from 'lodash-es'
import statsd from '../statsd.js'
import readFileAsync from '../readfile-async.js'
import { namePrefix } from './config.js'
import { decompress } from './compress.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
Expand Down Expand Up @@ -317,13 +317,13 @@ export default async function loadLunrResults({ version, language, query, limit
async function loadLunrIndex(indexName) {
const filePath = path.posix.join(__dirname, LUNR_DIR, `${indexName}.json.br`)
// Do not set to 'utf8' on file reads
return readFileAsync(filePath).then(decompress).then(JSON.parse).then(lunr.Index.load)
return fs.readFile(filePath).then(decompress).then(JSON.parse).then(lunr.Index.load)
}

async function loadLunrRecords(indexName) {
const filePath = path.posix.join(__dirname, LUNR_DIR, `${indexName}-records.json.br`)
// Do not set to 'utf8' on file reads
return readFileAsync(filePath).then(decompress).then(JSON.parse)
return fs.readFile(filePath).then(decompress).then(JSON.parse)
}

// Highlight a match within an attribute field
Expand Down
3 changes: 1 addition & 2 deletions script/i18n/fix-translation-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import path from 'path'
import { execSync } from 'child_process'
import { get, set } from 'lodash-es'
import fs from 'fs'
import readFileAsync from '../../lib/readfile-async.js'
import fm from '../../lib/frontmatter.js'
import matter from 'gray-matter'
import chalk from 'chalk'
Expand All @@ -31,7 +30,7 @@ async function main() {
const loadAndValidateContent = async (path, schema) => {
let fileContents
try {
fileContents = await readFileAsync(path, 'utf8')
fileContents = await fs.promises.readFile(path, 'utf8')
} catch (e) {
if (fs.existsSync(path)) {
console.error(e.message)
Expand Down
3 changes: 1 addition & 2 deletions script/i18n/homogenize-frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import path from 'path'
import fs from 'fs/promises'
import matter from 'gray-matter'
import walk from 'walk-sync'
import readFileAsync from '../../lib/readfile-async.js'
import fm from '../../lib/frontmatter.js'

// Run!
Expand Down Expand Up @@ -42,7 +41,7 @@ async function main() {
}

async function extractFrontmatter(path) {
const fileContents = await readFileAsync(path, 'utf8')
const fileContents = await fs.readFile(path, 'utf8')
return fm(fileContents)
}

Expand Down
3 changes: 1 addition & 2 deletions script/rendered-content-link-checker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import getRedirect from '../lib/get-redirect.js'
import warmServer from '../lib/warm-server.js'
import renderContent from '../lib/render-content/index.js'
import { deprecated } from '../lib/enterprise-server-releases.js'
import readFileAsync from '../lib/readfile-async.js'

const STATIC_PREFIXES = {
assets: path.resolve('assets'),
Expand Down Expand Up @@ -116,7 +115,7 @@ async function main(opts, files) {
}

if (list) {
const fileList = JSON.parse(await readFileAsync(list))
const fileList = JSON.parse(await fs.promises.readFile(list))
if (Array.isArray(fileList) && fileList.length > 0) {
files = fileList
} else {
Expand Down
3 changes: 1 addition & 2 deletions script/rest/test-open-api-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import fs from 'fs'
import path from 'path'
import _ from 'lodash'

import readFileAsync from '../../lib/readfile-async.js'
import frontmatter from '../../lib/read-frontmatter.js'
import getApplicableVersions from '../../lib/get-applicable-versions.js'
import { allVersions } from '../../lib/all-versions.js'
Expand Down Expand Up @@ -86,7 +85,7 @@ async function createCheckContentDirectory(contentFiles) {
}, {})

for (const filename of contentFiles) {
const { data } = frontmatter(await readFileAsync(filename, 'utf8'))
const { data } = frontmatter(await fs.promises.readFile(filename, 'utf8'))
const applicableVersions = getApplicableVersions(data.versions, filename)
const splitPath = filename.split('/')
const subCategory = splitPath[splitPath.length - 1].replace('.md', '')
Expand Down
9 changes: 4 additions & 5 deletions tests/content/category-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import matter from '../../lib/read-frontmatter.js'
import { zip, difference } from 'lodash-es'
import GithubSlugger from 'github-slugger'
import { decode } from 'html-entities'
import readFileAsync from '../../lib/readfile-async.js'
import loadSiteData from '../../lib/site-data.js'
import renderContent from '../../lib/render-content/index.js'
import getApplicableVersions from '../../lib/get-applicable-versions.js'
Expand Down Expand Up @@ -71,7 +70,7 @@ describe('category pages', () => {
const categoryDir = path.dirname(indexAbsPath)

// Get child article links included in each subdir's index page
const indexContents = await readFileAsync(indexAbsPath, 'utf8')
const indexContents = await fs.promises.readFile(indexAbsPath, 'utf8')
const { data } = matter(indexContents)
categoryVersions = getApplicableVersions(data.versions, indexAbsPath)
categoryChildTypes = []
Expand Down Expand Up @@ -101,7 +100,7 @@ describe('category pages', () => {
await Promise.all(
articleLinks.map(async (articleLink) => {
const articlePath = getPath(productDir, indexLink, articleLink)
const articleContents = await readFileAsync(articlePath, 'utf8')
const articleContents = await fs.promises.readFile(articlePath, 'utf8')
const { data } = matter(articleContents)

// Do not include map topics in list of published articles
Expand All @@ -123,7 +122,7 @@ describe('category pages', () => {
availableArticlePaths = (
await Promise.all(
childFilePaths.map(async (articlePath) => {
const articleContents = await readFileAsync(articlePath, 'utf8')
const articleContents = await fs.promises.readFile(articlePath, 'utf8')
const { data } = matter(articleContents)

// Do not include map topics nor hidden pages in list of available articles
Expand All @@ -137,7 +136,7 @@ describe('category pages', () => {

await Promise.all(
childFilePaths.map(async (articlePath) => {
const articleContents = await readFileAsync(articlePath, 'utf8')
const articleContents = await fs.promises.readFile(articlePath, 'utf8')
const { data } = matter(articleContents)

articleVersions[articlePath] = getApplicableVersions(data.versions, articlePath)
Expand Down
6 changes: 3 additions & 3 deletions tests/content/liquid-line-breaks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import walk from 'walk-sync'
import matter from '../../lib/read-frontmatter.js'
import { zip } from 'lodash-es'
import yaml from 'js-yaml'
import readFileAsync from '../../lib/readfile-async.js'
import fs from 'fs/promises'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const rootDir = path.join(__dirname, '../..')
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Liquid references', () => {
test.each([...contentMarkdownTuples, ...reusableMarkdownTuples])(
'in "%s"',
async (markdownRelPath, markdownAbsPath) => {
const fileContents = await readFileAsync(markdownAbsPath, 'utf8')
const fileContents = await fs.readFile(markdownAbsPath, 'utf8')
const { content } = matter(fileContents)

const matches = content.match(liquidRefsWithLinkBreaksRegex) || []
Expand All @@ -79,7 +79,7 @@ describe('Liquid references', () => {
const variableYamlTuples = zip(variableYamlRelPaths, variableYamlAbsPaths)

test.each(variableYamlTuples)('in "%s"', async (yamlRelPath, yamlAbsPath) => {
const fileContents = await readFileAsync(yamlAbsPath, 'utf8')
const fileContents = await fs.readFile(yamlAbsPath, 'utf8')
const dictionary = yaml.load(fileContents, { filename: yamlRelPath })

const matches = []
Expand Down
26 changes: 13 additions & 13 deletions tests/content/remove-liquid-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fileURLToPath } from 'url'
import path from 'path'
import cheerio from 'cheerio'
import matter from 'gray-matter'
import readFileAsync from '../../lib/readfile-async.js'
import fs from 'fs/promises'
import removeLiquidStatements from '../../script/helpers/remove-liquid-statements'
import removeDeprecatedFrontmatter from '../../script/helpers/remove-deprecated-frontmatter'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
Expand Down Expand Up @@ -39,7 +39,7 @@ function processFrontmatter(contents, file) {

describe('removing liquid statements only', () => {
test('removes liquid statements that specify "greater than version to deprecate"', async () => {
let contents = await readFileAsync(greaterThan, 'utf8')
let contents = await fs.readFile(greaterThan, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example1').text().trim()).toBe(`{% ifversion ghes %}\n
Expand All @@ -65,7 +65,7 @@ Alpha\n\n{% else %}\n\nBravo\n\n{% ifversion ghes > 2.16 %}\n\nCharlie\n
{% else %}\n\nBravo\n\n{% endif %}`)
})
test('removes liquid statements that specify all known versions, including some nested conditionals"', async () => {
let contents = await readFileAsync(unnecessary, 'utf8')
let contents = await fs.readFile(unnecessary, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example1').text().trim()).toBe(`Alpha`)
Expand All @@ -84,7 +84,7 @@ Alpha\n\n{% else %}\n\nBravo\n\n{% ifversion ghes > 2.16 %}\n\nCharlie\n
})

test('removes liquid statements that specify "and greater than version to deprecate"', async () => {
let contents = await readFileAsync(andGreaterThan1, 'utf8')
let contents = await fs.readFile(andGreaterThan1, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example1').text().trim()).toBe(
Expand All @@ -102,7 +102,7 @@ Alpha\n\n{% ifversion ghes > 2.16 %}\n\nBravo\n\n{% endif %}\n\n{% else %}\n\nCh
})

test('removes liquid statements that specify "and greater than version to deprecate" (alternate format)', async () => {
let contents = await readFileAsync(andGreaterThan2, 'utf8')
let contents = await fs.readFile(andGreaterThan2, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example1').text().trim()).toBe('{% ifversion ghes < 2.16 %}\n\nAlpha\n\n{% endif %}')
Expand All @@ -117,7 +117,7 @@ Alpha\n\n{% ifversion not fpt %}\n\nBravo\n\n{% endif %}\n\n{% else %}\n\nCharli
})

test('removes liquid statements that specify "not equals version to deprecate"', async () => {
let contents = await readFileAsync(notEquals, 'utf8')
let contents = await fs.readFile(notEquals, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example1').text().trim()).toBe('{% ifversion ghes %}\n\nAlpha\n\n{% endif %}')
Expand All @@ -136,7 +136,7 @@ Alpha\n\n{% endif %}`)

describe('removing liquid statements and content', () => {
test('removes interior content and liquid statements that specify "equals version to deprecate"', async () => {
let contents = await readFileAsync(equals, 'utf8')
let contents = await fs.readFile(equals, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example1').text().trim()).toBe('')
Expand All @@ -152,7 +152,7 @@ Alpha\n\n{% else %}\n\nCharlie\n\n{% endif %}`)
})

test('removes interior content and liquid statements that specify "less than next oldest than version to deprecate"', async () => {
let contents = await readFileAsync(lessThanNextOldest, 'utf8')
let contents = await fs.readFile(lessThanNextOldest, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example1').text().trim()).toBe('Alpha')
Expand All @@ -175,15 +175,15 @@ Charlie\n\n{% else %}\n\nDelta\n\n{% endif %}\n\nEcho`)

describe('updating frontmatter', () => {
test('updates frontmatter versions Enterprise if set to greater-than-or-equal-to version to deprecate', async () => {
let contents = await readFileAsync(frontmatter1, 'utf8')
let contents = await fs.readFile(frontmatter1, 'utf8')
contents = processFrontmatter(contents, frontmatter1)
const $ = cheerio.load(contents)
expect($.text().includes("ghes: '*'")).toBe(true)
expect($.text().includes("ghes: '>=2.13'")).toBe(false)
})

test('updates frontmatter versions Enterprise if set to greater-than-or-equal-to next oldest version', async () => {
let contents = await readFileAsync(frontmatter2, 'utf8')
let contents = await fs.readFile(frontmatter2, 'utf8')
contents = processFrontmatter(contents, frontmatter2)
const $ = cheerio.load(contents)
expect($.text().includes("ghes: '*'")).toBe(true)
Expand All @@ -193,7 +193,7 @@ describe('updating frontmatter', () => {

describe('whitespace', () => {
test('does not add newlines when whitespace control is used', async () => {
let contents = await readFileAsync(whitespace, 'utf8')
let contents = await fs.readFile(whitespace, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example1').text()).toBe('\n{% ifversion ghes %}\n Alpha\n{% endif %}\n')
Expand All @@ -203,7 +203,7 @@ describe('whitespace', () => {
})

test('does not add newlines when no newlines are present', async () => {
let contents = await readFileAsync(whitespace, 'utf8')
let contents = await fs.readFile(whitespace, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example5').text()).toBe('\n{% ifversion ghes %}\n Alpha\n{% endif %}\n')
Expand All @@ -214,7 +214,7 @@ describe('whitespace', () => {
})

test('only remove newlines when tag starts at beginning of line', async () => {
let contents = await readFileAsync(whitespace, 'utf8')
let contents = await fs.readFile(whitespace, 'utf8')
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
const $ = cheerio.load(contents)
expect($('.example8').text()).toBe('\nAlpha\nBravo\n')
Expand Down
3 changes: 1 addition & 2 deletions tests/content/site-data-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { loadPages } from '../../lib/page-data.js'
import getDataReferences from '../../lib/get-liquid-data-references.js'
import frontmatter from '../../lib/read-frontmatter.js'
import fs from 'fs/promises'
import readFileAsync from '../../lib/readfile-async.js'
import { jest } from '@jest/globals'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
Expand Down Expand Up @@ -41,7 +40,7 @@ describe('data references', () => {
await Promise.all(
pages.map(async (page) => {
const metadataFile = path.join('content', page.relativePath)
const fileContents = await readFileAsync(path.join(__dirname, '../..', metadataFile))
const fileContents = await fs.readFile(path.join(__dirname, '../..', metadataFile))
const { data: metadata } = frontmatter(fileContents, { filepath: page.fullPath })
const metadataRefs = getDataReferences(JSON.stringify(metadata))
metadataRefs.forEach((key) => {
Expand Down
Loading

0 comments on commit b4608a8

Please sign in to comment.