Skip to content

Commit

Permalink
feat(compiler): add CompilerCli8 class
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 6, 2023
1 parent 30866ef commit 29f1cd3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"sideEffects": false,
"scripts": {
"build:assets": "node tooling/fetch-aesophia-cli.mjs",
"build:assets": "node tooling/fetch-aesophia-cli.mjs && node tooling/fetch-aesophia-cli-8.mjs",
"build:types": "tsc && node tooling/downlevel/run.mjs",
"build:es": "babel src --config-file ./babel.esm.config.js --out-dir es --extensions .js,.ts --out-file-extension .mjs --source-maps true",
"build:api:node": "autorest tooling/autorest/node.yaml",
Expand Down
5 changes: 3 additions & 2 deletions src/contract/compiler/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CompilerError, InternalError, UnsupportedVersionError } from '../../uti
import semverSatisfies from '../../utils/semver-satisfies';
import { ensureError } from '../../utils/other';

const getPackagePath = (): string => {
export const getPackagePath = (): string => {
const path = dirname(fileURLToPath(import.meta.url));
if (basename(path) === 'dist') return resolve(path, '..');
if (basename(path) === 'compiler') return resolve(path, '../../..');
Expand All @@ -19,6 +19,7 @@ const getPackagePath = (): string => {
/**
* A wrapper around aesophia_cli, available only in Node.js.
* Requires Erlang installed, assumes that `escript` is available in PATH.
* @category contract
*/
export default class CompilerCli extends CompilerBase {
#path: string;
Expand All @@ -38,7 +39,7 @@ export default class CompilerCli extends CompilerBase {
this.#path = compilerPath;
if (ignoreVersion !== true) {
this.#ensureCompatibleVersion = this.version().then((version) => {
const versions = [version, '7.2.1', '8.0.0'] as const;
const versions = [version, '7.2.1', '9.0.0'] as const;
if (!semverSatisfies(...versions)) throw new UnsupportedVersionError('compiler', ...versions);
});
}
Expand Down
17 changes: 17 additions & 0 deletions src/contract/compiler/Cli8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { resolve } from 'path';
import CompilerCli, { getPackagePath } from './Cli';

/**
* @category contract
*/
export default class CompilerCli8 extends CompilerCli {
/**
* @param options - Options
* @param options.ignoreVersion - Don't ensure that the compiler is supported
*/
constructor(
{ ignoreVersion }: { ignoreVersion?: boolean } = {},
) {
super(resolve(getPackagePath(), './bin/aesophia_cli_8'), { ignoreVersion });
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './index-browser';

export { default as CompilerCli } from './contract/compiler/Cli';
export { default as CompilerCli8 } from './contract/compiler/Cli8';
export { default as getFileSystem } from './contract/compiler/getFileSystem';
export { default as CompilerHttpNode } from './contract/compiler/HttpNode';
25 changes: 25 additions & 0 deletions tooling/fetch-aesophia-cli-8.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createHash } from 'crypto';
import { dirname } from 'path';
import { writeFileSync, readFileSync, mkdirSync } from 'fs';

const path = './bin/aesophia_cli_8';
const hash = 'tZdsd7XH1e4C10MIzM0TY0IFcpkPBZqZMPdJ1ln9GDVsgjVCCK86YKCK5KtKqQzhNKSXaE01ZjAfTEYOSV7uIg==';

function ensureBinaryCorrect() {
const buffer = readFileSync(path);
const h = createHash('sha512').update(buffer).digest('base64');
if (h !== hash) throw new Error('Wrong hash');
}

try {
ensureBinaryCorrect();
} catch {
console.log('Fetching aesophia_cli_8');
const request = await fetch(
'https://github.com/aeternity/aesophia_cli/raw/df63ff9f4fdcfc437c90b90914fd1a7081d2bbbe/aesophia_cli',
);
const body = Buffer.from(await request.arrayBuffer());
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, body);
ensureBinaryCorrect();
}

0 comments on commit 29f1cd3

Please sign in to comment.