Skip to content

Commit

Permalink
Load .atom-build.js without using require
Browse files Browse the repository at this point in the history
Also add support for .atom-build.coffee

noseglid#553
noseglid#513
  • Loading branch information
bkietz authored Jan 27, 2018
1 parent 4e09cc0 commit 5ae65a5
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions lib/atom-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,51 @@ import EventEmitter from 'events';
function getConfig(file) {
const fs = require('fs');
const realFile = fs.realpathSync(file);
delete require.cache[realFile];
const contents = fs.readFileSync(realFile);

function fromScript(script) {
const globals = {
__dirname: require('path').dirname(realFile),
__filename: realFile,
module: { exports: {} },
require
};
globals.exports = globals.module.exports;
const scriptFn = new Function(...Object.keys(globals), script);
scriptFn(...Object.keys(globals).map(n => globals[n]));
return globals.module.exports;
};

switch (require('path').extname(file)) {
case '.json':
return JSON.parse(contents);

case '.js':
return require(realFile);
return fromScript(contents);

case '.coffee':
let coffee;
try
{
coffee = require('coffeescript');
}
catch (e) {}
if (coffee === undefined)
{
coffee = require('coffee-script');
}
return fromScript(coffee.compile(contents, { bare: true }));

case '.cson':
return require('cson-parser').parse(fs.readFileSync(realFile));
return require('cson-parser').parse(contents);

case '.yaml':
case '.yml':
return require('js-yaml').safeLoad(fs.readFileSync(realFile));
return require('js-yaml').safeLoad(contents);
}

return {};
}

function createBuildConfig(build, name) {
const conf = {
name: 'Custom: ' + name,
Expand Down

0 comments on commit 5ae65a5

Please sign in to comment.