-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
96 lines (75 loc) · 2.8 KB
/
loader.js
File metadata and controls
96 lines (75 loc) · 2.8 KB
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
/* globals Npm Plugin buildInjectorJs buildPackageJs */
'use strict'
class Loader {
constructor () {
this.path = Plugin.path
this.fs = Plugin.fs
this.mkdirp = Npm.require('mkdirp')
}
log (message) {
if (process.env.INJECTOR_DEBUG) {
console.log(message)
}
}
error (message) {
if (process.env.INJECTOR_DEBUG) {
console.error(message)
}
}
ensureInjectorPresent () {
// Ensure the webantic:node-injector package is included
const packagesPath = '.meteor/packages'
let currentPackagesManifest = String(this.fs.readFileSync(packagesPath))
const regExp = new RegExp(/^( *)webantic:node-injector/m)
if (!regExp.test(currentPackagesManifest)) {
currentPackagesManifest += '\nwebantic:node-injector\n'
this.fs.writeFileSync(packagesPath, currentPackagesManifest)
}
}
process () {
this.log('++++ WEBANTIC:LOADER ++++')
const mapper = Npm.require('@webantic/dependency-mapper').default
let npmDeps = mapper(process.cwd())
const thisAppName = Object.keys(npmDeps)[0]
npmDeps = npmDeps[thisAppName]
const meteorDeps = npmDeps ? npmDeps.allMeteorDependencies : {}
// If there are any Meteor deps
if (Object.keys(meteorDeps).length) {
this.ensureInjectorPresent(process.cwd())
// If the mapper failed, thisAppName will be missing or === "default"
if (thisAppName && thisAppName !== 'default') {
const modulesWithMeteorDeps = []
const dependencies = npmDeps.dependencies
for (const key in dependencies) {
if (Object.keys(dependencies[key].allMeteorDependencies).length) {
modulesWithMeteorDeps.push(key)
}
}
const packageJsPath = 'packages/node-injector/package.js'
const injectorJsPath = 'packages/node-injector/injector.js'
let currentPackageJs, currentInjectorJs
try {
currentPackageJs = String(this.fs.readFileSync(packageJsPath))
currentInjectorJs = String(this.fs.readFileSync(injectorJsPath))
} catch (ex) {
this.error(ex.message)
}
const packageJs = buildPackageJs(meteorDeps)
const injectorJs = buildInjectorJs(meteorDeps, modulesWithMeteorDeps)
if (currentPackageJs !== packageJs || currentInjectorJs !== injectorJs) {
console.log('\n*** Generating webantic:node-injector package ***')
this.mkdirp.sync('packages/node-injector')
if (currentPackageJs !== packageJs) {
this.fs.writeFileSync(packageJsPath, packageJs)
}
if (currentInjectorJs !== injectorJs) {
this.fs.writeFileSync(injectorJsPath, injectorJs)
}
}
}
}
this.log('---- WEBANTIC:LOADER ----')
}
}
var loader = new Loader()
loader.process()