import autoprefixer from 'gulp-autoprefixer';
import chalk from 'chalk';
import glob from 'glob';
import gulp from 'gulp';
import nib from 'nib';
import rename from 'gulp-rename';
import size from 'gulp-size';
import sourcemaps from 'gulp-sourcemaps';
import stylus from 'gulp-stylus';



/**
 * Compile stylus source to CSS
 */
gulp.task('styles', function gulpStylus() {

    var path = getStyleImportPath();

    return gulp
        .src('src/stylesheets/*.styl')
        .pipe(sourcemaps.init())
        .pipe(stylus({
            use: nib(),
            import: ['nib', path + '/helpers/*.styl'],
            include: path,
            compress: false
        }))
        .pipe(autoprefixer('last 2 version'))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('src/stylesheets'))
        .pipe(size());
});


/**
 * Compile stylus source to minified CSS in dist/
 */
gulp.task('styles:dist', function gulpStylusMin() {

    var path = getStyleImportPath();

    return gulp
        .src('src/stylesheets/*.styl')
        .pipe(stylus({
            use: nib(),
            import: ['nib', path + '/helpers/*.styl'],
            include: path,
            compress: true
        }))
        .pipe(autoprefixer('last 2 version'))
        .pipe(gulp.dest('dist/stylesheets'))
        .pipe(size());
});



/**
 * [getStyleImportPath description]
 * @return {[type]} [description]
 */
function getStyleImportPath() {
    var dir = glob.sync(process.cwd() + '/jspm_packages/apsis/stylus-helpers*');

    if ( !dir.length ) {
        console.log('');
        console.log(chalk.bold.red('>>>>> You have not installed Apsis stylus helpers. <<<<<<'));
        console.log(chalk.yellow('If you need them, run jspm install apsis:styles.'));
        console.log('');
    }

    return dir;
}

/**
 * [getStyleImportPath description]
 * @return {[type]} [description]
 */
function getStyleImportPath() {
    var packageDir = getPackageDir(require(process.cwd() + '/package.json')),
        dir = glob.sync(process.cwd() + '/' + packageDir + '/apsis/stylus-helpers*');

    if (!dir.length) {
        console.log('');
        console.log(chalk.bold.red('>>>>> You have not installed Apsis stylus helpers. <<<<<<'));
        console.log(chalk.yellow('If you need them, run jspm install apsis:styles.'));
        console.log('');
    }

    return dir;
}



function getPackageDir(pkgInfo) {
    var packageDir = 'jspm_packages';

    if ( !!pkgInfo.jspm ) {
        if ( !!pkgInfo.jspm.directories && !!pkgInfo.jspm.directories.packages ) {
            packageDir = pkgInfo.jspm.directories.packages;
        }
    } else if ( !!pkgInfo.directories && !!pkgInfo.directories.packages ) {
        packageDir = pkgInfo.directories.packages;
    } else {
        throw new Error('Can\t find package.json');
    }

    return packageDir;
}

@@ -143,42 +143,8 @@
 b';%0A
-import rename from 'gulp-rename';%0A
 impo
@@ -1281,486 +1281,8 @@
 %0A%0A%0A%0A
-/**%0A * %5BgetStyleImportPath description%5D%0A * @return %7B%5Btype%5D%7D %5Bdescription%5D%0A */%0Afunction getStyleImportPath() %7B%0A    var dir = glob.sync(process.cwd() + '/jspm_packages/apsis/stylus-helpers*');%0A%0A    if ( !dir.length ) %7B%0A        console.log('');%0A        console.log(chalk.bold.red('%3E%3E%3E%3E%3E You have not installed Apsis stylus helpers. %3C%3C%3C%3C%3C%3C'));%0A        console.log(chalk.yellow('If you need them, run jspm install apsis:styles.'));%0A        console.log('');%0A    %7D%0A%0A    return dir;%0A%7D%0A%0A
 /**%0A
@@ -1536,21 +1536,20 @@
 sis/
-stylus-helper
+ui-component
 s*')

update path to jspm ui-components module