-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.coffee
89 lines (71 loc) · 2.63 KB
/
index.coffee
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
async = require 'async'
path = require 'path'
fs = require 'fs'
_ = require 'underscore'
findCore = (pathToConfig) ->
return pathToConfig if fs.existsSync "#{pathToConfig}/wp-includes"
return "#{pathToConfig}/src" if fs.existsSync "#{pathToConfig}/src/wp-includes"
return false unless fs.existsSync "#{pathToConfig}/index.php"
indexFile = fs.readFileSync "#{pathToConfig}/index.php"
match = indexFile.toString().match /('|")(.*)\/wp-blog-header\.php/
path.join pathToConfig, match.pop?()
module.exports = (Impromptu, register, wp) ->
register 'isWP',
update: (done) ->
wp.root (err, root) ->
done err, !!root
register 'root',
update: (done) ->
paths = []
directory = path.resolve()
until path.resolve(directory) is '/'
paths.push directory
directory = path.dirname directory
async.detect paths, (path, cb) ->
fs.exists "#{path}/wp-config.php", (wpDirectory) ->
cb wpDirectory
, (result) ->
done null, result
register 'version',
update: (done) ->
wp.root (err, wpRoot) ->
return done err, false unless wpRoot
versionFile = path.join findCore(wpRoot), 'wp-includes/version.php'
fs.readFile versionFile, (err, data) ->
version = data.toString().match /\$wp_version = '([^']+)';/;
done err, version.pop?()
register 'majorVersion',
update: (done) ->
wp.version (err, version) ->
return done err, false unless version
major = version.match(/^[0-9\.]+/).pop?()
done err, major
register 'tablePrefix',
update: (done) ->
wp.root (err, wpRoot) ->
return done err, false unless wpRoot
fs.readFile "#{wpRoot}/wp-config.php", (err, data) ->
match = data.toString().match /\$table_prefix = ('|")?(.+)\1;/
return done err, match.pop?()
# Usage
#
# section 'wp:WP_DEBUG',
# content: wp._configConstants
# when: wp.isWP
# format: (constants) ->
# constants.WP_DEBUG
register '_configConstants',
update: (done) ->
wp.root (err, wpRoot) ->
return done err, false unless wpRoot
fs.readFile "#{wpRoot}/wp-config.php", (err, data) ->
lines = data.toString().split('\n')
async.map lines, (line, cb) ->
matches = line.match /^define\(\s*('|")([^'"]+)\1,\s*('|")?([^'"]*)\1?\s*\);/
return cb null, false unless matches
cb null, [matches[2], matches[4]]
, (err, matches) ->
async.filter matches, (match, cb) ->
cb !!match
, (keyValMatches) ->
done null, _.object keyValMatches