-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathData.js
More file actions
187 lines (167 loc) · 5.03 KB
/
Data.js
File metadata and controls
187 lines (167 loc) · 5.03 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const path = require('path');
const fs = require('fs-extra');
const globs = require('globs');
const JSONFile = require('./lib/JSONFile');
const Language = require('./data/Language');
/**
* @typedef {import('./Framework')} Framework
* @typedef {import('./lib/JSONFileItem')} JSONFileItem
*/
/**
* This class represents the course folder. It contains references to the config.json,
* all languages, each language file and subsequently each language file item.
* It is filename agnostic, except for config.[jsonext], such that there are no
* hard references to the other file names, allowing any filename to be used with the
* appropriate [jsonext] file extension (usually txt or json).
* It assumes all language files are located at course/[langName]/*.[jsonext] and
* the config file is located at course/config.[jsonext].
* It has _id and _parentId structure checking and _trackingId management included.
*/
class Data {
/**
* @param {Object} options
* @param {Framework} options.framework
* @param {string} options.sourcePath
* @param {string} options.courseDir
* @param {string} options.jsonext
* @param {string} options.trackingIdType
* @param {function} options.log
*/
constructor({
framework = null,
sourcePath = null,
courseDir = null,
jsonext = 'json',
trackingIdType = 'block',
log = console.log
} = {}) {
/** @type {Framework} */
this.framework = framework;
/** @type {string} */
this.sourcePath = sourcePath;
/** @type {string} */
this.courseDir = courseDir;
/** @type {string} */
this.jsonext = jsonext;
/** @type {string} */
this.trackingIdType = trackingIdType;
/** @type {function} */
this.log = log;
/** @type {JSONFile} */
this.configFile = null;
/** @type {[Language]} */
this.languages = null;
/** @type {string} */
this.coursePath = path.join(this.sourcePath, this.courseDir).replace(/\\/g, '/');
}
/** @returns {Data} */
load() {
this.languages = globs.sync(path.join(this.coursePath, '*/')).map(languagePath => {
const language = new Language({
framework: this.framework,
languagePath,
courseDir: this.courseDir,
jsonext: this.jsonext,
trackingIdType: this.trackingIdType,
log: this.log
});
language.load();
return language;
}).filter(lang => lang.isValid);
this.configFile = new JSONFile({
framework: this.framework,
path: path.join(this.coursePath, `config.${this.jsonext}`),
jsonext: this.jsonext
});
this.configFile.load();
return this;
}
/** @type {boolean} */
get hasChanged() {
return this.languages.some(language => language.hasChanged);
}
/** @type {[string]} */
get languageNames() {
return this.languages.map(language => language.name);
}
/**
* Fetch a Language instance by name.
* @param {string} name
* @returns {Language}
*/
getLanguage(name) {
const language = this.languages.find(language => language.name === name);
if (!language) {
const err = new Error(`Cannot find language '${name}'.`);
err.number = 10004;
throw err;
}
return language;
}
/**
* Returns a JSONFileItem representing the course/config.json file object.
* @returns {JSONFileItem}
* */
getConfigFileItem() {
return this.configFile.firstFileItem;
}
/**
* @param {string} fromName
* @param {string} toName
* @param {boolean} replace
* @returns {Language}
*/
copyLanguage(fromName, toName, replace = false) {
const fromLang = this.getLanguage(fromName);
const newPath = (`${fromLang.rootPath}/${toName}/`).replace(/\\/g, '/');
if (this.languageNames.includes(toName) && !replace) {
const err = new Error(`Folder already exists. ${newPath}`);
err.number = 10003;
throw err;
}
let toLang;
if (this.languageNames.includes(toName)) {
toLang = this.getLanguage(toName);
} else {
toLang = new Language({
framework: this.framework,
languagePath: newPath,
courseDir: this.courseDir,
jsonext: this.jsonext,
trackingIdType: this.trackingIdType
});
this.languages.push(toLang);
}
fs.mkdirpSync(newPath);
fromLang.files.forEach(file => {
const pathParsed = path.parse(file.path.replace(/\\/g, '/'));
const newLocation = `${newPath}${pathParsed.name}${pathParsed.ext}`;
fs.removeSync(newLocation);
fs.copyFileSync(file.path, newLocation);
});
toLang.load();
return toLang;
}
/** @returns {Data} */
checkIds() {
this.languages.forEach(lang => lang.checkIds());
return this;
}
/** @returns {Data} */
addTrackingIds() {
this.languages.forEach(lang => lang.addTrackingIds());
return this;
}
/** @returns {Data} */
removeTrackingIds() {
this.languages.forEach(lang => lang.removeTrackingIds());
return this;
}
/** @returns {Data} */
save() {
this.configFile.save();
this.languages.forEach(language => language.save());
return this;
}
}
module.exports = Data;