-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
domain.js
137 lines (115 loc) · 3.44 KB
/
domain.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
var cpExec = require('child_process').exec
var path = require('path')
var fs = require('fs')
var fileExists = function (p) {
return new Promise(function (resolve, reject) {
fs.exists(p, function (e) {
resolve(e)
})
})
}
function exec (cmd) {
return new Promise(function (resolve, reject) {
cpExec(cmd, function (err, stdout) {
if (err) {
reject(err)
} else {
resolve(stdout)
}
})
})
}
var getStandard = (function () {
var standardVersions = {}
var projectStandardVersions = {}
function projectStandardExists (projectRoot) {
if (projectStandardVersions[projectRoot]) {
return Promise.resolve(true)
} else {
return fileExists(path.join(projectRoot, 'node_modules', 'standard', 'package.json'))
}
}
function getProjectStandardVersion (projectRoot) {
return require(path.join(projectRoot, 'node_modules', 'standard', 'package.json')).version
}
var getGlobalStandard = (function () {
var globalStandardVersion = null
return function () {
var p
if (!globalStandardVersion) {
// Yes, it's a hack. Please send a PR if you have a better way.
p = exec('npm root -g')
.then(function (stdout) {
var globalStandardPath = path.join(stdout.trim(), 'standard')
globalStandardVersion = require(path.join(globalStandardPath, 'package.json')).version
if (!standardVersions[globalStandardVersion]) {
standardVersions[globalStandardVersion] = require(globalStandardPath)
}
return standardVersions[globalStandardVersion]
})
} else {
p = Promise.resolve(standardVersions[globalStandardVersion])
}
return p.then(function (std) {
return {
engine: std,
version: globalStandardVersion
}
})
}
})()
function getProjectStandard (projectRoot) {
return projectStandardExists(projectRoot)
.then(function (projectHasStandard) {
if (!projectHasStandard) {
return getGlobalStandard()
}
if (!projectStandardVersions[projectRoot]) {
projectStandardVersions[projectRoot] = getProjectStandardVersion(projectRoot)
}
var projectStandardVersion = projectStandardVersions[projectRoot]
if (!standardVersions[projectStandardVersion]) {
standardVersions[projectStandardVersion] = require(path.join(projectRoot, 'node_modules', 'standard'))
}
return {
engine: standardVersions[projectStandardVersion],
version: projectStandardVersion
}
})
}
return function (projectRoot) {
if (projectRoot) {
return getProjectStandard(projectRoot)
} else {
return getGlobalStandard()
}
}
})()
function lintText (args, cb) {
var projectRoot = args[0]
var textContent = args[1]
getStandard(projectRoot)
.then(function (standard) {
console.log(standard)
standard.engine.lintText(textContent, {}, function (error, result) {
if (error) {
cb(error)
} else {
result.version = standard.version
cb(null, result)
}
})
})
.catch(function (e) {
console.log(e)
cb(e)
})
}
function init (domainManager) {
var DOMAIN_NAME = 'brackets-standard'
if (!domainManager.hasDomain(DOMAIN_NAME)) {
domainManager.registerDomain(DOMAIN_NAME, {major: 0, minor: 1})
}
domainManager.registerCommand(DOMAIN_NAME, 'lintText', lintText, true)
}
exports.init = init