Skip to content

Commit

Permalink
refactor(fs): use node:fs, drop fs-extra in copyFiles.ts #5761
Browse files Browse the repository at this point in the history
## Problem
Eliminate fs-extra from the codebase.

## Solution
We can use `fs.cp` since node v16.7.0+.
Note that `overwrite` is now `force` based on
https://nodejs.org/api/fs.html#fscpsyncsrc-dest-options

Bump the dependency @types/node to avoid casting (fs as any).
  • Loading branch information
Hweinstock authored Oct 16, 2024
1 parent 6fa3a9e commit 5cbe42b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 33 deletions.
2 changes: 1 addition & 1 deletion buildspec/shared/linux-pre_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ if [ "$TOOLKITS_CODEARTIFACT_DOMAIN" ] && [ "$TOOLKITS_CODEARTIFACT_REPO" ] && [
fi

# TODO: move this to the "install" phase?
export NODE_OPTIONS=--max-old-space-size=8192
export NODE_OPTIONS='--max-old-space-size=8192'
npm 2>&1 ci | run_and_report 2 'npm WARN deprecated' 'Deprecated dependencies must be updated.'
18 changes: 15 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"webpack-merge": "^5.10.0"
},
"dependencies": {
"@types/node": "^22.7.5",
"vscode-nls": "^5.2.0",
"vscode-nls-dev": "^4.0.4"
}
Expand Down
26 changes: 14 additions & 12 deletions packages/amazonq/scripts/build/copyFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/* eslint-disable no-restricted-imports */
import * as fs from 'fs-extra'
import fs from 'fs'
import * as path from 'path'

// Moves all dependencies into `dist`
Expand Down Expand Up @@ -73,33 +73,35 @@ const tasks: CopyTask[] = [
},
]

async function copy(task: CopyTask): Promise<void> {
function copy(task: CopyTask): void {
const src = path.resolve(projectRoot, task.target)
const dst = path.resolve(outRoot, task.destination ?? task.target)

try {
await fs.copy(src, dst, {
fs.cpSync(src, dst, {
recursive: true,
overwrite: true,
force: true,
errorOnExist: false,
})
} catch (error) {
throw new Error(`Copy "${src}" to "${dst}" failed: ${error instanceof Error ? error.message : error}`)
}
}

void (async () => {
const args = process.argv.slice(2)
if (args.includes('--vueHr')) {
vueHr = true
console.log('Using Vue Hot Reload webpacks from core/')
}
const args = process.argv.slice(2)
if (args.includes('--vueHr')) {
vueHr = true
console.log('Using Vue Hot Reload webpacks from core/')
}

function main() {
try {
await Promise.all(tasks.map(copy))
tasks.map(copy)
} catch (error) {
console.error('`copyFiles.ts` failed')
console.error(error)
process.exit(1)
}
})()
}

void main()
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ export function injectJSDOM() {
})

// jsdom doesn't have support for structuredClone. See https://github.com/jsdom/jsdom/issues/3363
global.structuredClone = (val) => JSON.parse(JSON.stringify(val))
global.structuredClone = (val: any) => JSON.parse(JSON.stringify(val))
}
17 changes: 9 additions & 8 deletions packages/core/scripts/build/copyFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/* eslint-disable no-restricted-imports */
import * as fs from 'fs-extra'
import fs from 'fs'
import * as path from 'path'

// Moves all dependencies into `dist`
Expand Down Expand Up @@ -46,27 +46,28 @@ const tasks: CopyTask[] = [
},
]

async function copy(task: CopyTask): Promise<void> {
function copy(task: CopyTask): void {
const src = path.resolve(projectRoot, task.target)
const dst = path.resolve(outRoot, task.destination ?? task.target)

try {
await fs.copy(src, dst, {
fs.cpSync(src, dst, {
recursive: true,
overwrite: true,
force: true,
errorOnExist: false,
})
} catch (error) {
throw new Error(`Copy "${src}" to "${dst}" failed: ${error instanceof Error ? error.message : error}`)
}
}

void (async () => {
function main() {
try {
await Promise.all(tasks.map(copy))
tasks.map(copy)
} catch (error) {
console.error('`copyFiles.ts` failed')
console.error(error)
process.exit(1)
}
})()
}

void main()
17 changes: 9 additions & 8 deletions packages/toolkit/scripts/build/copyFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/* eslint-disable no-restricted-imports */
import * as fs from 'fs-extra'
import fs from 'fs'
import * as path from 'path'

// Copies various dependencies into "dist/".
Expand Down Expand Up @@ -100,27 +100,28 @@ const tasks: CopyTask[] = [
},
]

async function copy(task: CopyTask): Promise<void> {
function copy(task: CopyTask): void {
const src = path.resolve(projectRoot, task.target)
const dst = path.resolve(outRoot, task.destination ?? task.target)

try {
await fs.copy(src, dst, {
fs.cpSync(src, dst, {
recursive: true,
overwrite: true,
force: true,
errorOnExist: false,
})
} catch (error) {
throw new Error(`Copy "${src}" to "${dst}" failed: ${error instanceof Error ? error.message : error}`)
}
}

void (async () => {
function main() {
try {
await Promise.all(tasks.map(copy))
tasks.map(copy)
} catch (error) {
console.error('`copyFiles.ts` failed')
console.error(error)
process.exit(1)
}
})()
}

void main()

0 comments on commit 5cbe42b

Please sign in to comment.