|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | +* @license Apache-2.0 |
| 5 | +* |
| 6 | +* Copyright (c) 2024 The Stdlib Authors. |
| 7 | +* |
| 8 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +* you may not use this file except in compliance with the License. |
| 10 | +* You may obtain a copy of the License at |
| 11 | +* |
| 12 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +* |
| 14 | +* Unless required by applicable law or agreed to in writing, software |
| 15 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +* See the License for the specific language governing permissions and |
| 18 | +* limitations under the License. |
| 19 | +*/ |
| 20 | + |
| 21 | +'use strict'; |
| 22 | + |
| 23 | +/* |
| 24 | +* Update package `package.json` files by setting the `gyppfile` field to `true` for packages which contain a `binding.gyp` and/or `include.gypi` file. |
| 25 | +* |
| 26 | +* * *$1*: root search directory |
| 27 | +* |
| 28 | +* If not provided a root search directory, the root search directory is the current working directory. |
| 29 | +* |
| 30 | +* To enable verbose logging, set the `DEBUG` environment variable. |
| 31 | +* |
| 32 | +* ``` bash |
| 33 | +* $ DEBUG=* update_gypfile . |
| 34 | +* ``` |
| 35 | +*/ |
| 36 | + |
| 37 | +// MODULES // |
| 38 | + |
| 39 | +var join = require( 'path' ).join; |
| 40 | +var resolve = require( 'path' ).resolve; |
| 41 | +var proc = require( 'process' ); |
| 42 | +var logger = require( 'debug' ); |
| 43 | +var parseArgs = require( 'minimist' ); |
| 44 | +var isObject = require( '@stdlib/assert/is-plain-object' ); |
| 45 | +var cwd = require( '@stdlib/process/cwd' ); |
| 46 | +var exists = require( '@stdlib/fs/exists' ).sync; |
| 47 | +var findPkgs = require( '@stdlib/_tools/pkgs/find' ).sync; |
| 48 | +var writeFile = require( '@stdlib/fs/write-file' ).sync; |
| 49 | +var standardize = require( '@stdlib/_tools/package-json/standardize' ); |
| 50 | + |
| 51 | + |
| 52 | +// VARIABLES // |
| 53 | + |
| 54 | +var GYPFILES = [ |
| 55 | + 'binding.gyp', |
| 56 | + 'include.gypi' |
| 57 | +]; |
| 58 | +var debug = logger( 'update-gypfile' ); |
| 59 | +var opts; |
| 60 | +var args; |
| 61 | +var dir; |
| 62 | + |
| 63 | + |
| 64 | +// FUNCTIONS // |
| 65 | + |
| 66 | +/** |
| 67 | +* Updates package `package.json` files by setting the `gypfile` field if a package contains a `binding.gyp` and/or `include.gypi` file. |
| 68 | +* |
| 69 | +* @private |
| 70 | +* @param {string} dir - root search directory |
| 71 | +*/ |
| 72 | +function main( dir ) { |
| 73 | + var fpath; |
| 74 | + var opts; |
| 75 | + var pkgs; |
| 76 | + var pkg; |
| 77 | + var i; |
| 78 | + var j; |
| 79 | + |
| 80 | + debug( 'Searching for packages in %s.', dir ); |
| 81 | + opts = { |
| 82 | + 'dir': dir, |
| 83 | + 'pattern': '**/package.json' |
| 84 | + }; |
| 85 | + pkgs = findPkgs( opts ); |
| 86 | + debug( 'Found %d packages.', pkgs.length ); |
| 87 | + |
| 88 | + for ( i = 0; i < pkgs.length; i++ ) { |
| 89 | + fpath = join( pkgs[ i ], 'package.json' ); |
| 90 | + debug( 'Loading package file: %s (%d of %d).', fpath, i+1, pkgs.length ); |
| 91 | + |
| 92 | + try { |
| 93 | + pkg = require( fpath ); // eslint-disable-line stdlib/no-dynamic-require |
| 94 | + } catch ( err ) { |
| 95 | + debug( 'Encountered an error when loading package file: %s (%d of %d). Error: %s', fpath, i+1, pkgs.length, err.message ); |
| 96 | + continue; |
| 97 | + } |
| 98 | + if ( !isObject( pkg ) ) { |
| 99 | + debug( 'Unable to load package.json file: %s (%d of %d).', fpath, i+1, pkgs.length ); |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + if ( pkg.gypfile ) { |
| 104 | + debug( 'Current gypfile value: %s.', pkg.gypfile ); |
| 105 | + } |
| 106 | + debug( 'Updating gypfile.' ); |
| 107 | + for ( j = 0; j < GYPFILES.length; j++ ) { |
| 108 | + if ( exists( join( pkgs[ i ], GYPFILES[ j ] ) ) ) { |
| 109 | + pkg.gypfile = true; |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + debug( 'Standardizing package data.' ); |
| 115 | + pkg = standardize( pkg ); |
| 116 | + |
| 117 | + debug( 'Serializing package data.' ); |
| 118 | + pkg = JSON.stringify( pkg, null, 2 ); // 2-space indentation |
| 119 | + |
| 120 | + debug( 'Writing package data to file.' ); |
| 121 | + writeFile( fpath, pkg+'\n', { |
| 122 | + 'encoding': 'utf8' |
| 123 | + }); |
| 124 | + } |
| 125 | + debug( 'Finished updating all packages.' ); |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +// MAIN // |
| 130 | + |
| 131 | +// Parse command-line arguments: |
| 132 | +opts = {}; |
| 133 | +args = parseArgs( proc.argv.slice( 2 ), opts ); |
| 134 | + |
| 135 | +if ( args._[ 0 ] ) { |
| 136 | + dir = resolve( cwd(), args._[ 0 ] ); |
| 137 | +} else { |
| 138 | + dir = cwd(); |
| 139 | +} |
| 140 | +main( dir ); |
0 commit comments