-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
272 lines (251 loc) · 9.63 KB
/
Copy pathutil.js
File metadata and controls
272 lines (251 loc) · 9.63 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
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
/**
* @license
* MOST Web Framework 2.0 Codename Blueshift
* Copyright (c) 2017, THEMOST LP All rights reserved
*
* Use of this source code is governed by an BSD-3-Clause license that can be
* found in the LICENSE file at https://themost.io/license
*/
const _ = require('lodash');
const ejs = require('ejs');
const fs = require('fs-extra');
const path = require('path');
const configurationDefaults = {
"base":"server",
"out": "dist/server"
};
/**
*
* @param s
* @returns {*}
* @private
*/
function _dasherize(s) {
if (_.isString(s))
return _.trim(s).replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').replace(/^-/,'').toLowerCase();
return s;
}
/**
* @method dasherize
* @memberOf _
*/
if (typeof _.dasherize !== 'function') {
_.mixin({'dasherize' : _dasherize});
}
function writeFileFromTemplate(source, dest, data) {
return ejs.renderFile(source, data).then((res)=> {
return new Promise((resolve, reject)=> {
//write file
fs.writeFile(dest, res, (err) => {
if (err) {
return reject(err);
}
return resolve();
});
});
});
}
module.exports.writeFileFromTemplate = writeFileFromTemplate;
function contentFromTemplate(source, data) {
return ejs.renderFile(source, data);
}
module.exports.contentFromTemplate = contentFromTemplate;
function loadConfiguration() {
let config = require(path.resolve(process.cwd(), '.themost-cli.json'));
return Object.assign({}, configurationDefaults, config);
}
module.exports.loadConfiguration = loadConfiguration;
function getConfiguration() {
try {
return loadConfiguration();
}
catch(err) {
if (err.code === 'MODULE_NOT_FOUND') {
console.error('ERROR','Configuration cannot be found. It seems that current working directory does not contain a MOST Web Framework project.');
process.exit(1);
}
else {
console.error('ERROR','An error occurred while loading configuration.');
console.error(err);
process.exit(1);
}
}
}
module.exports.getConfiguration = getConfiguration;
class SimpleDataContext {
constructor(configuration) {
this.getConfiguration = ()=> configuration;
}
getStrategy(strategyCtor) {
return this.getConfiguration().getStrategy(strategyCtor);
}
model(name) {
let self = this;
if ((name === null) || (name === undefined))
return null;
let obj = self.getConfiguration().getStrategy(function DataConfigurationStrategy() {}).model(name);
if (_.isNil(obj)) {
return null;
}
//do some things for CLI only
//remove class path if any
delete obj.classPath;
//clear event listeners
obj.eventListeners = [];
let dataModule = require.resolve('@themost/data',{
paths:[path.resolve(process.cwd(), 'node_modules')]
});
// noinspection JSUnresolvedReference
let DataModel = require(dataModule).DataModel,
model = new DataModel(obj);
//set model context
model.context = self;
//return model
return model;
}
}
module.exports.SimpleDataContext = SimpleDataContext;
module.exports.getDataConfiguration = function getDataConfiguration(options) {
let DataConfiguration;
try {
let dataModule = require.resolve('@themost/data/data-configuration',{
paths:[path.resolve(process.cwd(), 'node_modules')]
});
// noinspection JSUnresolvedReference
DataConfiguration = require(dataModule).DataConfiguration;
}
catch(err) {
if (err.code === 'MODULE_NOT_FOUND') {
console.error('ERROR','MOST Web Framework data configuration module cannot be found.');
}
else {
console.error('ERROR','An error occurred while trying to initialize data configuration.');
console.error(err);
}
return process.exit(1);
}
console.log('INFO','Initializing configuration');
let res = new DataConfiguration(path.resolve(process.cwd(), options.base, 'config'));
//modify data configuration strategy
let dataConfigurationStrategy = res.getStrategy(function DataConfigurationStrategy() {});
let getModel = dataConfigurationStrategy.model;
dataConfigurationStrategy.model = function(name) {
let model = getModel.bind(this)(name);
if (model) {
//do some things for CLI only
//remove class path if any
delete model.classPath;
//clear event listeners
model.eventListeners = [];
}
return model;
};
return res;
};
module.exports.getBuilder = function getBuilder(config) {
let ODataConventionModelBuilder;
let dataModule = require.resolve('@themost/data',{
paths:[path.resolve(process.cwd(), 'node_modules')]
});
// noinspection JSUnresolvedReference
ODataConventionModelBuilder = require(dataModule).ODataConventionModelBuilder;
let dataObjectModule = require.resolve('@themost/data',{
paths:[path.resolve(process.cwd(), 'node_modules')]
});
//disable data model class loader
config.getStrategy(function ModelClassLoaderStrategy() {}).resolve = function(model) {
// noinspection JSUnresolvedReference
return require(dataObjectModule).DataObject;
};
return new ODataConventionModelBuilder(config);
};
module.exports.getApplication = function getApplication(options) {
let HttpApplication;
let appModule;
if (options.application) {
// try to create application from custom module
let [customModule, className] = options.application.split('#');
if (className) {
const ApplicationClass = require(customModule)[className];
// create a new instance of application passing the output directory as the current path
return new ApplicationClass(path.resolve(process.cwd(), options.out));
}
if (typeof customModule !== 'function') {
throw new Error(`Invalid application module. The module ${options.application} does not export a function.`);
}
return customModule(path.resolve(process.cwd(), options.out));
}
try {
appModule = require.resolve('@themost/web',{
paths:[path.resolve(process.cwd(), 'node_modules')]
});
// noinspection JSUnresolvedReference
HttpApplication = require(appModule).HttpApplication;
}
catch(err) {
if (err.code === 'MODULE_NOT_FOUND') {
console.error('ERROR','MOST Web Framework module cannot be found.');
}
else {
console.error('ERROR','An error occurred while trying to initialize MOST Web Framework Application.');
console.error(err);
}
return process.exit(1);
}
console.log('INFO','Initializing application');
let app = new HttpApplication(path.resolve(process.cwd(), options.out));
let strategy = app.getConfiguration().getStrategy(function DataConfigurationStrategy() {
});
//get adapter types
let adapterTypes = strategy.adapterTypes;
//get configuration adapter types
// noinspection JSUnresolvedReference
let configurationAdapterTypes = app.getConfiguration().getSourceAt('adapterTypes');
if (Array.isArray(configurationAdapterTypes)) {
configurationAdapterTypes.forEach((configurationAdapterType)=> {
if (typeof adapterTypes[configurationAdapterType.invariantName] === 'undefined') {
//load adapter type
let adapterModulePath = require.resolve(configurationAdapterType.type,{
paths:[path.resolve(process.cwd(), 'node_modules')]
});
let adapterModule = require(adapterModulePath);
adapterTypes[configurationAdapterType.invariantName] = {
invariantName:configurationAdapterType.invariantName,
name: configurationAdapterType.name,
createInstance:adapterModule.createInstance
};
}
});
}
// auto register application extensions
let disableExtensions = false;
if (Object.prototype.hasOwnProperty.call(options, 'disableExtensions')) {
disableExtensions = options.disableExtensions;
}
if (disableExtensions === false) {
console.log('INFO','Loading application extensions');
const extensionsDir = path.resolve(process.cwd(), options.out, 'extensions');
if (fs.existsSync(extensionsDir)) {
const extensionModules = fs.readdirSync(extensionsDir);
extensionModules.filter((extensionModule) => {
return path.extname(extensionModule) === '.js';
}).forEach((extensionModule) => {
require(path.resolve(extensionsDir, extensionModule));
});
}
}
// auto register application services
let disableServices = false;
if (Object.prototype.hasOwnProperty.call(options, 'disableServices')) {
disableServices = options.disableServices;
}
if (disableServices === false) {
console.log('INFO','Loading application services');
// get services configuration
// noinspection JSUnresolvedReference
const ServicesConfiguration = require(appModule).ServicesConfiguration;
// configure application
ServicesConfiguration.config(app);
}
return app;
};