@@ -2,12 +2,27 @@ var path = require('path');
22var fs = require ( 'fs-extra' ) ;
33var exec = require ( 'child_process' ) . exec ;
44var runSeries = require ( 'run-series' ) ;
5+ var glob = require ( 'glob' ) ;
56
67var common = require ( './util/common' ) ;
78var constants = require ( './util/constants' ) ;
89var pkg = require ( '../package.json' ) ;
910
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
1126 . map ( function ( d ) {
1227 return {
1328 name : 'plotly.js-' + d . name + '-dist' ,
@@ -23,18 +38,21 @@ var packagesSpecs = constants.partialBundlePaths
2338 main : 'plotly.js' ,
2439 dist : constants . pathToPlotlyDist ,
2540 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+ } ) ;
2751
28- packagesSpecs . forEach ( function ( d ) {
52+ function syncPartialBundlePkg ( d ) {
2953 var pkgPath = path . join ( constants . pathToBuild , d . name ) ;
3054
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 ) ;
3856
3957 function writePackageJSON ( cb ) {
4058 var cnt = {
@@ -61,6 +79,7 @@ packagesSpecs.forEach(function(d) {
6179 ) ;
6280 }
6381
82+
6483 function writeREADME ( cb ) {
6584 var moduleList = common . findModuleList ( d . index ) ;
6685
@@ -88,14 +107,7 @@ packagesSpecs.forEach(function(d) {
88107 'var Plotly = require(\'' + d . name + '\');' ,
89108 '```' ,
90109 '' ,
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
99111 ] ;
100112
101113 fs . writeFile (
@@ -109,31 +121,171 @@ packagesSpecs.forEach(function(d) {
109121 fs . copy ( d . dist , path . join ( pkgPath , d . main ) , cb ) ;
110122 }
111123
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' ,
116176 cb
117177 ) ;
118178 }
119179
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 ) ;
127242 }
128243
244+ var copyLicense = _copyLicense ( d , pkgPath ) ;
245+
246+ var publishToNPM = _publishToNPM ( d , pkgPath ) ;
247+
129248 runSeries ( [
130249 initDirectory ,
250+ listLocalFiles ,
131251 writePackageJSON ,
132252 writeREADME ,
133- copyMain ,
253+ writeMain ,
254+ copyLocaleFiles ,
134255 copyLicense ,
135256 publishToNPM
136257 ] , function ( err ) {
137258 if ( err ) throw err ;
138259 } ) ;
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