Skip to content

Commit

Permalink
Merge pull request martim00#14 from doconix/master
Browse files Browse the repository at this point in the history
This commit adds two things:
  • Loading branch information
martim00 authored Apr 13, 2018
2 parents fba80c2 + 2927b64 commit cfa1b03
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
31 changes: 21 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs');
const { sep: slash } = require('path');
const path = require('path');
const loaderUtils = require('loader-utils');
const extend = require('extend');

const spawn = require('child_process').spawn;

Expand All @@ -19,28 +20,31 @@ module.exports = function (source) {
folder: `.${slash}__javascript__`,
install: 'pip install transcrypt',
python_version: '3.x',
sourcemaps: true
sourcemaps: true,
keep_compiled: false
},
jiphy: {
switches: '',
folder: `.${slash}`,
install: 'pip install jiphy',
python_version: '2.x',
sourcemaps: false
sourcemaps: false,
keep_compiled: false
},
pj: {
switches: '--inline-map --source-name %f -s -',
folder: `.${slash}`,
install: 'pip install javascripthon',
python_version: '3.x',
streaming: true,
sourcemaps: true
sourcemaps: true,
keep_compiled: false
}
};

const options = loaderUtils.getOptions(this);
const compilerName = options && options.compiler || 'transcrypt';
const compiler = compilers[compilerName];
const compiler = extend(true, {}, compilers[compilerName], options || {});

if (!compiler) {
throw new Error(`py-loader only supports ${
Expand Down Expand Up @@ -105,26 +109,33 @@ module.exports = function (source) {
return;
}
cmd.get(`${compiler.name} ${compiler.switches} '${srcDir}${slash}${basename}.py'`, function(err, data, stderr) {

if (!err) {
const filename = `${srcDir}${slash}${compiler.folder}${slash}${basename}.js`;
js = fs.readFileSync(filename, "utf8");
fs.unlinkSync(filename);
if(delete_after){
fs.unlinkSync(`${srcDir}${slash}__${fileinfo.name}.py`);
if (!compiler.keep_compiled) {
fs.unlinkSync(filename);
if (delete_after) {
fs.unlinkSync(`${srcDir}${slash}__${fileinfo.name}.py`);
}
}

if (compiler.sourcemaps) {
const sourceMapFile = `${srcDir}${slash}${compiler.folder}${slash}extra${slash}sourcemap${slash}${basename}.js`;
sourceMap = fs.readFileSync(sourceMapFile + ".map", "utf8")
fs.unlinkSync(sourceMapFile + ".map");
callback(null, js, sourceMap); }
if (!compiler.keep_compiled) {
fs.unlinkSync(sourceMapFile + ".map");
}
callback(null, js, sourceMap);
}
else {
callback(null, js);
}

}
else {
console.log(stderr)
console.error(stderr)
console.error(data)
// console.error(`Some error occurred on ${properName(compiler.name)} compiler execution. Have you installed ${properName(compiler.name)}? If not, please run \`${compiler.install}\` (requires Python ${compiler.python_version})`);
callback(err);
}
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"index.js"
],
"dependencies": {
"extend": "^3.0.1",
"loader-utils": "^1.1.0",
"node-cmd": "^3.0.0"
},
Expand Down

0 comments on commit cfa1b03

Please sign in to comment.