Skip to content

Commit ad937d2

Browse files
authored
Merge pull request #3223 from plotly/publish-locales
Publish `plotly.js-locales` to npm
2 parents 427fd14 + 0d2c71e commit ad937d2

File tree

1 file changed

+183
-31
lines changed

1 file changed

+183
-31
lines changed

tasks/sync_packages.js

+183-31
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@ var path = require('path');
22
var fs = require('fs-extra');
33
var exec = require('child_process').exec;
44
var runSeries = require('run-series');
5+
var glob = require('glob');
56

67
var common = require('./util/common');
78
var constants = require('./util/constants');
89
var 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

Comments
 (0)