Skip to content

Commit

Permalink
feat: custom func-js bins
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinketer22 authored Mar 1, 2024
1 parent 9e49ada commit e482652
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import chalk from 'chalk';
import { create } from './create';
import { run } from './run';
import { build } from './build';
import { set } from './set';
import { test } from './test';
import { verify } from './verify';
import { convert } from './convert';
Expand All @@ -19,6 +20,7 @@ const runners: Record<string, Runner> = {
create,
run,
build,
set,
test,
help,
verify,
Expand Down
4 changes: 4 additions & 0 deletions src/cli/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ List of available commands:
- create
- run
- build
- custom
- help
- test
- verify
Expand Down Expand Up @@ -49,6 +50,9 @@ If contract name is not specified on the command line, the buildable contracts (
Flags:
--all - builds all buildable contracts instead of just one.`,
set: `Usage: blueprint set <key> [value]
Available keys:
- func - overrides @ton-community/func-js-bin version, effectively setting the func version. The required version may be passed as the value, otherwise available versions will be displayed.`,
test: `Usage: blueprint test
Just runs \`npm test\`, which by default runs \`jest\`.`,
Expand Down
111 changes: 111 additions & 0 deletions src/cli/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Args, Runner } from './Runner';
import { UIProvider } from '../ui/UIProvider';
import { readFile, writeFile } from 'fs/promises';
import { exec } from 'node:child_process';
import path from 'path';

const getVersions = (pkg: string, ui: UIProvider): Promise<string[]> => {
return new Promise((resolve, reject) => {
exec(`npm view ${pkg} versions --json`, (error, stdout, stderr) => {
if (stderr) {
ui.write(stderr);
}
if (stdout) {
if (error === null) {
try {
const resJson = JSON.parse(stdout);
if (Array.isArray(resJson)) {
resolve(resJson);
} else {
reject(new TypeError("Expect json array on stdout, but got:\n" + stdout));
}
} catch (e) {
reject(e);
}
return;
} else {
ui.write(stdout);
}
}
if (error) {
ui.write("Failed to get func-js-bin package versions!");
reject(error);
}
});
});
};

const install = (cmd: string, ui: UIProvider): Promise<void> => {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (stderr) {
ui.write(stderr);
}
if (stdout) {
ui.write(stdout);
}
if (error) {
reject(error);
return;
}
resolve();
});
});
}

export const set: Runner = async (args: Args, ui: UIProvider) => {
if (args._.length < 2) {
throw new Error('Please pass a key');
}

switch (args._[1]) {
case 'func': {
const pkg = '@ton-community/func-js-bin';

const funcVersions = await getVersions(pkg, ui);

let version = args._.length > 2 ? args._[2] : '';
if (!funcVersions.includes(version)) {
version = await ui.choose('Choose FunC version', funcVersions, (s) => s);
}

const packagePath = path.join(process.cwd(), 'package.json');
const packageContents = (await readFile(packagePath)).toString('utf-8');
const parsedPackage = JSON.parse(packageContents);

const packageManager: 'npm' | 'yarn' | 'pnpm' | 'other' = await ui.choose('Choose your package manager', ['npm', 'yarn', 'pnpm', 'other'], (s) => s);

if (packageManager === 'other') {
ui.write(`Please find out how to override @ton-community/func-js-bin version to ${version} using your package manager, do that, and then install the packages`);
return;
}

const overrideKey = packageManager === 'yarn' ? 'resolutions' : 'overrides';

parsedPackage[overrideKey] = {
...parsedPackage[overrideKey],
[pkg]: version,
};

ui.write('Updating package.json...');

await writeFile(packagePath, JSON.stringify(parsedPackage, null, 4));

const installCmd = packageManager === 'yarn' ? 'yarn' : `${packageManager} i`;

try {
ui.write('Installing dependencies...');
await install(installCmd, ui);
} catch (e) {
ui.write('Failed to install dependencies, rolling back package.json');
await writeFile(packagePath, packageContents);
throw e;
}

break;
}
default: {
throw new Error('Unknown key: ' + args._[1]);
}
}
};

0 comments on commit e482652

Please sign in to comment.