-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProject.js
More file actions
151 lines (136 loc) · 4.76 KB
/
Project.js
File metadata and controls
151 lines (136 loc) · 4.76 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import fs from 'fs-extra'
import path from 'path'
import globs from 'globs'
import { readValidateJSON, readValidateJSONSync } from '../util/JSONReadValidate.js'
import Plugin from './Plugin.js'
import Target from './Target.js'
export const MANIFEST_FILENAME = 'adapt.json'
export const FRAMEWORK_FILENAME = 'package.json'
/**
* A representation of the target Adapt Framework project
*/
export default class Project {
constructor ({
cwd = process.cwd(),
logger
} = {}) {
this.logger = logger
this.cwd = cwd
this.manifestFilePath = path.resolve(this.cwd, MANIFEST_FILENAME)
this.frameworkPackagePath = path.resolve(this.cwd, FRAMEWORK_FILENAME)
}
/** @returns {boolean} */
get isAdaptDirectory () {
try {
// are we inside an existing adapt_framework project.
const packageJSON = fs.readJSONSync(this.cwd + '/package.json')
return (packageJSON.name === 'adapt_framework')
} catch (err) {
// don't worry, we're not inside a framework directory.
}
return false
}
/** @returns {boolean} */
get containsManifestFile () {
if (!this.isAdaptDirectory) return false
return fs.existsSync(this.manifestFilePath)
}
/** @returns {string} */
get version () {
try {
return readValidateJSONSync(this.frameworkPackagePath).version
} catch (ex) {
return null
}
}
isNPM() {
return fs.existsSync(path.join(cwd, 'src/package.json'))
}
tryThrowInvalidPath () {
if (this.containsManifestFile) return
this.logger?.error('Fatal error: please run above commands in adapt course directory.')
throw new Error('Fatal error: please run above commands in adapt course directory.')
}
/** @returns {[Target]} */
async getInstallTargets () {
return Object.entries(await this.getManifestDependencies()).map(([name, requestedVersion]) => new Target({ name, requestedVersion, project: this, logger: this.logger }))
}
/** @returns {[string]} */
async getManifestDependencies () {
const manifest = await readValidateJSON(this.manifestFilePath)
return manifest.dependencies
}
/** @returns {[Plugin]} */
async getInstalledPlugins () {
return Object.entries(await this.getInstalledDependencies()).map(([name]) => new Plugin({ name, project: this, logger: this.logger }))
}
/** @returns {[Target]} */
async getUninstallTargets () {
return Object.entries(await this.getInstalledDependencies()).map(([name]) => new Target({ name, project: this, logger: this.logger }))
}
/** @returns {[Target]} */
async getUpdateTargets () {
return Object.entries(await this.getInstalledDependencies()).map(([name]) => new Target({ name, project: this, logger: this.logger }))
}
async getInstalledDependencies () {
const getDependencyBowerJSONs = async () => {
// TODO: npm implementation
const glob = `${this.cwd.replace(/\\/g, '/')}/src/**/bower.json`
const bowerJSONPaths = await new Promise((resolve, reject) => {
globs(glob, (err, matches) => {
if (err) return reject(err)
resolve(matches)
})
})
const bowerJSONs = []
for (const bowerJSONPath of bowerJSONPaths) {
try {
bowerJSONs.push(await fs.readJSON(bowerJSONPath))
} catch (err) {}
}
return bowerJSONs
}
const dependencies = (await getDependencyBowerJSONs())
.filter(bowerJSON => bowerJSON?.name && bowerJSON?.version)
.reduce((dependencies, bowerJSON) => {
dependencies[bowerJSON.name] = bowerJSON.version
return dependencies
}, {})
return dependencies
}
async getSchemaPaths () {
const glob = `${this.cwd.replace(/\\/g, '/')}/src/**/*.schema.json`
const bowerJSONPaths = await new Promise((resolve, reject) => {
globs(glob, (err, matches) => {
if (err) return reject(err)
resolve(matches)
})
})
return bowerJSONPaths
}
/**
* @param {Plugin} plugin
*/
add (plugin) {
if (typeof Plugin !== 'object' && !(plugin instanceof Plugin)) {
plugin = new Plugin({ name: plugin })
}
let manifest = { version: '0.0.0', dependencies: {} }
if (this.containsManifestFile) {
manifest = readValidateJSONSync(this.manifestFilePath)
}
manifest.dependencies[plugin.packageName] = plugin.sourcePath || plugin.requestedVersion || plugin.version
fs.writeJSONSync(this.manifestFilePath, manifest, { spaces: 2, replacer: null })
}
/**
* @param {Plugin} plugin
*/
remove (plugin) {
if (typeof Plugin !== 'object' && !(plugin instanceof Plugin)) {
plugin = new Plugin({ name: plugin })
}
const manifest = readValidateJSONSync(this.manifestFilePath)
delete manifest.dependencies[plugin.packageName]
fs.writeJSONSync(this.manifestFilePath, manifest, { spaces: 2, replacer: null })
}
}