Skip to content

Commit ecd9959

Browse files
committed
Add script to update the name field in tools package.json files
1 parent fa33f15 commit ecd9959

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
/*
5+
* Update the package `package.json` name field for tools packages.
6+
*
7+
* To enable verbose logging, set the `DEBUG` environment variable.
8+
*
9+
* ``` bash
10+
* $ DEBUG=* update_tools_package_json_name
11+
* ```
12+
*/
13+
14+
// MODULES //
15+
16+
var debug = require( 'debug' )( 'update-tools-package-name' );
17+
var join = require( 'path' ).join;
18+
var resolve = require( 'path' ).resolve;
19+
var writeFile = require( 'fs' ).writeFileSync;
20+
var hasOwnProp = require( '@stdlib/utils/has-own-property' );
21+
var SEP = require( '@stdlib/utils/path-sep' );
22+
var findPkgs = require( './../pkgs/find' ).sync;
23+
24+
25+
// VARIABLES //
26+
27+
var ROOT = resolve( __dirname, '../../tools' );
28+
var PREFIX = '@stdlib/tools/';
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Updates package `package.json` files by setting the `name` field.
35+
*
36+
* ## Notes
37+
*
38+
* * The implementation assumes that a package name can be inferred from the directory structure.
39+
*
40+
*
41+
* @private
42+
*/
43+
function main() {
44+
var fpath;
45+
var parts;
46+
var name;
47+
var opts;
48+
var pkgs;
49+
var pkg;
50+
var i;
51+
52+
debug( 'Searching for packages in %s.', ROOT );
53+
opts = {
54+
'dir': ROOT,
55+
'pattern': '**/package.json'
56+
};
57+
pkgs = findPkgs( opts );
58+
debug( 'Found %d packages.', pkgs.length );
59+
60+
for ( i = 0; i < pkgs.length; i++ ) {
61+
fpath = join( pkgs[ i ], 'package.json' );
62+
debug( 'Loading package file: %s (%d of %d).', fpath, i+1, pkgs.length );
63+
pkg = require( fpath );
64+
if ( !hasOwnProp( pkg, 'name' ) ) {
65+
debug( 'Found a package missing a `name` field: %s. Skipping.', fpath );
66+
continue;
67+
}
68+
name = fpath.substring( ROOT.length );
69+
debug( 'Package path: %s.', name );
70+
71+
// Remove initial slash and `/package.json`:
72+
name = name.substring( 1, name.length-13 );
73+
74+
// Split the path based on the platform:
75+
parts = name.split( SEP );
76+
77+
// Recombine into a package name:
78+
name = PREFIX + parts.join( '/' );
79+
debug( 'Package name: %s.', name );
80+
pkg.name = name;
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+
main();

0 commit comments

Comments
 (0)