Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Commit

Permalink
add support for non cwd project dirs and refactor to use node-sass
Browse files Browse the repository at this point in the history
  • Loading branch information
richiksc committed May 30, 2018
1 parent 2595aab commit ff0d47d
Show file tree
Hide file tree
Showing 4 changed files with 1,276 additions and 100 deletions.
7 changes: 4 additions & 3 deletions bin/rocketsass.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ const yaml = require('read-yaml');

let pathValue = ['./css/scss/'];
let rcOptions = {};
let projectDir;

const rcPath = findUp.sync('.rocketsass.yml');
if (rcPath != null) {
rcOptions = yaml.sync(rcPath);
console.log(pathValue);
pathValue = rcOptions.paths.map((x) => {
if (syspath.isAbsolute(x)) {
return x;
}
return syspath.resolve(syspath.dirname(rcPath), x);
return syspath.relative(process.cwd(), syspath.resolve(syspath.dirname(rcPath), x));
});
console.log(pathValue);
projectDir = syspath.dirname(rcPath);
}

program
Expand All @@ -41,6 +41,7 @@ try {
ignorePrefix: program.ignore || rcOptions.ignore || '_',
style: program.style || rcOptions.style || rocketsass.OutputStyle.NESTED,
silent: program.silent || false,
projectDir,
}, console);
} catch (error) {
console.error(colors.red(error));
Expand Down
29 changes: 24 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const eol = require('os').EOL;
const syspath = require('path');
const { exec } = require('child_process');
const sass = require('node-sass');
const util = require('./src/util');
require('colors');

Expand Down Expand Up @@ -81,6 +81,7 @@ module.exports = {
* @typedef CompileOptions
* @property {string} ignorePrefix - If the filename starts with this, do not compile.
* @property {OutputStyle} style - The default style for the CSS output
* @property {string} projectDir - The project root directory
*/

/**
Expand All @@ -102,26 +103,44 @@ module.exports = {
});

this.getAllConfigs(path, fileList, (configs) => {
if (logCondition) logger.log(`Compiling sass with ${options.style.green} style...`);
if (logCondition) logger.log(`Compiling Sass from ${path} with ${options.style.green} style...`);
configs.forEach((config) => {
// Skip files without compileDest
if (typeof config.compileDest === 'undefined') {
if (logCondition) logger.error(`File '${config.target}' does not include compileDest, skipping...`.red);
return;
}

let destination = config.compileDest;
let destination =
(options.projectDir != null ?
relativePath(syspath.resolve(options.projectDir, config.compileDest))
: config.compileDest);
if (config.relativePath) {
destination = syspath.resolve(syspath.dirname(config.target), config.compileDest);
}

const outputStyle = config.style || defaultStyle;
exec(`sass ${config.target} ${destination} --style ${outputStyle}`, (execErr) => {
if (execErr) throw execErr;
sass.render({
file: config.target,
outFile: destination,
outputStyle,
sourceMap: true,
}, (sassErr, result) => {
if (sassErr) throw sassErr;
const styleClause = (outputStyle === defaultStyle) ? '' : ` with ${outputStyle} style`.green;
if (logCondition) {
logger.log(`${util.fixedLengthString(relativePath(config.target), 40)} ${'->'.cyan} ${relativePath(destination)}${styleClause}`);
}
if (!sassErr) {
fs.writeFile(destination, result.css, (writeErr) => {
if (writeErr) {
if (logCondition) {
logger.error(`Write failed to ${destination}`, writeErr);
}
throw writeErr;
}
});
}
});
});
});
Expand Down
Loading

0 comments on commit ff0d47d

Please sign in to comment.