-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
162 lines (128 loc) · 3.75 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
154
155
156
157
158
159
160
161
162
'use strict';
// Import required modules
const { dest, parallel, series, src, watch } = require('gulp');
const del = require('del');
const fs = require('fs');
const path = require('path');
const postcss = require('gulp-postcss');
const prompt = require('prompt-sync')({ sigint: true });
const rename = require('gulp-rename');
const sass = require('gulp-sass')(require('sass'));
const importJSON = require(path.join(__dirname, 'lib', 'importJSON'));
const dbModule = (name) => path.join(__dirname, 'utility/db', name);
const cleanDb = require(dbModule('cleanDb'));
const initDb = require(dbModule('initDb'));
const insertTestDataModule = require(dbModule('insertTestData'));
// Set src and destination paths for css compilation
const cssPaths = {
src: 'src/stylesheets/main.scss',
dest: 'public/css'
};
// Compile and optimise css from sass file
function compileStyles() {
let cssnanoOptions = {
normalizeWhitespace: false
};
if (process.env.NODE_ENV === 'production')
cssnanoOptions = {};
const plugins = [
require('autoprefixer'),
require('cssnano')({
preset: [ 'default', cssnanoOptions ]
}),
require('postcss-sort-media-queries')
];
return src(cssPaths.src)
.pipe(sass().on('error', sass.logError))
.pipe(postcss(plugins))
.pipe(dest(cssPaths.dest));
}
// Clean css destination directory
function cleanStyles() {
return del([ cssPaths.dest ]);
}
// Clean config dir of all non-sample files
function cleanConfig() {
const configFiles =
fs.readdirSync(path.join(__dirname, 'config'))
.filter(file => !file.endsWith('.sample.json'))
.map(file => `./config/${file}`);
return del(configFiles);
}
// Copy all sample files to their non-sample counterpart
function copyConfig() {
return src('config/*.sample.json')
.pipe(rename(path => {
path.basename = path.basename.split('.sample')[0];
}))
.pipe(dest('config/'));
}
// Allow user to edit config files
function setConfig(cb) {
const writeConfig = (file, contents) => {
console.log(`Writing config to ${file}.json`);
fs.writeFileSync(
path.join(__dirname, 'config', `${file}.json`),
JSON.stringify(contents)
);
};
const configFiles =
fs.readdirSync(path.join(__dirname, 'config'))
.filter(file => !file.endsWith('.sample.json'))
.map(file => file.split('.json')[0]);
console.log('\nEditing config files');
console.log('\nWhen prompted for a new value, press enter to keep ' +
'the existing one');
for (const file of configFiles) {
const contents = importJSON(file);
console.log(`\nConsidering ${file}.json`);
console.log('Current contents:');
console.log(contents);
const answer =
prompt('Would you like to edit the config? (y/N) ');
if (answer !== 'y')
continue;
for (const [ k, v ] of Object.entries(contents)) {
let value =
prompt(` - ${k} (${v}): `).trim();
if (value.length === 0)
continue;
switch (typeof v) {
case 'number':
value = Number(value);
break;
case 'boolean':
value = value === 'true';
break;
}
contents[k] = value;
}
writeConfig(file, contents);
}
cb();
}
async function insertTestData() {
await insertTestDataModule(process.env.CUSTOM_DOMAIN);
}
// Task to build stylesheet from start to finish
exports.styles = series(cleanStyles, compileStyles);
// Task to watch for changes in sass files, then compile on changes
exports.watchStyles = () => {
watch('src/stylesheets/**/*.scss', exports.styles);
};
// Create tables and relationships in database
exports.dbInit = initDb;
// Clean db
exports.dbClean = cleanDb;
// Clean all data and insert test data into database
exports.dbTestData = series(initDb, cleanDb, insertTestData);
// Build stylesheet, and generate config files
exports.default =
parallel(
exports.styles,
series(
cleanConfig,
copyConfig,
setConfig
)
);