Skip to content

Commit

Permalink
In Node.js v10.12.0, recursive option was added to [fs.mkdirSync](h…
Browse files Browse the repository at this point in the history
…ttps://nodejs.org/api/fs.html#fsmkdirsyncpath-options). This option allows you to create a directory and all its parent directories if they do not exist.

In Node.js v14.14.0, [`fs.rmSync`](https://nodejs.org/api/fs.html#fsrmsyncpath-options) was added. It's a built-in module that removes files and directories. The `recursive` option is used to remove directories and their contents.

Since this repository requires Node.js 18 and up, it's safe to assume `mkdirp` and `rimraf` can be safely replaced, thus reducing the number of dependencies in this project.
  • Loading branch information
wojtekmaj authored Feb 19, 2024
1 parent 22a3939 commit 6dd1270
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 92 deletions.
33 changes: 0 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,12 @@
"markdownlint-rule-search-replace": "^1.2.0",
"mdast-util-gfm-table": "^2.0.0",
"micromark-extension-gfm-table": "^2.0.0",
"mkdirp": "^3.0.0",
"mockdate": "^3.0.5",
"nock": "^13.5.0",
"nodemon": "3.0.3",
"npm-merge-driver-install": "^3.0.0",
"nth-check": "2.1.1",
"prettier": "^3.2.4",
"rimraf": "^5.0.0",
"robots-parser": "^3.0.0",
"sass": "^1.52.3",
"start-server-and-test": "^2.0.3",
Expand Down
5 changes: 2 additions & 3 deletions src/audit-logs/scripts/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
* per page.
*/
import { existsSync } from 'fs'
import { readFile, writeFile } from 'fs/promises'
import { mkdirp } from 'mkdirp'
import { mkdir, readFile, writeFile } from 'fs/promises'
import path from 'path'

import { getContents, getCommitSha } from '#src/workflows/git-utils.js'
Expand Down Expand Up @@ -184,7 +183,7 @@ async function main() {
const auditLogVersionDirPath = path.join(AUDIT_LOG_DATA_DIR, version)

if (!existsSync(auditLogVersionDirPath)) {
await mkdirp(auditLogVersionDirPath)
await mkdir(auditLogVersionDirPath, { recursive: true })
}

auditLogTypes.forEach(async (type) => {
Expand Down
10 changes: 4 additions & 6 deletions src/automated-pipelines/lib/update-markdown.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import walk from 'walk-sync'
import { existsSync, lstatSync, unlinkSync } from 'fs'
import { existsSync, lstatSync, unlinkSync, rmSync } from 'fs'
import path from 'path'
import { readFile, writeFile, readdir } from 'fs/promises'
import { mkdir, readFile, writeFile, readdir } from 'fs/promises'
import matter from 'gray-matter'
import { rimraf } from 'rimraf'
import { mkdirp } from 'mkdirp'
import { difference, isEqual } from 'lodash-es'

import { allVersions } from '#src/versions/lib/all-versions.js'
Expand Down Expand Up @@ -139,7 +137,7 @@ async function updateDirectory(
const initialDirectoryListing = await getDirectoryInfo(directory)
// If there are no children on disk, remove the directory
if (initialDirectoryListing.directoryContents.length === 0 && !rootDirectoryOnly) {
rimraf(directory)
rmSync(directory, { recursive: true, force: true })
return
}

Expand Down Expand Up @@ -430,7 +428,7 @@ function isRootIndexFile(indexFile) {
// Creates a new directory if it doesn't exist
async function createDirectory(targetDirectory) {
if (!existsSync(targetDirectory)) {
await mkdirp(targetDirectory)
await mkdir(targetDirectory, { recursive: true })
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/automated-pipelines/tests/update-markdown.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { expect } from '@jest/globals'
import { tmpdir } from 'os'
import { mkdirp } from 'mkdirp'
import { cp, rm, readFile } from 'fs/promises'
import { existsSync } from 'fs'
import { existsSync, mkdirSync } from 'fs'
import path from 'path'
import matter from 'gray-matter'

Expand Down Expand Up @@ -56,7 +55,7 @@ describe('automated content directory updates', () => {
// structure and contents after running updateContentDirectory.
beforeAll(async () => {
process.env.TEST_OS_ROOT_DIR = tempDirectory
mkdirp.sync(`${tempContentDirectory}`)
mkdirSync(`${tempContentDirectory}`, { recursive: true })
await cp('src/automated-pipelines/tests/fixtures/content', tempContentDirectory, {
recursive: true,
})
Expand Down
10 changes: 4 additions & 6 deletions src/codeql-cli/scripts/sync.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env node

import { readFile, writeFile, copyFile } from 'fs/promises'
import { existsSync } from 'fs'
import { mkdir, readFile, writeFile, copyFile } from 'fs/promises'
import { existsSync, rmSync } from 'fs'
import walk from 'walk-sync'
import { mkdirp } from 'mkdirp'
import { execFileSync, execSync } from 'child_process'
import path from 'path'
import matter from 'gray-matter'
import { rimraf } from 'rimraf'

import { updateContentDirectory } from '../../automated-pipelines/lib/update-markdown.js'
import { convertContentToDocs } from './convert-markdown-for-docs.js'
Expand Down Expand Up @@ -74,8 +72,8 @@ async function setupEnvironment() {
}

// refresh the temp directory
rimraf.sync(TEMP_DIRECTORY)
await mkdirp(TEMP_DIRECTORY)
rmSync(TEMP_DIRECTORY, { recursive: true, force: true })
await mkdir(TEMP_DIRECTORY, { recursive: true })
}

// copy the raw rst files to the temp directory and convert them
Expand Down
3 changes: 1 addition & 2 deletions src/early-access/scripts/symlink-from-local-repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//
// [end-readme]

import { rimraf } from 'rimraf'
import fs from 'fs'
import path from 'path'
import { program } from 'commander'
Expand Down Expand Up @@ -69,7 +68,7 @@ const destinationDirsMap = destinationDirNames.reduce((map, dirName) => {
// Remove all existing early access directories from this repo
destinationDirNames.forEach((dirName) => {
const destDir = destinationDirsMap[dirName]
rimraf.sync(destDir)
fs.rmSync(destDir, { recursive: true, force: true })
console.log(`- Removed symlink for early access directory '${dirName}' from this repo`)
})

Expand Down
3 changes: 1 addition & 2 deletions src/frame/tests/get-remote-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'fs'
import path from 'path'
import os from 'os'

import { rimraf } from 'rimraf'
import { expect, test, describe, beforeAll, afterAll } from '@jest/globals'
import nock from 'nock'
import getRemoteJSON, { cache } from '#src/frame/lib/get-remote-json.js'
Expand All @@ -23,7 +22,7 @@ describe('getRemoteJSON', () => {

afterAll(() => {
process.env.GET_REMOTE_JSON_DISK_CACHE_ROOT = envVarValueBefore
rimraf.sync(tempDir)
fs.rmSync(tempDir, { recursive: true, force: true })
})

afterEach(() => {
Expand Down
3 changes: 1 addition & 2 deletions src/ghes-releases/scripts/archive-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import path from 'path'
import fs from 'fs'
import scrape from 'website-scraper'
import { program } from 'commander'
import { rimraf } from 'rimraf'
import http from 'http'

import createApp from '#src/frame/lib/app.js'
Expand Down Expand Up @@ -149,7 +148,7 @@ async function main() {
}

// remove temp directory
rimraf.sync(tmpArchivalDirectory)
fs.rmSync(tmpArchivalDirectory, { recursive: true, force: true })

const app = createApp()
const server = http.createServer(app)
Expand Down
5 changes: 2 additions & 3 deletions src/ghes-releases/scripts/remove-static-files.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 { rimraf } from 'rimraf'
import walk from 'walk-sync'

import { allVersions } from '#src/versions/lib/all-versions.js'
Expand All @@ -33,7 +32,7 @@ walk(ghesReleaseNotesDir)
// Check if the directory name contains a deprecated GHES version
.filter((dir) => deprecatedVersionsHyphenated.some((version) => dir.includes(version)))
// Remove the directory
.map((dir) => rimraf.sync(path.join(ghesReleaseNotesDir, dir)))
.map((dir) => fs.rmSync(path.join(ghesReleaseNotesDir, dir), { recursive: true, force: true }))

// webhooks and GraphQL
const supportedMiscVersions = supportedEnterpriseVersions.map((v) => v.miscVersionName)
Expand All @@ -60,6 +59,6 @@ function removeFiles(dir, baseName, supportedVersions) {
.forEach((file) => {
const fullPath = path.join(dir, file)
console.log(`removing ${fullPath}`)
rimraf.sync(fullPath)
fs.rmSync(fullPath, { recursive: true, force: true })
})
}
12 changes: 5 additions & 7 deletions src/ghes-releases/scripts/sync-automated-pipeline-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
//
// [end-readme]

import { existsSync } from 'fs'
import { readFile, readdir, writeFile, cp } from 'fs/promises'
import { rimrafSync } from 'rimraf'
import { existsSync, rmSync } from 'fs'
import { mkdir, readFile, readdir, writeFile, cp } from 'fs/promises'
import { difference, intersection } from 'lodash-es'
import { mkdirp } from 'mkdirp'

import { deprecated, supported } from '#src/versions/lib/enterprise-server-releases.js'

Expand Down Expand Up @@ -83,7 +81,7 @@ for (const pipeline of pipelines) {
const removeFiles = difference(existingDataDir, expectedDirectory)
for (const directory of removeFiles) {
console.log(`Removing src/${pipeline}/data/${directory}`)
rimrafSync(`src/${pipeline}/data/${directory}`)
rmSync(`src/${pipeline}/data/${directory}`, { recursive: true, force: true })
}

// Get a list of data directories to create (release) and create them
Expand Down Expand Up @@ -134,11 +132,11 @@ const addRelNoteDirs = difference(supportedHyphenated, ghesReleaseNotesDirs)
const removeRelNoteDirs = intersection(deprecatedHyphenated, ghesReleaseNotesDirs)
for (const directory of removeRelNoteDirs) {
console.log(`Removing data/release-notes/enterprise-server/${directory}`)
rimrafSync(`data/release-notes/enterprise-server/${directory}`)
rmSync(`data/release-notes/enterprise-server/${directory}`, { recursive: true, force: true })
}
for (const directory of addRelNoteDirs) {
console.log(`Create new directory data/release-notes/enterprise-server/${directory}`)
await mkdirp(`data/release-notes/enterprise-server/${directory}`)
await mkdir(`data/release-notes/enterprise-server/${directory}`, { recursive: true })
await cp(
`data/release-notes/PLACEHOLDER-TEMPLATE.yml`,
`data/release-notes/enterprise-server/${directory}/PLACEHOLDER.yml`,
Expand Down
5 changes: 2 additions & 3 deletions src/github-apps/scripts/sync.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node

import { existsSync } from 'fs'
import { mkdirp } from 'mkdirp'
import { readFile, writeFile } from 'fs/promises'
import { mkdir, readFile, writeFile } from 'fs/promises'
import path from 'path'
import { slug } from 'github-slugger'
import yaml from 'js-yaml'
Expand Down Expand Up @@ -130,7 +129,7 @@ export async function syncGitHubAppsData(openApiSource, sourceSchemas, progAcces

// When a new version is added, we need to create the directory for it
if (!existsSync(targetDirectory)) {
await mkdirp(targetDirectory)
await mkdir(targetDirectory, { recursive: true })
}

for (const pageType of Object.keys(githubAppsData)) {
Expand Down
3 changes: 1 addition & 2 deletions src/graphql/scripts/sync.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node
import fs from 'fs/promises'
import path from 'path'
import { mkdirp } from 'mkdirp'
import yaml from 'js-yaml'
import { execSync } from 'child_process'
import { getContents, hasMatchingRef } from '#src/workflows/git-utils.js'
Expand Down Expand Up @@ -163,7 +162,7 @@ function getVersionType(graphqlVersion) {

async function updateFile(filepath, content) {
console.log(`Updating file ${filepath}`)
await mkdirp(path.dirname(filepath))
fs.mkdirSync(path.dirname(filepath), { recursive: true })
return fs.writeFile(filepath, content, 'utf8')
}

Expand Down
18 changes: 8 additions & 10 deletions src/rest/scripts/update-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
//
// [end-readme]

import { readdir, copyFile, readFile, writeFile, rename } from 'fs/promises'
import { mkdir, readdir, copyFile, readFile, writeFile, rename } from 'fs/promises'
import path from 'path'
import { program, Option } from 'commander'
import { execSync } from 'child_process'
import { rimraf } from 'rimraf'
import { mkdirp } from 'mkdirp'
import { fileURLToPath } from 'url'
import walk from 'walk-sync'
import { existsSync } from 'fs'
import { existsSync, rmSync } from 'fs'

import { syncRestData, getOpenApiSchemaFiles } from './utils/sync.js'
import { validateVersionsOptions } from './utils/get-openapi-schemas.js'
Expand Down Expand Up @@ -72,8 +70,8 @@ main()
async function main() {
const pipelines = Array.isArray(output) ? output : [output]
await validateInputParameters()
rimraf.sync(TEMP_OPENAPI_DIR)
await mkdirp(TEMP_OPENAPI_DIR)
rmSync(TEMP_OPENAPI_DIR, { recursive: true, force: true })
await mkdir(TEMP_OPENAPI_DIR, { recursive: true })

// If the source repo is github, this is the local development workflow
// and the files in github must be bundled and dereferenced first.
Expand All @@ -97,7 +95,7 @@ async function main() {
await copyFile(file, path.join(TEMP_OPENAPI_DIR, baseName))
}

rimraf.sync(TEMP_BUNDLED_OPENAPI_DIR)
rmSync(TEMP_BUNDLED_OPENAPI_DIR, { recursive: true, force: true })
await normalizeDataVersionNames(TEMP_OPENAPI_DIR)

// The REST_API_DESCRIPTION_ROOT repo contains all current and
Expand All @@ -118,7 +116,7 @@ async function main() {
derefDir.forEach((schema) => {
// if the schema does not start with a current version name, delete it
if (!currentOpenApiVersions.find((version) => schema.startsWith(version))) {
rimraf.sync(path.join(TEMP_OPENAPI_DIR, schema))
rmSync(path.join(TEMP_OPENAPI_DIR, schema), { recursive: true, force: true })
}
})
}
Expand Down Expand Up @@ -183,8 +181,8 @@ async function getBundledFiles() {
}

// Create a tmp directory to store schema files generated from github/github
rimraf.sync(TEMP_OPENAPI_DIR)
await mkdirp(TEMP_BUNDLED_OPENAPI_DIR)
rmSync(TEMP_OPENAPI_DIR, { recursive: true, force: true })
await mkdir(TEMP_BUNDLED_OPENAPI_DIR, { recursive: true })

console.log(
`\n🏃‍♀️🏃🏃‍♀️Running \`bin/openapi bundle\` in branch '${githubBranch}' of your github/github checkout to generate the dereferenced OpenAPI schema files.\n`,
Expand Down
5 changes: 2 additions & 3 deletions src/rest/scripts/utils/sync.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { readFile, writeFile } from 'fs/promises'
import { mkdir, readFile, writeFile } from 'fs/promises'
import { existsSync } from 'fs'
import path from 'path'
import { mkdirp } from 'mkdirp'

import { updateRestFiles } from './update-markdown.js'
import { allVersions } from '#src/versions/lib/all-versions.js'
Expand Down Expand Up @@ -51,7 +50,7 @@ export async function syncRestData(sourceDirectory, restSchemas, progAccessSourc
)
}
if (!existsSync(targetDirectoryPath)) {
await mkdirp(targetDirectoryPath)
await mkdir(targetDirectoryPath, { recursive: true })
}
const targetPath = path.join(targetDirectoryPath, REST_SCHEMA_FILENAME)
await writeFile(targetPath, JSON.stringify(formattedOperations, null, 2))
Expand Down
Loading

0 comments on commit 6dd1270

Please sign in to comment.