From 3a8436130e5cc5bef23d7bce22000644a7886dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Sat, 11 Sep 2021 15:28:37 +0300 Subject: [PATCH] fix(node-16): avoid deprecation warnings of `fs.rmdirSync` (#277) --- .github/workflows/ci.yml | 52 +++++++++++++--------------- lib/file-client/file-utils.ts | 11 ++++++ lib/file-client/repository-client.ts | 3 +- lib/file-client/results-writer.ts | 3 +- test/unit/file-client.test.ts | 3 +- test/unit/repository-client.test.ts | 3 +- test/utils.ts | 3 +- 7 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 lib/file-client/file-utils.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b10cbd53..eb20c423 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,10 +18,10 @@ jobs: run: echo "::set-output name=dir::$(yarn cache dir)" - uses: actions/cache@v2 with: - path: ${{ steps.yarn-cache-dir-path.outputs.dir }} - key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-yarn- + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- - run: | yarn install yarn build @@ -37,7 +37,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [12.11, 14, 16] + node-version: [12.11, 14, 16.8] steps: - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} @@ -49,10 +49,10 @@ jobs: run: echo "::set-output name=dir::$(yarn cache dir)" - uses: actions/cache@v2 with: - path: ${{ steps.yarn-cache-dir-path.outputs.dir }} - key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-yarn- + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- - run: | yarn install yarn build @@ -62,7 +62,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [12.11, 14, 16] + node-version: [12.11, 14, 16.8] steps: - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} @@ -74,22 +74,20 @@ jobs: run: echo "::set-output name=dir::$(yarn cache dir)" - uses: actions/cache@v2 with: - path: ${{ steps.yarn-cache-dir-path.outputs.dir }} - key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-yarn- + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- - run: | yarn install yarn build - run: yarn test:integration - env: - NODE_NO_WARNINGS: 1 smoke-test: runs-on: ubuntu-latest strategy: matrix: - node-version: [12.11, 14, 16] + node-version: [12.11, 14, 16.8] steps: - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} @@ -101,22 +99,20 @@ jobs: run: echo "::set-output name=dir::$(yarn cache dir)" - uses: actions/cache@v2 with: - path: ${{ steps.yarn-cache-dir-path.outputs.dir }} - key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-yarn- + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- - run: | yarn install yarn build - run: yarn test:smoke - env: - NODE_NO_WARNINGS: 1 repositories-test: runs-on: ubuntu-latest strategy: matrix: - node-version: [12.11, 14, 16] + node-version: [12.11, 14, 16.8] steps: - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} @@ -128,10 +124,10 @@ jobs: run: echo "::set-output name=dir::$(yarn cache dir)" - uses: actions/cache@v2 with: - path: ${{ steps.yarn-cache-dir-path.outputs.dir }} - key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-yarn- + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- - run: yarn install name: Install eslint-remote-tester - run: yarn install diff --git a/lib/file-client/file-utils.ts b/lib/file-client/file-utils.ts new file mode 100644 index 00000000..dbacd6b2 --- /dev/null +++ b/lib/file-client/file-utils.ts @@ -0,0 +1,11 @@ +import fs from 'fs'; + +export function removeDirectorySync(dir: string): void { + // Available in Node 16 + if (fs.rmSync != null && typeof fs.rmSync === 'function') { + return fs.rmSync(dir, { recursive: true }); + } + + // Fallback to older Node versions + return fs.rmdirSync(dir, { recursive: true }); +} diff --git a/lib/file-client/repository-client.ts b/lib/file-client/repository-client.ts index 4df3ad87..84e87c36 100644 --- a/lib/file-client/repository-client.ts +++ b/lib/file-client/repository-client.ts @@ -1,6 +1,7 @@ import fs from 'fs'; import simpleGit from 'simple-git'; +import { removeDirectorySync } from './file-utils'; import { CACHE_LOCATION, URL } from './file-constants'; import config from '@config'; @@ -69,7 +70,7 @@ export async function removeCachedRepository( const repoLocation = `${CACHE_LOCATION}/${repository}`; if (fs.existsSync(repoLocation)) { - fs.rmdirSync(repoLocation, { recursive: true }); + removeDirectorySync(repoLocation); } } diff --git a/lib/file-client/results-writer.ts b/lib/file-client/results-writer.ts index 0455e060..e970fe91 100644 --- a/lib/file-client/results-writer.ts +++ b/lib/file-client/results-writer.ts @@ -3,6 +3,7 @@ import { isMainThread } from 'worker_threads'; import objectHash from 'object-hash'; import ResultsStore from './results-store'; +import { removeDirectorySync } from './file-utils'; import { CACHE_LOCATION, RESULTS_COMPARE_LOCATION, @@ -27,7 +28,7 @@ const RESULT_EXTENSION = RESULT_PARSER_TO_EXTENSION[config.resultParser]; export function prepareResultsDirectory(): void { if (isMainThread) { if (fs.existsSync(RESULTS_LOCATION)) { - fs.rmdirSync(RESULTS_LOCATION, { recursive: true }); + removeDirectorySync(RESULTS_LOCATION); } fs.mkdirSync(RESULTS_LOCATION); diff --git a/test/unit/file-client.test.ts b/test/unit/file-client.test.ts index 4bc5a075..9f53dc06 100644 --- a/test/unit/file-client.test.ts +++ b/test/unit/file-client.test.ts @@ -13,6 +13,7 @@ import { Result, RESULT_PARSER_TO_COMPARE_TEMPLATE, } from '@file-client/result-templates'; +import { removeDirectorySync } from '@file-client/file-utils'; import { mockConfig } from '__mocks__/@config'; import { getComparisonResults } from '../utils'; @@ -35,7 +36,7 @@ const readComparisonCache = () => function removeResultsDirectory(): void { if (resultsDirectoryExists()) { - fs.rmdirSync(RESULTS_LOCATION, { recursive: true }); + removeDirectorySync(RESULTS_LOCATION); } } diff --git a/test/unit/repository-client.test.ts b/test/unit/repository-client.test.ts index ec75bfc8..154af130 100644 --- a/test/unit/repository-client.test.ts +++ b/test/unit/repository-client.test.ts @@ -5,6 +5,7 @@ import { getCacheStatus, removeCachedRepository, } from '@file-client/repository-client'; +import { removeDirectorySync } from '@file-client/file-utils'; import SimpleGit from '__mocks__/simple-git'; const EXPECTED_CACHE = './node_modules/.cache-eslint-remote-tester'; @@ -16,7 +17,7 @@ describe('repository-client', () => { // Clear previous cache if (fs.existsSync(EXPECTED_CACHE)) { - fs.rmdirSync(EXPECTED_CACHE, { recursive: true }); + removeDirectorySync(EXPECTED_CACHE); } // Initialize client diff --git a/test/utils.ts b/test/utils.ts index e66e8d43..f4b7d101 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -10,6 +10,7 @@ import { RESULTS_COMPARE_LOCATION, } from '@file-client'; import { ComparisonTypes } from '@file-client/result-templates'; +import { removeDirectorySync } from '@file-client/file-utils'; import { Config, ConfigToValidate } from '@config/types'; declare const console: { log: jest.Mock; error: (...args: any) => void }; @@ -166,7 +167,7 @@ function parsePtyOutput(output: string[]): string[] { */ export function clearRepositoryCache(): void { if (fs.existsSync(CACHE_LOCATION)) { - fs.rmdirSync(CACHE_LOCATION, { recursive: true }); + removeDirectorySync(CACHE_LOCATION); } }