Skip to content

Commit af294ac

Browse files
authored
perf(cli): use fs.promises (#459)
* Use fs.promises in svgr cli Node v10 got fs.promises support with already promisified api. See here https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_promises_api Also we get helpful mkdir recursive flag which replaces mkdirp and make-dir packages. In this diff I replaced util.promisify with fs.promises where possible and output-file-sync with combination of promisified fs.mkdir and fs.writeFile.
1 parent 1f015eb commit af294ac

File tree

7 files changed

+25
-43
lines changed

7 files changed

+25
-43
lines changed

packages/cli/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
"chalk": "^4.0.0",
4040
"commander": "^5.1.0",
4141
"dashify": "^2.0.0",
42-
"glob": "^7.1.4",
43-
"output-file-sync": "^2.0.1"
42+
"glob": "^7.1.4"
4443
},
4544
"devDependencies": {
4645
"del": "^5.0.0"

packages/cli/src/dirCommand.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
/* eslint-disable no-underscore-dangle, no-console */
2-
import fs from 'fs'
3-
import { promisify } from 'util'
2+
import { promises as fs } from 'fs'
43
import path from 'path'
54
import chalk from 'chalk'
6-
import outputFileSync from 'output-file-sync'
75
import { loadConfig } from '@svgr/core'
8-
import { convertFile, stat, transformFilename, CASE, politeWrite } from './util'
9-
10-
const access = promisify(fs.access)
11-
const readdir = promisify(fs.readdir)
6+
import { convertFile, transformFilename, CASE, politeWrite } from './util'
127

138
async function exists(file) {
149
try {
15-
await access(file)
10+
await fs.access(file)
1611
return true
1712
} catch (error) {
1813
return false
@@ -67,7 +62,8 @@ export default async function dirCommand(
6762
return { transformed: false, dest }
6863
}
6964

70-
outputFileSync(dest, code)
65+
await fs.mkdir(path.dirname(dest), { recursive: true })
66+
await fs.writeFile(dest, code)
7167
politeWrite(program, chalk.white(logOutput))
7268
return { transformed: true, dest }
7369
}
@@ -76,15 +72,15 @@ export default async function dirCommand(
7672
const indexFile = path.join(dest, `index.${ext}`)
7773
const config = loadConfig.sync(options, { filePath: indexFile })
7874
const indexTemplate = config.indexTemplate || defaultIndexTemplate
79-
fs.writeFileSync(indexFile, indexTemplate(files))
75+
await fs.writeFile(indexFile, indexTemplate(files))
8076
}
8177

8278
async function handle(filename, root) {
83-
const stats = await stat(filename)
79+
const stats = await fs.stat(filename)
8480

8581
if (stats.isDirectory()) {
8682
const dirname = filename
87-
const files = await readdir(dirname)
83+
const files = await fs.readdir(dirname)
8884
const results = await Promise.all(
8985
files.map(async (relativeFile) => {
9086
const absFile = path.join(dirname, relativeFile)
@@ -106,7 +102,7 @@ export default async function dirCommand(
106102

107103
await Promise.all(
108104
filenames.map(async (file) => {
109-
const stats = await stat(file)
105+
const stats = await fs.stat(file)
110106
const root = stats.isDirectory() ? file : path.dirname(file)
111107
await handle(file, root)
112108
}),

packages/cli/src/fileCommand.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-underscore-dangle */
2-
import { stat, convert, convertFile, exitError } from './util'
2+
import { promises as fs } from 'fs'
3+
import { convert, convertFile, exitError } from './util'
34

45
async function output(promise) {
56
process.stdout.write(`${await promise}\n`)
@@ -38,7 +39,7 @@ async function fileCommand(program, filenames, config) {
3839
}
3940

4041
const [filename] = filenames
41-
const stats = await stat(filename)
42+
const stats = await fs.stat(filename)
4243

4344
if (stats.isDirectory()) {
4445
exitError('Directory are not supported without `--out-dir` option instead.')

packages/cli/src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import program from 'commander'
33
import path from 'path'
44
import glob from 'glob'
5-
import fs from 'fs'
5+
import fs, { promises as fsPromises } from 'fs'
66
import { loadConfig } from '@svgr/core'
77
import pkg from '../package.json'
88
import fileCommand from './fileCommand'
99
import dirCommand from './dirCommand'
10-
import { stat, exitError } from './util'
10+
import { exitError } from './util'
1111

1212
function noUndefinedKeys(obj) {
1313
return Object.entries(obj).reduce((obj, [key, value]) => {
@@ -129,7 +129,7 @@ async function run() {
129129
await Promise.all(
130130
filenames.map(async (filename) => {
131131
try {
132-
await stat(filename)
132+
await fsPromises.stat(filename)
133133
} catch (error) {
134134
errors.push(`${filename} does not exist`)
135135
}

packages/cli/src/index.test.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import fs from 'fs'
1+
import { promises as fs } from 'fs'
22
import path from 'path'
33
import childProcess from 'child_process'
44
import util from 'util'
55
import del from 'del'
66

7-
const readdir = util.promisify(fs.readdir)
87
const exec = util.promisify(childProcess.exec)
98

109
const svgr = path.join(__dirname, 'index.js')
@@ -152,7 +151,7 @@ describe('cli', () => {
152151
const outDir = `__fixtures_build__/filename-case-${index}`
153152
await del(outDir)
154153
await cli(`${args} ${inDir} --out-dir=${outDir}`)
155-
expect(await readdir(outDir)).toMatchSnapshot(args)
154+
expect(await fs.readdir(outDir)).toMatchSnapshot(args)
156155
},
157156
10000,
158157
)
@@ -162,14 +161,14 @@ describe('cli', () => {
162161
const outDir = '__fixtures_build__/ext'
163162
await del(outDir)
164163
await cli(`--ext=ts ${inDir} --out-dir=${outDir}`)
165-
expect(await readdir(outDir)).toMatchSnapshot()
164+
expect(await fs.readdir(outDir)).toMatchSnapshot()
166165
}, 10000)
167166

168167
it('should support "--ignore-existing"', async () => {
169168
const inDir = '__fixtures__/simple'
170169
const outDir = '__fixtures__/simple-existing'
171170
await cli(`${inDir} --out-dir=${outDir} --ignore-existing`)
172-
const content = fs.readFileSync(path.join(outDir, 'File.js'), 'utf-8')
171+
const content = await fs.readFile(path.join(outDir, 'File.js'), 'utf-8')
173172
expect(content).toBe('// nothing')
174173
}, 10000)
175174

@@ -185,7 +184,7 @@ describe('cli', () => {
185184
const outDir = `__fixtures_build__/prefix-exports`
186185
await del(outDir)
187186
await cli(`${inDir} --out-dir=${outDir}`)
188-
const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8')
187+
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
189188
expect(content).toMatchSnapshot()
190189
}, 10000)
191190

@@ -196,7 +195,7 @@ describe('cli', () => {
196195
await cli(
197196
`${inDir} --out-dir=${outDir} --config-file=__fixtures__/custom-index.config.js`,
198197
)
199-
const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8')
198+
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
200199
expect(content).toMatchSnapshot()
201200
}, 10000)
202201

@@ -207,7 +206,7 @@ describe('cli', () => {
207206
await cli(
208207
`${inDir} --out-dir=${outDir} --index-template=__fixtures__/custom-index-template.js`,
209208
)
210-
const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8')
209+
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
211210
expect(content).toMatchSnapshot()
212211
}, 10000)
213212
})

packages/cli/src/util.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
/* eslint-disable no-console */
2-
import fs from 'fs'
2+
import { promises as fs } from 'fs'
33
import chalk from 'chalk'
4-
import util from 'util'
54
import svgrConvert from '@svgr/core'
65
import svgo from '@svgr/plugin-svgo'
76
import jsx from '@svgr/plugin-jsx'
87
import prettier from '@svgr/plugin-prettier'
98
import camelcase from 'camelcase'
109
import dashify from 'dashify'
1110

12-
export const readFile = util.promisify(fs.readFile)
13-
export const stat = util.promisify(fs.stat)
14-
1511
export const CASE = {
1612
KEBAB: 'kebab', // kebab-case
1713
CAMEL: 'camel', // camelCase
@@ -42,7 +38,7 @@ export function convert(code, config, state) {
4238
}
4339

4440
export async function convertFile(filePath, config = {}) {
45-
const code = await readFile(filePath, 'utf-8')
41+
const code = await fs.readFile(filePath, 'utf-8')
4642
return convert(code, config, { filePath })
4743
}
4844

yarn.lock

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8212,15 +8212,6 @@ osenv@^0.1.4, osenv@^0.1.5:
82128212
os-homedir "^1.0.0"
82138213
os-tmpdir "^1.0.0"
82148214

8215-
output-file-sync@^2.0.1:
8216-
version "2.0.1"
8217-
resolved "https://registry.yarnpkg.com/output-file-sync/-/output-file-sync-2.0.1.tgz#f53118282f5f553c2799541792b723a4c71430c0"
8218-
integrity sha512-mDho4qm7WgIXIGf4eYU1RHN2UU5tPfVYVSRwDJw0uTmj35DQUt/eNp19N7v6T3SrR0ESTEf2up2CGO73qI35zQ==
8219-
dependencies:
8220-
graceful-fs "^4.1.11"
8221-
is-plain-obj "^1.1.0"
8222-
mkdirp "^0.5.1"
8223-
82248215
p-cancelable@^0.4.0:
82258216
version "0.4.1"
82268217
resolved "https://registry.yarnpkg.com/p-cancelable/-/p-cancelable-0.4.1.tgz#35f363d67d52081c8d9585e37bcceb7e0bbcb2a0"

0 commit comments

Comments
 (0)