-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathsync_packages.js
311 lines (277 loc) · 8.1 KB
/
sync_packages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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 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',
index: d.index,
main: 'plotly-' + d.name + '.js',
dist: d.dist,
desc: 'Ready-to-use plotly.js ' + d.name + ' distributed bundle.',
};
})
.concat([{
name: 'plotly.js-dist',
index: path.join(constants.pathToLib, 'index.js'),
main: 'plotly.js',
dist: constants.pathToPlotlyDist,
desc: 'Ready-to-use plotly.js distributed bundle.',
}])
.forEach(syncPartialBundlePkg);
// sync "minified partial bundle" packages
constants.partialBundlePaths
.map(function(d) {
return {
name: 'plotly.js-' + d.name + '-dist-min',
index: d.index,
main: 'plotly-' + d.name + '.min.js',
dist: d.distMin,
desc: 'Ready-to-use minified plotly.js ' + d.name + ' distributed bundle.',
};
})
.concat([{
name: 'plotly.js-dist-min',
index: path.join(constants.pathToLib, 'index.js'),
main: 'plotly.min.js',
dist: constants.pathToPlotlyDistMin,
desc: 'Ready-to-use minified 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',
});
function syncPartialBundlePkg(d) {
var pkgPath = path.join(constants.pathToBuild, d.name);
var initDirectory = _initDirectory(d, pkgPath);
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
]
};
fs.writeFile(
path.join(pkgPath, 'package.json'),
JSON.stringify(cnt, null, 2) + '\n',
cb
);
}
function writeREADME(cb) {
var moduleList = common.findModuleList(d.index);
var cnt = [
'# ' + d.name,
'',
d.desc,
'',
'Contains trace modules ' + common.formatEnumeration(moduleList) + '.',
'',
'For more info on plotly.js, go to https://github.com/plotly/plotly.js',
'',
'## Installation',
'',
'```',
'npm install ' + d.name,
'```',
'## Usage',
'',
'```js',
'// ES6 module',
'import Plotly from \'' + d.name + '\';',
'',
'// CommonJS',
'var Plotly = require(\'' + d.name + '\');',
'```',
'',
copyrightAndLicense
];
fs.writeFile(
path.join(pkgPath, 'README.md'),
cnt.join('\n'),
cb
);
}
function copyMain(cb) {
fs.copy(d.dist, path.join(pkgPath, d.main), cb);
}
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 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\'})',
'```',
'',
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,
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);
};
}