diff --git a/tools/publish-dev/index.js b/tools/publish-dev/index.js index 7f8111a..1bf8202 100644 --- a/tools/publish-dev/index.js +++ b/tools/publish-dev/index.js @@ -1,59 +1,13 @@ "use strict"; -const cp = require( "child_process" ); -const fs = require( "fs" ); -const path = require( "path" ); - -// paths - -const packagejson = require.resolve( "pegjs/package.json" ); -const pegjs = path.dirname( packagejson ); -const npmignore = path.join( pegjs, ".npmignore" ); -const npmrc = path.join( pegjs, ".npmrc" ); - -// variabes - -const APP = require( "./package.json" ).name; -const VERSION = require( packagejson ).version; +const publish = require( "./publish" ); const { BUILD_REASON, - GIT_BRANCH, - NPM_TOKEN, - -} = process.env; - -let { - - GIT_COMMIT_SHA, } = process.env; -if ( GIT_COMMIT_SHA === "not-found" ) GIT_COMMIT_SHA = GIT_BRANCH; - -// local helpers - -function die( err ) { - - console.error( err ); - process.exit( 1 ); - -} - -function exec( command, print = true ) { - - const result = cp.execSync( command, { - cwd: pegjs, - stdio: print ? "inherit" : void 0, - } ); - - return print ? void 0 : result.toString( "utf8" ); - -} - -// assertions - if ( BUILD_REASON && BUILD_REASON === "PullRequest" ) { console.log( "Skipping publish, PR's are not published." ); @@ -61,21 +15,4 @@ if ( BUILD_REASON && BUILD_REASON === "PullRequest" ) { } -if ( ! GIT_BRANCH ) die( "`process.env.GIT_BRANCH` is required by " + APP ); -if ( ! NPM_TOKEN ) die( "`process.env.NPM_TOKEN` is required by " + APP ); - -// update version field in `pegjs/package.json` - -const GIT_COMMIT_SHORT_SHA = exec( "git rev-parse --short " + GIT_COMMIT_SHA, false ); -const dev = `${ VERSION }-${ GIT_BRANCH }.${ GIT_COMMIT_SHORT_SHA }`; - -exec( `npm --no-git-tag-version -f version ${ dev }` ); - -// publish pegjs@dev - -fs.writeFileSync( npmrc, `//registry.npmjs.org/:_authToken=${ NPM_TOKEN }` ); -fs.unlinkSync( npmignore ); - -exec( "npm publish --tag=dev" ); - -fs.unlinkSync( npmrc ); +publish( "pegjs" ); diff --git a/tools/publish-dev/publish.js b/tools/publish-dev/publish.js new file mode 100644 index 0000000..e4e2fd8 --- /dev/null +++ b/tools/publish-dev/publish.js @@ -0,0 +1,82 @@ +"use strict"; + +const cp = require( "child_process" ); +const fs = require( "fs" ); +const path = require( "path" ); + +function publish( id ) { + + // paths + + const packagejson = require.resolve( id + "/package.json" ); + const directory = path.dirname( packagejson ); + const npmignore = path.join( directory, ".npmignore" ); + const npmrc = path.join( directory, ".npmrc" ); + + // variabes + + const APP = require( "./package.json" ).name; + const VERSION = require( packagejson ).version; + + const { + + GIT_BRANCH, + NPM_TOKEN, + + } = process.env; + + let { + + GIT_COMMIT_SHA, + + } = process.env; + + if ( GIT_COMMIT_SHA === "not-found" ) GIT_COMMIT_SHA = GIT_BRANCH; + + // local helpers + + function die( err ) { + + console.error( err ); + process.exit( 1 ); + + } + + function exec( command, print = true ) { + + const result = cp.execSync( command, { + cwd: directory, + stdio: print ? "inherit" : void 0, + } ); + + return print ? void 0 : result.toString( "utf8" ); + + } + + // assertions + + if ( ! GIT_BRANCH ) die( "`process.env.GIT_BRANCH` is required by " + APP ); + if ( ! NPM_TOKEN ) die( "`process.env.NPM_TOKEN` is required by " + APP ); + + // update version field in `/package.json` + + const GIT_COMMIT_SHORT_SHA = exec( "git rev-parse --short " + GIT_COMMIT_SHA, false ); + const dev = `${ VERSION }-${ GIT_BRANCH }.${ GIT_COMMIT_SHORT_SHA }`; + + exec( `npm --no-git-tag-version -f version ${ dev }` ); + + // add npm token and remove ignore file (this is a DEV release after all) + + fs.writeFileSync( npmrc, `//registry.npmjs.org/:_authToken=${ NPM_TOKEN }` ); + + if ( fs.existsSync( npmignore ) ) fs.unlinkSync( npmignore ); + + // publish @dev + + exec( "npm publish --tag=dev" ); + + fs.unlinkSync( npmrc ); + +} + +module.exports = publish;