@@ -276,6 +276,23 @@ function cronSchedule( pkg ) {
276276 return mins + ' ' + hrs + ' * * ' + dow ;
277277}
278278
279+ /**
280+ * Gets the latest version of a package on npm.
281+ *
282+ * @private
283+ * @param {string } pkg - package name
284+ * @returns {string } latest version published to npm
285+ */
286+ function npmVersion ( pkg ) {
287+ var command = 'npm view ' + pkg + ' version' ;
288+ try {
289+ return trim ( shell ( command ) . toString ( ) ) ;
290+ } catch ( error ) {
291+ debug ( 'Encountered an error when attempting to get the latest version of package `%s`: %s' , pkg , error . message ) ;
292+ return null ;
293+ }
294+ }
295+
279296/**
280297* Cleans up the `gh-pages` cache directory.
281298*
@@ -789,9 +806,11 @@ function publish( pkg, clbk ) {
789806 ! contains ( dep , '_tools' ) &&
790807 ( ! contains ( dep , 'plot' ) || dep === '@stdlib/plot' )
791808 ) {
792- pkgJSON . dependencies [ dep ] = ( isTopLevelNS ) ?
793- 'github:stdlib-js/' + replace ( dep , '@stdlib/' , '' ) + '#main' :
794- '^0.0.x' ;
809+ if ( isTopLevelNS ) {
810+ pkgJSON . dependencies [ dep ] = 'github:stdlib-js/' + replace ( dep , '@stdlib/' , '' ) + '#main' ;
811+ } else {
812+ pkgJSON . dependencies [ dep ] = '^' + npmVersion ( dep ) ;
813+ }
795814 }
796815 } else {
797816 pkgJSON . dependencies [ dep ] = mainJSON . dependencies [ dep ] ;
@@ -809,9 +828,11 @@ function publish( pkg, clbk ) {
809828 ! contains ( dep , '_tools' ) &&
810829 ( ! contains ( dep , 'plot' ) || dep === '@stdlib/plot' )
811830 ) {
812- pkgJSON . devDependencies [ dep ] = ( isTopLevelNS ) ?
813- 'github:stdlib-js/' + replace ( dep , '@stdlib/' , '' ) :
814- '^0.0.x' ;
831+ if ( isTopLevelNS ) {
832+ pkgJSON . devDependencies [ dep ] = 'github:stdlib-js/' + replace ( dep , '@stdlib/' , '' ) + '#main' ;
833+ } else {
834+ pkgJSON . devDependencies [ dep ] = '^' + npmVersion ( dep ) ;
835+ }
815836 }
816837 } else {
817838 mainVersion = mainJSON . devDependencies [ dep ] ;
@@ -901,7 +922,11 @@ function publish( pkg, clbk ) {
901922 }
902923 if ( repoExists ) {
903924 console . log ( 'Publishing ' + dist + ' to GitHub...' ) ;
904- ghpages . publish ( dist , ghpagesOpts , invokeCallback ) ;
925+ if ( flags . patch ) {
926+ ghpages . publish ( dist , ghpagesOpts , publishToNPM ) ;
927+ } else {
928+ ghpages . publish ( dist , ghpagesOpts , invokeCallback ) ;
929+ }
905930 if ( flags [ 'overwrite-topics' ] ) {
906931 topics ( 'stdlib-js/' + distPkg , createTopics ( pkgJSON . keywords ) ) ;
907932 }
@@ -922,6 +947,29 @@ function publish( pkg, clbk ) {
922947 } , onRepoCreation ) ;
923948 }
924949
950+ /**
951+ * Publishes the package to npm.
952+ *
953+ * @private
954+ */
955+ function publishToNPM ( ) {
956+ console . log ( 'Publishing ' + dist + ' to npm...' ) ;
957+
958+ // Replace GitHub links to individual packages with npm links:
959+ shell ( 'find ' + dist + ' -type f -name \'*.md\' -print0 | xargs -0 sed -Ei \'/tree\\/main/b; s/@stdlib\\/([^:]*)\\]: https:\\/\\/github.com\\/stdlib-js/@stdlib\\/\\1\\]: https:\\/\\/www.npmjs.com\\/package\\/@stdlib/g' ) ;
960+
961+ // Replace list with links to other branches from installation section:
962+ shell ( 'find ' + dist + ' -type f -name \'*.md\' -print0 | xargs -0 perl -0777 -i -pe "s/\\`\\`\\`\n\nAlternatively,[^<]+<\\/section>/\\`\\`\\`\n\n<\\/section>/"' ) ;
963+
964+ // Replace all stdlib GitHub dependencies with the respective npm packages:
965+ shell ( 'find ' + dist + '/package.json -type f -print0 | xargs -0 sed -Ei \'s/"github:stdlib-js[^"]*"/"^0.0.x"/g\'' ) ;
966+
967+ shell ( 'npm publish --access public' , {
968+ 'cwd' : dist
969+ } ) ;
970+ invokeCallback ( ) ;
971+ }
972+
925973 /**
926974 * Adds executable permission to `bin/cli` files.
927975 *
0 commit comments