-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate_readmes.js
More file actions
67 lines (55 loc) · 2 KB
/
update_readmes.js
File metadata and controls
67 lines (55 loc) · 2 KB
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
// this script updates the readme attribute of the package.json, from README.md
var fs = require('fs');
var f = fs.readFileSync('package.json',{encoding:'utf8'});
var o = JSON.parse(f);
var package_json_README = o.readme;
var README = fs.readFileSync('README.md',{encoding:'utf8'});
/// expecting a CALLBACK(), and, space-separated string like an actual commandline
function system( callback, commandline )
{
if ( arguments.length < 2 )
return;
var args = '';
for ( var i = 1; i < arguments.length; i++ ) {
if ( arguments[i] instanceof Array )
args += ' ' + arguments[i].join(' ');
else
args += ' ' + arguments[i];
}
args = args.trim().split(/\s+/);
var res = [];
var spawn = require('child_process').spawn;
var child = spawn( args[0], args.slice(1), { env: process.env });
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', function (data) {
res.push( data );
});
child.stderr.on('data', function (data) {
process.stdout.write( 'stderr: ' + data );
});
child.on('close', function (code) {
//return callback( JSON.stringify(res) );
return callback(res.join(' '));
});
}
// write readme and diff against it, then remove it
var pkg_readme = 'tmp_' + (Math.random()+'').slice(-4) + '.txt';
var finish_callback = function(s) {
process.stdout.write(s);
fs.unlinkSync( pkg_readme );
if ( s.trim().length > 0 ) {
// write newer package.json using README.md
process.stdout.write( "writing: \"backup.package.json\"\n" );
fs.writeFileSync('backup.package.json',f,{encoding:'utf8'});
o.readme = README;
process.stdout.write( "overwriting: \"package.json\"\n" );
fs.writeFileSync( "package.json", JSON.stringify(o,null,' '), {encoding:'utf8'} );
} else {
process.stdout.write( "no change\n" );
}
};
// write file to diff
fs.writeFileSync( pkg_readme, package_json_README, {encoding:'utf8'} );
// do diff and then finish
system( finish_callback, "diff README.md " + pkg_readme );