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

add includeBytes "macro" that helps factory contract #206

Merged
merged 3 commits into from
Sep 7, 2022
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
1 change: 1 addition & 0 deletions examples/babel.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"plugins": [
"near-sdk-js/lib/build-tools/include-bytes",
"near-sdk-js/lib/build-tools/near-bindgen-exporter",
["@babel/plugin-proposal-decorators", {"version": "legacy"}]
],
Expand Down
7 changes: 7 additions & 0 deletions lib/build-tools/include-bytes.d.ts

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

30 changes: 30 additions & 0 deletions lib/build-tools/include-bytes.js

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

37 changes: 37 additions & 0 deletions src/build-tools/include-bytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fs from 'fs'
import path from 'path'
export default function ({ types: t }) {
return {
visitor: {
CallExpression(p, state) {
let name = p.node.callee.name
let args = p.node.arguments

if (name === 'includeBytes') {
// Get the path of file
var filename = this.file.opts.filename

// User settings
let root = state.opts.root || path.dirname(filename)

// Read binary file into bytes, so encoding is 'latin1' (each byte is 0-255, become one character)
const encoding = 'latin1'

// Require first arg to be string
t.assertStringLiteral(args[0])

// Error if filename is not found
if (filename === undefined || filename === 'unknown')
throw new Error('`includeBytes` function called outside of file')

// Generate and locate the file
let fileRelPath = args[0].value // Get literal string value
let filePath = path.join(root, fileRelPath)
let fileSrc = fs.readFileSync(filePath, { encoding }).toString(encoding)

p.replaceWith(t.stringLiteral(fileSrc))
}
},
},
}
}
1 change: 1 addition & 0 deletions tests/babel.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"plugins": [
"near-sdk-js/lib/build-tools/include-bytes",
"near-sdk-js/lib/build-tools/near-bindgen-exporter",
["@babel/plugin-proposal-decorators", {"version": "legacy"}]
],
Expand Down
2 changes: 1 addition & 1 deletion tests/src/promise_batch_api.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"files": [
"src/index.ts",
"src/build-tools/near-bindgen-exporter.js",
"src/build-tools/include-bytes.js",
]
}