From b6a1a6babfcf12bd3a38535481873154d2c919fd Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Mon, 30 Oct 2023 10:59:42 +0500 Subject: [PATCH] fix(copyFile): With network storage shell.cp faster than copyFileSync --- src/cmd/build/index.ts | 3 ++- src/services/tocs.ts | 4 ++-- src/steps/processPages.ts | 4 ++-- src/utils/file.ts | 17 +++++++++++------ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/cmd/build/index.ts b/src/cmd/build/index.ts index 6c82e9a2..d34321cf 100644 --- a/src/cmd/build/index.ts +++ b/src/cmd/build/index.ts @@ -303,7 +303,6 @@ function preparingTemporaryFolders(userOutputFolder: string) { // Create temporary input/output folders shell.rm('-rf', args.input, args.output); shell.mkdir(args.input, args.output); - shell.chmod('-R', 'u+w', args.input); copyFiles( args.rootInput, @@ -315,4 +314,6 @@ function preparingTemporaryFolders(userOutputFolder: string) { ignore: ['node_modules/**', '*/node_modules/**'], }), ); + + shell.chmod('-R', 'u+w', args.input); } diff --git a/src/services/tocs.ts b/src/services/tocs.ts index 2f02156b..61d5fb25 100644 --- a/src/services/tocs.ts +++ b/src/services/tocs.ts @@ -1,5 +1,5 @@ import {dirname, extname, join, normalize, parse, relative, resolve, sep} from 'path'; -import {copyFileSync, existsSync, readFileSync, writeFileSync} from 'fs'; +import {existsSync, readFileSync, writeFileSync} from 'fs'; import {dump, load} from 'js-yaml'; import shell from 'shelljs'; import walkSync from 'walk-sync'; @@ -205,7 +205,7 @@ function _copyTocDir(tocPath: string, destDir: string) { writeFileSync(to, updatedFileContent); } else { - copyFileSync(from, to); + shell.cp(from, to); } }); } diff --git a/src/steps/processPages.ts b/src/steps/processPages.ts index f80c1df7..59f0d4a8 100644 --- a/src/steps/processPages.ts +++ b/src/steps/processPages.ts @@ -1,7 +1,7 @@ import type {DocInnerProps} from '@diplodoc/client'; import {basename, dirname, extname, join, relative, resolve} from 'path'; import shell from 'shelljs'; -import {copyFileSync, readFileSync, writeFileSync} from 'fs'; +import {readFileSync, writeFileSync} from 'fs'; import {bold} from 'chalk'; import {dump, load} from 'js-yaml'; import {asyncify, mapLimit} from 'async'; @@ -312,7 +312,7 @@ function copyFileWithoutChanges( const from = resolvedPathToFile; const to = resolve(outputDir, filename); - copyFileSync(from, to); + shell.cp(from, to); } async function processingFileToMd(path: PathData, metaDataOptions: MetaDataOptions): Promise { diff --git a/src/utils/file.ts b/src/utils/file.ts index 021f1dad..c599f84f 100644 --- a/src/utils/file.ts +++ b/src/utils/file.ts @@ -1,6 +1,5 @@ import {dirname, resolve} from 'path'; import shell from 'shelljs'; -import {copyFileSync} from 'fs'; import {logger} from './logger'; export function copyFiles( @@ -8,14 +7,20 @@ export function copyFiles( outputFolderPath: string, files: string[], ): void { - for (const pathToAsset of files) { - const outputDir: string = resolve(outputFolderPath, dirname(pathToAsset)); + const dirs = new Set(); + + files.forEach((pathToAsset) => { + const outputDir = resolve(outputFolderPath, dirname(pathToAsset)); const from = resolve(inputFolderPath, pathToAsset); const to = resolve(outputFolderPath, pathToAsset); - shell.mkdir('-p', outputDir); - copyFileSync(from, to); + if (!dirs.has(outputDir)) { + dirs.add(outputDir); + shell.mkdir('-p', outputDir); + } + + shell.cp(from, to); logger.copy(pathToAsset); - } + }); }