@@ -2,6 +2,7 @@ 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' ) ;
@@ -19,7 +20,9 @@ var copyrightAndLicense = [
19
20
'Docs released under the [Creative Commons license](https://github.com/plotly/documentation/blob/source/LICENSE).' ,
20
21
''
21
22
] . join ( '\n' ) ;
22
- var packagesSpecs = constants . partialBundlePaths
23
+
24
+ // sync "partial bundle" packages
25
+ constants . partialBundlePaths
23
26
. map ( function ( d ) {
24
27
return {
25
28
name : 'plotly.js-' + d . name + '-dist' ,
@@ -35,18 +38,21 @@ var packagesSpecs = constants.partialBundlePaths
35
38
main : 'plotly.js' ,
36
39
dist : constants . pathToPlotlyDist ,
37
40
desc : 'Ready-to-use plotly.js distributed bundle.' ,
38
- } ] ) ;
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
+ } ) ;
39
51
40
- packagesSpecs . forEach ( function ( d ) {
52
+ function syncPartialBundlePkg ( d ) {
41
53
var pkgPath = path . join ( constants . pathToBuild , d . name ) ;
42
54
43
- function initDirectory ( cb ) {
44
- if ( common . doesDirExist ( pkgPath ) ) {
45
- cb ( ) ;
46
- } else {
47
- fs . mkdir ( pkgPath , cb ) ;
48
- }
49
- }
55
+ var initDirectory = _initDirectory ( d , pkgPath ) ;
50
56
51
57
function writePackageJSON ( cb ) {
52
58
var cnt = {
@@ -73,6 +79,7 @@ packagesSpecs.forEach(function(d) {
73
79
) ;
74
80
}
75
81
82
+
76
83
function writeREADME ( cb ) {
77
84
var moduleList = common . findModuleList ( d . index ) ;
78
85
@@ -114,31 +121,170 @@ packagesSpecs.forEach(function(d) {
114
121
fs . copy ( d . dist , path . join ( pkgPath , d . main ) , cb ) ;
115
122
}
116
123
117
- function copyLicense ( cb ) {
118
- fs . copy (
119
- path . join ( constants . pathToRoot , 'LICENSE' ) ,
120
- 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' ,
121
176
cb
122
177
) ;
123
178
}
124
179
125
- function publishToNPM ( cb ) {
126
- if ( process . env . DRYRUN ) {
127
- console . log ( 'dry run, did not publish ' + d . name ) ;
128
- cb ( ) ;
129
- return ;
130
- }
131
- 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
+ '```' ,
209
+ '' ,
210
+ copyrightAndLicense
211
+ ] ;
212
+
213
+ fs . writeFile (
214
+ path . join ( pkgPath , 'README.md' ) ,
215
+ cnt . join ( '\n' ) ,
216
+ cb
217
+ ) ;
218
+ }
219
+
220
+ function writeMain ( cb ) {
221
+ var cnt = [ constants . licenseSrc , '' ] ;
222
+ localeFiles . forEach ( function ( f ) {
223
+ var n = path . basename ( f , '.js' ) ;
224
+ cnt . push ( 'exports[\'' + n + '\'] = require(\'./' + n + '.js\');' ) ;
225
+ } ) ;
226
+ cnt . push ( '' ) ;
227
+
228
+ fs . writeFile (
229
+ path . join ( pkgPath , d . main ) ,
230
+ cnt . join ( '\n' ) ,
231
+ cb
232
+ ) ;
233
+ }
234
+
235
+ function copyLocaleFiles ( cb ) {
236
+ runSeries ( localeFiles . map ( function ( f ) {
237
+ return function ( cb ) {
238
+ fs . copy ( f , path . join ( pkgPath , path . basename ( f ) ) , cb ) ;
239
+ } ;
240
+ } ) , cb ) ;
132
241
}
133
242
243
+ var copyLicense = _copyLicense ( d , pkgPath ) ;
244
+
245
+ var publishToNPM = _publishToNPM ( d , pkgPath ) ;
246
+
134
247
runSeries ( [
135
248
initDirectory ,
249
+ listLocalFiles ,
136
250
writePackageJSON ,
137
251
writeREADME ,
138
- copyMain ,
252
+ writeMain ,
253
+ copyLocaleFiles ,
139
254
copyLicense ,
140
255
publishToNPM
141
256
] , function ( err ) {
142
257
if ( err ) throw err ;
143
258
} ) ;
144
- } ) ;
259
+ }
260
+
261
+ function _initDirectory ( d , pkgPath ) {
262
+ return function ( cb ) {
263
+ if ( common . doesDirExist ( pkgPath ) ) {
264
+ cb ( ) ;
265
+ } else {
266
+ fs . mkdir ( pkgPath , cb ) ;
267
+ }
268
+ } ;
269
+ }
270
+
271
+ function _copyLicense ( d , pkgPath ) {
272
+ return function ( cb ) {
273
+ fs . copy (
274
+ path . join ( constants . pathToRoot , 'LICENSE' ) ,
275
+ path . join ( pkgPath , 'LICENSE' ) ,
276
+ cb
277
+ ) ;
278
+ } ;
279
+ }
280
+
281
+ function _publishToNPM ( d , pkgPath ) {
282
+ return function ( cb ) {
283
+ if ( process . env . DRYRUN ) {
284
+ console . log ( 'dry run, did not publish ' + d . name ) ;
285
+ cb ( ) ;
286
+ return ;
287
+ }
288
+ exec ( 'npm publish' , { cwd : pkgPath } , cb ) . stdout . pipe ( process . stdout ) ;
289
+ } ;
290
+ }
0 commit comments