diff --git a/scripts/publish_latest_tag_to_npm.ts b/scripts/publish_latest_tag_to_npm.ts index fd20591..c932a0f 100644 --- a/scripts/publish_latest_tag_to_npm.ts +++ b/scripts/publish_latest_tag_to_npm.ts @@ -1,12 +1,9 @@ -import { colors, gt, parse, Spinner } from "./script_deps.ts"; +import { colors, delay, gt, parse } from "./script_deps.ts"; import { run } from "./run.ts"; const PACKAGE_NAME = `@vippsmobilepay/sdk`; -const spinnerTags = new Spinner({ message: `Fetching latest tags...` }); -spinnerTags.start(); -await run(`git fetch --tags`); -spinnerTags.stop(); +await run(`git fetch --tags`, `Fetching latest tags...`, true); // Finding latest tagged commit across branches const latestTaggedCommit = await run(`git rev-list --tags --max-count=1`); @@ -18,12 +15,10 @@ const latestTagName = await run(`git describe --tags ${trimmedCommit}`); const trimmedTag = latestTagName.trim(); console.log(colors.gray(`Latest tag name: ${trimmedTag}`)); -const spinnerNpm = new Spinner({ - message: `Fetching latest published npm version...`, -}); -spinnerNpm.start(); -const latestNpmVersion = await run(`npm show ${PACKAGE_NAME} version`); -spinnerNpm.stop(); +const latestNpmVersion = await run( + `npm view ${PACKAGE_NAME} version`, + `Fetching latest published npm version...`, +); console.log(colors.gray(`Latest npm version: ${latestNpmVersion}`)); try { @@ -54,12 +49,10 @@ if (build?.toLowerCase().trim() !== "y") { Deno.exit(0); } -const spinnerBuild = new Spinner({ message: `Building...` }); -spinnerBuild.start(); const buildOutput = await run( `${Deno.execPath()} run -A scripts/build_npm.ts ${trimmedTag}`, + `Building...`, ); -spinnerBuild.stop(); console.log(buildOutput); const publish = prompt( @@ -72,18 +65,15 @@ if (publish?.toLowerCase().trim() !== "y") { Deno.exit(0); } -const spinnerPublish = new Spinner({ message: `Publishing...` }); -spinnerPublish.start(); // Igonre npm errors since we are checking for them later -await run(`npm publish ./npm --access public`, true); -spinnerPublish.stop(); +await run(`npm publish ./npm --access public`, `Publishing...`, true); -const spinnerNewNpm = new Spinner({ - message: `Checking new published npm package...`, -}); -spinnerNewNpm.start(); -const newNpmVersion = await run(`npm show ${PACKAGE_NAME} version`); -spinnerNewNpm.stop(); +const newNpmVersion = await run( + `npm view ${PACKAGE_NAME} version`, + `Checking new published npm package...`, +); +// Wait for npm to update +await delay(4000); if (newNpmVersion.trim() !== trimmedTag) { console.log( diff --git a/scripts/run.ts b/scripts/run.ts index 7663655..cf04c39 100644 --- a/scripts/run.ts +++ b/scripts/run.ts @@ -1,24 +1,48 @@ +import { Spinner } from "./script_deps.ts"; + +type CommandResult = { + ok: true; + output: string; +} | { + ok: false; + error: string; +}; + const getCommandOutput = async ( command: Deno.Command, ignoreErrors = false, -) => { +): Promise => { const { code, stdout, stderr } = await command.output(); const error = new TextDecoder().decode(stderr); if ((code || error) && !ignoreErrors) { - console.error(error); - Deno.exit(1); + return { ok: false, error: error || "Unknown error" }; } const output = new TextDecoder().decode(stdout); - return output; + return { ok: true, output }; }; // The command might terminate the program. -export const run = async (command: string, ignoreErrors = false) => { +export const run = async ( + command: string, + spinnerMessage?: string, + ignoreErrors = false, +) => { + const spinner = spinnerMessage + ? new Spinner({ message: spinnerMessage }) + : undefined; + if (spinner) spinner.start(); + const words = command.split(" "); const cmd = words[0]; const args = words.slice(1); const denoCmd = new Deno.Command(cmd, { args }); - const output = await getCommandOutput(denoCmd, ignoreErrors); - return output; + const result = await getCommandOutput(denoCmd, ignoreErrors); + if (spinner) spinner.stop(); + + if (!result.ok) { + console.error(result.error); + Deno.exit(1); + } + return result.output; }; diff --git a/scripts/script_deps.ts b/scripts/script_deps.ts index f7c836d..df688a9 100644 --- a/scripts/script_deps.ts +++ b/scripts/script_deps.ts @@ -1,5 +1,6 @@ export { gt, parse } from "https://deno.land/std@0.212.0/semver/mod.ts"; export { Spinner } from "https://deno.land/std@0.212.0/cli/mod.ts"; export * as colors from "https://deno.land/std@0.212.0/fmt/colors.ts"; +export { delay } from "https://deno.land/std@0.212.0/async/delay.ts"; export { build, emptyDir } from "https://deno.land/x/dnt@0.39.0/mod.ts";