From 281a51ce5eda276bf266d2fcedc1730776d7383d Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Tue, 5 Mar 2024 15:13:49 -0500 Subject: [PATCH] build: add script to update gypfile fields style: add empty line --- .../package-json/scripts/update_gypfile | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100755 lib/node_modules/@stdlib/_tools/package-json/scripts/update_gypfile diff --git a/lib/node_modules/@stdlib/_tools/package-json/scripts/update_gypfile b/lib/node_modules/@stdlib/_tools/package-json/scripts/update_gypfile new file mode 100755 index 000000000000..4cc89104e734 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/package-json/scripts/update_gypfile @@ -0,0 +1,140 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/* +* Update package `package.json` files by setting the `gyppfile` field to `true` for packages which contain a `binding.gyp` and/or `include.gypi` file. +* +* * *$1*: root search directory +* +* If not provided a root search directory, the root search directory is the current working directory. +* +* To enable verbose logging, set the `DEBUG` environment variable. +* +* ``` bash +* $ DEBUG=* update_gypfile . +* ``` +*/ + +// MODULES // + +var join = require( 'path' ).join; +var resolve = require( 'path' ).resolve; +var proc = require( 'process' ); +var logger = require( 'debug' ); +var parseArgs = require( 'minimist' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var cwd = require( '@stdlib/process/cwd' ); +var exists = require( '@stdlib/fs/exists' ).sync; +var findPkgs = require( '@stdlib/_tools/pkgs/find' ).sync; +var writeFile = require( '@stdlib/fs/write-file' ).sync; +var standardize = require( '@stdlib/_tools/package-json/standardize' ); + + +// VARIABLES // + +var GYPFILES = [ + 'binding.gyp', + 'include.gypi' +]; +var debug = logger( 'update-gypfile' ); +var opts; +var args; +var dir; + + +// FUNCTIONS // + +/** +* Updates package `package.json` files by setting the `gypfile` field if a package contains a `binding.gyp` and/or `include.gypi` file. +* +* @private +* @param {string} dir - root search directory +*/ +function main( dir ) { + var fpath; + var opts; + var pkgs; + var pkg; + var i; + var j; + + debug( 'Searching for packages in %s.', dir ); + opts = { + 'dir': dir, + 'pattern': '**/package.json' + }; + pkgs = findPkgs( opts ); + debug( 'Found %d packages.', pkgs.length ); + + for ( i = 0; i < pkgs.length; i++ ) { + fpath = join( pkgs[ i ], 'package.json' ); + debug( 'Loading package file: %s (%d of %d).', fpath, i+1, pkgs.length ); + + try { + pkg = require( fpath ); // eslint-disable-line stdlib/no-dynamic-require + } catch ( err ) { + debug( 'Encountered an error when loading package file: %s (%d of %d). Error: %s', fpath, i+1, pkgs.length, err.message ); + continue; + } + if ( !isObject( pkg ) ) { + debug( 'Unable to load package.json file: %s (%d of %d).', fpath, i+1, pkgs.length ); + continue; + } + + if ( pkg.gypfile ) { + debug( 'Current gypfile value: %s.', pkg.gypfile ); + } + debug( 'Updating gypfile.' ); + for ( j = 0; j < GYPFILES.length; j++ ) { + if ( exists( join( pkgs[ i ], GYPFILES[ j ] ) ) ) { + pkg.gypfile = true; + break; + } + } + + debug( 'Standardizing package data.' ); + pkg = standardize( pkg ); + + debug( 'Serializing package data.' ); + pkg = JSON.stringify( pkg, null, 2 ); // 2-space indentation + + debug( 'Writing package data to file.' ); + writeFile( fpath, pkg+'\n', { + 'encoding': 'utf8' + }); + } + debug( 'Finished updating all packages.' ); +} + + +// MAIN // + +// Parse command-line arguments: +opts = {}; +args = parseArgs( proc.argv.slice( 2 ), opts ); + +if ( args._[ 0 ] ) { + dir = resolve( cwd(), args._[ 0 ] ); +} else { + dir = cwd(); +} +main( dir );