Skip to content

Commit 39cc434

Browse files
committed
chore: use async fs.exists where possible
1 parent 4d110c3 commit 39cc434

File tree

10 files changed

+28
-21
lines changed

10 files changed

+28
-21
lines changed

lib/apt.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Ajv from "#core/ajv";
99
import Git from "#core/api/git";
1010
import { readConfig } from "#core/config";
1111
import ejs from "#core/ejs";
12+
import { exists } from "#core/fs";
1213
import glob from "#core/glob";
1314
import Semver from "#core/semver";
1415
import tar from "#core/tar";
@@ -71,7 +72,7 @@ export default class {
7172
var res;
7273

7374
// check dists
74-
if ( !fs.existsSync( this.distsRoot ) ) {
75+
if ( !( await exists( this.distsRoot ) ) ) {
7576
const git = new Git( this.root ),
7677
upstream = git.upstream;
7778

lib/commands/docker/build.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import fs from "node:fs";
21
import DockerBuilder from "#core/api/docker/builder";
32
import env from "#core/env";
3+
import { exists } from "#core/fs";
44
import Command from "#lib/command";
55

66
export default class extends Command {
@@ -66,7 +66,7 @@ export default class extends Command {
6666
if ( !Array.isArray[ composeFiles ] ) composeFiles = [ composeFiles ];
6767

6868
for ( const composeFile of composeFiles ) {
69-
if ( !fs.existsSync( pkg.root + "/" + composeFile ) ) return result( [ 400, `Compose file "${ composeFile }" not found` ] );
69+
if ( !( await exists( pkg.root + "/" + composeFile ) ) ) return result( [ 400, `Compose file "${ composeFile }" not found` ] );
7070
}
7171

7272
for ( const composeFile of composeFiles ) {

lib/commands/git/install-hooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "node:fs";
22
import os from "node:os";
33
import path from "node:path";
44
import Git from "#core/api/git";
5-
import { chmodSync } from "#core/fs";
5+
import { chmodSync, exists } from "#core/fs";
66
import * as utils from "#core/utils";
77
import Command from "#lib/command";
88

@@ -31,7 +31,7 @@ export default class extends Command {
3131
const { pkg, git, hooksPath } = res.data;
3232

3333
// create hooks dir
34-
if ( !fs.existsSync( hooksPath ) ) {
34+
if ( !( await exists( hooksPath ) ) ) {
3535
fs.mkdirSync( hooksPath, {
3636
"recursive": true,
3737
} );

lib/commands/git/pre-commit.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "node:fs";
22
import File from "#core/file";
3+
import { exists } from "#core/fs";
34
import glob from "#core/glob";
45
import Command from "#lib/command";
56
import Git from "#lib/git";
@@ -127,7 +128,7 @@ export default class extends Command {
127128
if ( !res1.ok ) return result( [ 500, `Unable to update git index` ] );
128129

129130
// update working tree, if file wasn't modified after add and wasn't removed
130-
if ( !modified[ filename ] && fs.existsSync( filename ) ) fs.writeFileSync( filename, res.data );
131+
if ( !modified[ filename ] && ( await exists( filename ) ) ) fs.writeFileSync( filename, res.data );
131132
}
132133

133134
// report

lib/commands/git/remove-hooks.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "node:fs";
22
import os from "node:os";
33
import path from "node:path";
44
import Git from "#core/api/git";
5+
import { exists } from "#core/fs";
56
import Command from "#lib/command";
67

78
export default class extends Command {
@@ -29,7 +30,7 @@ export default class extends Command {
2930
const { git, hooksPath, hooksPathInstalled } = res.data;
3031

3132
// remove "pre-commit" hook
32-
if ( fs.existsSync( path.join( hooksPath, "pre-commit" ) ) ) {
33+
if ( await exists( path.join( hooksPath, "pre-commit" ) ) ) {
3334
fs.rmSync( path.join( hooksPath, "pre-commit" ) );
3435
console.log( `Git "pre-commit" hook removed` );
3536
}
@@ -38,7 +39,7 @@ export default class extends Command {
3839
}
3940

4041
// remove "commit-msg" hook
41-
if ( fs.existsSync( path.join( hooksPath, "commit-msg" ) ) ) {
42+
if ( await exists( path.join( hooksPath, "commit-msg" ) ) ) {
4243
fs.rmSync( path.join( hooksPath, "commit-msg" ) );
4344
console.log( `Git "commit-msg" hook removed` );
4445
}

lib/commands/package/link.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import childProcess from "node:child_process";
22
import fs from "node:fs";
33
import path from "node:path";
44
import { readConfig } from "#core/config";
5+
import { exists } from "#core/fs";
56
import glob from "#core/glob";
67
import Command from "#lib/command";
78

@@ -117,7 +118,7 @@ export default class extends Command {
117118
if ( !process.cli.options[ "dry-run" ] ) {
118119

119120
// create node_modules
120-
if ( !fs.existsSync( pkg.root + "/node_modules/" + path.dirname( dep.name ) ) ) {
121+
if ( !( await exists( pkg.root + "/node_modules/" + path.dirname( dep.name ) ) ) ) {
121122
fs.mkdirSync( pkg.root + "/node_modules/" + path.dirname( dep.name ), {
122123
"recursive": true,
123124
} );
@@ -137,7 +138,7 @@ export default class extends Command {
137138
}
138139

139140
// remove node modules
140-
if ( dep.removeNodeModules && fs.existsSync( dep.path + "/node_modules" ) ) {
141+
if ( dep.removeNodeModules && ( await exists( dep.path + "/node_modules" ) ) ) {
141142
console.log( `Remove "node_modules" for package: ${ dep.name }` );
142143

143144
if ( !process.cli.options[ "dry-run" ] ) {

lib/package.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { readConfig, writeConfig } from "#core/config";
66
import env from "#core/env";
77
import File from "#core/file";
88
import FileTree from "#core/file-tree";
9-
import { chmodSync } from "#core/fs";
9+
import { chmodSync, exists } from "#core/fs";
1010
import glob from "#core/glob";
1111
import GlobPatterns from "#core/glob/patterns";
1212
import Semver from "#core/semver";
@@ -1009,12 +1009,12 @@ export default class Package {
10091009
config,
10101010
updates = new Map();
10111011

1012-
if ( fs.existsSync( this.root + "/.github/dependabot.yaml" ) ) {
1012+
if ( await exists( this.root + "/.github/dependabot.yaml" ) ) {
10131013
filename = "dependabot.yaml";
10141014

10151015
config = readConfig( this.root + "/.github/dependabot.yaml" );
10161016
}
1017-
else if ( fs.existsSync( this.root + "/.github/dependabot.yml" ) ) {
1017+
else if ( await exists( this.root + "/.github/dependabot.yml" ) ) {
10181018
filename = "dependabot.yml";
10191019

10201020
config = readConfig( this.root + "/.github/dependabot.yml" );
@@ -1093,7 +1093,7 @@ export default class Package {
10931093
}
10941094

10951095
if ( !updates.size ) {
1096-
if ( fs.existsSync( this.root + "/.github" ) ) {
1096+
if ( await exists( this.root + "/.github" ) ) {
10971097
await fs.promises.rm( this.root + "/.github/" + filename, {
10981098
"force": true,
10991099
} );

lib/package/docs.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ejs from "#core/ejs";
55
import env from "#core/env";
66
import File from "#core/file";
77
import FileTree from "#core/file-tree";
8+
import { exists } from "#core/fs";
89
import glob from "#core/glob";
910
import Markdown from "#core/markdown";
1011
import ansi from "#core/text/ansi";
@@ -166,7 +167,7 @@ export default class Docs {
166167
const fileTree = new FileTree();
167168

168169
// generate README.md
169-
if ( !fs.existsSync( this.locationPath + "/README.md" ) ) {
170+
if ( !( await exists( this.locationPath + "/README.md" ) ) ) {
170171
const readmeTmpl = utils.resolve( "#resources/templates/docs/README-default.md", import.meta.url );
171172

172173
if ( this.location !== "/" ) {
@@ -180,7 +181,7 @@ export default class Docs {
180181
else {
181182

182183
// copy root readme
183-
if ( fs.existsSync( this.#package.root + "/README.md" ) ) {
184+
if ( await exists( this.#package.root + "/README.md" ) ) {
184185
fileTree.add( {
185186
"path": "README.md",
186187
"buffer": fs.readFileSync( this.#package.root + "/README.md" ),
@@ -198,7 +199,7 @@ export default class Docs {
198199
}
199200

200201
// generate default sidebar
201-
if ( !fs.existsSync( this.locationPath + "/_sidebar.md" ) ) {
202+
if ( !( await exists( this.locationPath + "/_sidebar.md" ) ) ) {
202203
fileTree.add( {
203204
"path": "_sidebar.md",
204205
"buffer": await ejs.renderFile( utils.resolve( "#resources/templates/docs/_sidebar.md", import.meta.url ), options ),
@@ -288,7 +289,7 @@ export default class Docs {
288289
async #buildReadme ( options ) {
289290
const readmePath = this.locationPath + "/README.md";
290291

291-
if ( this.location === "/" || options.generateReadme === false || !fs.existsSync( readmePath ) ) return;
292+
if ( this.location === "/" || options.generateReadme === false || !( await exists( readmePath ) ) ) return;
292293

293294
const template = utils.resolve( "#resources/templates/docs/README-wrapper.md.ejs", import.meta.url ),
294295
fileTree = new FileTree();

lib/package/localization.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "node:path";
33
import url from "node:url";
44
import CloudTranslationApi from "#core/api/google/cloud/translation";
55
import env from "#core/env";
6+
import { exists } from "#core/fs";
67
import glob from "#core/glob";
78
import GlobPatterns from "#core/glob/patterns";
89
import PoFile from "#core/locale/po-file";
@@ -102,7 +103,7 @@ export default class {
102103

103104
// find po file nearest package
104105
while ( true ) {
105-
if ( fs.existsSync( poFilePackageRoot + "/package.json" ) ) break;
106+
if ( await exists( poFilePackageRoot + "/package.json" ) ) break;
106107

107108
const parent = path.dirname( poFilePackageRoot );
108109
if ( parent === poFilePackageRoot ) break;
@@ -206,7 +207,7 @@ export default class {
206207
async #initTranslationMemory () {
207208
const location = url.pathToFileURL( env.getXdgDataDir( "softvisio-cli/translation-memory.sqlite" ) );
208209

209-
if ( !fs.existsSync( path.dirname( url.fileURLToPath( location ) ) ) ) {
210+
if ( !( await exists( path.dirname( url.fileURLToPath( location ) ) ) ) ) {
210211
fs.mkdirSync( path.dirname( url.fileURLToPath( location ) ), {
211212
"recursive": true,
212213
} );

lib/package/release.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import GitHub from "#core/api/github";
55
import ejs from "#core/ejs";
66
import env from "#core/env";
77
import File from "#core/file";
8+
import { exists } from "#core/fs";
89
import ansi from "#core/text/ansi";
910
import { TmpFile } from "#core/tmp";
1011
import { confirm, resolve } from "#core/utils";
@@ -353,7 +354,7 @@ You need to merge with the "${ latestPreRelease.toVersionString() }" first.`,
353354
log = "# Changelog\n\n" + log;
354355

355356
// prepend log
356-
if ( fs.existsSync( this.#pkg.root + "/CHANGELOG.md" ) ) {
357+
if ( await exists( this.#pkg.root + "/CHANGELOG.md" ) ) {
357358
const currentLog = fs
358359
.readFileSync( this.#pkg.root + "/CHANGELOG.md", "utf8" )
359360
.replace( /# Changelog/, "" )

0 commit comments

Comments
 (0)