Skip to content

Commit

Permalink
Update some readFileSync to await readFile with top level await (gith…
Browse files Browse the repository at this point in the history
…ub#20525)

* Update some readFileSync to await readFile with top level await

* More updates

* Update all-products.js

* Use 'lib/readfile-async.js' in runtime files for better performance

* Remove unnecessary use of 'for await...of' loops

* Revert to importing 'fs/promises'

Co-authored-by: James M. Greene <jamesmgreene@github.com>
  • Loading branch information
heiskr and JamesMGreene authored Jul 29, 2021
1 parent 47f358b commit 0b1ff73
Show file tree
Hide file tree
Showing 23 changed files with 111 additions and 98 deletions.
4 changes: 2 additions & 2 deletions .github/actions-scripts/create-enterprise-issue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import fs from 'fs'
import fs from 'fs/promises'
import path from 'path'
import { getOctokit } from '@actions/github'
import enterpriseDates from '../../lib/enterprise-dates.js'
Expand Down Expand Up @@ -74,7 +74,7 @@ async function run() {
process.exit(0)
}

const milestoneSteps = fs.readFileSync(
const milestoneSteps = await fs.readFile(
path.join(
process.cwd(),
`.github/actions-scripts/enterprise-server-issue-templates/${milestone}-issue.md`
Expand Down
4 changes: 2 additions & 2 deletions .github/actions-scripts/enterprise-algolia-label.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node

import fs from 'fs'
import fs from 'fs/promises'
import { setOutput } from '@actions/core'

const eventPayload = JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'))
const eventPayload = JSON.parse(await fs.readFile(process.env.GITHUB_EVENT_PATH, 'utf8'))

// This workflow-run script does the following:
// 1. Gets an array of labels on a PR.
Expand Down
17 changes: 11 additions & 6 deletions lib/all-products.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import fs from 'fs'
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(fs.readFileSync(homepage, 'utf8'))
const { data } = frontmatter(await readFileAsync(homepage, 'utf8'))
export const productIds = data.children
const externalProducts = data.externalProducts

const internalProducts = {}

productIds.forEach((productId) => {
for (const productId of productIds) {
const relPath = productId
const dir = path.posix.join('content', relPath)

// Early Access may not exist in the current checkout
if (!fs.existsSync(dir)) return
try {
await fs.readdir(dir)
} catch (e) {
continue
}

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

Expand All @@ -35,7 +40,7 @@ productIds.forEach((productId) => {
}

internalProducts[productId].versions = applicableVersions
})
}

export const productMap = Object.assign({}, internalProducts, externalProducts)

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 semver from 'semver'
import fs from 'fs'
import path from 'path'
import readFileAsync from './readfile-async.js'

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

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

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

// GHES Release Lifecycle Dates:
Expand Down
2 changes: 1 addition & 1 deletion lib/process-learning-tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async function processLearningTracks(rawLearningTracks, context)

let featuredTrack

for await (const rawTrackName of rawLearningTracks) {
for (const rawTrackName of rawLearningTracks) {
let isFeaturedTrack = false

// Track names in frontmatter may include Liquid conditionals.
Expand Down
10 changes: 5 additions & 5 deletions script/content-migrations/deduplicate-enterprise-assets.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import fs from 'fs'
import fs from 'fs/promises'
import path from 'path'
import walk from 'walk-sync'
import xJimp from 'jimp'
Expand Down Expand Up @@ -38,7 +38,7 @@ async function main() {
// the image in the local /assets/images directory, then we can
// delete the enterprise image and the reference in the Markdown
// will just work
if (fs.existsSync(existingFileToCompare)) {
if (await fs.readFile(existingFileToCompare)) {
// Buffer.compare and Jimp both return 0 if files match
let compareResult = 1
try {
Expand All @@ -52,8 +52,8 @@ async function main() {
const diff = await jimp.diff(existingImageToCompare, enterpriseImage)
compareResult = diff.percent
} else {
const existingImageToCompare = await fs.readFileSync(existingFileToCompare)
const enterpriseImage = await fs.readFileSync(file)
const existingImageToCompare = await fs.readFile(existingFileToCompare)
const enterpriseImage = await fs.readFile(file)
compareResult = Buffer.compare(
Buffer.from(existingImageToCompare),
Buffer.from(enterpriseImage)
Expand All @@ -63,7 +63,7 @@ async function main() {
console.log(file)
console.log(err)
}
if (compareResult === 0) fs.unlinkSync(file)
if (compareResult === 0) await fs.unlink(file)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions script/content-migrations/update-short-titles-from-csv.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import fs from 'fs'
import fs from 'fs/promises'
import path from 'path'
import readFrontmatter from '../../lib/read-frontmatter.js'
import csv from 'csv-parse'
Expand Down Expand Up @@ -36,11 +36,11 @@ async function main() {

async function updateFrontmatter(csvData) {
const filePath = path.join(process.cwd(), csvData[4])
const fileContent = fs.readFileSync(filePath, 'utf8')
const fileContent = await fs.readFile(filePath, 'utf8')
const { content, data } = readFrontmatter(fileContent)
data.shortTitle = csvData[3]
const newContents = readFrontmatter.stringify(content, data, { lineWidth: 10000 })
fs.writeFileSync(filePath, newContents)
await fs.writeFile(filePath, newContents)
}

// Ensure the columns being read out are in the location expected
Expand Down
8 changes: 4 additions & 4 deletions script/create-glossary-from-spreadsheet.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
import { fileURLToPath } from 'url'
import path from 'path'
import fs from 'fs'
import fs from 'fs/promises'
import yaml from 'js-yaml'
const __dirname = path.dirname(fileURLToPath(import.meta.url))

Expand All @@ -13,7 +13,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))

const inputFile = path.join(__dirname, '../data/glossary.yml')

const glossary = yaml.load(fs.readFileSync(inputFile, 'utf8'))
const glossary = yaml.load(await fs.readFile(inputFile, 'utf8'))

console.log(glossary)
const external = []
Expand All @@ -27,6 +27,6 @@ glossary.forEach((term) => {
}
})

fs.writeFileSync(path.join(__dirname, '../data/glossaries/internal.yml'), yaml.dump(internal))
await fs.writeFile(path.join(__dirname, '../data/glossaries/internal.yml'), yaml.dump(internal))

fs.writeFileSync(path.join(__dirname, '../data/glossaries/external.yml'), yaml.dump(external))
await fs.writeFile(path.join(__dirname, '../data/glossaries/external.yml'), yaml.dump(external))
44 changes: 24 additions & 20 deletions script/enterprise-server-releases/create-graphql-files.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import fs from 'fs'
import fs from 'fs/promises'
import path from 'path'
import program from 'commander'
import xMkdirp from 'mkdirp'
Expand Down Expand Up @@ -50,10 +50,12 @@ const oldVersionId = allVersions[oldVersion].miscVersionName
// copy the schema file wholesale (there are separate schema files per version)
const newSchemaFile = path.join(graphqlStaticDir, `schema-${newVersionId}.json`)
const oldSchemaFile = path.join(graphqlStaticDir, `schema-${oldVersionId}.json`)
fs.copyFileSync(oldSchemaFile, newSchemaFile)
await fs.copyFile(oldSchemaFile, newSchemaFile)

// check that it worked
if (!fs.existsSync(newSchemaFile)) {
try {
await fs.readFile(newSchemaFile)
} catch (e) {
console.log(`Error! Can't find ${newSchemaFile}.`)
process.exit(1)
}
Expand All @@ -64,10 +66,10 @@ const changesFile = path.join(graphqlStaticDir, 'upcoming-changes.json')
const objectsFile = path.join(graphqlStaticDir, 'prerendered-objects.json')
const inputObjectsFile = path.join(graphqlStaticDir, 'prerendered-input-objects.json')

const previews = JSON.parse(fs.readFileSync(previewsFile))
const changes = JSON.parse(fs.readFileSync(changesFile))
const objects = JSON.parse(fs.readFileSync(objectsFile))
const inputObjects = JSON.parse(fs.readFileSync(inputObjectsFile))
const previews = JSON.parse(await fs.readFile(previewsFile))
const changes = JSON.parse(await fs.readFile(changesFile))
const objects = JSON.parse(await fs.readFile(objectsFile))
const inputObjects = JSON.parse(await fs.readFile(inputObjectsFile))
// The prerendered objects file for the "old version" contains hardcoded links with the old version number.
// We need to update those links to include the new version to prevent a test from failing.
const regexOldVersion = new RegExp(oldVersion, 'gi')
Expand Down Expand Up @@ -104,33 +106,35 @@ if (!Object.keys(inputObjects).includes(newVersionId)) {
}

// write the new files
fs.writeFileSync(previewsFile, JSON.stringify(previews, null, 2))
fs.writeFileSync(changesFile, JSON.stringify(changes, null, 2))
fs.writeFileSync(objectsFile, JSON.stringify(objects, null, 2))
fs.writeFileSync(inputObjectsFile, JSON.stringify(inputObjects, null, 2))
await fs.writeFile(previewsFile, JSON.stringify(previews, null, 2))
await fs.writeFile(changesFile, JSON.stringify(changes, null, 2))
await fs.writeFile(objectsFile, JSON.stringify(objects, null, 2))
await fs.writeFile(inputObjectsFile, JSON.stringify(inputObjects, null, 2))

// now create the new version directory in data/graphql
const srcDir = path.join(graphqlDataDir, oldVersionId)
const destDir = path.join(graphqlDataDir, newVersionId)
mkdirp(destDir)

// copy the files
fs.readdirSync(srcDir).forEach((file) => {
const files = await fs.readdir(srcDir)
for (const file of files) {
const srcFile = path.join(srcDir, file)
const destFile = path.join(destDir, file)
fs.copyFileSync(srcFile, destFile)
})
await fs.copyFile(srcFile, destFile)
}

// check that it worked
if (!fs.existsSync(destDir)) {
try {
const destDirResult = await fs.readdir(destDir)
if (!destDirResult.length) {
console.log(`Error! The directory created at ${destDir} is empty.`)
process.exit(1)
}
} catch (e) {
console.log(`Error! A new directory was not successfully created at ${destDir}.`)
process.exit(1)
}

if (!fs.readdirSync(destDir).length) {
console.log(`Error! The directory created at ${destDir} is empty.`)
process.exit(1)
}

// print success message
console.log(`Done! Copied ${oldVersion} GraphQL files to ${newVersion} files.`)
15 changes: 8 additions & 7 deletions script/enterprise-server-releases/create-rest-files.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import fs from 'fs'
import fs from 'fs/promises'
import path from 'path'
import program from 'commander'
import { allVersions } from '../../lib/all-versions.js'
Expand Down Expand Up @@ -59,24 +59,25 @@ async function main() {
const newDecoratedFile = path.join(decoratedDir, newDecoratedFilename)

// check that the old files exist
if (!fs.existsSync(oldDereferencedFile)) {
let oldDereferencedContent
try {
oldDereferencedContent = await fs.readFile(oldDereferencedFile, 'utf8')
} catch (e) {
console.log(`Error! Can't find ${oldDereferencedFile} for ${oldVersion}.`)
process.exit(1)
}

const oldDereferencedContent = fs.readFileSync(oldDereferencedFile, 'utf8')

// Replace old version with new version
// (ex: enterprise-server@3.0 -> enterprise-server@3.1)
const regexOldVersion = new RegExp(oldVersion, 'gi')
const newDereferenceContent = oldDereferencedContent.replace(regexOldVersion, newVersion)

// Write processed dereferenced schema to disk
fs.writeFileSync(newDereferencedFile, newDereferenceContent)
await fs.writeFile(newDereferencedFile, newDereferenceContent)
console.log(`Created ${newDereferencedFile}.`)

const dereferencedSchema = JSON.parse(
fs.readFileSync(path.join(process.cwd(), newDereferencedFile))
await fs.readFile(path.join(process.cwd(), newDereferencedFile))
)

// Store all operations in an array of operation objects
Expand All @@ -86,6 +87,6 @@ async function main() {
await Promise.all(operations.map((operation) => operation.process()))

// Write processed operations to disk
fs.writeFileSync(newDecoratedFile, JSON.stringify(operations, null, 2))
await fs.writeFile(newDecoratedFile, JSON.stringify(operations, null, 2))
console.log(`Done! Created ${newDecoratedFile}.`)
}
6 changes: 3 additions & 3 deletions script/enterprise-server-releases/release-banner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import fs from 'fs'
import fs from 'fs/promises'
import path from 'path'
import program from 'commander'
import yaml from 'js-yaml'
Expand Down Expand Up @@ -43,7 +43,7 @@ if (!Object.keys(allVersions).includes(options.version)) {
}

// Load the release candidate variable
const releaseCandidateData = yaml.load(fs.readFileSync(releaseCandidateYaml, 'utf8'))
const releaseCandidateData = yaml.load(await fs.readFile(releaseCandidateYaml, 'utf8'))

// Create or remove the variable
if (options.action === 'create') {
Expand All @@ -55,7 +55,7 @@ if (options.action === 'remove') {
}

// Update the file
fs.writeFileSync(releaseCandidateYaml, yaml.dump(releaseCandidateData))
await fs.writeFile(releaseCandidateYaml, yaml.dump(releaseCandidateData))

// Display next steps
console.log(`\nDone! Commit the update to ${releaseCandidateFile}. This ${options.action}s the banner for ${options.version}.
Expand Down
Loading

0 comments on commit 0b1ff73

Please sign in to comment.