Skip to content

Commit 6df50a1

Browse files
committed
publish plotly.js-locales pkg to npm
... during post-publish step.
1 parent 488dbca commit 6df50a1

File tree

1 file changed

+169
-23
lines changed

1 file changed

+169
-23
lines changed

tasks/sync_packages.js

+169-23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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');
@@ -19,7 +20,9 @@ var copyrightAndLicense = [
1920
'Docs released under the [Creative Commons license](https://github.com/plotly/documentation/blob/source/LICENSE).',
2021
''
2122
].join('\n');
22-
var packagesSpecs = constants.partialBundlePaths
23+
24+
// sync "partial bundle" packages
25+
constants.partialBundlePaths
2326
.map(function(d) {
2427
return {
2528
name: 'plotly.js-' + d.name + '-dist',
@@ -35,18 +38,21 @@ var packagesSpecs = constants.partialBundlePaths
3538
main: 'plotly.js',
3639
dist: constants.pathToPlotlyDist,
3740
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+
});
3951

40-
packagesSpecs.forEach(function(d) {
52+
function syncPartialBundlePkg(d) {
4153
var pkgPath = path.join(constants.pathToBuild, d.name);
4254

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);
5056

5157
function writePackageJSON(cb) {
5258
var cnt = {
@@ -73,6 +79,7 @@ packagesSpecs.forEach(function(d) {
7379
);
7480
}
7581

82+
7683
function writeREADME(cb) {
7784
var moduleList = common.findModuleList(d.index);
7885

@@ -114,31 +121,170 @@ packagesSpecs.forEach(function(d) {
114121
fs.copy(d.dist, path.join(pkgPath, d.main), cb);
115122
}
116123

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',
121176
cb
122177
);
123178
}
124179

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);
132241
}
133242

243+
var copyLicense = _copyLicense(d, pkgPath);
244+
245+
var publishToNPM = _publishToNPM(d, pkgPath);
246+
134247
runSeries([
135248
initDirectory,
249+
listLocalFiles,
136250
writePackageJSON,
137251
writeREADME,
138-
copyMain,
252+
writeMain,
253+
copyLocaleFiles,
139254
copyLicense,
140255
publishToNPM
141256
], function(err) {
142257
if(err) throw err;
143258
});
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

Comments
 (0)