Skip to content

Commit a1238e7

Browse files
committed
touch up sync_packages.js
- factor up findModuleList from stats.js, to list which trace modules are included - fixup cb when build/* exist - add DRYRUN env var, to skip `npm publish`
1 parent b3480f7 commit a1238e7

File tree

3 files changed

+57
-59
lines changed

3 files changed

+57
-59
lines changed

tasks/stats.js

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var path = require('path');
22
var fs = require('fs');
33

4-
var falafel = require('falafel');
54
var gzipSize = require('gzip-size');
65
var prettySize = require('prettysize');
76

@@ -11,7 +10,6 @@ var pkg = require('../package.json');
1110

1211
var pathDistREADME = path.join(constants.pathToDist, 'README.md');
1312
var cdnRoot = 'https://cdn.plot.ly/plotly-';
14-
var coreModules = ['scatter'];
1513

1614
var ENC = 'utf-8';
1715
var JS = '.js';
@@ -156,12 +154,12 @@ function makeBundleHeaderInfo(pathObj) {
156154
function makeBundleInfo(pathObj) {
157155
var name = pathObj.name;
158156
var sizes = findSizes(pathObj);
159-
var moduleList = coreModules.concat(scrapeContent(pathObj));
157+
var moduleList = common.findModuleList(pathObj.index);
160158

161159
return [
162160
'### plotly.js ' + name,
163161
'',
164-
formatBundleInfo(name, moduleList),
162+
'The `' + name + '` partial bundle contains trace modules ' + common.formatEnumeration(moduleList) + '.',
165163
'',
166164
'| Way to import | Location |',
167165
'|---------------|----------|',
@@ -197,50 +195,3 @@ function findSizes(pathObj) {
197195

198196
return sizes;
199197
}
200-
201-
function scrapeContent(pathObj) {
202-
var code = fs.readFileSync(pathObj.index, ENC);
203-
var moduleList = [];
204-
205-
falafel(code, function(node) {
206-
if(isModuleNode(node)) {
207-
var moduleName = node.value.replace('./', '');
208-
moduleList.push(moduleName);
209-
}
210-
});
211-
212-
return moduleList;
213-
}
214-
215-
function isModuleNode(node) {
216-
return (
217-
node.type === 'Literal' &&
218-
node.parent &&
219-
node.parent.type === 'CallExpression' &&
220-
node.parent.callee &&
221-
node.parent.callee.type === 'Identifier' &&
222-
node.parent.callee.name === 'require' &&
223-
node.parent.parent &&
224-
node.parent.parent.type === 'ArrayExpression'
225-
);
226-
}
227-
228-
function formatBundleInfo(bundleName, moduleList) {
229-
var enumeration = moduleList.map(function(moduleName, i) {
230-
var len = moduleList.length,
231-
ending;
232-
233-
if(i === len - 2) ending = ' and';
234-
else if(i < len - 1) ending = ',';
235-
else ending = '';
236-
237-
return '`' + moduleName + '`' + ending;
238-
});
239-
240-
return [
241-
'The', '`' + bundleName + '`',
242-
'partial bundle contains the',
243-
enumeration.join(' '),
244-
'trace modules.'
245-
].join(' ');
246-
}

tasks/sync_packages.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,27 @@ var packagesSpecs = constants.partialBundlePaths
1111
.map(function(d) {
1212
return {
1313
name: 'plotly.js-' + d.name + '-dist',
14+
index: d.index,
1415
main: 'plotly-' + d.name + '.js',
1516
dist: d.dist,
16-
desc: 'Ready-to-use plotly.js ' + d.name + ' distributed bundle',
17+
desc: 'Ready-to-use plotly.js ' + d.name + ' distributed bundle.',
1718
};
1819
})
1920
.concat([{
2021
name: 'plotly.js-dist',
22+
index: path.join(constants.pathToLib, 'index.js'),
2123
main: 'plotly.js',
2224
dist: constants.pathToPlotlyDist,
23-
desc: 'Ready-to-use plotly.js distributed bundle',
25+
desc: 'Ready-to-use plotly.js distributed bundle.',
2426
}]);
2527

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

2931
function initDirectory(cb) {
30-
if(!common.doesDirExist(pkgPath)) {
32+
if(common.doesDirExist(pkgPath)) {
33+
cb();
34+
} else {
3135
fs.mkdir(pkgPath, cb);
3236
}
3337
}
@@ -53,11 +57,15 @@ packagesSpecs.forEach(function(d) {
5357
}
5458

5559
function writeREADME(cb) {
60+
var moduleList = common.findModuleList(d.index);
61+
5662
var cnt = [
5763
'# ' + d.name,
5864
'',
5965
d.desc,
6066
'',
67+
'Contains trace modules ' + common.formatEnumeration(moduleList) + '.',
68+
'',
6169
'For more info on plotly.js, go to https://github.com/plotly/plotly.js',
6270
'',
6371
'## Copyright and license',
@@ -82,14 +90,14 @@ packagesSpecs.forEach(function(d) {
8290
}
8391

8492
function publishToNPM(cb) {
93+
if(process.env.DRYRUN) {
94+
console.log('dry run, did not publish ' + d.name);
95+
cb();
96+
return;
97+
}
8598
exec('npm publish', {cwd: pkgPath}, cb).stdout.pipe(process.stdout);
8699
}
87100

88-
// TODO
89-
// - add minified file?
90-
// - what to do with dist/topojson/
91-
// - what to do with mathjax/
92-
93101
runSeries([
94102
initDirectory,
95103
writePackageJSON,

tasks/util/common.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var fs = require('fs');
22
var exec = require('child_process').exec;
3+
var falafel = require('falafel');
34

45
exports.execCmd = function(cmd, cb, errorCb) {
56
cb = cb ? cb : function() {};
@@ -66,3 +67,41 @@ exports.touch = function(filePath) {
6667
exports.throwOnError = function(err) {
6768
if(err) throw err;
6869
};
70+
71+
exports.findModuleList = function(pathToIndex) {
72+
var code = fs.readFileSync(pathToIndex, 'utf-8');
73+
// In v1.x, all partial bundles include the 'scatter' module
74+
var moduleList = ['scatter'];
75+
76+
falafel(code, function(node) {
77+
if(
78+
node.type === 'Literal' &&
79+
node.parent &&
80+
node.parent.type === 'CallExpression' &&
81+
node.parent.callee &&
82+
node.parent.callee.type === 'Identifier' &&
83+
node.parent.callee.name === 'require' &&
84+
node.parent.parent &&
85+
node.parent.parent.type === 'ArrayExpression'
86+
) {
87+
var moduleName = node.value.replace('./', '');
88+
moduleList.push(moduleName);
89+
}
90+
});
91+
92+
return moduleList;
93+
};
94+
95+
exports.formatEnumeration = function(list) {
96+
var len = list.length;
97+
98+
return list.map(function(l, i) {
99+
var ending;
100+
101+
if(i === len - 2) ending = ' and';
102+
else if(i < len - 1) ending = ',';
103+
else ending = '';
104+
105+
return '`' + l + '`' + ending;
106+
}).join(' ');
107+
};

0 commit comments

Comments
 (0)