이번 우아한테크코스 프론트엔드 파트로 신청을 하게되어 프리코스를 진행하게 되었다. 프리코스는 총 4주 동안 4번의 과제가 있었다. 오늘은 1주차에 했던 과제에 대해서 리뷰를 해볼까한다. (많이 늦었지만,,)
주차별 해당 깃허브 링크를 들어간다.
해당 프로젝트를 fork를 딴다.
프로젝트 fork딴다란??
GitHub에서 다른 사람의 프로젝트를 복사해서 자신의 저장소로 가져오는 것을 말한다. 포크를 통해 원본 프로젝트의 복사본을 자신의 저장소로 가져와 독립적으로 수정할 수 있다. 수정 사항이 완료되면, 원본 프로젝트에 변경 사항을 제안하는 Pull Request를 생성하여 기여할 수 있다.
주제 : 입력한 문자열에서 숫자를 추출하여 더하는 계산기를 구현한다.
project-root
│
├── __tests__ # 테스트 폴더
│ └── ApplicationTest.js # 애플리케이션 테스트 파일
│
├── src # 소스 코드 폴더
│ ├── App.js # App 컴포넌트
│ └── index.js # 메인 엔트리 파일
│
├── .gitignore # Git 무시 파일
├── .npmrc # npm 설정 파일
├── package-lock.json # npm 패키지 잠금 파일
├── package.json # npm 패키지 설정 파일
└── README.md # 프로젝트 설명 파일
1. README.md
README.md 파일에서는 프로젝트에 대한 설명을 적고, 기능명세서를 적는 파일로 활용했다. 작업을 시작하기전에 기능 별로 구현할 것들을 미리 적었다.
ex)구현할 기능 목록
2. index.js
index.js
파일은 App.js
에서 App
클래스를 불러와 인스턴스를 생성하고 run
메서드를 실행함으로써 프로그램의 실행을 시작한다. run
메서드는 비동기 작업을 포함할 수 있으며, 이 파일은 전체 애플리케이션의 초기화와 실행을 담당하는 엔트리 포인트로 사용된다.
3. App.js
App.js
는 문자열 계산기 애플리케이션의 주요 로직을 구현한 App
클래스이다. run
메서드에서 문자열 입력을 받아 처리하며, 커스텀 구분자와 에러 처리 로직이 포함되어 있다. 주로 문자열을 특정 구분자로 분리하여 숫자로 변환하고, 합산 결과를 출력하는 기능을 수행한다. 즉, 이 파일은 문자열 계산기의 핵심 기능을 담당하는 애플리케이션 로직이다.
4. tests/ApplicationTest.js
App
클래스의 기능을 테스트하는 Jest 테스트 코드이다. mockQuestions
와 getLogSpy
함수를 사용해 App 클래스의 입력과 출력을 제어하고 검증한다. 여기서는 커스텀 구분자를 사용하는 기능과 예외 상황에서 오류를 발생시키는 로직이 올바르게 작동하는지 테스트한다.
app.js
파일
1. run 메서드
async run() {
const input = await Console.readLineAsync('덧셈할 문자열을 입력해 주세요.');
const result = this.handleString(input);
Console.print(`결과 : ${result}`);
}
2. handleString
메서드
handleString(input) {
if (!input.trim()) this.throwError(App.ERROR_MESSAGES.EMPTY_STRING);
if (/\d\s+\d/.test(input)) this.throwError(App.ERROR_MESSAGES.INVALID_FORMAT);
const {values, delimiter} = this.checkDelimiter(input);
return this.calculateSumFromString(values, delimiter);
}
checkDelimiter
메서드를 호출하고, 구분자와 숫자 값을 가져온다.calculateSumFromString
을 통해 합산 결과를 반환한다.3. checkDelimiter 메서드
checkDelimiter(input) {
let delimiter = App.DEFAULT_DELIMITER;
let values = input;
const match = input.match(App.CUSTOM_DELIMITER_PATTERN);
if (match) {
if (!isNaN(match[1])) this.throwError(App.ERROR_MESSAGES.INVALID_DELIMITER);
delimiter = new RegExp(match[1]);
values = input.split('\\n')[1];
}
return {values, delimiter};
}
,
또는 :
를 설정한다.CUSTOM_DELIMITER_PATTERN
을 사용해 커스텀 구분자(//구분자\n형식)가 있는지 확인한다.values
에 저장한다.{values, delimiter}
객체를 반환한다.4. calculateSumFromString 메서드
calculateSumFromString(values, delimiter) {
const valueArray = values.split(delimiter).map(Number);
const hasNegative = valueArray.some(num => num < 0);
if (hasNegative) this.throwError(App.ERROR_MESSAGES.NEGATIVE_NUMBER);
return valueArray.reduce((sum, num) => sum + num, 0);
}
5. throwError 메서드
throwError(message) {
throw new Error(`[ERROR] ${message}`);
}
npm test
명령어로 테스트 코드 실행npm start
명령어로 직접 프로그램 실행노드 버전 20.17.0 이상으로 하라고 하는거다.
그래서 난 노트북 노드 버전을 확인해봤다.
?? 20.12.2 길래 얼릉 노드 버전을 업데이트 시키기로 했다.
자 이제 노드를 20.18.0 버전으로 업데이트 시켰다. (최신 버전)
이제 과제를 시작해보자!
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver/ranges/simplify.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver/functions/sort.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver/ranges/subset.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver/ranges/to-comparators.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver/functions/valid.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/functions/inc.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/notifications.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver/ranges/valid.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/classes/index.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/internal/lrucache.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/functions/lt.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/functions/lte.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/object-entries.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/ranges/ltr.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/object-fit.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/functions/major.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/object-observe.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/ranges/max-satisfying.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/ranges/min-satisfying.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/object-values.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/ranges/min-version.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/objectrtc.js'
npm warn cleanup Failed to remove some directories [
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/console/node_modules/color-convert',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/console/node_modules/color-convert'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/console/node_modules/color-convert'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver/classes'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver/classes'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/istanbul-lib-instrument',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/istanbul-lib-instrument/src'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/istanbul-lib-instrument/src'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/functions'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/functions'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/core/node_modules/chalk',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/core/node_modules/chalk'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/core/node_modules/chalk'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/console/node_modules/chalk',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/console/node_modules/chalk'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/console/node_modules/chalk'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/regions'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/regions'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@babel/helpers',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@babel/helpers/lib/helpers'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@babel/helpers/lib/helpers'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@babel',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@babel/helpers/lib/helpers'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@babel/helpers/lib/helpers'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/regions'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/regions'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/internal'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/internal'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/istanbul-lib-instrument'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/istanbul-lib-instrument'
npm warn cleanup }
npm warn cleanup ],
npm warn cleanup [
npm warn cleanup '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot',
npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver'] {
npm warn cleanup errno: -66,
npm warn cleanup code: 'ENOTEMPTY',
npm warn cleanup syscall: 'rmdir',
npm warn cleanup path: '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/jest-snapshot/node_modules/semver'
npm warn cleanup }
npm warn cleanup ]
npm warn cleanup ]
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/functions/minor.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/offline-apps.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/functions/neq.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/offscreencanvas.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/ranges/outside.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/ogg-vorbis.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/@jest/reporters/node_modules/semver/internal/parse-options.js'
npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open '/Users/hoyy_choi/Desktop/javascript-calculator-7/node_modules/caniuse-lite/data/features/ogv.js'
npm error code EACCES
npm error syscall open
npm error path /Users/hoyy_choi/.npm/_cacache/tmp/997c9190
npm error errno -13
npm error
npm error Your cache folder contains root-owned files, due to a bug in
npm error previous versions of npm which has since been addressed.
npm error
npm error To permanently fix this problem, please run:
npm error sudo chown -R 501:20 "/Users/hoyy_choi/.npm"
npm error A complete log of this run can be found in: /Users/hoyy_choi/.npm/_logs/2024-10-16T03_59_54_339Z-debug-0.log
갑자기 이런 오류가 발생했다..
구글링 해본결과,
npm 캐시 폴더에 접근 권한 문제가 발생한 경우였다. 예전에 npm을 실행할 때 root 권한으로 실행된 적이 있어, 일부 캐시 파일이나 폴더가 root 사용자에 의해 소유된 상태인 것이었다. 이 때문에 현재 사용자로 npm을 실행할 때 접근 권한 오류가 발생하고 있던 것!
그래서 난 우선적으로 sudo 명령어를 이용했다.
sudo npm install
으로 작업을 해결했다. 관리자 모드로 계속 의존성 패키지를 받아야하나??
→
npm
캐시 폴더에 대한 접근 권한을 현재 사용자로 변경합니다. 터미널에 다음 명령어를 입력하세요:bash
Copy code
sudo chown -R $(whoami) ~/.npm
~/.npm
)의 소유권을 현재 사용자로 변경합니다. *$(whoami)
는 현재 사용자의 이름을 반환합니다.이렇게 한후,
캐시 문제를 방지하기 위해 npm 캐시를 정리하는 것이 좋습니다. 다음 명령어를 실행하여 npm 캐시를 정리할 수 있습니다:
bash
Copy code
npm cache clean --force
해주니,
다시 아래 명령어가 잘 동작하기 시작했다
npm install
첫 주차 프리코스를 진행하면서, 계속해서 머릿속에서 "어떻게 하면 더 직관적이고 이해하기 쉬운 코드를 작성할 수 있을까?"라는 고민을 했습니다. 코드는 다른 사람이 봐도 쉽게 이해할 수 있어야 한다고 생각했기 때문에, 매일 조금씩 코드를 개선하고자 노력했습니다.
우선 기능을 모두 작성한 후, 전체적인 로직이 잘 동작하는 것을 확인했습니다. 이후, 가독성을 높이기 위해 기능별로 코드를 모듈화했습니다. 그 과정에서 발생할 수 있는 다양한 에러 상황을 고려하며 코드를 점진적으로 수정해 나갔습니다. 마지막으로 파일별로 기능을 분리할지 고민했으나, 이번 과제의 특성상 기능이 단순하고 코드의 양이 적었기 때문에 단일 책임 원칙을 적용하는 것이 더 좋다고 판단했습니다. 그래서 하나의 클래스 안에서 기능별로 함수를 나누어 구현했습니다.
특히, 우아한테크코스에서 제공한 라이브러리를 보면서 많은 것을 배웠습니다. 단순한 print, readLineAsync 같은 함수들이 어떻게 개발되었는지 살펴보며, 편리하게 설계된 라이브러리의 중요성을 깨달았습니다. 이를 보며 저도 언젠가 직접 라이브러리를 개발하고 배포해보고 싶다는 생각이 들었습니다. 앞으로 남은 2주차도 최선을 다해 참여하며 더 많은 경험을 쌓아 나가고 싶습니다.