Skip to content

Commit

Permalink
Supports compass and defaults to sass. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoelkemp committed Mar 17, 2014
1 parent 3225202 commit 9283555
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 81 deletions.
84 changes: 16 additions & 68 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
var gux = require('node-unique-extensions'),
fs = require('fs'),
q = require('q'),
lib = require('./helpers/LibHelper'),
npmh = require('./helpers/NpmHelper'),
GruntHelper = require('./helpers/GruntHelper');

var Ya = function () {
this.directory = '.';
function Ya () {
this.directory = '.';
this.processedPromises = [];
this.extensions = [];
};
this.extensions = [];
}

Ya.prototype.init = function (directory) {
this.directory = directory || this.directory;

this.grunt = new GruntHelper(this.directory);
this.grunt.on('added', this.onAddedExtensions.bind(this));

hasPackageJsonFile(this.directory)
npmh.hasPackageJsonFile(this.directory)
.then(function (hasFile) {
if (! hasFile) {
return createEmptyPackageJsonFile(this.directory);
return npmh.createEmptyPackageJsonFile(this.directory);
}
}.bind(this))
.then(installDependencies)
Expand Down Expand Up @@ -100,11 +100,12 @@ Ya.prototype.getExtensionSettings = function (ext) {

return this.isExtensionSupported(ext)
.then(function (isSupported) {
var settings;

if (isSupported) {
settings = require(this.getSettingsFilepath(ext));
return settings;
// Settings will either be an object literal
// for simple preprocessors that have static settings
// or a promise that resolves with the settings
// for preprocessors performing async to determine settings
return require(this.getSettingsFilepath(ext));

} else {
return null;
Expand Down Expand Up @@ -168,15 +169,15 @@ Ya.prototype.processExtension = function (ext) {
.then(function (extSettings) {
// Cache for later chains
settings = extSettings;
return lib.isLibInstalled(settings.lib);
return npmh.isLibInstalled(settings.lib);
})
.then(function (isInstalled) {
if (isInstalled) {
console.log(settings.lib + ' already installed');
return settings;
}

return lib.installLib(settings.lib)
return npmh.installLib(settings.lib)
.then(function () {
console.log(settings.lib + ' installed');
// Add to the config, a grunt target for that extension
Expand All @@ -192,7 +193,7 @@ Ya.prototype.processExtension = function (ext) {
function installDependencies() {
var dependencies = ['grunt', 'grunt-cli', 'load-grunt-tasks', 'grunt-contrib-watch'];

return q.all(dependencies.map(lib.isLibInstalled))
return q.all(dependencies.map(npmh.isLibInstalled))
.then(function (results) {
// Only install the libs that haven't already been installed
var notInstalled = dependencies.filter(function (dep, idx) {
Expand All @@ -201,7 +202,7 @@ function installDependencies() {

if (notInstalled.length) console.log('Installing: ', notInstalled);

return q.all(notInstalled.map(lib.installLib));
return q.all(notInstalled.map(npmh.installLib));
});
}

Expand Down Expand Up @@ -230,57 +231,4 @@ function processSupportedExtensions(extensions) {
return q.all(extensions.map(this.processExtension.bind(this)));
}

function hasPackageJsonFile(directory) {
if (! directory) throw new Error('directory not given');

var deferred = q.defer();

// TODO: Are we guaranteed for the package.json file to live in supplied directory?
directory = slashDir(directory);

fs.exists(directory + 'package.json', function (exists) {
deferred.resolve(exists);
});

return deferred.promise;
}

// Returns a json object representing an empty package.json file
function getDummyPackageJson () {
return {
'author': '',
'name': '',
'description': '',
'version': '',
'repository': {
'url': ''
},
'dependencies': {},
'devDependencies': {
'ya.js': '*'
},
'main': '',
'license': ''
};
}

function createEmptyPackageJsonFile(directory) {
directory = slashDir(directory);

var emptyPackageFile = JSON.stringify(getDummyPackageJson(), null, 2),
deferred = q.defer();

fs.writeFile(directory + 'package.json', emptyPackageFile, function (err) {
if (err) deferred.reject();
else deferred.resolve();
});

return deferred.promise;
}

// Helper to return a slash-trailed version of the directory name
function slashDir(directory) {
return directory[directory.length - 1] === '/' ? directory : directory + '/';
}

module.exports = new Ya();
89 changes: 76 additions & 13 deletions settings/scss-settings.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
module.exports.lib = 'grunt-contrib-sass';
// Special case when the extension doesn't match the target
// Note: grunt-contrib-sass expects the target to be named sass
module.exports.targetName = 'sass';
module.exports.target = {
dist: {
files: [{
expand: true,
src: ['**/*.{scss, sass}', '!node_modules/**/*.{scss, sass}'],
ext: '.css'
}]
}
};
var q = require('q'),
deferred = q.defer(),
exec = require('child_process').exec,
utils = require('../helpers/Utils');

var
compassSettings = {
lib: 'grunt-contrib-compass',
targetName: 'compass',
target: {
dist: {
options: {
sassDir: '.',
cssDir: '.'
}
}
}
},

sassSettings = {
lib: 'grunt-contrib-sass',
targetName: 'sass',
target: {
dist: {
files: [{
expand: true,
src: ['**/*.{scss, sass}', '!node_modules/**/*.{scss, sass}'],
ext: '.css'
}]
}
}
};

// Resolves with whether or not the Compass gem is installed
function isCompassInstalled() {
var deferred = q.defer(),
cmd = 'compass';

exec(cmd, function (err) {
deferred.resolve(! err);
});

return deferred.promise;
}

// Whether or not the directory structure was auto-generated
// from compass create.
// Note: auto-generation is determined by the existence of
// the sass and stylesheets directories
function wasCompassCreate() {
return q.all([utils.exists('sass'), utils.exists('stylesheets')])
.then(function (results) {
return results[0] && results[1];
});
}


// Check if compass is installed
isCompassInstalled()
.then(function (isInstalled) {
if (isInstalled) {
wasCompassCreate().then(function (wasGenenerated) {
// Use the sass/ and stylesheets/ folders
if (wasGenenerated) {
compassSettings.target.dist.options.sassDir = 'sass';
compassSettings.target.dist.options.cssDir = 'stylesheets';
}

deferred.resolve(compassSettings);
});
} else {
deferred.resolve(sassSettings);
}
});

module.exports = deferred.promise;

0 comments on commit 9283555

Please sign in to comment.