diff --git a/index.js b/index.js index 901c550..a2fba66 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,14 @@ 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; @@ -16,10 +16,10 @@ Ya.prototype.init = function (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) @@ -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; @@ -168,7 +169,7 @@ 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) { @@ -176,7 +177,7 @@ Ya.prototype.processExtension = function (ext) { 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 @@ -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) { @@ -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)); }); } @@ -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(); \ No newline at end of file diff --git a/settings/scss-settings.js b/settings/scss-settings.js index 6c77f35..e16809b 100644 --- a/settings/scss-settings.js +++ b/settings/scss-settings.js @@ -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' - }] - } -}; \ No newline at end of file +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; \ No newline at end of file