AngularJS 개발시 반복적인 작업을 제거하고 생산성을 높일 수 있는 통합 도구인 Yeoman의 사용법에 대해 알아보고, 처음 개발을 시작할 때 코드의 틀을 잡아 주는 몇가지 Seed 프로젝트를 통하여 어떤 구조로 애플리케이션을 만들어야 하는지 알아두자
1. 필요 요소 기술
- UI : Bootstrap from Twitter 간혹 Foundation을 이용
- Build/Preview : Grunt
- Test : 기본은 Karma이고, Mocha를 사용하고 싶다면 Karma-Mocha Karma구동을 위한 Mocha adapter 존재
- Package Management : Bower from Twitter
이들의 통합적으로 사용하기 위한 툴로 yeoman 을 설치하면 된다. 간단한 명령으로 AngularJS 관련 코드를 자동 생성하여 준다
yo - the scaffolding tool from Yeoman
bower - the package management tool
+ component.json : 의존관계 설정
+ .bowerrc : 컴포넌트 설치 위치정보
grunt - the build tool
+ Gruntfile.js : ant와 같은 수행 Task 정의
+ package.json : Node.js위에 구동되므로 package.json에 Grunt contributor 라이브러리 정의
karma - grunt test 시에 수행
+ karma.conf.js : 로컬 테스트 환경
+ karma-e2e.conf.js : Real Browser 테스트 환경 즉, end-to-end (Client<->Server) 테스트 수행. 사용 port 8080
- Yeoman command for AngularJS
// 설치 : grunt-cli, bower가 같이 설치된다
npm install -g yo
// Yeoman을 위한 generator 설치 for AngularJS & Karam
// 하기 에러 내역 참조하여 generator-angular의 script-base.js 파일 수정
// Generator는 Yeoman에서 생성하는 boilerplate 나 scaffolding 코드를 만들어주는
// Yeoman의 플러그인이라 생각하면 된다. 이미 만들어지 Generator 목록이 존재한다
// > https://github.com/yeoman (generator-* 목록들)
// > Generator 만들기 참조
npm install -g generator-webapp generator-angular generator-karma
// 명령어 (프로젝트 디렉토리에서 수행)
yo angular 또는 yo angular:app 애플리케이션 생성
yo angular:controller myControllerName 컨트롤러 생성
yo angular:directive myDirectiveName 디렉티브 생성
yo angular:filter myFilterName 필터 생성
yo angular:service myServiceName 서비스 생성
- yo angular:* 사용시 에러 발생 해결방법
예) yo angular:controller Test 수행시 하기와 같은 에러 발생
~/angularjs/sd_01> yo angular:controller Test
path.js:360
throw new TypeError('Arguments to path.join must be strings');
^
TypeError: Arguments to path.join must be strings
at path.js:360:15
at Array.filter (native)
at Object.exports.join (path.js:358:36)
at Generator (/usr/local/lib/node_modules/generator-angular/script-base.js:38:29)
at new Generator (/usr/local/lib/node_modules/generator-angular/controller/index.js:10:14)
==> 해결하기
1) /usr/local/lib/node_modules/generator-angular/script-base.js 의 38 번째 줄 내용을 하기와 같이 수정함
37 if (!this.options.coffee &&
38 //this.expandFiles(path.join(this.appPath, '/scripts/**/*.coffee'), {}).length > 0) { // 주석처리
39 this.expandFiles(path.join(process.cwd(), '/scripts/**/*.coffee'), {}).length > 0) { // process.cwd() 로 바꿈
40 this.options.coffee = true;
41 }
2) 재수행 : 친절하게도 TestCase까지 자동 생성해 준다
~/angularjs/sd_01> yo angular:controller Test
create app/scripts/controllers/Test.js
create test/spec/controllers/Test.js
- Yeoman command for Bower : 의존관계 라이브러리에 대한 관리 담당
// 명령어
bower search <dep> - search for a dependency in the Bower registry
bower install <dep>..<depN> - install one or more dependencies
bower list - list out the dependencies you have installed for a project
bower update <dep> - update a dependency to the latest version available
// 설치하기
// .bowerrc 에 설정된 디렉토리로 다운로드된 라이브러리가 설치된다
$ bower install angular-ui
bower cloning git://github.com/angular-ui/angular-ui.git
bower cached git://github.com/angular-ui/angular-ui.git
bower fetching angular-ui
bower checking out angular-ui#v0.4.0
bower copying /Users/nulpulum/.bower/cache/angular-ui/bd4cf7fc7bfe2a2118a7705a22201834
bower installing angular-ui#0.4.0
$ cd app/components/
$ ls
angular angular-scenario es5-shim json3
angular-mocks angular-ui jquery
$ cd ../..
$ cat .bowerrc
{
"directory": "app/components"
}
- Yeoman command for Grunt : 프러덕션 빌드, 미리보기, 테스팅
+ grunt는 task에 대한 여러 target들이 존재하는 구조이다.
+ 명령을 grunt <task> 로 주면 task밑의 모든 target이 순서대로 수행된다
+ 명령을 grunt <task>:<target> 을 지정하여 특정 target만 수행할 수도 있다
+ task와 관련한 여러가지 Plugins 이 이미 만들어져 있고 직접 만들 수도 있다 (기존 Plugins 목록)
// 명령어
grunt test - 테스트하기. run the unit tests for an app
grunt server - 브라우져에서 보기. 변경사항 실시간 반영됨. preview an app you have generated (with Livereload)
grunt - 배포본 빌드하기. build an optimized, production-ready version of your app (warning 무시 옵션 --force 사용가능)
// 브라우져 미리보기 in app root folder
grunt server
// warning -jslint 같은- 무시하고 계속 build하기
grunt --force
3. 기본 코드 생성
- Grunt-Express : Grunt와 Express 연동하기
- Yeoman-Angular-Express : Yeoman을 이용하여 Angular project 생성한 후 Express와 통합하는 예제
- Angular-Socket.io-Seed : Angular와 Socket.io를 접목 시키고자 할때 사용 (포스팅)
4. Addy Osmani 님이 이야기 하는 Automation Front-end workflow
결론적으로 Yeoman을 사용하여 기반 코드를 생성하고 빌드/테스팅 자동화를 한다. 그리고 필요한 코드들은 Seed 프로젝트를 참조하여 자기것으로 만들면 되겠다
<참조>
- 원문 : Google, Twitter and AngularJS
'Dev Environment > Build System' 카테고리의 다른 글
[Yeoman] 자신만의 Generator 만들기 - 1 (0) | 2013.04.25 |
---|---|
[Yeoman] generator-angular 와 Express.js 연동하기 (0) | 2013.04.22 |
[Grunt] Grunt API 활용 - Task 만들기 (0) | 2013.01.31 |
[Grunt] Javascript code using Grunt.js (0) | 2013.01.28 |
[Grunt] Javascript code using Ant (0) | 2013.01.28 |