Skip to content

Commit

Permalink
Generate management canister Candid file (#265)
Browse files Browse the repository at this point in the history
* Generate management canister Candid file

* Adjust test description

* Update package-lock.json
  • Loading branch information
rvanasa authored Feb 13, 2024
1 parent e271baf commit 2bde9e2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 231 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
},
"scripts": {
"prepare": "husky install && cd rust && npm install",
"postinstall": "npm run generate",
"lint": "eslint . --ext ts",
"lint:fix": "npm run lint -- --fix",
"format": "prettier --write src",
Expand All @@ -219,8 +220,9 @@
"compile:browser": "esbuild ./src/browser.ts --bundle --outfile=out/browser.js --external:vscode --format=cjs --platform=node --minify",
"compile:server": "esbuild ./src/server/server.ts --bundle --outfile=out/server.js --external:vscode --format=cjs --platform=node --minify",
"compile:motoko": "esbuild motoko --bundle --outfile=out/motoko.js --format=cjs --platform=node --minify",
"generate": "node scripts/generate",
"test": "jest",
"package": "vsce package && npm test && npm run --silent lint",
"package": "npm run generate && vsce package && npm test && npm run --silent lint",
"publish": "vsce publish"
},
"lint-staged": {
Expand Down
39 changes: 39 additions & 0 deletions scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fs = require('fs');
const path = require('path');
const https = require('https');

const generatedDirectory = path.join(__dirname, '../src/generated');
if (!fs.existsSync(generatedDirectory)) {
fs.mkdirSync(generatedDirectory);
}

https
.get(
'https://raw.githubusercontent.com/dfinity/interface-spec/master/spec/_attachments/ic.did',
(response) => {
if (response.statusCode !== 200) {
throw new Error(
`HTTP ${response.statusCode}: ${response.statusMessage}`,
);
}
const data = [];
response.on('data', (chunk) => data.push(chunk));
response.on('end', () => {
fs.writeFile(
path.join(generatedDirectory, 'aaaaa-aa.did.ts'),
`export default \`${data.join('')}\`;\n`,
(err) => {
if (err) {
throw err;
}
},
);
});
},
)
.on('error', (err) => {
console.error(
'Error while downloading management canister Candid file:',
);
throw err;
});
229 changes: 0 additions & 229 deletions src/candid/aaaaa-aa.did.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/server/server.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import icCandid from '../generated/aaaaa-aa.did';

describe('server', () => {
test('generated IC Candid file has expected format', () => {
expect(icCandid).toContain('service ic : {\n');
});
});
2 changes: 1 addition & 1 deletion src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import {
resolveVirtualPath,
} from './utils';
import { pascalCase } from 'change-case';
import icCandid from '../candid/aaaaa-aa.did';
import icCandid from '../generated/aaaaa-aa.did';

const errorCodes: Record<
string,
Expand Down

0 comments on commit 2bde9e2

Please sign in to comment.