Total: Today: Yesterday:
연구소/node.js | 2016. 4. 15. 18:47 | Posted by 자수씨

회사에서 게임 개발/운영 업무를 하다보니 신규 컨텐츠 제작 시 반복적인 신규 리소스 배포작업을 하고 있었습니다.


node.js 공부도 할겸 배포 스크립트 만들어봤습니다.



#h3 배포하기


목표로 했었던 배포 환경을 포스팅용으로 구성해보았습니다.


  • source: 복사할 소스 이미지들이 있는 디렉토리
  • destination/project: 프로젝트(코딩)에 리소스 디렉토리
  • destination/share: 공유 디렉토리


PS E:\temp\sources\resources> tree /a /f

E:.

+---destination

|   +---project

|   |   +---A

|   |   +---B

|   |   \---C

|   \---share

|       +---1

|       +---2

|       \---3

\---source

        02.png

        03.png

        04.png



```Gruntfile.js```
### js

'use strict';

module.exports = function(grunt) {
var readline = require('readline');
var path = require('path');
var sourceDir = 'E:/temp/sources/resources/source';
var shareDestDir = {
root: 'E:/temp/sources/resources/destination/share',
bg: '1',
logo: '2',
icon: '3'
};
var projectDestDir = {
root: 'E:/temp/sources/resources/destination/project',
bg: 'A',
logo: 'B',
icon: 'C'
};
var resourceId = -1;

// Project configuration.
grunt.initConfig({
deploy: {
main: {
files: [
{cwd: sourceDir, src: ['02.png'], dest: [shareDestDir, projectDestDir], type: 'bg'},
{cwd: sourceDir, src: ['03.png'], dest: [shareDestDir, projectDestDir], type: 'logo'},
{cwd: sourceDir, src: ['04.png'], dest: [shareDestDir, projectDestDir], type: 'icon'},
]
}
}
});

grunt.registerTask('question', function() {
var done = this.async();
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Input resource ID: ', function(answer) {
rl.close();
resourceId = answer;
grunt.log.writeln('입력받은 아이디: ' + resourceId);
done();
});
});

grunt.registerMultiTask('deploy', function() {

var options = this.options({
encoding: grunt.file.defaultEncoding,
timestamp: false,
mode: false
});

var copyOptions = {
encoding: options.encoding,
process: options.process,
noProcess: options.noProcess
};

this.files.forEach(function(filePair) {
var type = filePair.type;
filePair.src.forEach(function(src) {
var srcPath = path.join(filePair.cwd, src);
var destFileName = type + '_' + resourceId + '.png';
filePair.dest.forEach(function(dest) {
if (dest[type]) {
var destDirName = path.join(dest.root, dest[type]);
var destPath = path.join(destDirName, destFileName);
grunt.file.copy(srcPath, destPath, copyOptions);
grunt.log.writeln('Copy to ' + destPath);
}
});
});
});
});
grunt.registerTask('default', [
'question', 'deploy'
]);
};


기존에 ```copy``` 태스크를 ```deploy``` 로 변경하였으며, 하드코딩으로 파일 복사가 아닌 ```grunt.initConfig({...})``` 에 설정을 읽어들여 파일을 복사하도록 수정하였습니다.



빌드 스크립트 실행결과는 다음과 같습니다.


PS E:\temp\sources\grunt> grunt

Running "question" task

Input resource ID: 0001

입력받은 아이디: 0001


Running "deploy:main" (deploy) task

Copy to E:\temp\sources\resources\destination\share\1\bg_0001.png

Copy to E:\temp\sources\resources\destination\project\A\bg_0001.png

Copy to E:\temp\sources\resources\destination\share\2\logo_0001.png

Copy to E:\temp\sources\resources\destination\project\B\logo_0001.png

Copy to E:\temp\sources\resources\destination\share\3\icon_0001.png

Copy to E:\temp\sources\resources\destination\project\C\icon_0001.png


Done, without errors.



파일들이 복사된 경로를 트리형태로 보면 아래와 같습니다.


PS E:\temp\sources\resources> tree /a /f

E:.

+---destination

|   +---project

|   |   +---A

|   |   |       bg_0001.png

|   |   |

|   |   +---B

|   |   |       logo_0001.png

|   |   |

|   |   \---C

|   |           icon_0001.png

|   |

|   \---share

|       +---1

|       |       bg_0001.png

|       |

|       +---2

|       |       logo_0001.png

|       |

|       \---3

|               icon_0001.png

|

\---source

        02.png

        03.png

        04.png



지금까지 21번이나 복사하는 작업을 반복했는데, 왜 이런걸 만들 생각을 그동안 못했는지 눙물이 납니다 ㅠㅠ