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

Commit

Permalink
Increased package stability
Browse files Browse the repository at this point in the history
  • Loading branch information
stevezhu committed Dec 18, 2014
1 parent 59b30a7 commit 5825700
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 83 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.build*
.sass/
sass_options.json
8 changes: 8 additions & 0 deletions .npm/plugin/compileSass/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
summary: "Meteor package for using sass or scss stylesheets.",
version: "1.3.5",
version: "1.3.6",
git: "https://github.com/stevezhu/meteor-sass.git"
});

Expand All @@ -14,7 +14,8 @@ Package.registerBuildPlugin({
npmDependencies: {
'node-sass': '0.9.6',
'lodash': '2.4.1',
'minimist': '1.1.0'
'minimist': '1.1.0',
'mkdirp': '0.5.0'
}
});

Expand Down
89 changes: 35 additions & 54 deletions plugin/compile-sass-utils.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,73 @@
var _ = Npm.require('lodash'),
path = Npm.require('path'),
fs = Npm.require('fs'),
mkdirp = Npm.require('mkdirp'),
files = Npm.require('./files.js');

var args = Npm.require('minimist')(process.argv.slice(2))._,
command = _.first(args);

CompileSassUtils = {
_: _,
path: path,
isPublishPackage: command === 'publish',
isTestPackages: command === 'test-packages',
debug: function(/* arguments */) {
debug: function(/* arguments */) { // CORRECT
if (process.env.DEBUG_SASS === 'true') {
console.log.apply(null, arguments);
}
},
isInPackage: function(compileStep) {
// also returns true if isTestingPackage
isInPackage: function(compileStep) { // CORRECT
if (!_.has(compileStep, 'isInPackage')) {
compileStep.isInPackage = !_.isNull(compileStep.packageName);
}
return compileStep.isInPackage;
},
isTestingPackage: function(compileStep) { // CORRECT
if (!_.has(compileStep, 'isTestingPackage')) {
var packageName = compileStep.packageName;
compileStep.isTestingPackage = !_.isNull(packageName) && packageName.substring(0, 11) === 'local-test:';
}
return compileStep.isTestingPackage;
},
updateCompileStepPaths: function(compileStep) {
var isInPackage = this.isInPackage(compileStep);
this.debug(isInPackage ? 'THIS IS IN A PACKAGE' : 'THIS IS IN AN APP');

compileStep.appDir = this.isTestPackages || this.isPublishPackage ? files.findPackageDir(compileStep._fullInputPath) : files.findAppDir(process.cwd());
compileStep.rootDir = this.isTestPackages ? path.join(process.cwd(), args[1]) : files.findAppDir();

// uses cwd for findAppDir because packages might not be in the app dir
compileStep.rootDir = isInPackage ? files.findPackageDir(compileStep._fullInputPath) : files.findAppDir(process.cwd());
this.debug('rootDir', compileStep.rootDir);

// relative to the rootDir
compileStep.relativePath = path.relative(compileStep.rootDir, compileStep._fullInputPath);
compileStep.relativeDir = path.dirname(compileStep.relativePath);
if (this.isInPackage(compileStep)) {
compileStep.packageDir = files.findPackageDir(compileStep._fullInputPath);
// relative to the packageDir
compileStep.relativePath = path.relative(compileStep.packageDir, compileStep._fullInputPath);
compileStep.relativeDir = path.dirname(compileStep.relativePath);
}
},
PACKAGE_LINKS_DIR: '.sass/',
/**
* For creating hard links to packages because node-sass/libsass includePaths don't work when there are colons in the name
*/
createPackageLink: function(compileStep) {
if (!this.isTestPackages && !this.isPublishPackage) {
var dir = path.join(compileStep.appDir, path.join(this.PACKAGE_LINKS_DIR, 'packages/'));
this.mkdirp(dir);
createPackageLink: function(compileStep) { // CORRECT
if (!this.isTestingPackage(compileStep)
&& path.relative(compileStep.packageDir, compileStep.rootDir) !== '') {
var dir = path.join(compileStep.rootDir, this.PACKAGE_LINKS_DIR, 'packages/');
var packageName = compileStep.packageName.split(':');

// length equals 2
// packageName is in the form `author:packageName`
if (packageName.length === 2) {
dir = path.join(dir, packageName[0]);
this.mkdirp(dir);
packageName = packageName[1];
} else {
}
// length equals 1
// packageName is in the form `packageName`
else {
packageName = packageName[0];
}

mkdirp.sync(dir);

try {
fs.linkSync(compileStep.rootDir, path.join(dir, packageName));
fs.linkSync(compileStep.packageDir, path.join(dir, packageName));
} catch (err) {
// if it already exists just don't create it (default behavior)
// and don't throw the error either
if (err.code !== 'EEXIST') throw err;
}
}
Expand Down Expand Up @@ -104,37 +117,5 @@ CompileSassUtils = {
process.chdir(dir);
func();
process.chdir(cwd);
},
/**
* Make directory recursively
* @param {String} p path
*/
mkdirp: function(p) {
var beg = 0, end = p.length;
var paths = [];

// remove the last /
// so that the last path doesn't run twice
if (_.last(p) === path.sep) {
end = p.length - 1;
}
// for first /
if (p.indexOf(path.sep) === 0) {
paths.push(path.sep);
beg = 1;
}
p = p.substring(beg, end);

_.each(p.split(path.sep), function(dir) {
if (paths.length > 0) {
dir = path.join(_.last(paths), dir);
}
paths.push(dir);
try {
fs.mkdirSync(dir);
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}
});
}
};
43 changes: 23 additions & 20 deletions plugin/compile-sass.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ var utils = CompileSassUtils;
var OPTIONS_FILENAME = 'sass_options.json';
var INCLUDE_PATHS_FILENAME = 'sass_include_paths.json';

var generateHandler = function(exitEarly, handler) {
var generateHandler = function(shouldProcess, handler) {
return function(compileStep) {
if (exitEarly(compileStep)) {
return;
}
utils.updateCompileStepPaths(compileStep);
utils.debug('compileStep', compileStep);
if (shouldProcess(compileStep)) {
utils.updateCompileStepPaths(compileStep);
utils.debug('compileStep', compileStep);

handler.call(this, compileStep);
handler.call(this, compileStep);
}
};
};

Expand All @@ -33,15 +32,13 @@ Plugin.registerSourceHandlers = function(extensions, options, handler) {
*/
Plugin.registerSourceHandlers(['sass', 'scss'], {archMatching: 'web'}, generateHandler(function(compileStep) {
// if the filename begins with '_', it is a partial
// therefore, ignore it
return path.basename(compileStep.inputPath)[0] === '_';
return path.basename(compileStep.inputPath)[0] !== '_';
}, function(compileStep) {
utils.runInDir(compileStep.appDir, function() {
utils.runInDir(compileStep.rootDir, function() {
var future = new Future();

// ==== LOAD OPTIONS ====
var optionsPath = path.join(compileStep.appDir, OPTIONS_FILENAME);
utils.debug('optionsPath', optionsPath);
var optionsPath = path.join(compileStep.rootDir, OPTIONS_FILENAME);
var options = utils.readJSON(optionsPath, compileStep, {});

// ==== SET OPTIONS ====
Expand All @@ -65,13 +62,15 @@ Plugin.registerSourceHandlers(['sass', 'scss'], {archMatching: 'web'}, generateH
utils.debug('includePaths', options.includePaths);

options.file = compileStep._fullInputPath;

options.success = function(css, sourceMap) {
var result = {
css: css,
sourceMap: sourceMap
};
future.return(result);
};

// parses the error string
options.error = function(err) {
var re = /^(.*\.(?:scss|sass)):([0-9]+?): (.*)$/;
Expand Down Expand Up @@ -109,40 +108,44 @@ Plugin.registerSourceHandlers(['sass', 'scss'], {archMatching: 'web'}, generateH
}));

/**
* sass_include_paths.json SHOULD BE PUT IN PACKAGES ONLY EITHER FOR TESTING OR FOR A SASS LIBRARY
* sass_include_paths.json SHOULD BE PUT IN PACKAGES FOR A SASS LIBRARY (ex. Bourbon) OR WHEN TESTING A PACKAGE
*
* - When adding sass_include_paths.json to a package, it must be added before any sass/scss.
* - Paths in sass_include_paths.json should be relative to sass_include_paths.json
* - sass_options.json is only stored in packages for testing purposes
*
* 1. Merge `includePaths` from each 'sass_include_paths.json' added using `api.addFiles` to 'sass_options.json'
* 2. Store 'sass_options.json' in the app root dir
* 2. Store 'sass_options.json' in the root dir
*/
Plugin.registerSourceHandler(INCLUDE_PATHS_FILENAME, {archMatching: 'os'}, generateHandler(function(compileStep) {
return !utils.isInPackage(compileStep); // don't process while publishing this package
// only process if file is in a package
// or if testing a package in which case the compileStep.packageName
// would be `local-test:packageName`
return utils.isInPackage(compileStep);
}, function(compileStep) {
utils.createPackageLink(compileStep);

// ==== OPTIONS ====

var optionsPath = path.join(compileStep.appDir, OPTIONS_FILENAME);
var optionsPath = path.join(compileStep.rootDir, OPTIONS_FILENAME);
var options = utils.readJSON(optionsPath, compileStep, {});
_.defaults(options, {
packageIncludePaths: {}
});

// the includePaths loaded from sass_include_paths.json
var includePaths = utils.readJSON(compileStep._fullInputPath, compileStep, []);
includePaths = _.map(includePaths, function(includePath) {
if (utils.isTestPackages || utils.isPublishPackage) {
// should be relative to root dir
var includePathsArray = _.map(includePaths, function(includePath) {
if (utils.isTestingPackage(compileStep)) {
includePath = path.join(compileStep.relativeDir, includePath);
} else {
includePath = path.join(utils.PACKAGE_LINKS_DIR, 'packages/', compileStep.packageName.replace(/:/g, path.sep), compileStep.relativeDir, includePath);
includePath = path.join(utils.PACKAGE_LINKS_DIR, 'packages/', compileStep.packageName.replace(':', path.sep), compileStep.relativeDir, includePath);
}
return includePath;
});

options.packageIncludePaths[compileStep.packageName] = includePaths;
options.packageIncludePaths[compileStep.packageName] = includePathsArray;

// ==== WRITE ====

Expand Down
7 changes: 0 additions & 7 deletions sass_options.json

This file was deleted.

0 comments on commit 5825700

Please sign in to comment.