-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
330 lines (296 loc) · 9.69 KB
/
index.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"use strict";
// Require Node Dependencies
const { readFileSync, writeFileSync, existsSync } = require("fs");
const { join, extname } = require("path");
// Require Third-party Dependencies
const TOML = require("@iarna/toml");
const is = require("@slimio/is");
const argc = require("@slimio/arg-checker");
const cloneDeep = require("lodash.clonedeep");
const { freezedProperty } = require("@slimio/immutable");
// Require Internal Dependencies
const { assertFilePath, assertVersion } = require("./src/assert");
/**
* @typedef {object} Doc
* @property {string[]} include
* @property {number} port
*/
/**
* @typedef {object} psp
* @property {boolean} npmrc
* @property {boolean} jsdoc
* @property {string[]} disabled_dependency
* @property {string[]} exclude
*/
/**
* @typedef {object} Payload
* @property {string} name Name config
* @property {string} version Version config
* @property {string} type Type project config
* @property {string} org Organization name
* @property {string} platform platform
* @property {object} dependencies Addon dependencies config
* @property {object} notes other notes for put somes keys/values
* @property {Doc} doc
* @property {psp} psp
* @property {string} config config path
*/
// Symbols
const symDep = Symbol("dependencies");
const symDoc = Symbol("doc");
const symPsp = Symbol("psp");
const symNotes = Symbol("notes");
class Manifest {
/**
* @class
* @memberof Manifest
* @param {!Payload} payload Payload config
*
* @throws {TypeError}
* @throws {Error}
*
* @example
* const manifest = new Manifest({
* name: "my-project",
* version: "1.0.0",
* type: "NAPI"
* });
*/
constructor(payload) {
argc(payload, is.plainObject);
const {
name, version, type, org, dependencies, doc, psp, notes, platform = "Any", required_core, config
} = Object.assign({}, Manifest.DEFAULT_OPTIONS, payload);
argc(name, is.string);
argc(doc, is.plainObject);
argc(psp, is.plainObject);
argc(platform, is.string);
argc(org, [is.string, is.nullOrUndefined]);
argc(required_core, [is.string, is.nullOrUndefined]);
argc(config, [is.string, is.nullOrUndefined]);
if (is.string(required_core) && type !== "Addon") {
throw new Error("required_core is only available for 'Addon' projects!");
}
const { port = Manifest.DEFAULT_DOC_PORT, include = [] } = doc;
// eslint-disable-next-line camelcase
const { jsdoc = true, npmrc = true, disabled_dependency = [], exclude = [] } = psp;
const validSemver = assertVersion("payload.version", version);
if (!Manifest.TYPES.has(type)) {
throw new TypeError(`payload.type must be one <string> of the Set : ${[...Manifest.TYPES]}`);
}
argc(dependencies, is.plainObject);
argc(notes, is.plainObject);
argc(include, is.array);
argc(exclude, is.array);
argc(disabled_dependency, is.array);
argc(port, is.number);
argc(npmrc, is.boolean);
argc(jsdoc, is.boolean);
// Note: doc.include must contain string with a '.js' extension
const includeFinal = include.filter((file) => typeof file === "string" && extname(file) === ".js");
// eslint-disable-next-line
freezedProperty(this, "required_core", required_core || null);
freezedProperty(this, "name", name);
freezedProperty(this, "type", type);
freezedProperty(this, "version", validSemver);
freezedProperty(this, "platform", platform);
freezedProperty(this, "org", org || null);
freezedProperty(this, "config", config || null);
Reflect.defineProperty(this, symDep, {
value: Object.create(null)
});
Reflect.defineProperty(this, symNotes, {
value: Object.create(null)
});
Reflect.defineProperty(this, symDoc, { value: { port, include: includeFinal } });
Reflect.defineProperty(this, symPsp, { value: { jsdoc, npmrc, disabled_dependency, exclude } });
for (const [name, version] of Object.entries(dependencies)) {
this.addDependency(name, version);
}
for (const [key, value] of Object.entries(notes)) {
Reflect.set(this[symNotes], key, value);
}
}
/**
* @function addDependency
* @memberof Manifest
* @param {!string} name dependency name
* @param {!string} version dependency version
* @returns {void}
*
* @throws {Error}
*/
addDependency(name, version) {
if (this.type !== "Addon") {
throw new Error("Dependencies are only allowed on 'Addon' projects");
}
assertVersion(`payload.dependencies.${name}`, version);
const isWrited = Reflect.set(this[symDep], name, version);
if (!isWrited) {
throw new Error(`Unable to write dependency '${name}' version '${version}'`);
}
}
/**
* @function hasDependency
* @memberof Manifest
* @param {!string} name dependency name
* @returns {boolean}
*/
hasDependency(name) {
return Reflect.has(this[symDep], name);
}
/**
* @version 0.1.0
* @member {object} dependencies
* @memberof Manifest
* @returns {symbol<string>}
*/
get dependencies() {
return cloneDeep(this[symDep]);
}
/**
* @version 0.1.0
* @member {object} notes
* @memberof Manifest
* @returns {symbol<string>}
*/
get notes() {
return cloneDeep(this[symNotes]);
}
/**
* @version 0.2.0
* @member {Doc} doc
* @memberof Manifest
* @returns {symbol<string>}
*/
get doc() {
return cloneDeep(this[symDoc]);
}
/**
* @version 0.3.0
* @member {psp} psp
* @memberof Manifest
* @returns {symbol<string>}
*/
get psp() {
return cloneDeep(this[symPsp]);
}
/**
* @version 0.1.0
*
* @static
* @function create
* @description Create a new manifest file on the disk, return a Manifest Object.
* @memberof Manifest
* @param {!Payload} config Config manifest
* @param {string} [filePath] filePath
* @param {boolean} [lightMode=false] activate light mode
* @returns {Manifest}
*
* @throws {Error}
* @example
* const manifest = Manifest.create({
* name: "my-project",
* version: "1.0.0",
* type: "NAPI"
* });
* console.log(manifest.dependencies);
* console.log(manifest.toJSON());
*/
static create(config, filePath = Manifest.DEFAULT_FILE, lightMode = false) {
assertFilePath(filePath);
if (existsSync(filePath)) {
throw new Error(`Can't create new manifest at ${filePath}!`);
}
const manifest = new Manifest(config);
const json = lightMode ? { name: manifest.name, version: manifest.version, type: manifest.type } : manifest.toJSON();
writeFileSync(filePath, TOML.stringify(json));
return manifest;
}
/**
* @version 0.1.0
*
* @static
* @function open
* @description Open and read an existing manifest file (.toml). Return a Manifest Object.
* @memberof Manifest
* @param {string} [filePath] File path
* @returns {Manifest}
*
* @example
* const manifest = Manifest.open(); // Will open the local manifest
* console.log(manifest.name);
* console.log(manifest.version);
*/
static open(filePath = Manifest.DEFAULT_FILE) {
assertFilePath(filePath);
const buf = readFileSync(filePath);
const payload = TOML.parse(buf.toString());
return new Manifest(payload);
}
/**
* @version 0.1.0
*
* @static
* @function writeOnDisk
* @description Write the manifest file (.toml) on the disk (the file must exist).
* @memberof Manifest
* @param {!Manifest} manifest manifest
* @param {string} [filePath] filePath
* @returns {void}
*
* @throws {TypeError}
* @throws {Error}
*
* @example
* const manifest = Manifest.open();
* Manifest.writeOnDisk(manifest);
*/
static writeOnDisk(manifest, filePath = Manifest.DEFAULT_FILE) {
if (!(manifest instanceof Manifest)) {
throw new TypeError("manifest param must be instanceof Manifest Object");
}
assertFilePath(filePath);
if (!existsSync(filePath)) {
throw new Error(`Unable to write ${filePath} on disk!`);
}
writeFileSync(filePath, TOML.stringify(manifest.toJSON()));
}
/**
* @version 0.1.0
*
* @public
* @function toJSON
* @memberof Manifest
* @description Transform Manifest Object to a valid JSON payload.
* @returns {Payload}
*/
toJSON() {
const ret = {
name: this.name,
version: this.version,
type: this.type,
org: this.org,
required_core: this.required_core,
dependencies: this.dependencies,
notes: this.notes,
platform: this.platform,
doc: this.doc,
psp: this.psp,
config: this.config
};
return ret;
}
}
Manifest.DEFAULT_FILE = join(process.cwd(), "slimio.toml");
Manifest.DEFAULT_DOC_PORT = 2000;
Manifest.DEFAULT_OPTIONS = {
dependencies: Object.create(null),
notes: Object.create(null),
doc: { include: [], port: Manifest.DEFAULT_DOC_PORT },
psp: Object.create(null)
};
/** @type {Readonly<Set<string>>} */
Manifest.TYPES = Object.freeze(new Set(["Addon", "NAPI", "CLI", "Package", "Service", "Degraded"]));
Object.preventExtensions(Manifest);
module.exports = Manifest;