Skip to content

Commit

Permalink
build: add script to update gypfile fields
Browse files Browse the repository at this point in the history
style: add empty line
  • Loading branch information
Planeshifter committed Mar 5, 2024
1 parent e1b84fa commit 281a51c
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions lib/node_modules/@stdlib/_tools/package-json/scripts/update_gypfile
Original file line number Diff line number Diff line change
@@ -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.

This comment has been minimized.

Copy link
@kgryte

kgryte Mar 5, 2024

Member

s/gyppfile/gypfile/

*
* * *$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 );

0 comments on commit 281a51c

Please sign in to comment.