Search
🖼️

(작성 중) admin 용 이미지 리소스 복사 스크립트 작동 방식 개선 및 모듈화 및 성능 최적화

Date
2023/09/22
Authors
Published
Slug
admin-resource-copy-script

배경

문제 상황

ㅁㄴㅇㄹㅁ
ㅁㄴㅇㄹㅁㄴㅇㄹ
ㅁㄴㅇㄹㄴㅁㄹ

해결책

로컬에 있는 파일을 복사하기 위해 방법을 검색해 보았다.
정보 수집을 위해 같은 스튜디오 내에 있는 어드민 개발자 분에게 연락을 드렸다
비슷한 상황이라 파이썬으로 작성한 스크립트를 이용해 파일을 주기적으로 복사한다고 이야기 해주셨다
쉘 스크립트에 비해서 관리가 편하게 느껴졌지만 약간의 아쉬움이 느껴졌다
되도록 같은 스튜디오에서 언어와 기술 스택이 통일성 있게 구성되도록 신경쓰고 있었기 때문이다
게임 개발의 특성상 각 게임팀 위주로 폐쇄적인 특성을 가지고 있어서 갈라파고스화 된 부분이 있었다
하지만 프론트엔드 개발자끼리는 정보
찾아보니 node.js의 fs모듈로도 작업이 가능하다는 것을 확인하였다

코드 개선 과정

1차 코드

2차 코드

최종 코드

import { promises as fs } from 'fs'; import path from 'path'; import { config } from './config.mjs'; import { $ } from 'zx'; const main = async () => { const { targetPath, sourcePath, additionalPaths, prefixes } = config; await fs.mkdir(targetPath, { recursive: true }); const files = await collectFiles(sourcePath, additionalPaths, prefixes); await copyFiles(files, targetPath).then(() => { console.log(`.png 파일들이 모두 ${targetPath}로 복사되었습니다.`); }); }; const collectFiles = async (sourcePaths, additionalPaths, prefixes) => { const findPatterns = prefixes.map(prefix => `-name ${prefix}`).join(' -o '); const findTasks = [ ...sourcePaths.map(path => findFiles(path, findPatterns)), ...additionalPaths.map(path => findFiles(path, '-name *.png')) ]; const allFiles = await Promise.all(findTasks); return allFiles.flat(); }; const copyFiles = async (files, targetPath) => { const copyTasks = files.map(file => fs.copyFile(file, `${targetPath}/${path.basename(file)}`)); await Promise.all(copyTasks); }; const findFiles = async (path, pattern) => { const command = ['find', path, '-type', 'f'].concat(pattern.split(' ')); let files = await $`${command}`; return files.stdout.split('\n').filter(Boolean); }; main().catch(console.error);
JavaScript
복사