-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompile-coffee-deps.js
94 lines (74 loc) · 2.64 KB
/
compile-coffee-deps.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
// This is necessary so we don't have coffeescript files being required
const path = require('path')
const fs = require('fs')
const { execSync } = require('child_process')
const scriptsToCheck = require('../external-scripts.json')
const cwd = path.join(__dirname, '..')
const getModulePackageJson = npmModule =>
path.join(cwd, 'node_modules', npmModule, 'package.json')
const compileCoffeeFilesInsidePackage = npmModule => {
console.log(`Compilling .coffee files inside module ${npmModule}`)
try {
execSync(`yarn coffee -c node_modules/${npmModule}`, {
cwd,
})
} catch (err) {
console.error(
`Error while trying to compile coffee files on package ${npmModule}`,
)
console.error(err)
return false
}
return true
}
function compileSlackAdapter() {
console.log('Compilling .coffee files inside slack adapter')
const packageJsonPath = getModulePackageJson('hubot-slack')
if (!fs.existsSync(packageJsonPath)) {
console.warn(`No package.json file found for hubot slack adapter.`)
console.warn(
`If this package does exists you will have to compile the .coffee files on it by running:`,
)
console.warn(` $ yarn coffeescript -c node_modules/slack-adapter`)
console.warn(
`If you are not using that adapter you can safely ignore this warning and remove this call on:`,
)
console.warn(` tools/compile-coffee-deps.js`)
return
}
const hasCompiled = compileCoffeeFilesInsidePackage('hubot-slack')
// no need to change package.json, since main there does not have an extension.
}
function compileExternalScripts() {
scriptsToCheck.forEach(npmModule => {
const packageJsonPath = getModulePackageJson(npmModule)
if (!fs.existsSync(packageJsonPath)) {
console.warn(
`No package.json file found for hubot script package ${npmModule}.`,
)
console.warn(
`If this package does exists you will have to compile the .coffee files on it by running:`,
)
console.warn(`yarn coffeescript -c node_modules/${npmModule}`)
return
}
const packageJson = require(`${npmModule}/package.json`)
const mainFile = path.parse(packageJson.main)
const ext = path.extname(packageJson.main)
const shouldCompile = mainFile.ext === '.coffee'
if (shouldCompile) {
const hasCompiled = compileCoffeeFilesInsidePackage(npmModule)
if (!hasCompiled) return
packageJson.main = path.format({
...mainFile,
ext: '.js',
base: null,
})
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
}
})
}
;(function run() {
compileSlackAdapter()
compileExternalScripts()
})()