Skip to content

Commit

Permalink
Improve installer version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
salamaashoush committed Apr 28, 2024
1 parent e27e881 commit db2fcb8
Show file tree
Hide file tree
Showing 42 changed files with 428 additions and 233 deletions.
15 changes: 15 additions & 0 deletions apps/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# pact-toolbox

## 0.0.10

### Patch Changes

- Improve installer version detection
- Updated dependencies
- @pact-toolbox/installer@0.0.7
- @pact-toolbox/network@0.0.9
- @pact-toolbox/utils@0.0.6
- @pact-toolbox/script@0.0.9
- @pact-toolbox/test@0.0.9
- @pact-toolbox/fabricator@0.0.2
- @pact-toolbox/init@0.0.7
- @pact-toolbox/prelude@0.0.9

## 0.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pact-toolbox",
"version": "0.0.9",
"version": "0.0.10",
"description": "A tool to help you build, test and deploy your Pact smart contracts",
"keywords": [
"pact",
Expand Down
12 changes: 5 additions & 7 deletions apps/cli/src/commands/doctor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getPactInstallationInfo, installPact } from '@pact-toolbox/installer';
import { isDockerInstalled, logger } from '@pact-toolbox/utils';
import { installPact } from '@pact-toolbox/installer';
import { isAnyPactInstalled, isDockerInstalled, logger } from '@pact-toolbox/utils';
import { defineCommand } from 'citty';

