Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish plotly.js-locales to npm #3223

Merged
merged 3 commits into from
Nov 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 183 additions & 31 deletions tasks/sync_packages.js
Original file line number Diff line number Diff line change
@@ -2,12 +2,27 @@ var path = require('path');
var fs = require('fs-extra');
var exec = require('child_process').exec;
var runSeries = require('run-series');
var glob = require('glob');

var common = require('./util/common');
var constants = require('./util/constants');
var pkg = require('../package.json');

var packagesSpecs = constants.partialBundlePaths
var year = (new Date()).getFullYear();

var copyrightAndLicense = [
'## Copyright and license',
'',
'Code and documentation copyright ' + year + ' Plotly, Inc.',
'',
'Code released under the [MIT license](https://github.com/plotly/plotly.js/blob/master/LICENSE).',
'',
'Docs released under the [Creative Commons license](https://github.com/plotly/documentation/blob/source/LICENSE).',
''
].join('\n');

// sync "partial bundle" packages
constants.partialBundlePaths
.map(function(d) {
return {
name: 'plotly.js-' + d.name + '-dist',
@@ -23,18 +38,21 @@ var packagesSpecs = constants.partialBundlePaths
main: 'plotly.js',
dist: constants.pathToPlotlyDist,
desc: 'Ready-to-use plotly.js distributed bundle.',
}]);
}])
.forEach(syncPartialBundlePkg);

// sync "locales" package
syncLocalesPkg({
name: 'plotly.js-locales',
dir: path.join(constants.pathToLib, 'locales'),
main: 'index.js',
desc: 'Ready-to-use plotly.js locales',
});

packagesSpecs.forEach(function(d) {
function syncPartialBundlePkg(d) {
var pkgPath = path.join(constants.pathToBuild, d.name);

function initDirectory(cb) {
if(common.doesDirExist(pkgPath)) {
cb();
} else {
fs.mkdir(pkgPath, cb);
}
}
var initDirectory = _initDirectory(d, pkgPath);

function writePackageJSON(cb) {
var cnt = {
@@ -61,6 +79,7 @@ packagesSpecs.forEach(function(d) {
);
}


function writeREADME(cb) {
var moduleList = common.findModuleList(d.index);

@@ -88,14 +107,7 @@ packagesSpecs.forEach(function(d) {
'var Plotly = require(\'' + d.name + '\');',
'```',
'',
'## Copyright and license',
'',
'Code and documentation copyright 2018 Plotly, Inc.',
'',
'Code released under the [MIT license](https://github.com/plotly/plotly.js/blob/master/LICENSE).',
'',
'Docs released under the [Creative Commons license](https://github.com/plotly/documentation/blob/source/LICENSE).',
''
copyrightAndLicense
];

fs.writeFile(
@@ -109,31 +121,171 @@ packagesSpecs.forEach(function(d) {
fs.copy(d.dist, path.join(pkgPath, d.main), cb);
}

function copyLicense(cb) {
fs.copy(
path.join(constants.pathToRoot, 'LICENSE'),
path.join(pkgPath, 'LICENSE'),
var copyLicense = _copyLicense(d, pkgPath);

var publishToNPM = _publishToNPM(d, pkgPath);

runSeries([
initDirectory,
writePackageJSON,
writeREADME,
copyMain,
copyLicense,
publishToNPM
], function(err) {
if(err) throw err;
});
}

function syncLocalesPkg(d) {
var pkgPath = path.join(constants.pathToBuild, d.name);

var initDirectory = _initDirectory(d, pkgPath);

var localeFiles;
function listLocalFiles(cb) {
var localeGlob = path.join(constants.pathToLib, 'locales', '*.js');
glob(localeGlob, function(err, _localeFiles) {
if(err) cb(null);
localeFiles = _localeFiles;
cb();
});
}

function writePackageJSON(cb) {
var cnt = {
name: d.name,
version: pkg.version,
description: d.desc,
license: pkg.license,
main: d.main,
repository: pkg.repository,
bugs: pkg.bugs,
author: pkg.author,
keywords: pkg.keywords,
files: [
'LICENSE',
'README.md',
d.main
].concat(localeFiles.map(function(f) { return path.basename(f); }))
};

fs.writeFile(
path.join(pkgPath, 'package.json'),
JSON.stringify(cnt, null, 2) + '\n',
cb
);
}

function publishToNPM(cb) {
if(process.env.DRYRUN) {
console.log('dry run, did not publish ' + d.name);
cb();
return;
}
exec('npm publish', {cwd: pkgPath}, cb).stdout.pipe(process.stdout);
function writeREADME(cb) {
var cnt = [
'# ' + d.name,
'',
d.desc,
'',
'For more info on plotly.js, go to https://github.com/plotly/plotly.js',
'',
'## Installation',
'',
'```',
'npm install ' + d.name,
'```',
'## Usage',
'',
'For example to setup the `fr` locale:',
'',
'```js',
'// ES6 module',
'import Plotly from \'plotly.js\';',
'import locale from \'' + d.name + '/fr' + '\';',
'',
'// CommonJS',
'var Plotly = require(\'plotly.js\');',
'var locale = require(\'' + d.name + '/fr\');',
'',
'// then',
'Plotly.register(locale);',
'Plotly.setPlotConfig({locale: \'fr\'})',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call 👍

'```',
'',
copyrightAndLicense
];

fs.writeFile(
path.join(pkgPath, 'README.md'),
cnt.join('\n'),
cb
);
}

function writeMain(cb) {
var cnt = [constants.licenseSrc, ''];
localeFiles.forEach(function(f) {
var n = path.basename(f, '.js');
cnt.push('exports[\'' + n + '\'] = require(\'./' + n + '.js\');');
});
cnt.push('');

fs.writeFile(
path.join(pkgPath, d.main),
cnt.join('\n'),
cb
);
}

function copyLocaleFiles(cb) {
runSeries(localeFiles.map(function(f) {
return function(cb) {
fs.copy(f, path.join(pkgPath, path.basename(f)), cb);
};
}), cb);
}

var copyLicense = _copyLicense(d, pkgPath);

var publishToNPM = _publishToNPM(d, pkgPath);

runSeries([
initDirectory,
listLocalFiles,
writePackageJSON,
writeREADME,
copyMain,
writeMain,
copyLocaleFiles,
copyLicense,
publishToNPM
], function(err) {
if(err) throw err;
});
});
}

function _initDirectory(d, pkgPath) {
return function(cb) {
if(common.doesDirExist(pkgPath)) {
cb();
} else {
fs.mkdir(pkgPath, cb);
}
};
}

function _copyLicense(d, pkgPath) {
return function(cb) {
fs.copy(
path.join(constants.pathToRoot, 'LICENSE'),
path.join(pkgPath, 'LICENSE'),
cb
);
};
}

function _publishToNPM(d, pkgPath) {
return function(cb) {
if(process.env.DRYRUN) {
console.log('dry run, did not publish ' + d.name);
cb();
return;
}
exec('npm publish', {cwd: pkgPath}, cb).stdout.pipe(process.stdout);
};
}