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

Patch 1 #555

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 34 additions & 6 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 Expand Up @@ -68,7 +96,7 @@ export default class CustomFile extends EventEmitter {
const os = require('os');
const fs = require('fs');
const path = require('path');
this.files = [].concat.apply([], [ 'json', 'cson', 'yaml', 'yml', 'js' ].map(ext => [
this.files = [].concat.apply([], [ 'json', 'cson', 'yaml', 'yml', 'js', 'coffee' ].map(ext => [
path.join(this.cwd, `.atom-build.${ext}`),
path.join(os.homedir(), `.atom-build.${ext}`)
])).filter(fs.existsSync);
Expand Down
16 changes: 16 additions & 0 deletions spec/custom-provider-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,20 @@ describe('custom provider', () => {
});
});
});

describe('when .atom-build.coffee exists', () => {
it('it should provide targets', () => {
fs.writeFileSync(`${directory}.atom-build.coffee`, fs.readFileSync(`${__dirname}/fixture/.atom-build.coffee`));
expect(builder.isEligible()).toEqual(true);

waitsForPromise(() => {
return Promise.resolve(builder.settings()).then(settings => {
const s = settings[0];
expect(s.exec).toEqual('echo');
expect(s.args).toEqual([ 'hello', 'world', 'from', 'coffeescript' ]);
expect(s.name).toEqual('Custom: from coffeescript');
});
});
});
});
});
4 changes: 4 additions & 0 deletions spec/fixture/.atom-build.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports =
cmd: 'echo'
args: [ 'hello', 'world', 'from', 'js' ]
name: 'from js'