From b2f380c17970de8d431345bcfcce4e048dbf535b Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Tue, 1 Feb 2022 16:43:45 -0300 Subject: [PATCH] Deconstruct fs import. --- lib/index.js | 15 ++++++++++----- lib/run-context.js | 5 ++--- lib/run-result.js | 7 +++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index 82e39be..69e4665 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,13 @@ /* eslint-disable max-params */ 'use strict'; -const fs = require('fs'); +const { + writeFile, + mkdirSync, + existsSync, + rmdirSync, + rmSync = rmdirSync +} = require('fs'); const _ = require('lodash'); const path = require('path'); const sinon = require('sinon'); @@ -59,7 +65,7 @@ YeomanTest.prototype.gruntfile = function (options, done) { const out = ['module.exports = function (grunt) {', config, '};']; - fs.writeFile('Gruntfile.js', out.join('\n'), done); + writeFile('Gruntfile.js', out.join('\n'), done); }; /** @@ -85,12 +91,11 @@ YeomanTest.prototype.testDirectory = function (dir, cb) { process.chdir('/'); try { - if (fs.existsSync(dir)) { - const {rmdirSync, rmSync = rmdirSync} = fs; + if (existsSync(dir)) { rmSync(dir, {recursive: true}); } - fs.mkdirSync(dir, {recursive: true}); + mkdirSync(dir, {recursive: true}); process.chdir(dir); cb(); } catch (error) { diff --git a/lib/run-context.js b/lib/run-context.js index e0c8acc..ff467f3 100644 --- a/lib/run-context.js +++ b/lib/run-context.js @@ -1,6 +1,6 @@ 'use strict'; const crypto = require('crypto'); -const fs = require('fs'); +const {existsSync, rmdirSync, rmSync = rmdirSync} = require('fs'); const path = require('path'); const assert = require('assert'); const _ = require('lodash/string'); @@ -422,8 +422,7 @@ RunContext.prototype.cleanTestDirectory = function (force = false) { throw new Error('Cleanup test dir called with false tmpdir option.'); } - if (this.targetDirectory && fs.existsSync(this.targetDirectory)) { - const {rmdirSync, rmSync = rmdirSync} = fs; + if (this.targetDirectory && existsSync(this.targetDirectory)) { rmSync(this.targetDirectory, {recursive: true}); } }; diff --git a/lib/run-result.js b/lib/run-result.js index 4186e3a..e3e4f24 100644 --- a/lib/run-result.js +++ b/lib/run-result.js @@ -1,6 +1,6 @@ 'use strict'; const assert = require('assert'); -const fs = require('fs'); +const {existsSync, readFileSync, rmdirSync, rmSync = rmdirSync} = require('fs'); const MemFsEditor = require('mem-fs-editor'); const path = require('path'); @@ -124,7 +124,6 @@ class RunResult { */ cleanup() { process.chdir(this.oldCwd); - const {rmdirSync, rmSync = rmdirSync} = fs; rmSync(this.cwd, {recursive: true}); return this; } @@ -143,7 +142,7 @@ class RunResult { if (this.fs) { file = this.fs.read(filename, 'utf8'); } else { - file = fs.readFileSync(filename, 'utf8'); + file = readFileSync(filename, 'utf8'); } return json ? JSON.parse(file) : file; @@ -155,7 +154,7 @@ class RunResult { return this.fs.exists(filename); } - return fs.existsSync(filename); + return existsSync(filename); } /**