|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | +* @license Apache-2.0 |
| 5 | +* |
| 6 | +* Copyright (c) 2020 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 | +/** |
| 22 | +* Script to update the versions of packages containing distributable files. |
| 23 | +* |
| 24 | +* ## Usage |
| 25 | +* |
| 26 | +* ```bash |
| 27 | +* $ NODE_PATH=./path/to/stdlib/lib/node_modules node /path/to/stdlib/dist/scripts/update_versions |
| 28 | +* ``` |
| 29 | +*/ |
| 30 | + |
| 31 | +/* eslint-disable no-console */ |
| 32 | + |
| 33 | +'use strict'; |
| 34 | + |
| 35 | +// MODULES // |
| 36 | + |
| 37 | +var join = require( 'path' ).join; |
| 38 | +var readJSON = require( '@stdlib/fs/read-json' ).sync; |
| 39 | +var writeFile = require( '@stdlib/fs/write-file' ).sync; |
| 40 | +var objectKeys = require( '@stdlib/utils/keys' ); |
| 41 | +var bundleDirs = require( './utils/bundle_dirs.js' ); |
| 42 | +var version = require( './utils/root_pkg_version.js' ); |
| 43 | + |
| 44 | + |
| 45 | +// MAIN // |
| 46 | + |
| 47 | +/** |
| 48 | +* Main execution sequence. |
| 49 | +* |
| 50 | +* @private |
| 51 | +*/ |
| 52 | +function main() { |
| 53 | + var fopts; |
| 54 | + var fpath; |
| 55 | + var hash; |
| 56 | + var list; |
| 57 | + var keys; |
| 58 | + var pkg; |
| 59 | + var err; |
| 60 | + var txt; |
| 61 | + var v; |
| 62 | + var i; |
| 63 | + var k; |
| 64 | + |
| 65 | + // Get the root version: |
| 66 | + v = version(); |
| 67 | + if ( v instanceof Error ) { |
| 68 | + console.error( v.message ); |
| 69 | + return; |
| 70 | + } |
| 71 | + // Get the list of distributable bundle package directories: |
| 72 | + list = bundleDirs(); |
| 73 | + if ( list instanceof Error ) { |
| 74 | + console.error( list.message ); |
| 75 | + return; |
| 76 | + } |
| 77 | + // For each package, update the version to match the root version: |
| 78 | + fopts = { |
| 79 | + 'encoding': 'utf8' |
| 80 | + }; |
| 81 | + hash = {}; |
| 82 | + for ( i = 0; i < list.length; i++ ) { |
| 83 | + fpath = join( list[ i ], 'package.json' ); |
| 84 | + pkg = readJSON( fpath, fopts ); |
| 85 | + if ( pkg instanceof Error ) { |
| 86 | + console.error( pkg.message ); |
| 87 | + return; |
| 88 | + } |
| 89 | + pkg.version = v; |
| 90 | + hash[ fpath ] = pkg; |
| 91 | + } |
| 92 | + // Save the changes: |
| 93 | + keys = objectKeys( hash ); |
| 94 | + for ( i = 0; i < keys.length; i++ ) { |
| 95 | + k = keys[ i ]; |
| 96 | + txt = JSON.stringify( hash[ k ], null, 2 ); |
| 97 | + err = writeFile( k, txt+'\n', fopts ); |
| 98 | + if ( err instanceof Error ) { |
| 99 | + console.error( err.message ); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +main(); |
0 commit comments