Skip to content

Commit

Permalink
added mkdirp to gulp utils
Browse files Browse the repository at this point in the history
- write file now creates directories that don't exist in the path of a new file.
  • Loading branch information
Willem Wyndham committed Aug 30, 2019
1 parent 6184a0f commit 806d5a6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
7 changes: 3 additions & 4 deletions blank_project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "",
"version": "0.0.1",
"scripts": {
"build": "mkdir -p out/ && gulp",
"build": "gulp",
"deploy:contract": "near deploy",
"deploy:pages": "gh-pages -d src",
"deploy": "npm run build && npm run deploy:contract && npm run deploy:pages",
Expand All @@ -12,13 +12,12 @@
"test": "npm run build && jest test --env=near-shell/test_environment"
},
"devDependencies": {
"assemblyscript-json": "github:nearprotocol/assemblyscript-json#staging",
"bignum": "github:MaxGraey/bignum.wasm",
"gh-pages": "^2.1.1",
"gulp": "^4.0.2",
"jest": "^24.8.0",
"jest-environment-node": "^24.8.0",
"near-shell": "github:nearprotocol/near-shell#staging"
"near-runtime-ts": "github:nearprotocol/near-runtime-ts#staging",
"near-shell": "file:.."
},
"wasmStudio": {
"name": "Hello World Example",
Expand Down
40 changes: 26 additions & 14 deletions gulp-utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
// FUTURE PEOPLE: This file is called "gulp-utils" but it's not related to the deprecated library called "gulp-utils". Don't panic.
// function generateBindings(inputFile, outputFile, callback) {
// const asc = getAsc();
// asc.main([
// inputFile,
// "--baseDir", process.cwd(),
// "--nearFile", outputFile,
// "--measure"
// ], callback);
// }
var path = require("path");

function compile(inputFile, outputFile, callback) {
Expand All @@ -31,9 +21,25 @@ function getAsc() {
}

asc = require("assemblyscript/bin/asc");

const fs = require("fs");
const pathModule = require("path");

// Create parent directories if they don't exist
function mkdirp(path){
let dirname = pathModule.dirname(path);
let paths = []
while (!fs.existsSync(dirname)){
paths.unshift(pathModule.basename(dirname));
dirname = pathModule.dirname(dirname);
}
if (paths.length > 0){
for (const i in paths){
fs.mkdirSync(pathModule.join(dirname, ...paths.slice(0,i + 1)))
}
}
}

asc.main = (main => (args, options, fn) => {
if (typeof options === "function") {
fn = options;
Expand All @@ -45,22 +51,28 @@ function getAsc() {
stdout: process.stdout || asc.createMemoryStream(logLn),
stderr: process.stderr || asc.createMemoryStream(logLn),
readFile: (filename, baseDir) => {
baseDir = pathModule.relative(process.cwd(), baseDir);
let path = pathModule.join(baseDir, filename);
if (!fs.existsSync(path)) {
return null;
}

return fs.readFileSync(path).toString("utf8");
},
writeFile: (filename, contents) => {
const name = filename.startsWith("../") ? filename.substring(3) : filename;
mkdirp(name);
fs.writeFileSync(name, contents);
},
listFiles: () => []
listFiles: (dirname, baseDir) => {
try {
return fs.readdirSync(path.join(baseDir, dirname)).filter(file => /^(?!.*\.d\.ts$).*\.ts$/.test(file));
} catch (e) {
return null;
}
}
}, fn);
})(asc.main);
return asc;
}


module.exports = { compile };

0 comments on commit 806d5a6

Please sign in to comment.