-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
163 lines (134 loc) · 5.59 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
163
'use strict'
const gulp = require('gulp')
const path = require('path')
const fs = require('fs')
const minimist = require('minimist')
const log = require('fancy-log')
const _ = require('lodash')
const ForwardReference = require('undertaker-forward-reference')
// local .env file, overriding any global env variables
let parentEnvPath = path.join('..', '.env')
let envPath = fs.existsSync('.env') ? '.env' : (fs.existsSync(parentEnvPath) ? parentEnvPath : null)
if (envPath) {
let result = require('dotenv').config({ path: envPath })
log.warn(`Loading ENV variables from ${path.join(process.cwd(), envPath)}`)
for (let k in result.parsed) {
process.env[k] = result.parsed[k]
}
}
// development .env file, overriding any global env variables, or repo/plugin specific variables
let devEnv = path.join(__dirname, '.env')
if (fs.existsSync(devEnv)) {
let result = require('dotenv').config({path: devEnv})
log.warn('LOADING DEVELOPMENT ENV VARIABLES FROM ' + devEnv)
for (let k in result.parsed) {
process.env[k] = result.parsed[k]
}
}
// enable forward-referencing tasks, see https://github.com/gulpjs/gulp/issues/1028
gulp.registry(ForwardReference())
// define default config
let defaults = {
// sets up the plugin folder structure
paths: {
// Path to plugin source files - this is where the main plugin entry file is located. Set this to a dot (.) if the
// main plugin file and sake.config.js are in teh same directory. The path is relative to the current working directory.
// Mostly, this is the only path a plugin/repo needs to explicitly set
src: '.',
// where plugin assets are located, relative to `src`
assets: 'assets',
// where plugin CSS/SCSS assets are located, relative to `src`
css: 'assets/css',
// where plugin JS/COFFEE assets are located, relative to `src`
js: 'assets/js',
// where plugin image assets are located, relative to `src`
images: 'assets/img',
// where plugin font assets are located, relative to `src`
fonts: 'assets/fonts',
// the directory where plugin files are copied during the build task, relative to current working directory
build: 'build',
// path to the directory where production (WC and WP.org SVN) repos are cloned, may be an absolute path or relative to current working directory
tmp: '/tmp/sake',
// array of paths that should be excluded from the build
exclude: []
},
// Task-specific settings, set the key to task name and provide any settings as needed. Since sake uses Gulp behind the scenes
// and Gulp prefers code over configuration, there isn't a lot to do here. As you can see, some of these values can be defined
// as environment variables, as this makes more sense - ie whether you want to use browsersync or not is specific tp your local
// dev environment and workflow, not to a particular repo.
tasks: {
makepot: {
reportBugsTo: 'https://woocommerce.com/my-account/marketplace-ticket-form/',
domainPath: 'i18n/languages'
},
watch: {
useBrowserSync: process.env.USE_BROWSERSYNC || false
},
browserSync: {
url: process.env.BROWSERSYNC_URL || 'plugins-skyverge.test'
}
},
// which framework version this plugin uses - valid values: 'v5', 'v4', or pass boolean `false` to indicate a non-frameworked plugin
framework: 'v5',
// which deploy type does this plugin use - either 'wc' or 'wp', defaults to 'wc', specify `null` or `false` for no automated deploy
deploy: 'wc',
// the e-commerce platform this plugin is for, 'wc' or 'edd'
platform: 'wc'
}
// load local configuration
// TODO: allow passing in config file path or config as string (for multi-plugin repos?)
let localConfig = {}
// support supplying a single / parent config file in multi-plugin repos
let parentConfigPath = path.join(process.cwd(), '../sake.config.js')
let found = false
if (fs.existsSync(parentConfigPath)) {
log.warn('Found config file in parent folder')
localConfig = require(parentConfigPath)
found = true
}
// load local, plugin-specific config file
let configFilePath = path.join(process.cwd(), 'sake.config.js')
if (fs.existsSync(configFilePath)) {
localConfig = _.merge(localConfig, require(configFilePath))
found = true
}
if (!found) {
log.warn('Could not find local config file, using default config values.')
}
let config = _.merge(defaults, localConfig)
// parse CLI options
let options = minimist(process.argv.slice(2), {
boolean: ['minify'],
default: {
minify: true,
debug: false
}
})
const sake = require('./lib/sake')(config, options)
sake.initConfig()
let plugins = require('gulp-load-plugins')()
// Attach browsersync as a plugin - not really a plugin, but it helps to
// pass around the browsersync instance between tasks. Unfortunately, we
// always have to load and create an instance of it, because gulp-if does not
// support lazy evaluation yet: https://github.com/robrich/gulp-if/issues/75
plugins.browserSync = require('browser-sync').create()
// load gulp plugins and tasks
require('fs').readdirSync(path.join(__dirname, 'tasks')).forEach((file) => {
require(path.join(__dirname, 'tasks', file))(gulp, plugins, sake)
})
gulp.task('default', gulp.series('compile'))
// show notification on task errors
const notifier = require('node-notifier')
const stripAnsi = require('strip-ansi')
let loggedErrors = []
gulp.on('error', (event) => {
if (loggedErrors.indexOf(event.error) === -1) {
notifier.notify({
title: `Error running task ${event.name}`,
message: stripAnsi(event.error.toString()),
sound: 'Frog'
})
// ensure the same error is only displayed once
loggedErrors.push(event.error)
}
})