-
Notifications
You must be signed in to change notification settings - Fork 46
/
config.js
88 lines (77 loc) · 2.08 KB
/
config.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
'use strict';
var path = require('path')
, fs = require('fs')
module.exports = {
getConfig: getConfig
, createConfig: createConfig
, updateWordpressVersion: updateWordpressVersion
}
var home = process.env.HOME || process.env.USERPROFILE
, configDirectory = path.join(home, '.yeoman-wordpress')
, configPath = path.join(configDirectory, 'config.json')
, defaults = {
authorName: ''
, authorURI: ''
, themeUrl: 'https://github.com/automattic/_s'
, latestVersion: '3.6'
}
/**
* Read the config file
* And trigger the callback function with errors and
* datas as parameters
*/
function getConfig(cb) {
try {
fs.readFile(configPath, 'utf8', function(err, data) {
if (err) {
cb(true, defaults)
return
}
cb(false, JSON.parse(data))
})
}
catch(e) {
cb(true, defaults)
}
}
/**
* Create the config file
*
* @param object values Values to write in the config file
* @param function cb Callback function
*/
function createConfig(values, cb) {
var configValues = {
authorName: values.authorName || defaults.authorName
, authorURI: values.authorURI || defaults.authorURI
, themeUrl: values.themeUrl || defaults.themeUrl
, latestVersion: values.latestVersion || defaults.latestVersion
}
var configData = ['{\n\t'
, '"authorName": "'+configValues.authorName+'",\n\t'
, '"authorURI": "'+configValues.authorURI+'",\n\t'
, '"themeUrl": "'+configValues.themeUrl+'",\n\t'
, '"latestVersion": "'+configValues.latestVersion+'"\n'
, '}'
].join('')
fs.mkdir(configDirectory, '0777', function() {
fs.writeFile(configPath, configData, 'utf8', cb)
})
}
/**
* Update the config file to bump
* the Wordpress version
*
* @param string version Wordpress version
*/
function updateWordpressVersion(version) {
getConfig(function(err, values) {
var newValues = {
authorName: values.authorName
, authorURI: values.authorURI
, themeUrl: values.themeUrl
, latestVersion: version
}
createConfig(newValues)
})
}