export const doctorCommand = defineCommand({
Expand All @@ -9,17 +9,15 @@ export const doctorCommand = defineCommand({
},
run: async () => {
// check if pact is installed
const { isInstalled, latestVersion } = await getPactInstallationInfo();
const isInstalled = await isAnyPactInstalled();
if (!isInstalled) {
logger.warn(`Pact is not installed!`);
const answer = await logger.prompt(`Would you like to install Pact ${latestVersion}?`, {
const answer = await logger.prompt(`Would you like to install pact latest version?`, {
type: 'confirm',
default: true,
});
if (answer === true) {
await installPact({
version: latestVersion,
});
await installPact();
}
}
// check if docker is installed
Expand Down
11 changes: 5 additions & 6 deletions apps/cli/src/commands/pact/use.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { activatePactVersion } from '@pact-toolbox/installer';
import { getInstalledPactVersion, logger, normalizeVersion } from '@pact-toolbox/utils';
import { activatePactVersion, isActivePactVersion, isNightlyPactVersion } from '@pact-toolbox/installer';
import { logger } from '@pact-toolbox/utils';
import { defineCommand } from 'citty';

export const useCommand = defineCommand({
Expand All @@ -16,10 +16,9 @@ export const useCommand = defineCommand({
},
},
run: async ({ args }) => {
let currentVersion = await getInstalledPactVersion();
const isNightly =
currentVersion?.includes('5.0') || currentVersion?.includes('nightly') || currentVersion?.includes('dev');
if (currentVersion && normalizeVersion(currentVersion) === normalizeVersion(args.version) && !isNightly) {
const isNightly = isNightlyPactVersion(args.version);
const isActive = await isActivePactVersion(args.version);
if (isActive && !isNightly) {
logger.info(`Already using pact version ${args.version}`);
return;
}
Expand Down
2 changes: 0 additions & 2 deletions apps/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { resolveConfig } from '@pact-toolbox/config';
import { versionCheckMiddleware } from '@pact-toolbox/installer';
import { startLocalNetwork } from '@pact-toolbox/network';
import { defineCommand } from 'citty';

Expand Down Expand Up @@ -41,7 +40,6 @@ export const startCommand = defineCommand({
},
},
run: async ({ args }) => {
await versionCheckMiddleware();
const config = await resolveConfig();
const { network, quiet, tunnel, clipboard } = args;
//@ts-ignore
Expand Down
6 changes: 6 additions & 0 deletions apps/pactlings/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# pactlings

## 0.0.10

### Patch Changes

- Improve installer version detection

## 0.0.9

### Patch Changes
Expand Down
3 changes: 3 additions & 0 deletions apps/pactlings/bin/pactlings.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node
'use strict';
import '../dist/cli.js';
24 changes: 4 additions & 20 deletions apps/pactlings/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactlings",
"version": "0.0.9",
"version": "0.0.10",
"description": "A tool to help you build, test and deploy your Pact smart contracts",
"keywords": [
"pact",
Expand All @@ -11,10 +11,10 @@
"author": "Salama Ashoush <[email protected]>",
"license": "MIT",
"type": "module",
"private": false,
"private": true,
"main": "./src/index.ts",
"bin": {
"pact-toolbox": "./src/cli.ts"
"pactlings": "./src/cli.ts"
},
"files": [
"dist"
Expand All @@ -38,12 +38,11 @@
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"bin": {
"pact-toolbox": "./bin/pact-toolbox.mjs"
"pactlings": "./bin/pactlings.mjs"
}
},
"scripts": {
"cli": "tsx src/cli.ts",
"playground": "tsx src/playground.ts",
"build": "tsup",
"dev": "tsup --watch",
"lint": "oxlint --import-plugin -D all && prettier -c src",
Expand All @@ -55,21 +54,6 @@
"verify": "tsc --noEmit --skipLibCheck"
},
"dependencies": {
"@kadena/client": "^1.11.0",
"@kadena/pactjs": "^0.4.2",
"@kadena/pactjs-cli": "^1.11.0",
"@kadena/types": "^0.6.1",
"@pact-toolbox/client-utils": "workspace:*",
"@pact-toolbox/config": "workspace:*",
"@pact-toolbox/fabricator": "workspace:*",
"@pact-toolbox/init": "workspace:*",
"@pact-toolbox/installer": "workspace:*",
"@pact-toolbox/network": "workspace:*",
"@pact-toolbox/prelude": "workspace:*",
"@pact-toolbox/runtime": "workspace:*",
"@pact-toolbox/script": "workspace:*",
"@pact-toolbox/test": "workspace:*",
"@pact-toolbox/utils": "workspace:*",
"citty": "^0.1.6",
"defu": "^6.1.4",
"pathe": "^1.1.2"
Expand Down
7 changes: 7 additions & 0 deletions packages/fabricator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @pact-toolbox/fabricator

## 0.0.2

### Patch Changes

- Updated dependencies
- @pact-toolbox/utils@0.0.6

## 0.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/fabricator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pact-toolbox/fabricator",
"version": "0.0.1",
"version": "0.0.2",
"description": "A tool to generate Pact contracts and components",
"repository": "kadena-community/pact-toolbox",
"author": "Salama Ashoush <[email protected]>",
Expand Down
7 changes: 7 additions & 0 deletions packages/init/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @pact-toolbox/init

## 0.0.7

### Patch Changes

- Updated dependencies
- @pact-toolbox/utils@0.0.6

## 0.0.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/init/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pact-toolbox/init",
"version": "0.0.6",
"version": "0.0.7",
"repository": "kadena-community/pact-toolbox",
"author": "Salama Ashoush <[email protected]>",
"license": "MIT",
Expand Down
8 changes: 8 additions & 0 deletions packages/installer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @pact-toolbox/installer

## 0.0.7

### Patch Changes

- Improve installer version detection
- Updated dependencies
- @pact-toolbox/utils@0.0.6

## 0.0.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/installer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pact-toolbox/installer",
"version": "0.0.6",
"version": "0.0.7",
"repository": "kadena-community/pact-toolbox",
"author": "Salama Ashoush <[email protected]>",
"license": "MIT",
Expand Down
23 changes: 15 additions & 8 deletions packages/installer/src/activate.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { logger } from '@pact-toolbox/utils';
import { logger, normalizeVersion } from '@pact-toolbox/utils';
import { existsSync } from 'node:fs';
import { mkdir, readFile, symlink, unlink } from 'node:fs/promises';
import { join } from 'pathe';
import { KADENA_BIN_DIR, PACT_ROOT_DIR, PACT_SYMLINK } from './constants';
import { KADENA_BIN_DIR, PACT_SYMLINK } from './constants';
import { listInstalledPactVersions } from './list';
import { PactRemoteAssetInfo } from './releaseInfo';

export interface PactMetadata {
export interface InstalledPactVersionMetadata extends PactRemoteAssetInfo {
version: string;
binary: string;
files: string[];
}
export async function activatePactVersion(version: string, binary?: string) {
const versionDir = join(PACT_ROOT_DIR, version);
const metadataPath = join(versionDir, 'metadata.json');
const managedVersions = await listInstalledPactVersions();
const installedVersion = managedVersions.find((v) => normalizeVersion(v.version).includes(normalizeVersion(version)));
if (!installedVersion) {
throw new Error(`${version} is not installed`);
}
const versionPath = installedVersion.path;
const metadataPath = join(versionPath, 'metadata.json');
if (!existsSync(metadataPath)) {
throw new Error(`Could not find metadata for version ${version}`);
}
const metadata: PactMetadata = await readFile(metadataPath, 'utf-8')
const metadata: InstalledPactVersionMetadata = await readFile(metadataPath, 'utf-8')
.then(JSON.parse)
.catch(() => {
throw new Error(`Could not find metadata for version ${version}`);
});
binary = binary || metadata.binary;
const pactBinary = binary.startsWith('/') ? binary : join(versionDir, binary);
const pactBinary = binary.startsWith('/') ? binary : join(versionPath, binary);
if (!existsSync(pactBinary)) {
throw new Error(`Could not find binary ${pactBinary}`);
}
Expand All @@ -32,5 +39,5 @@ export async function activatePactVersion(version: string, binary?: string) {
await unlink(PACT_SYMLINK);
}
await symlink(pactBinary, PACT_SYMLINK, 'file');
logger.success(`Switched to pact version ${version}`);
logger.success(`Switched to pact version ${metadata.version}`);
}
57 changes: 57 additions & 0 deletions packages/installer/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { logger } from '@pact-toolbox/utils';
import { appendFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join } from 'pathe';

export function getShellProfile(shell: string) {
if (shell.includes('zsh')) {
return join(homedir(), '.zshrc');
}

if (shell.includes('bash')) {
return join(homedir(), '.bashrc');
}

if (shell.includes('fish')) {
return join(homedir(), '.config/fish/config.fish');
}

return null;
}

export function getShellProfileScript() {
return `
# Pact Toolbox
export PATH="$PATH:$HOME/.kadena/bin"
`;
}

export function isKadenaBinInPath() {
const path = process.env.PATH;
if (!path) {
return false;
}
return path.includes('.kadena/bin');
}
export async function updateShellProfileScript() {
if (isKadenaBinInPath()) {
return;
}
const shell = process.env.SHELL;
if (!shell) {
throw new Error('SHELL environment variable is not set');
}
const profile = getShellProfile(shell);
const script = getShellProfileScript();
if (!profile) {
throw new Error('Shell profile not found');
}
if (!script) {
throw new Error('Shell profile script not found');
}
if (profile.includes(script)) {
return;
}
await appendFile(profile, script);
logger.box(`Shell profile updated. Please restart your shell to apply changes.\n or run \n\`. ${profile}\``);
}
24 changes: 6 additions & 18 deletions packages/installer/src/extract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeVersion, writeFileAtPath } from '@pact-toolbox/utils';
import { normalizeVersion } from '@pact-toolbox/utils';
import { existsSync } from 'node:fs';
import { chmod, mkdir, rm } from 'node:fs/promises';
import { join } from 'pathe';
Expand All @@ -8,12 +8,11 @@ import { Open } from 'unzipper';
export interface ExtractTarballOptions {
filter?: string[];
executable?: string;
writeMetadata?: boolean;
}
export async function extractTarball(
tarball: string,
dest: string,
{ executable = 'pact', filter, writeMetadata = true }: ExtractTarballOptions = {},
{ executable = 'pact', filter }: ExtractTarballOptions = {},
) {
const isTarball = tarball.endsWith('.tar.gz');
let version = dest.split('/').pop();
Expand Down Expand Up @@ -48,19 +47,8 @@ export async function extractTarball(
if (binary) {
await chmod(join(dest, binary), 0o755);
}
if (writeMetadata) {
// write metadata file
await writeFileAtPath(
join(dest, 'metadata.json'),
JSON.stringify(
{
version,
binary,
files,
},
null,
2,
),
);
}
return {
binary,
files,
};
}
1 change: 1 addition & 0 deletions packages/installer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './activate';
export * from './constants';
export * from './env';
export * from './extract';
export * from './install';
export * from './list';
Expand Down
Loading

0 comments on commit db2fcb8

Please sign in to comment.