1 | create-ts-index
|
2 | ----
|
3 | [![Download Status](https://img.shields.io/npm/dw/create-ts-index.svg)](https://npmcharts.com/compare/create-ts-index?minimal=true) [![Github Star](https://img.shields.io/github/stars/imjuni/create-ts-index.svg?style=popout)](https://github.com/imjuni/create-ts-index) [![Github Issues](https://img.shields.io/github/issues-raw/imjuni/create-ts-index.svg)](https://github.com/imjuni/create-ts-index/issues) [![NPM version](https://img.shields.io/npm/v/create-ts-index.svg)](https://www.npmjs.com/package/create-ts-index) [![License](https://img.shields.io/npm/l/create-ts-index.svg)](https://github.com/imjuni/create-ts-index/blob/master/LICENSE) [![cti](https://circleci.com/gh/imjuni/create-ts-index.svg?style=shield)](https://app.circleci.com/pipelines/github/imjuni/create-ts-index?branch=master)
|
4 |
|
5 | # 설치
|
6 | ```
|
7 | npm install create-ts-index --save-dev
|
8 | ```
|
9 |
|
10 | # 소개
|
11 | TypeScript 프로젝트를 개발할 때 export를 위한 index.ts 파일을 생성합니다. index.ts 파일을 export 용도로만 사용하거나 라이브러리 프로젝트를 개발할 때 유용합니다. 예를들면 [blueprint.js](http://blueprintjs.com/)와 같은 라이브러리 프로젝트를 개발한다면 컴포넌트를 다른 프로젝트에서 사용할 수 있도록 모든 파일을 export 해야 합니다. 이런 경우 create-ts-index를 아주 유용하게 사용할 수 있습니다. 라이브러리 프로젝트가 아니라도, create-ts-index를 사용해서 export 용 index.ts 파일을 사용한다면 import 구문을 간단하게 만들 수 있고 협업할 때 import 형식에 대한 고민을 줄일 수 있어서 유용합니다.
|
12 |
|
13 | 예를들어, 아래와 같은 디렉터리가 있다고 가정합시다.
|
14 |
|
15 | ```
|
16 | src/
|
17 | app.ts
|
18 | component/
|
19 | Nav.ts
|
20 | Button.ts
|
21 | ```
|
22 |
|
23 | create-ts-index create sub-command는 아래와 같이 export index.ts 파일을 생성합니다.
|
24 |
|
25 | ```
|
26 | src/
|
27 | app.ts
|
28 | > index.ts
|
29 | // created from 'create-ts-index'
|
30 | export * from './component';
|
31 | export * from './app';
|
32 | component/
|
33 | Nav.ts
|
34 | Button.ts
|
35 | > index.ts
|
36 | // created from 'create-ts-index'
|
37 | export * from './Nav';
|
38 | export * from './Button';
|
39 | ```
|
40 |
|
41 | 만약 create-ts-index entrypoint sub-command를 사용하면 아래와 같이 동작합니다.
|
42 |
|
43 | ```
|
44 | src/
|
45 | app.ts
|
46 | component/
|
47 | Nav.ts
|
48 | Button.ts
|
49 | > entrypoint.ts
|
50 | // created from 'create-ts-index'
|
51 | export * from './src/app.ts'
|
52 | export * from './src/component/Nav.ts'
|
53 | export * from './src/component/Button.ts'
|
54 | ```
|
55 |
|
56 | index.ts 파일을 webpack entrypoint로 사용할 수 있습니다.
|
57 |
|
58 | # 옵션
|
59 | ## 라이브러리로 사용할 경우
|
60 | * `fileFirst: boolean` 생성되는 export 파일내용에서 파일이름을 먼저 export 할지 디렉터리 이름을 먼저 export 할지 결정합니다. 기본 값은 `false` 입니다.
|
61 | * `addNewline: boolean` 파일 마지막에 줄바꿈 문자를 추가할지 말지를 결정합니다. 기본 값은 `true` 입니다.
|
62 | * `useSemicolon: boolean` 줄 마지막에 `;` 문자를 추가할지 말지를 결정합니다. 기본 값은 `true` 입니다.
|
63 | * `useTimestamp: boolean` 파일 처음에 주석을 작성할 때 시간(YYYY-MM-DD HH:mm 형식)을 추가할지 말지를 결정합니다. 기본 값은 `false` 입니다.
|
64 | * `includeCWD: boolean` 작업디렉터리로 전달된 cwd에 대해서 index.ts 파일을 생성할 것인지에 대해서 결정합니다. 기본 값은 `true` 입니다.
|
65 | * `excludes: string[]` 제외할 디렉터리를 전달합니다. 기본 값은 `['@types', 'typings', '__test__', '__tests__']` 입니다.
|
66 | * `fileExcludePatterns: string[]` 제외할 파일이름 패턴을 전달합니다. 기본 값은 `[]` 입니다. 전달된 패턴은 indexOf 함수를 사용하여 파일이름에서 검색되며 indexOf 함수 결과가 0 이상인 경우 제외됩니다.
|
67 | * `targetExts: string[]` export 구문을 생성할 때 사용할 확장자명을 전달합니다. 기본 값은 `['ts', 'tsx']` 입니다. 확장자명을 전달할 때는 예와같이 점 문자를 제외하고 전달해야합니다.
|
68 | * `globOptions: glob.IOptions` [node-glob](https://github.com/isaacs/node-glob) 옵션 값을 전달할 수 있습니다. 자세한 내용은 링크문서를 참고하세요.
|
69 | * `quote` export 구문에서 사용할 따옴표 문자를 전달합니다. 기본 값은 홑따옴표 입니다.
|
70 | * `verbose` 실행할 때 더 많은 로그 메시지를 출력합니다.
|
71 | * `withoutComment` index.ts 파일 또는 entrypoint.ts 파일 맨 윗줄에 추가되는 주석을 제거합니다.
|
72 | * `withoutBackupFile` index.ts 파일 또는 entrypoint.ts 파일을 생성할 때 이미 파일이 있는 경우 백업 파일을 생성하는 기능을 사용하지 않습니다.
|
73 | * `output` index.ts 파일 또는 entrypoint.ts 파일 이름의 이름을 변경합니다.
|
74 |
|
75 | ## CLI(command-line interface)로 사용할 경우
|
76 | * `-f --filefirst` 생성되는 export 파일내용에서 파일이름을 먼저 export 할지 디렉터리 이름을 먼저 export 할지 결정합니다. 옵션을 생략하면 `false`, 전달하면 `true` 이며, 기본 값은 false 입니다.
|
77 | * `-n --addnewline` 파일 마지막에 줄바꿈 문자를 추가할지 말지를 결정합니다. 옵션을 생략하면 `true`, 전달하면 `false` 입니다.
|
78 | * `-s --usesemicolon` 줄 마지막에 `;` 문자를 추가할지 말지를 결정합니다. 옵션을 생략하면 `true`, 전달하면 `false` 입니다.
|
79 | * `-t --usetimestamp` 파일 처음에 주석을 작성할 때 시간(YYYY-MM-DD HH:mm 형식)을 추가할지 말지를 결정합니다. 옵션을 생략하면 `false` 전달하면 `true` 입니다.
|
80 | * `-c --includecwd` 작업디렉터리로 전달된 cwd에 대해서 index.ts 파일을 생성할 것인지에 대해서 결정합니다. 옵션을 생략하면 `true` 전달하면 `false` 입니다.
|
81 | * `-e --excludes [comma separated exclude directories]` 제외할 디렉터리를 전달합니다. 기본 값은 `['@types', 'typings', '__test__', '__tests__']` 입니다. 사용자 설정 값을 전달하려고 하는 경우 아래 예제와 같이 쉼표로 구분해서 전달하세요.
|
82 | * `-i --fileexcludes [comma separated extname]` 제외할 파일이름 패턴을 전달합니다.기본 값은 `[]` 입니다. 사용자 설정 값을 전달하려고 하는 경우 쉼표로 구분해서 전달하세요.
|
83 | * `-x --targetexts [comma separated extname]` export 구문을 생성할 때 사용할 확장자명을 전달합니다. 기본 값은 `['ts', 'tsx']` 입니다. 확장자명을 전달할 때는 예와같이 점 문자를 제외하고 전달해야합니다. 또한 사용자 설정 값을 전달하려고 하는 경우 아래 예제와 같이 쉼표로 구분해서 전달하세요.
|
84 | * `-v --verbose` 로그 메시지를 더 상세하게 출력합니다. 1.5.0 버전부터 로그 메시지 출력을 간소화 하였습니다. 하여 기존과 같이 상세한 로그를 보고자 하는 경우 이 옵션을 추가해야 합니다.
|
85 | * `-q --quote` 따옴표를 결정한다. 쌍따옴표와 홑따옴표를 전달할 수 있고 전달한 문자를 사용해서 따옴표를 출력한다.
|
86 | * `-w --withoutcomment` index.ts 파일 또는 entrypoint.ts 파일 맨 윗줄에 추가되는 주석을 제거합니다.
|
87 | * `-b --withoutbackup` index.ts 파일 또는 entrypoint.ts 파일을 생성할 때 이미 파일이 있는 경우 백업 파일을 생성하는 기능을 사용하지 않습니다.
|
88 | * `-o --output` index.ts 파일 또는 entrypoint.ts 파일 이름의 이름을 변경합니다.
|
89 |
|
90 | # 사용법
|
91 | ## 라이브러리로 사용하는 경우
|
92 | ### TypeScritIndexWriter 사용
|
93 | ```
|
94 | const tsiw = new TypeScritIndexWriter();
|
95 | const option = TypeScritIndexWriter.getDefaultOption('./src');
|
96 |
|
97 | (async () => {
|
98 | await tsiw.create(option);
|
99 |
|
100 | // or
|
101 |
|
102 | await tsiw.createEntrypoint(option);
|
103 | })();
|
104 | ```
|
105 |
|
106 | ### CommandModule 사용
|
107 | ```
|
108 | (async () => {
|
109 | const option = CreateTsIndexOption.getOption({});
|
110 | const createCommand = new CreateCommandModule();
|
111 | await createCommand.do(process.cwd(), option);
|
112 | });
|
113 | ```
|
114 |
|
115 | ## CLI로 사용하기
|
116 | Git-style sub-command를 사용합니다.
|
117 |
|
118 | * create
|
119 | * index.ts 파일을 재귀적으로 생성합니다.
|
120 | * entrypoint
|
121 | * entrypoint.ts 파일을 생성합니다.
|
122 | * init
|
123 | * `.ctirc` 파일을 생성합니다.
|
124 | * clean
|
125 | * entrypoint.ts 파일과 index.ts 파일을 모두 삭제합니다. 복원할 수 없으니 주의하세요.
|
126 |
|
127 | ```
|
128 | # 기본 사용법
|
129 | ## create 명령어
|
130 | cti ./src
|
131 | cti create ./src
|
132 |
|
133 | ## entrypoint 명령어
|
134 | cti entrypoint ./src
|
135 |
|
136 | # 줄바꿈 문자 포함하지 않기
|
137 | cti create -n ./src
|
138 | ## or
|
139 | cti entrypoint -n ./src
|
140 |
|
141 | # 제외할 디렉터리를 직접 전달하는 방법
|
142 | cti create -n -e @types typings __test__ __tests__ pages ./src
|
143 | ## or
|
144 | cti entrypoint -n -e @types typings __test__ __tests__ pages ./src
|
145 |
|
146 | # entrypoint.ts 와 index.ts 파일 삭제하기
|
147 | cti clean ./src
|
148 |
|
149 | # 하위 디렉터리를 포함해서 실행하기
|
150 | for f in *; do cti create ./$f; done
|
151 |
|
152 | # 여러 디렉터리를 전달하기
|
153 | cti create ./src/server ./src/client ./src/module
|
154 | ```
|
155 |
|
156 | ## CLI 에 .ctirc 설정파일 적용하기
|
157 | create-ts-index cli는 1.7.0버전에서 .ctirc 설정파일을 지원합니다. 설정파일 이름은 오직 `.ctirc` 만 가능합니다. `.ctirc`는 여러 개를 만들어서 적용할 수 있습니다. create-ts-index는 제일 먼저 인자로 주어진 디렉터리에서 `.ctirc` 파일을 찾습니다. 다음은 cti 스크립트가 실행된 디렉터리에서 `.ctirc` 파일을 찾습니다. 그리고 스크립트 디렉터리, 인자 디렉터리 순으로 설정을 차례대로 적용하고 스크립트를 실행합니다. 단, cti 스크립트 인자로 주어진 설정은 우선적으로 적용됩니다. `.ctirc` 파일은 [json5](https://json5.org) 형식으로 기술되며 주석을 쓰거나 키 값을 쌍따옴표를 쓰지 않고 기술할 수 있습니다. 자세한 스펙은 링크를 참고하세요.
|
158 |
|
159 | 아래 예제는 `.ctirc` 파일을 어떻게 찾고, 적용하는지를 보여줍니다.
|
160 | ```
|
161 | # execute on /Users/cti/github/create-ts-index
|
162 | sh> cti create ./example/type01
|
163 |
|
164 | # search configuration file on "/Users/cti/github/create-ts-index"
|
165 | # search configuration file on "/Users/cti/github/create-ts-index/example/type01"
|
166 | # apply configuration by "/Users/cti/github/create-ts-index"
|
167 | # apply configuration by "/Users/cti/github/create-ts-index/example/type01"
|
168 | # every configuration is overwrited
|
169 | ```
|
170 |
|
171 | ### .ctirc 파일 생성하기
|
172 | cli를 사용해서 `.ctirc` 파일을 생성할 수 있습니다.
|
173 |
|
174 | ```bash
|
175 | # 현재 디렉터리에 생성
|
176 | > cti init
|
177 |
|
178 | # 여러 디렉터리에 생성
|
179 | > cti init ./example/type03 ./example/type02
|
180 | ```
|
181 |
|
182 | # Language
|
183 | * [English](https://github.com/imjuni/create-ts-index/blob/master/README.md)
|
184 | * [Korean](https://github.com/imjuni/create-ts-index/blob/master/README.ko.md)
|