-
Notifications
You must be signed in to change notification settings - Fork 1
/
slushfile.js
152 lines (131 loc) · 5.35 KB
/
slushfile.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
/*
* slush-sample
* https://github.com/siteworxcarlos/slush-atom
*
* Copyright (c) 2016, Carlos
* Licensed under the MIT license.
*/
var gulp = require('gulp'),
install = require('gulp-install'),
conflict = require('gulp-conflict'),
template = require('gulp-template'),
replace = require('gulp-replace'),
insert = require('gulp-insert'),
rename = require('gulp-rename'),
gulpif = require('gulp-if'),
objectMerge = require('object-merge'),
_ = require('underscore.string'),
inquirer = require('inquirer'),
path = require('path'),
config = {},
latestAnswers = null;
(function getConfig() {
try {
config = require(process.cwd() + '/slush-atom-config.json'); // Try to get local-to-project config file
} catch(e) {
// No local config file
}
var globalConfig = require('./slush-atom-config.json'); // Fall back to generic configs
config = objectMerge(globalConfig, config);
})();
gulp.task('refreshAnswers', function(done) {
var prompts = [{
name: 'appName',
message: 'What is the name of your atom?',
validate: function(val) {
if(val && val.length > 0) { return true; }
return 'Please provide a name to use for your atom.';
}
},{
name: 'folder',
message: 'What folder should the atom pattern (mustache) be stored in?',
default: function(answers) {
return '00-'+answers.appName;
}
},{
name: 'orderNumber',
message: 'What order should this atom be in, in that folder? (ex. 00 is first)',
default: '00'
}
];
//Ask
inquirer.prompt(prompts, function (answers) {
latestAnswers = answers;
gulp.start('createFiles'); // NOTE: Will be deprecated in gulp 4.0
done();
});
});
function haveAnswers() {
if(!latestAnswers) {
console.error('Need to run task `refreshAnswers` before creating files');
return false;
}
return true;
}
gulp.task('createPattern', function(done) {
if(!haveAnswers()) { return; }
var templatePath = (config.mustacheFile.slushTemplate ? __dirname : '') + config.mustacheFile.template;
//output mustache file in project directory
return gulp.src(templatePath)
.pipe(template(latestAnswers))
.pipe(rename(function(file) {
file.basename = latestAnswers.orderNumber + '-' + latestAnswers.appName;
}))
.pipe(conflict('./'))
.pipe(gulp.dest(config.mustacheOutputPath + latestAnswers.folder))
.pipe(install());
});
var scssFileTasks = [],
lastScssImportTask = null;
(function () {
for(var i = 0; i < config.scssFiles.length; i++) {
var fileInfo = config.scssFiles[i],
taskName = 'createScssFile-' + i;
createScssFileTask(fileInfo, taskName);
scssFileTasks.push(taskName);
// Since these all are potentially writing to the same file, make each one dependent on the last one completing
// only need to track the name of the last one to trigger the whole chain
taskName = 'importScssFile-' + i;
createScssImportTask(fileInfo, taskName, (i === 0 ? null : ['importScssFile-' + (i - 1)]));
lastScssImportTask = taskName;
}
})();
function createScssFileTask(fileInfo, taskName) {
gulp.task(taskName, function(done) {
if(!haveAnswers()) { return; }
var src = fileInfo.slushTemplate ? __dirname + fileInfo.template : fileInfo.template,
filename = latestAnswers.appName + config.scssNamespaceConnector + fileInfo.name;
// Output folder with scss files in project directory
return gulp.src(src)
.pipe(template(latestAnswers))
.pipe(rename(function(file) {
file.basename = "_" + filename;
}))
.pipe(conflict(config.scssFileOutputPath + latestAnswers.appName))
.pipe(gulp.dest(config.scssFileOutputPath + latestAnswers.appName))
.pipe(install());
});
}
function createScssImportTask(fileInfo, taskName, prevTask) {
// NOTE: Will be deprecated in gulp 4.0 - use series instead
if(prevTask !== null) { gulp.task(taskName, prevTask, task); }
else { gulp.task(taskName, task); }
function task(done) {
if(!haveAnswers()) { return; }
var filename = latestAnswers.appName + config.scssNamespaceConnector + fileInfo.name,
dirName = fileInfo.includedFromFile.substring(0, fileInfo.includedFromFile.lastIndexOf("/")),
replacing = (fileInfo.importReplace ? true : false),
importStr = '@import "' + fileInfo.scssImportPath + latestAnswers.appName + '/' + filename + '";';
// Update the scss file that includes this new file
return gulp.src(fileInfo.includedFromFile)
// Prepend the placeholder text with the new import
.pipe(gulpif(replacing, replace(fileInfo.importReplace,importStr+'\n'+fileInfo.importReplace)))
// Appending the new import
.pipe(gulpif(!replacing, insert.append('\n'+importStr)))
.pipe(gulp.dest(dirName))
.pipe(install());
}
}
// NOTE: Will be deprecated in gulp 4.0, use series and/or parallel instead
gulp.task('createFiles',['createPattern', lastScssImportTask].concat(scssFileTasks));
gulp.task('default', ['refreshAnswers']);