Skip to content

Commit ddfd4e8

Browse files
committed
Add script to update the contributors field of package.json files
1 parent fcd1b56 commit ddfd4e8

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
/*
5+
* Update package `package.json` files by setting the `contributors` field.
6+
*
7+
* This script is called with the following arguments:
8+
*
9+
* * *$1*: root search directory
10+
*
11+
* If not provided a root search directory, the root search directory is the current working directory.
12+
*
13+
* To enable verbose logging, set the `DEBUG` environment variable.
14+
*
15+
* ``` bash
16+
* $ DEBUG=* update_contributors .
17+
* ```
18+
*/
19+
20+
// MODULES //
21+
22+
var debug = require( 'debug' )( 'update-package-contributors' );
23+
var join = require( 'path' ).join;
24+
var resolve = require( 'path' ).resolve;
25+
var writeFile = require( 'fs' ).writeFileSync;
26+
var parseArgs = require( 'minimist' );
27+
var cwd = require( '@stdlib/utils/cwd' );
28+
var findPkgs = require( './../../pkgs/find' ).sync;
29+
var standardize = require( './../standardize-json' );
30+
31+
32+
// VARIABLES //
33+
34+
var CONTRIBUTORS = [
35+
{
36+
'name': 'The Stdlib Authors',
37+
'url': 'https://github.com/stdlib-js/stdlib/graphs/contributors'
38+
}
39+
];
40+
var opts;
41+
var args;
42+
var dir;
43+
44+
45+
// FUNCTIONS //
46+
47+
/**
48+
* Updates package `package.json` files by setting the `contributors` field.
49+
*
50+
* @private
51+
* @param {string} dir - root search directory
52+
*/
53+
function main( dir ) {
54+
var fpath;
55+
var opts;
56+
var pkgs;
57+
var pkg;
58+
var i;
59+
60+
debug( 'Searching for packages in %s.', dir );
61+
opts = {
62+
'dir': dir,
63+
'pattern': '**/package.json'
64+
};
65+
pkgs = findPkgs( opts );
66+
debug( 'Found %d packages.', pkgs.length );
67+
68+
for ( i = 0; i < pkgs.length; i++ ) {
69+
fpath = join( pkgs[ i ], 'package.json' );
70+
debug( 'Loading package file: %s (%d of %d).', fpath, i+1, pkgs.length );
71+
pkg = require( fpath );
72+
73+
if ( pkg.contributors ) {
74+
debug( 'Current contributors: %s.', JSON.stringify( pkg.contributors ) );
75+
}
76+
debug( 'Updating contributors.' );
77+
pkg.contributors = CONTRIBUTORS;
78+
79+
debug( 'Standardizing package data.' );
80+
pkg = standardize( pkg );
81+
82+
debug( 'Serializing package data.' );
83+
pkg = JSON.stringify( pkg, null, 2 ); // 2-space indentation
84+
85+
debug( 'Writing package data to file.' );
86+
writeFile( fpath, pkg, {
87+
'encoding': 'utf8'
88+
});
89+
}
90+
debug( 'Finished updating all packages.' );
91+
} // end FUNCTION main()
92+
93+
94+
// MAIN //
95+
96+
// Parse command-line arguments:
97+
opts = {};
98+
args = parseArgs( process.argv.slice( 2 ), opts );
99+
100+
if ( args._[ 0 ] ) {
101+
dir = resolve( cwd(), args._[ 0 ] );
102+
} else {
103+
dir = cwd();
104+
}
105+
main( dir );

0 commit comments

Comments
 (0)