Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor copy API in async/await #1021

Merged
merged 15 commits into from Oct 24, 2023

Conversation

SukkaW
Copy link
Contributor

@SukkaW SukkaW commented Oct 19, 2023

The PR is the first part of proposal #1020:

  • The PR only migrates the copy API. Other APIs will be migrated in the following PRs
  • The PR adds Promise-based implementations for some utility functions. The callback-based implementations are retained, but they will be removed once the rest of the library has migrated to Promise-based implementations.

@SukkaW SukkaW marked this pull request as ready for review October 20, 2023 02:39
@SukkaW SukkaW changed the title (WIP) Refactor copy API in async/await Refactor copy API in async/await Oct 20, 2023
@SukkaW
Copy link
Contributor Author

SukkaW commented Oct 20, 2023

@RyanZim The PR is ready to be reviewed now. npm run lint passes, and all test cases pass as well, check CI run here: https://github.com/SukkaW/node-fs-extra/actions/runs/6582803108

@@ -5,7 +5,7 @@ const os = require('os')
const fse = require('../..')
const path = require('path')
const assert = require('assert')
const copy = require('../copy')
const { copy } = require('../')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to change the import here. lib/copy/copy.js now exports the promise-only API (instead of the callback-only API), while lib/copy/index.js now exports the unified API.

@@ -33,7 +33,7 @@ describe('utimes', () => {
fse.emptyDir(TEST_DIR, done)
// reset stubs
gracefulFsStub = {}
utimes = proxyquire('../utimes', { 'graceful-fs': gracefulFsStub })
utimes = proxyquire('../utimes', { '../fs': gracefulFsStub })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mock here has to be replaced, as lib/util/utimes.js no longer uses graceful-fs directly. It now uses the unified API from lib/fs/index.js.

Copy link
Collaborator

@RyanZim RyanZim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left a several code comments. Additionally, I have a few other concerns:

  1. copy is not expected to return a value, yet we use return throughout the code for final async calls inside functions; this could result in unintentionally returning a value in the future; perhaps we should use await instead?
  2. Is there a reason for duplicating the util functions vs. just wrapping them in universalify? We could wrap them in fromCallback here, then port them to promise implementations and wrap in fromPromise in a separate PR. Anything I'm missing? (I have not reviewed those changes yet)

lib/copy/copy.js Outdated Show resolved Hide resolved
lib/copy/copy.js Outdated Show resolved Hide resolved
lib/copy/copy.js Outdated Show resolved Hide resolved
lib/copy/copy.js Outdated Show resolved Hide resolved
lib/copy/copy.js Outdated Show resolved Hide resolved
lib/copy/copy.js Outdated Show resolved Hide resolved
lib/copy/copy.js Outdated Show resolved Hide resolved
lib/copy/copy.js Show resolved Hide resolved
lib/copy/copy.js Outdated Show resolved Hide resolved
@SukkaW SukkaW requested a review from RyanZim October 21, 2023 16:17
@@ -68,25 +70,25 @@ describe('utimes', () => {
it('should close open file desciptors after encountering an error', done => {
const fakeFd = Math.random()

gracefulFsStub.open = (pathIgnored, flagsIgnored, modeIgnored, callback) => {
gracefulFsStub.open = u((pathIgnored, flagsIgnored, modeIgnored, callback) => {
Copy link
Contributor Author

@SukkaW SukkaW Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gracefulFsStub needs to be universalify as the utimesMillis is now Promise-based and then universalized.

@SukkaW
Copy link
Contributor Author

SukkaW commented Oct 21, 2023

@RyanZim I've addressed all the code review suggestions. I've also universalized the related internal utility functions to reduce the duplicated implementations. The CI still passes after the latest changes: https://github.com/SukkaW/node-fs-extra/actions/runs/6598480115

Note that the coprDir has been heavily refactored (no more items array mutation!).

Copy link
Collaborator

@RyanZim RyanZim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple comments

lib/copy/copy.js Outdated Show resolved Hide resolved
lib/util/utimes.js Outdated Show resolved Hide resolved
lib/copy/copy.js Outdated Show resolved Hide resolved
lib/util/utimes.js Outdated Show resolved Hide resolved
@SukkaW SukkaW requested a review from RyanZim October 24, 2023 06:03
@RyanZim RyanZim merged commit 40c5161 into jprichardson:master Oct 24, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants