Repository navigation
Expand file tree
/
Copy pathojet.js
More file actions
90 lines (83 loc) · 2.31 KB
/
Copy pathojet.js
File metadata and controls
90 lines (83 loc) · 2.31 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
/**
Copyright (c) 2015, 2026, Oracle and/or its affiliates.
Licensed under The Universal Permissive License (UPL), Version 1.0
as shown at https://oss.oracle.com/licenses/upl/
*/
'use strict';
const pkg = require('./package.json');
const tasks = require('./lib/tasks');
class Ojet {
/**
*
* @param {string} options.cwd path to invoke ojet from
* @param {boolean} options.logs whether to allow ojet logging
*/
constructor({ cwd = '.', logs = true }) {
this._cwd = cwd;
this._logs = logs;
this.version = Ojet._version;
}
/**
*
* @param {string} options.task name of task to execute
* @param {string} options.scope scope of the task to execute
* @param {Array<string>} options.parameters parameters to execute the task with
* @param {Object} options.options options to execute the task with
* @returns {Promise<undefined>}
*/
async execute({ task, scope, parameters = [], options = {} }) {
this._setOptions();
let result;
// eslint-disable-next-line prefer-rest-params
this._logExecuteSummary(arguments[0]);
try {
const tasksList = [task, ...(scope ? [scope, ...parameters] : parameters)];
result = await tasks(tasksList, options);
} catch (error) {
result = Promise.reject(error);
} finally {
Ojet._clearOptions();
}
return result;
}
_setOptions() {
const { _cwd, _logs } = this;
const OJET = {
cwd: process.cwd(),
logs: _logs
};
process.env.OJET = JSON.stringify(OJET);
process.chdir(_cwd);
}
_logExecuteSummary({ task, scope, parameters = [], options }) {
if (this._logs) {
let message = `OJET API: ojet ${task}`;
if (scope) {
message += ` ${scope}`;
}
if (parameters.length) {
parameters.forEach((parameter) => {
message += ` ${parameter}`;
});
}
if (options) {
Object.keys(options).forEach((key) => {
message += ` --${key}=${options[key]}`;
});
}
console.log(message);
}
}
}
Ojet._version = pkg.version;
Ojet._clearOptions = function () {
const OJET = JSON.parse(process.env.OJET || false);
if (OJET && OJET.cwd) {
process.chdir(OJET.cwd);
}
Ojet._clearEnvVariables();
};
Ojet._clearEnvVariables = function () {
delete process.env.OJET;
};
module.exports = Ojet;