Skip to content

Commit 76ce1ac

Browse files
author
anttisalo
committed
Merge pull request #91 from hannu/fix-socketio-paths
Find sass variables with config parameter instead of magic file. Fix socket.io target paths
2 parents 060d412 + fe9c4b0 commit 76ce1ac

14 files changed

+102
-74
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ When using the build as a subfolder of your application, tune your server to res
156156
will let Angular application to deal with routing itself. However, the static files should be resolved as they are
157157
stored.
158158

159+
**sassVariables** (string, optional) **(Experimental feature)**
160+
161+
Path to the file that contains SASS variables editable in the designer mode.
162+
Note: Designer mode is enabled only when running styleguide's built-in server.
163+
159164
## Demo
160165

161166
Build demo styleguide and start a server

bin/styleguide

+21-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ var styleguide = require(__dirname + '/../lib/styleguide.js'),
1616
configPath,
1717
sourcePath,
1818
sourceFiles,
19-
outputPath;
19+
outputPath,
20+
options;
2021

2122
argv = yargs
2223
.usage('This is how ' + chalk.cyan.bold('YOU') + ' can generate ' +
@@ -41,24 +42,31 @@ outputPath = path.resolve(argv.output);
4142
configPath = argv.config ? path.resolve(argv.config) : undefined;
4243
config = configPath ? require(configPath) : {};
4344

44-
gulp.task('styleguide', function() {
45-
// Resolve overviewPath in relation to config file location
46-
if (config.overviewPath) {
47-
config.overviewPath = path.resolve(path.dirname(argv.config), config.overviewPath);
45+
// Resolve overviewPath and sassVariables files in relation to config file location
46+
if (config.overviewPath) {
47+
config.overviewPath = path.resolve(path.dirname(argv.config), config.overviewPath);
48+
}
49+
if (config.sassVariables) {
50+
config.sassVariables = path.resolve(path.dirname(argv.config), config.sassVariables);
51+
}
52+
options = extend({
53+
socketIo: !!argv.server,
54+
sass: {
55+
loadPath: neat.includePaths
4856
}
49-
var defaultConfig = {
50-
socketIo: !!argv.server,
51-
sass: {
52-
loadPath: neat.includePaths
53-
}
54-
};
57+
}, config);
58+
59+
gulp.task('styleguide', function() {
5560
gulp.src(sourceFiles)
56-
.pipe(styleguide(extend(defaultConfig, config)))
61+
.pipe(styleguide(options))
5762
.pipe(gulp.dest(outputPath));
5863
});
5964

6065
gulp.task('serve', function() {
61-
var serverModule = require('../lib/server')(sourcePath, outputPath),
66+
var serverModule = require('../lib/server')({
67+
rootPath: outputPath,
68+
sassVariables: options.sassVariables
69+
}),
6270
app = serverModule.app,
6371
server = serverModule.server;
6472

demo/source/styleguide_config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"title": "Demo Styleguide",
33
"overviewPath": "overview.md",
4-
"commonClass": ["custom-class-1", "custom-class-2"]
4+
"commonClass": ["custom-class-1", "custom-class-2"],
5+
"sassVariables": "styles/_styleguide_variables.scss"
56
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
$color-turtle: #00cc00;
21
$color-donatello: #841d85;
32
$color-leonardo: #0000ff;
43
$color-michelangelo: #FFA500;
54
$color-raphael: #ff0000;
6-
5+
$color-turtle: #00cc00;
76
$font-primary: "Helvetica Neue", Helvetica, Arial, sans-serif;

gulpfile.js

+32-18
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,39 @@ var gulp = require('gulp'),
1515
distPath = './lib/dist',
1616
fs = require('fs'),
1717
chalk = require('chalk'),
18+
extend = require('node.extend'),
1819
configPath = util.env.config ? util.env.config.replace(/\/$/, '') : null,
1920
outputPath = util.env.output ? util.env.output.replace(/\/$/, '') : '',
2021
sourcePath = util.env.source ? util.env.source.replace(/\/$/, '') : '',
21-
config = configPath ? require(configPath) : {},
22-
socketIo = false;
22+
options = {};
23+
24+
function parseOptions() {
25+
var config = configPath ? require(configPath) : {};
26+
// Resolve overviewPath in relation to config file location
27+
if (config.overviewPath) {
28+
config.overviewPath = path.resolve(path.dirname(configPath), config.overviewPath);
29+
}
30+
if (config.sassVariables) {
31+
config.sassVariables = path.resolve(path.dirname(configPath), config.sassVariables);
32+
}
33+
options = extend({
34+
sass: {
35+
loadPath: neat.includePaths
36+
},
37+
socketIo: false
38+
}, config);
39+
}
40+
41+
parseOptions();
2342

2443
/* Tasks for development */
2544
gulp.task('serve', function() {
26-
var serverModule = require('./lib/server')(sourcePath, outputPath),
45+
// Since we are running our own server we can enable socketIO
46+
options.socketIo = true;
47+
var serverModule = require('./lib/server')({
48+
rootPath: outputPath,
49+
sassVariables: options.sassVariables
50+
}),
2751
app = serverModule.app,
2852
server = serverModule.server;
2953

@@ -52,20 +76,8 @@ gulp.task('styleguide', function() {
5276
process.exit(1)
5377
return 1;
5478
}
55-
// Resolve overviewPath in relation to config file location
56-
var overviewPath;
57-
if (config.overviewPath) {
58-
overviewPath = path.resolve(path.dirname(configPath), config.overviewPath);
59-
}
6079
return gulp.src([sourcePath + '/**/*.scss'])
61-
.pipe(styleguide({
62-
extraHead: config.extraHead,
63-
overviewPath: overviewPath,
64-
socketIo: socketIo,
65-
sass: {
66-
loadPath: neat.includePaths
67-
}
68-
}))
80+
.pipe(styleguide(options))
6981
.pipe(gulp.dest(outputPath));
7082
});
7183

@@ -110,7 +122,10 @@ gulp.task('demo', function() {
110122
configPath = __dirname + '/demo/source/styleguide_config.json';
111123
outputPath = __dirname + '/demo/output';
112124
sourcePath = __dirname + '/demo/source';
113-
return runSequence('styleguide', 'serve');
125+
// We need to re-parse options since configPath has changed
126+
parseOptions();
127+
// Run serve first so socketIO options is enabled when building styleguide
128+
return runSequence('serve', 'styleguide');
114129
});
115130

116131
gulp.task('html', function() {
@@ -125,7 +140,6 @@ gulp.task('assets', function() {
125140

126141
gulp.task('watch', [], function() {
127142
// Do intial full build and create styleguide
128-
socketIo = true;
129143
runSequence(['serve', 'build'], 'styleguide');
130144

131145
gulp.watch('lib/app/sass/**/*.scss', function() {

lib/app/js/directives/design.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
angular.module('sgApp')
4-
.directive('sgDesign', function($rootScope) {
4+
.directive('sgDesign', function($rootScope, Variables) {
55
return {
66
replace: true,
77
restrict: 'A',
@@ -11,6 +11,7 @@ angular.module('sgApp')
1111
templateUrl: 'views/partials/design.html',
1212
link: function(scope) {
1313
scope.onChange = function(key, value) {
14+
Variables.setValue('$' + key, value);
1415
$rootScope.config.settings[key] = value;
1516
};
1617

lib/app/js/services/Variables.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
var Variables = function() {
66

7-
var settingsUrl = '../../sass/_styleguide_variables.scss',
8-
socket,
7+
var socket,
98
values = {};
109

1110
this.getValue = function(valueName) {
@@ -63,7 +62,6 @@
6362
socket.on('variables saved to server', function(data) {
6463
console.log('EVENT: variables saved to server');
6564
console.log(data);
66-
console.log(that);
6765
});
6866
}
6967

lib/io.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
var fs = require('fs');
22

3-
module.exports = function(io, sourcePath, outputPath) {
3+
module.exports = function(io, options) {
44

55
var loadVariables, saveVariables;
66

7+
if (!fs.existsSync(options.sassVariables)) {
8+
console.error('Could not find SASS variables file', options.sassVariables);
9+
return;
10+
}
11+
712
loadVariables = function(socket) {
813
return function() {
9-
fs.readFile(sourcePath + '/styles/_styleguide_variables.scss', {encoding: 'utf8'}, function(err, data) {
14+
fs.readFile(options.sassVariables, {encoding: 'utf8'}, function(err, data) {
1015
var line, splitData, values = {};
1116
if (err) {
1217
return console.error(err);
@@ -40,10 +45,10 @@ module.exports = function(io, sourcePath, outputPath) {
4045
sassVariableNames.sort();
4146

4247
for (var i = 0;i < sassVariableNames.length;i++) {
43-
lines.push(sassVariableNames[i] + ': ' + sassVariables[sassVariableNames[i]]);
48+
lines.push(sassVariableNames[i] + ': ' + sassVariables[sassVariableNames[i]] + ';');
4449
}
4550

46-
fs.writeFile(outputPath + '/styles/_styleguide_variables.scss', lines.join('\n'), function(err, data) {
51+
fs.writeFile(options.sassVariables, lines.join('\n'), function(err, data) {
4752
if (err) {
4853
return console.error(err);
4954
}

lib/server.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function(sourcePath, outputPath) {
1+
module.exports = function(options) {
22
var app,
33
cookieParser = require('cookie-parser'),
44
bodyParser = require('body-parser'),
@@ -19,12 +19,11 @@ module.exports = function(sourcePath, outputPath) {
1919
extended: true
2020
}));
2121
app.use(cookieParser());
22-
app.use(express.static(outputPath));
22+
app.use(express.static(options.rootPath));
2323

2424
// Let Angular handle all routing
2525
app.all('/*', function(req, res) {
26-
var rootPath = __dirname + '/..';
27-
res.sendFile(path.resolve(outputPath + '/index.html'));
26+
res.sendFile(path.resolve(options.rootPath + '/index.html'));
2827
});
2928

3029
// catch 404 and forward to error handler
@@ -37,7 +36,7 @@ module.exports = function(sourcePath, outputPath) {
3736
// Setup socket.io
3837
server = require('http').Server(app);
3938
io = require('socket.io')(server);
40-
require('./io')(io, sourcePath, outputPath);
39+
require('./io')(io, options);
4140

4241
return {
4342
app: app,

lib/styleguide.js

+5-12
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,20 @@ module.exports = function(opt) {
3333
if (!opt) opt = {};
3434
opt = {
3535
title: opt.title || 'Styleguide Generator',
36-
outputPath: opt.outputPath,
3736
sass: opt.sass || {},
3837
overviewPath: opt.overviewPath || __dirname + '/overview.md',
3938
extraHead: opt.extraHead || '',
4039
appRoot: opt.appRoot || '',
4140
commonClass: opt.commonClass || '',
42-
socketIo: opt.socketIo
41+
socketIo: opt.socketIo,
42+
sassVariables: opt.sassVariables || ''
4343
};
4444

4545
if (typeof opt.extraHead === 'object') opt.extraHead = opt.extraHead.join('\n');
4646
if (!opt.kssOpt) opt.kssOpt = {};
4747

4848
// Files object acts as our buffer
49-
var filesBuffer = {},
50-
settingsStr = '',
51-
settingsRe = new RegExp('_styleguide_variables.scss');
49+
var filesBuffer = {};
5250

5351
// A stream through which each file will pass
5452
// We have 4 different streams that we have to wait until the whole process is completed
@@ -72,11 +70,6 @@ module.exports = function(opt) {
7270
return console.error('Styleguide does not support streams!');
7371
}
7472

75-
if (settingsRe.test(file.path)) {
76-
console.log('_styleguide_variables.scss found! Generating settings.');
77-
settingsStr = file.contents.toString();
78-
}
79-
8073
filesBuffer[file.path] = file.contents.toString('utf8');
8174

8275
// Make sure file goes through the next gulp plugin
@@ -105,8 +98,8 @@ module.exports = function(opt) {
10598
delete styleguide.config.outputPath
10699

107100
// If settings file is found, generate settings object
108-
if (settingsStr) {
109-
styleguide.config.settings = parseSettings(settingsStr);
101+
if (opt.sassVariables) {
102+
styleguide.config.settings = parseSettings(fs.readFileSync(opt.sassVariables));
110103
}
111104

112105
// Create JSON containing KSS data

test/project/source/styleguide_config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"<!-- extraHead comment -->"
44
],
55
"overviewPath": "overview.md",
6-
"commonClass": ["custom-class-1", "custom-class-2"]
6+
"commonClass": ["custom-class-1", "custom-class-2"],
7+
"sassVariables": "styles/_styleguide_variables.scss"
78
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
$color-turtle: #00cc00;
2-
$color-donatello: #841d85;
3-
$color-leonardo: #0000ff;
4-
$color-michelangelo: #FFA500;
5-
$color-raphael: #ff0000;
6-
7-
$font-primary: "Helvetica Neue", Helvetica, Arial, sans-serif;
1+
$color-red: #ff0000;
2+
$color-green: #00ff00;
3+
$color-blue: #0000ff;

test/project/source/styles/style.scss

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
// This section demonstrates how to present your color variables using
66
// styleguide
77
//
8-
// $color-turtle - Heroes in a half-shell
9-
// $color-leonardo - Leads
10-
// $color-donatello - Does machines
11-
// $color-raphael - Cool but crude
12-
// $color-michelangelo - Party dude
8+
// $color-red - Red test color
9+
// $color-green - Green test color
10+
// $color-blue - Blue test color
1311
//
1412
// Markup:
1513
// <div class="styleguide-color" style="background: {$modifiers};"></div>
@@ -90,10 +88,10 @@ small, .font_small {font-size: 0.707em;}
9088
border-radius: 5px;
9189

9290
&.green {
93-
background: $color-turtle;
91+
background: $color-green;
9492
}
9593
&.red {
96-
background: $color-raphael;
94+
background: $color-red;
9795
}
9896
&:hover {
9997
background: yellow;

test/structure.js

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var gulp = require('gulp'),
1515
'<script src="your/custom/script.js"></script>'
1616
],
1717
commonClass: ['custom-class-1', 'custom-class-2'],
18+
sassVariables: './test/project/source/styles/_styleguide_variables.scss',
1819
sass: {
1920
// Options passed to gulp-ruby-sass
2021
}
@@ -157,6 +158,15 @@ describe('styleguide.json', function() {
157158
jsonData.config.commonClass.should.eql(['custom-class-1', 'custom-class-2']);
158159
});
159160

161+
it('should contain all SASS variables from defined file', function() {
162+
var sassData = {
163+
'color-red': '#ff0000',
164+
'color-green': '#00ff00',
165+
'color-blue': '#0000ff'
166+
}
167+
jsonData.config.settings.should.eql(sassData);
168+
})
169+
160170
it('should not reveal outputPath', function() {
161171
should.not.exist(jsonData.config.outputPath);
162172
});

0 commit comments

Comments
 (0)