This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
forked from sourcey/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (164 loc) · 6.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Copyright (c) 2016 Kam Low
*
* @license MIT
**/
var fs = require('fs'),
path = require('path'),
Promise = require('bluebird'),
tmp = require('tmp'),
grunt = require('grunt'),
package = require('./package'),
_ = require('lodash');
// Ensures temporary files are cleaned up on program close, even if errors are encountered.
tmp.setGracefulCleanup();
var defaults = {
silent: false,
port: 4400,
targetDir: path.resolve(process.cwd(), 'public'),
targetFile: 'index.html',
appDir: path.resolve(__dirname, 'app'),
modules: path.resolve(__dirname, 'node_modules'),
scrollmagic: path.resolve(__dirname, 'node_modules/scrollmagic/scrollmagic/minified'),
configFile: path.resolve(__dirname, 'app/lib/config.js'),
cacheDir: tmp.dirSync({ unsafeCleanup: true, prefix: 'spectacle-' }).name
};
function resolveOptions(options) {
var opts = _.extend({}, defaults, options);
// Replace some absolute paths
if (opts.specFile && opts.specFile.indexOf('test/fixtures') === 0)
opts.specFile = path.resolve(__dirname, opts.specFile);
if (opts.logoFile && opts.logoFile.indexOf('test/fixtures') === 0)
opts.logoFile = path.resolve(__dirname, opts.logoFile);
if (opts.faviconFile && opts.faviconFile.indexOf('test/fixtures') === 0)
opts.faviconFile = path.resolve(__dirname, opts.faviconFile);
return opts;
}
/**
* Run Spectacle and configured tasks
**/
module.exports = function (options) {
var opts = resolveOptions(options);
//
//= Load the specification and init configuration
function loadData() {
var specPath = path.resolve(opts.specFile);
delete require.cache[specPath];
return require(path.resolve(opts.appDir + '/lib/preprocessor'))(
options, require(specPath));
}
var config = require(path.resolve(opts.configFile))(grunt, opts, loadData());
//
//= Setup Grunt to do the heavy lifting
grunt.initConfig(_.merge({ pkg: package }, config));
if(opts.silent) {
grunt.log.writeln = function() {}
grunt.log.write = function() {}
grunt.log.header = function() {}
grunt.log.ok = function() {}
}
var cwd = process.cwd(); // change CWD for loadNpmTasks global install
var exists = grunt.file.exists(path.join(path.resolve('node_modules'),
'grunt-contrib-concat',
'package.json'));
if (!exists)
process.chdir(__dirname);
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-compile-handlebars');
grunt.loadNpmTasks('grunt-prettify');
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-curl');
process.chdir(cwd);
grunt.registerTask('predentation', 'Remove indentation from generated <pre> tags.', function() {
var html = fs.readFileSync(opts.cacheDir + '/' + opts.targetFile, 'utf8');
html = html.replace(/<pre.*?><code.*?>([\s\S]*?)<\/code><\/pre>/gmi, function(x, y) {
var lines = x.split('\n'), level = null;
if (lines) {
// Determine the level of indentation
lines.forEach(function(line) {
if (line[0] === '<') return;
var wsp = line.search(/\S/);
level = (level === null || (wsp < line.length && wsp < level)) ? wsp : level;
});
// Remove indentation
var regex = new RegExp('^\\s{' + level + '}');
lines.forEach(function(line, index, lines) {
lines[index] = line.replace(regex, '');
});
}
return lines.join('\n');
});
fs.writeFileSync(opts.cacheDir + '/' + opts.targetFile, html);
});
grunt.registerTask('stylesheets', ['sass:scss', 'concat:css', 'cssmin']);
grunt.registerTask('javascripts', ['concat:js', 'uglify']);
grunt.registerTask('templates', ['clean:html', 'compile-handlebars', 'predentation', 'prettify']);
grunt.registerTask('foundation', ['sass:foundation_scss', 'concat:foundation_css']); // 'concat:foundation_js'
grunt.registerTask('default', ['stylesheets', 'javascripts', 'foundation', 'templates']);
grunt.registerTask('server', ['connect']);
grunt.registerTask('develop', ['server', 'watch']);
// Reload template data when watch files change
grunt.event.on('watch', function(action, filepath) {
// if (filepath == config.specFile)
grunt.config.set('compile-handlebars.compile.templateData', loadData());
});
// Report, etc when all tasks have completed.
var donePromise = new Promise(function(resolve, reject) {
grunt.task.options({
error: function(e) {
if(!opts.silent) {
console.warn('Task error:', e);
}
// TODO: fail here or push on?
reject(e);
},
done: function() {
if(!opts.silent) {
console.log('All tasks complete');
}
resolve();
}
});
});
//
//= Run the shiz
if (opts.startServer) {
grunt.task.run('server');
}
else {
if (!opts.disableCss) {
grunt.task.run(['foundation', 'stylesheets']);
}
if (!opts.disableJs) {
grunt.task.run('javascripts');
}
grunt.task.run('copy:images');
grunt.task.run('copy:scrollmagic');
if (opts.faviconFile) {
if(opts.faviconFile.startsWith('http')){
grunt.task.run('curl:favicon');
}else{
grunt.task.run('copy:favicon');
}
}
if (opts.logoFile) {
if(opts.logoFile.startsWith('http')){
grunt.task.run('curl:logo');
}else{
grunt.task.run('copy:logo');
}
}
grunt.task.run('templates');
if (opts.developmentMode) {
grunt.task.run('develop');
}
}
grunt.task.start();
return donePromise;
};