@@ -2,12 +2,27 @@ var path = require('path');
2
2
var fs = require ( 'fs-extra' ) ;
3
3
var exec = require ( 'child_process' ) . exec ;
4
4
var runSeries = require ( 'run-series' ) ;
5
+ var glob = require ( 'glob' ) ;
5
6
6
7
var common = require ( './util/common' ) ;
7
8
var constants = require ( './util/constants' ) ;
8
9
var pkg = require ( '../package.json' ) ;
9
10
10
- var packagesSpecs = constants . partialBundlePaths
11
+ var year = ( new Date ( ) ) . getFullYear ( ) ;
12
+
13
+ var copyrightAndLicense = [
14
+ '## Copyright and license' ,
15
+ '' ,
16
+ 'Code and documentation copyright ' + year + ' Plotly, Inc.' ,
17
+ '' ,
18
+ 'Code released under the [MIT license](https://github.com/plotly/plotly.js/blob/master/LICENSE).' ,
19
+ '' ,
20
+ 'Docs released under the [Creative Commons license](https://github.com/plotly/documentation/blob/source/LICENSE).' ,
21
+ ''
22
+ ] . join ( '\n' ) ;
23
+
24
+ // sync "partial bundle" packages
25
+ constants . partialBundlePaths
11
26
. map ( function ( d ) {
12
27
return {
13
28
name : 'plotly.js-' + d . name + '-dist' ,
@@ -23,18 +38,21 @@ var packagesSpecs = constants.partialBundlePaths
23
38
main : 'plotly.js' ,
24
39
dist : constants . pathToPlotlyDist ,
25
40
desc : 'Ready-to-use plotly.js distributed bundle.' ,
26
- } ] ) ;
41
+ } ] )
42
+ . forEach ( syncPartialBundlePkg ) ;
43
+
44
+ // sync "locales" package
45
+ syncLocalesPkg ( {
46
+ name : 'plotly.js-locales' ,
47
+ dir : path . join ( constants . pathToLib , 'locales' ) ,
48
+ main : 'index.js' ,
49
+ desc : 'Ready-to-use plotly.js locales' ,
50
+ } ) ;
27
51
28
- packagesSpecs . forEach ( function ( d ) {
52
+ function syncPartialBundlePkg ( d ) {
29
53
var pkgPath = path . join ( constants . pathToBuild , d . name ) ;
30
54
31
- function initDirectory ( cb ) {
32
- if ( common . doesDirExist ( pkgPath ) ) {
33
- cb ( ) ;
34
- } else {
35
- fs . mkdir ( pkgPath , cb ) ;
36
- }
37
- }
55
+ var initDirectory = _initDirectory ( d , pkgPath ) ;
38
56
39
57
function writePackageJSON ( cb ) {
40
58
var cnt = {
@@ -61,6 +79,7 @@ packagesSpecs.forEach(function(d) {
61
79
) ;
62
80
}
63
81
82
+
64
83
function writeREADME ( cb ) {
65
84
var moduleList = common . findModuleList ( d . index ) ;
66
85
@@ -88,14 +107,7 @@ packagesSpecs.forEach(function(d) {
88
107
'var Plotly = require(\'' + d . name + '\');' ,
89
108
'```' ,
90
109
'' ,
91
- '## Copyright and license' ,
92
- '' ,
93
- 'Code and documentation copyright 2018 Plotly, Inc.' ,
94
- '' ,
95
- 'Code released under the [MIT license](https://github.com/plotly/plotly.js/blob/master/LICENSE).' ,
96
- '' ,
97
- 'Docs released under the [Creative Commons license](https://github.com/plotly/documentation/blob/source/LICENSE).' ,
98
- ''
110
+ copyrightAndLicense
99
111
] ;
100
112
101
113
fs . writeFile (
@@ -109,31 +121,171 @@ packagesSpecs.forEach(function(d) {
109
121
fs . copy ( d . dist , path . join ( pkgPath , d . main ) , cb ) ;
110
122
}
111
123
112
- function copyLicense ( cb ) {
113
- fs . copy (
114
- path . join ( constants . pathToRoot , 'LICENSE' ) ,
115
- path . join ( pkgPath , 'LICENSE' ) ,
124
+ var copyLicense = _copyLicense ( d , pkgPath ) ;
125
+
126
+ var publishToNPM = _publishToNPM ( d , pkgPath ) ;
127
+
128
+ runSeries ( [
129
+ initDirectory ,
130
+ writePackageJSON ,
131
+ writeREADME ,
132
+ copyMain ,
133
+ copyLicense ,
134
+ publishToNPM
135
+ ] , function ( err ) {
136
+ if ( err ) throw err ;
137
+ } ) ;
138
+ }
139
+
140
+ function syncLocalesPkg ( d ) {
141
+ var pkgPath = path . join ( constants . pathToBuild , d . name ) ;
142
+
143
+ var initDirectory = _initDirectory ( d , pkgPath ) ;
144
+
145
+ var localeFiles ;
146
+ function listLocalFiles ( cb ) {
147
+ var localeGlob = path . join ( constants . pathToLib , 'locales' , '*.js' ) ;
148
+ glob ( localeGlob , function ( err , _localeFiles ) {
149
+ if ( err ) cb ( null ) ;
150
+ localeFiles = _localeFiles ;
151
+ cb ( ) ;
152
+ } ) ;
153
+ }
154
+
155
+ function writePackageJSON ( cb ) {
156
+ var cnt = {
157
+ name : d . name ,
158
+ version : pkg . version ,
159
+ description : d . desc ,
160
+ license : pkg . license ,
161
+ main : d . main ,
162
+ repository : pkg . repository ,
163
+ bugs : pkg . bugs ,
164
+ author : pkg . author ,
165
+ keywords : pkg . keywords ,
166
+ files : [
167
+ 'LICENSE' ,
168
+ 'README.md' ,
169
+ d . main
170
+ ] . concat ( localeFiles . map ( function ( f ) { return path . basename ( f ) ; } ) )
171
+ } ;
172
+
173
+ fs . writeFile (
174
+ path . join ( pkgPath , 'package.json' ) ,
175
+ JSON . stringify ( cnt , null , 2 ) + '\n' ,
116
176
cb
117
177
) ;
118
178
}
119
179
120
- function publishToNPM ( cb ) {
121
- if ( process . env . DRYRUN ) {
122
- console . log ( 'dry run, did not publish ' + d . name ) ;
123
- cb ( ) ;
124
- return ;
125
- }
126
- exec ( 'npm publish' , { cwd : pkgPath } , cb ) . stdout . pipe ( process . stdout ) ;
180
+ function writeREADME ( cb ) {
181
+ var cnt = [
182
+ '# ' + d . name ,
183
+ '' ,
184
+ d . desc ,
185
+ '' ,
186
+ 'For more info on plotly.js, go to https://github.com/plotly/plotly.js' ,
187
+ '' ,
188
+ '## Installation' ,
189
+ '' ,
190
+ '```' ,
191
+ 'npm install ' + d . name ,
192
+ '```' ,
193
+ '## Usage' ,
194
+ '' ,
195
+ 'For example to setup the `fr` locale:' ,
196
+ '' ,
197
+ '```js' ,
198
+ '// ES6 module' ,
199
+ 'import Plotly from \'plotly.js\';' ,
200
+ 'import locale from \'' + d . name + '/fr' + '\';' ,
201
+ '' ,
202
+ '// CommonJS' ,
203
+ 'var Plotly = require(\'plotly.js\');' ,
204
+ 'var locale = require(\'' + d . name + '/fr\');' ,
205
+ '' ,
206
+ '// then' ,
207
+ 'Plotly.register(locale);' ,
208
+ 'Plotly.setPlotConfig({locale: \'fr\'})' ,
209
+ '```' ,
210
+ '' ,
211
+ copyrightAndLicense
212
+ ] ;
213
+
214
+ fs . writeFile (
215
+ path . join ( pkgPath , 'README.md' ) ,
216
+ cnt . join ( '\n' ) ,
217
+ cb
218
+ ) ;
219
+ }
220
+
221
+ function writeMain ( cb ) {
222
+ var cnt = [ constants . licenseSrc , '' ] ;
223
+ localeFiles . forEach ( function ( f ) {
224
+ var n = path . basename ( f , '.js' ) ;
225
+ cnt . push ( 'exports[\'' + n + '\'] = require(\'./' + n + '.js\');' ) ;
226
+ } ) ;
227
+ cnt . push ( '' ) ;
228
+
229
+ fs . writeFile (
230
+ path . join ( pkgPath , d . main ) ,
231
+ cnt . join ( '\n' ) ,
232
+ cb
233
+ ) ;
234
+ }
235
+
236
+ function copyLocaleFiles ( cb ) {
237
+ runSeries ( localeFiles . map ( function ( f ) {
238
+ return function ( cb ) {
239
+ fs . copy ( f , path . join ( pkgPath , path . basename ( f ) ) , cb ) ;
240
+ } ;
241
+ } ) , cb ) ;
127
242
}
128
243
244
+ var copyLicense = _copyLicense ( d , pkgPath ) ;
245
+
246
+ var publishToNPM = _publishToNPM ( d , pkgPath ) ;
247
+
129
248
runSeries ( [
130
249
initDirectory ,
250
+ listLocalFiles ,
131
251
writePackageJSON ,
132
252
writeREADME ,
133
- copyMain ,
253
+ writeMain ,
254
+ copyLocaleFiles ,
134
255
copyLicense ,
135
256
publishToNPM
136
257
] , function ( err ) {
137
258
if ( err ) throw err ;
138
259
} ) ;
139
- } ) ;
260
+ }
261
+
262
+ function _initDirectory ( d , pkgPath ) {
263
+ return function ( cb ) {
264
+ if ( common . doesDirExist ( pkgPath ) ) {
265
+ cb ( ) ;
266
+ } else {
267
+ fs . mkdir ( pkgPath , cb ) ;
268
+ }
269
+ } ;
270
+ }
271
+
272
+ function _copyLicense ( d , pkgPath ) {
273
+ return function ( cb ) {
274
+ fs . copy (
275
+ path . join ( constants . pathToRoot , 'LICENSE' ) ,
276
+ path . join ( pkgPath , 'LICENSE' ) ,
277
+ cb
278
+ ) ;
279
+ } ;
280
+ }
281
+
282
+ function _publishToNPM ( d , pkgPath ) {
283
+ return function ( cb ) {
284
+ if ( process . env . DRYRUN ) {
285
+ console . log ( 'dry run, did not publish ' + d . name ) ;
286
+ cb ( ) ;
287
+ return ;
288
+ }
289
+ exec ( 'npm publish' , { cwd : pkgPath } , cb ) . stdout . pipe ( process . stdout ) ;
290
+ } ;
291
+ }
0 commit comments