This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
gulpfile.js
153 lines (137 loc) · 4.86 KB
/
gulpfile.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
'use strict';
/*--- requires --------------------------------------------------------------*/
var ase = require('ase-utils'),
clean = require('gulp-clean'),
colors = require('./source/colors.js'),
fs = require('fs'),
gulp = require('gulp'),
handlebars = require('handlebars'),
jeditor = require("gulp-json-editor"),
map = require('through2-map'),
rename = require('gulp-rename'),
shell = require('gulp-shell'),
spy = require('through2-spy');
/*--- paths and files ------------------------------------------------------*/
var config = {
ase: './ibm-colors.ase',
partials: './source/templates/partials/*',
templates: './source/templates/*.hbs',
output: './'
};
/*--- build task ------------------------------------------------------------*/
gulp.task('ase', function() {
let aseObj = {
"version": colors.version,
"groups": [],
"colors": []
};
aseObj.groups = colors.palettes.map(function(obj){
const color = obj.name;
const aseGroup = {
name: color
};
const formArray = obj.values.map(function(colors){
const hex = function(hex) {
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [
parseInt(result[1], 16) / 255,
parseInt(result[2], 16) / 255,
parseInt(result[3], 16) / 255
] : null;
}
const formColor = {
"name": color + ' ' + colors.grade,
"model": 'RGB',
"color": hex(colors.value),
"type": "global"
};
return formColor;
});
aseGroup.colors = formArray;
return aseGroup;
});
aseObj["colors"] = [].concat.apply([], aseObj.colors);
fs.writeFileSync(config.output + 'ibm-colors.ase', ase.encode(aseObj));
});
gulp.task('clr', [ 'templates' ], function () {
gulp.src("./ibm-colors.scl")
.pipe(shell([
'chmod u+x ./lib/color-tool',
'./lib/color-tool create-clr ibm-colors.scl ibm-colors.clr',
]))
.pipe(clean())
})
gulp.task('package', () =>
gulp.src("./package.json")
.pipe(jeditor({
'version': colors.version
}))
.pipe(gulp.dest(config.output))
);
gulp.task('partials', () =>
gulp.src(config.partials)
.pipe(spy.obj(chunk =>
// register each file in the partials folder as a handlebars partial,
// using its own path name, to be compiled on demand when referenced
handlebars.registerPartial(chunk.relative, chunk.contents.toString())
))
);
function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
red: parseInt(result[1], 16) / 255,
green: parseInt(result[2], 16) / 255,
blue: parseInt(result[3], 16) / 255,
alpha: 1
} : null;
};
gulp.task('sketchpalette', [ 'templates' ], function () {
gulp.src("./ibm-colors.json")
.pipe(jeditor(function(json) {
let sketchPalette = {
compatibleVersion: "1.4",
pluginVersion: "1.5",
};
sketchPalette.colors = Object.keys(json).map(function (color) { return json[color]; });
sketchPalette.colors = sketchPalette.colors.map(function(color) {
var arr = Object.keys(color).map(function (grade) { return color[grade]; });
return arr
});
sketchPalette.colors = [].concat.apply([], sketchPalette.colors);
sketchPalette.colors = sketchPalette.colors.map(hex => (hexToRgb(hex)));
return sketchPalette;
}))
.pipe(rename('ibm-colors.sketchpalette'))
.pipe(gulp.dest(config.output))
})
gulp.task('templates', ['partials'], () =>
gulp.src(config.templates)
.pipe(map.obj(chunk => {
// compile each handlebars file in the templates folder, then evaluate
// the compiled template with the color definitions as context data,
// and replace the pipeline item with the output buffer
var template = handlebars.compile(chunk.contents.toString());
chunk.contents = new Buffer(template({ colors: colors }));
return chunk;
}))
.pipe(rename(path => {
// a template file of the form AAAA.BBB.hbs produces output file AAAA.BBB
var dot = path.basename.lastIndexOf('.');
path.extname = path.basename.substring(dot);
path.basename = path.basename.substring(0, dot);
}))
.pipe(gulp.dest(config.output))
);
gulp.task('build', [ 'package', 'ase', 'clr', 'sketchpalette' ]);
gulp.task('test', ['build']);
/*--- default task ----------------------------------------------------------*/
gulp.task('default', [ 'build' ]);