Skip to content

Commit d409652

Browse files
committed
Add npm publishing and dep versioning to script
1 parent dadfde6 commit d409652

3 files changed

Lines changed: 63 additions & 7 deletions

File tree

.github/workflows/standalone_publish.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ jobs:
7979
- name: 'Publish packages'
8080
env:
8181
GITHUB_TOKEN: ${{ secrets.REPO_GITHUB_TOKEN }}
82+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
8283
run: |
84+
# Save NPM_TOKEN to user's .npmrc:
85+
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
86+
8387
node lib/node_modules/@stdlib/_tools/scripts/publish_packages.js --patch --skip-toplevel
8488
8589
# Archive list of to be published packages:

.github/workflows/standalone_publish_custom.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,9 @@ jobs:
104104
env:
105105
GITHUB_TOKEN: ${{ secrets.REPO_GITHUB_TOKEN }}
106106
DEBUG: ${{ env.ACTIONS_STEP_DEBUG && 'scripts:publish-packages' || '' }}
107+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
107108
run: |
109+
# Save NPM_TOKEN to user's .npmrc:
110+
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
111+
108112
node lib/node_modules/@stdlib/_tools/scripts/publish_packages.js ${{ github.event.inputs.packages }} ${{ github.event.inputs.patch == 'true' && '--patch ' || '' }}${{ github.event.inputs.dry-run == 'true' && '--skip-upload ' || '' }}${{ github.event.inputs.skip == 'skip-individual' && '--skip-individual ' || '' }}${{ github.event.inputs.skip == 'skip-toplevel' && '--skip-toplevel ' || '' }}${{ github.event.inputs.only-unpublished == 'true' && '--only-unpublished ' || '' }}${{ github.event.inputs.pattern && format('--pattern {0}', github.event.inputs.pattern ) || '' }}

lib/node_modules/@stdlib/_tools/scripts/publish_packages.js

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)