-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from near/include-bytes
add includeBytes "macro" that helps factory contract
- Loading branch information
Showing
7 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,6 @@ | |
"files": [ | ||
"src/index.ts", | ||
"src/build-tools/near-bindgen-exporter.js", | ||
"src/build-tools/include-bytes.js", | ||
] | ||
} |