-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
140 lines (110 loc) · 4.15 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
var fs = require('fs');
var gulp = require('gulp');
var replace = require('gulp-replace'); // search and replace contents of a file
const siteUrl = 'https://www.cssmakeovers.com';
const baseRoot = '../cssmakeovers';
const basePath = `${baseRoot}/deploy`;
// Meta data
const pagesInfoAll = require(`${baseRoot}/src/content/pages-info.json`);
const pagesInfo = pagesInfoAll.filter(info => info.publish === true);
/* ------- COPY IMAGES ------- */
// * the ! marks files to skip
// * base specifies folder levels to omit from the created dest folders. in this case, dest folders begin with /patterns/ and /sites/ instead of /deploy/patterns/ and /deploy/sites/.
// * learn more: https://stackoverflow.com/questions/35845039/how-base-option-affects-gulp-src-gulp-dest
gulp.task('copy-imgs', function() {
gulp.src([
`${basePath}/patterns/**/img/*.*`,
`${basePath}/sites/**/img/*.*`,
`!${basePath}/patterns/**/img/fig-*.*`,
`!${basePath}/sites/**/img/fig-*.*`,
`!${basePath}/patterns/**/img/z_*.*`,
`!${basePath}/sites/**/img/z_*.*`],
{ base: `${basePath}/` })
.pipe(gulp.dest('./'));
});
/* ------- COPY CSS ------- */
// Use this instead if want to move the files into the individual pattern and site directories instead of /css/:
// { base: `${basePath}/css/`}
gulp.task('copy-css', function() {
gulp.src([
`${basePath}/css/standalone.css`,
`${basePath}/css/patterns/**/*.css`,
`${basePath}/css/sites/**/*.css`
],
{ base: `${basePath}/` })
.pipe(gulp.dest('./'));
});
/* ------- COPY PAGES AND REPLACE HTML ------- */
gulp.task('copy-replace-html', function() {
const htmlFilesToCopy = [];
for (const page of pagesInfo) {
htmlFilesToCopy.push(`${basePath}/${page.type}s/${page.folderName}/standalone.html`);
}
gulp.src(htmlFilesToCopy, { base: '../cssmakeovers/deploy/'})
.pipe(replace('<link rel="stylesheet" href="/css/standalone.css">', '<link rel="stylesheet" href="../../css/standalone.css">'))
.pipe(replace('<script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-125025519-2"></script>', ''))
.pipe(replace('<script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","UA-125025519-2");</script>', ''))
.pipe(replace(`
</body>
</html>`, `
</body>
</html>`))
.pipe(gulp.dest('./'));
});
/* ------- CREATE README FILES ------- */
// Sort array of objects based on key
// Modified from https://stackoverflow.com/questions/16648076/sort-array-on-key-value
function sortOn(arr, key) {
var sorted = arr;
sorted.sort(function(a, b){
if (a[key] < b[key]) {
return -1;
} else if (a[key] > b[key]) {
return 1;
}
return 0;
});
return sorted;
}
/**
* Create main README.md for the repo
*/
gulp.task('create-repo-readme', function(cb) {
const intro = fs.readFileSync('./src/readme-intro.md');
const sortedPagesInfo = sortOn(pagesInfo, 'title'); // alphabetize them by title
const markdown = {};
markdown.patterns = [];
markdown.sites = [];
// Create MD list item for each pattern or site
for (const page of sortedPagesInfo) {
markdown[`${page.type}s`].push(`* [${page.title}](${siteUrl}/${page.type}s/${page.folderName}/)`);
}
// Create full MD content
const content = `${intro}
## Patterns
Links to their pages on the site:
${markdown.patterns.join("\n")}
## Sites
Links to their pages on the site:
${markdown.sites.join("\n")}
`;
// Create README file
fs.writeFileSync('./README.md', content, cb);
});
/**
* Create README.md for each pattern and site
*/
gulp.task('create-patterns-sites-readmes', function(cb) {
for (const page of pagesInfo) {
const content = `# CSS Makeovers: ${page.title}
* [View page with explanation](${siteUrl}/${page.type}s/${page.folderName}/)
* [View standalone page](${siteUrl}/${page.type}s/${page.folderName}/standalone.html)
`;
// create README file
fs.writeFileSync(`./${page.type}s/${page.folderName}/README.md`, content, cb);
}
});
// ---------------------
// --- Run the tasks ---
// ---------------------
gulp.task('default', ['copy-imgs', 'copy-css', 'copy-replace-html', 'create-repo-readme', 'create-patterns-sites-readmes']);