Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for NPM to update before checking + Ignore tag errors #29

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions scripts/publish_latest_tag_to_npm.ts
Original file line number Diff line number Diff line change
@@ -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`);
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
38 changes: 31 additions & 7 deletions scripts/run.ts
Original file line number Diff line number Diff line change
@@ -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<CommandResult> => {
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;
};
1 change: 1 addition & 0 deletions scripts/script_deps.ts
Original file line number Diff line number Diff line change
@@ -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";