/
pattern-lab--php-twig.js
197 lines (173 loc) · 7.01 KB
/
pattern-lab--php-twig.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
191
192
193
194
195
196
197
'use strict';
const core = require('./core');
const path = require('path');
const yaml = require('js-yaml');
const fs = require('fs');
const inject = require('gulp-inject');
const _ = require('lodash');
const browserSync = require('browser-sync');
const glob = require('glob');
module.exports = (gulp, config, tasks) => {
let plConfig = yaml.safeLoad(fs.readFileSync(config.patternLab.configFile, 'utf8'));
const plRoot = path.join(config.patternLab.configFile, '../..');
const plSource = path.join(plRoot, plConfig.sourceDir);
const plPublic = path.join(plRoot, plConfig.publicDir);
const plMeta = path.join(plSource, '/_meta');
const consolePath = path.join(plRoot, 'core/console');
const watchTriggeredTasks = [];
function plBuild(done, errorShouldExit) {
core.sh(`php ${consolePath} --generate`, errorShouldExit, (err) => {
if (config.browserSync.enabled) {
browserSync.get('server').reload();
}
done(err);
});
}
gulp.task('pl', done => plBuild(done, true));
// Begin `<link>` & `<script>` injecting code.
// Will look for these HTML comments in `plSource/_meta/*.twig:
// `<!-- inject:css -->`
// `<!-- endinject -->`
// `<!-- inject:js -->`
// `<!-- endinject -->`
function injectPl(done) {
let sources = [];
if (config.patternLab.injectFiles) {
sources = sources.concat(config.patternLab.injectFiles);
}
sources = sources.concat(glob.sync(path.normalize(`${config.js.dest}/*.js`)));
sources = sources.concat(glob.sync(path.normalize(`${config.css.dest}/*.css`)));
// We need to make sure our JS deps like jQuery are loaded before ours (`config.js.destName`)
const rearrangedSources = [];
sources.forEach((source) => {
if (path.basename(source) === config.js.destName) {
// add to end
rearrangedSources.push(source);
} else {
// add to beginning
rearrangedSources.unshift(source);
}
});
gulp
.src(['*.twig'], { cwd: plMeta })
.pipe(inject(gulp.src(rearrangedSources, { read: false }), {
relative: false,
addRootSlash: false,
addSuffix: '?cacheBuster={{ cacheBuster }}',
addPrefix: path.join('../..', path.relative(plPublic, process.cwd()))
}))
.pipe(gulp.dest(plMeta))
.on('end', done);
}
gulp.task('inject:pl', injectPl);
const plFullDependencies = ['inject:pl'];
// turns scss files full of variables into json files that PL can iterate on
function scssToJson(done) {
config.patternLab.scssToJson.forEach((pair) => {
const scssVarList = _.filter(fs.readFileSync(pair.src, 'utf8').split('\n'), item => _.startsWith(item, pair.lineStartsWith));
let varsAndValues = _.map(scssVarList, (item) => {
// assuming `item` is `$color-gray: hsl(0, 0%, 50%); // main gray color`
const x = item.split(':');
const y = x[1].split(';');
return {
name: x[0].trim(), // i.e. $color-gray
value: y[0].replace(/!.*/, '').trim(), // i.e. hsl(0, 0%, 50%) after removing `!default`
comment: y[1].replace('//', '').trim() // any inline comment coming after, i.e. `// main gray color`
};
});
if (!pair.allowVarValues) {
varsAndValues = _.filter(varsAndValues, item => !_.startsWith(item.value, '$'));
}
fs.writeFileSync(pair.dest, JSON.stringify({
items: varsAndValues,
meta: {
description: `To add to these items, use Sass variables that start with <code>${pair.lineStartsWith}</code> in <code>${pair.src}</code>`
}
}, null, ' '));
});
done();
}
if (config.patternLab.scssToJson) {
gulp.task('pl:scss-to-json', scssToJson);
plFullDependencies.push('pl:scss-to-json');
gulp.task('watch:pl:scss-to-json', () => {
const files = config.patternLab.scssToJson.map(file => file.src);
gulp.watch(files, scssToJson);
});
tasks.watch.push('watch:pl:scss-to-json');
}
function getTwigNamespaceConfig(workingDir) {
workingDir = workingDir || process.cwd(); // eslint-disable-line no-param-reassign
const twigNamespaceConfig = {};
config.patternLab.twigNamespaces.sets.forEach((namespaceSet) => {
const pathArray = namespaceSet.paths.map((pathBase) => {
const results = glob.sync(path.join(pathBase, '**/*.twig')).map((pathItem) => { // eslint-disable-line arrow-body-style
return path.relative(workingDir, path.dirname(pathItem));
});
results.unshift(path.relative(workingDir, pathBase));
return results;
});
twigNamespaceConfig[namespaceSet.namespace] = {
paths: core.uniqueArray(core.flattenArray(pathArray))
};
});
return twigNamespaceConfig;
}
function addTwigNamespaceConfigToDrupal(done) {
const twigNamespaceConfig = getTwigNamespaceConfig(path.dirname(config.drupal.themeFile));
const drupalThemeFile = yaml.safeLoad(fs.readFileSync(config.drupal.themeFile, 'utf8'));
if (!drupalThemeFile['component-libraries']) {
drupalThemeFile['component-libraries'] = {};
}
Object.assign(drupalThemeFile['component-libraries'], twigNamespaceConfig);
const newThemeFile = yaml.safeDump(drupalThemeFile);
fs.writeFileSync(config.drupal.themeFile, newThemeFile, 'utf8');
done();
}
function addTwigNamespaceConfigToPl(done) {
const twigNamespaceConfig = getTwigNamespaceConfig(plRoot);
plConfig = yaml.safeLoad(fs.readFileSync(config.patternLab.configFile, 'utf8'));
if (!plConfig.plugins) {
Object.assign(plConfig, {
plugins: {
twigNamespaces: { enabled: true, namespaces: {} }
}
});
} else if (!plConfig.plugins.twigNamespaces) {
Object.assign(plConfig.plugins, {
twigNamespaces: { enabled: true, namespaces: {} }
});
} else if (!plConfig.plugins.twigNamespaces.namespaces) {
plConfig.plugins.twigNamespaces.namespaces = {};
}
Object.assign(plConfig.plugins.twigNamespaces.namespaces, twigNamespaceConfig);
const newConfigFile = yaml.safeDump(plConfig);
fs.writeFileSync(config.patternLab.configFile, newConfigFile, 'utf8');
done();
}
if (config.patternLab.twigNamespaces) {
gulp.task('twigNamespaces', (done) => {
addTwigNamespaceConfigToPl(() => {
if (config.patternLab.twigNamespaces.addToDrupalThemeFile) {
addTwigNamespaceConfigToDrupal(done);
}
done();
});
});
plFullDependencies.push('twigNamespaces');
watchTriggeredTasks.push('twigNamespaces');
}
gulp.task('pl:full', gulp.series(plFullDependencies, plBuild));
const watchedExtensions = config.patternLab.watchedExtensions.join(',');
gulp.task('watch:pl', () => {
const plGlob = [path.normalize(`${plSource}/**/*.{${watchedExtensions}}`)];
const src = config.patternLab.extraWatches
? [].concat(plGlob, config.patternLab.extraWatches)
: plGlob;
// plBuild goes last after any deps
watchTriggeredTasks.push(plBuild);
gulp.watch(src, gulp.series(watchTriggeredTasks));
});
tasks.watch.push('watch:pl');
tasks.compile.push('pl:full');
};