-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path_CeL.loader.nodejs.js
149 lines (130 loc) · 4.36 KB
/
_CeL.loader.nodejs.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
/**
* @name framework loader for node.js.
*
* @fileoverview Example to include CeL (CeJS) in node.js. node.js 下之 CeL
* 簡易加載器。本檔僅包括含入並運用 node.loader.js 的最精簡程式碼。<br />
* Copy from:
* https://github.com/kanasimi/CeJS/blob/master/_for%20include/_CeL.loader.nodejs.js
*
* usage: See ../README.md
*/
typeof CeL !== 'function' && (function() {
"use strict";
var full_root = module.filename /* require('electron').app.getPath('userData') */
// TODO:
// https://www.electronjs.org/docs/latest/api/app#appgetpathname
// https://stackoverflow.com/questions/71365401/how-to-read-config-file-in-electronjs-app
&& module.filename.replace(/[^\\\/]+$/, ''),
// WARNING: repository_path_list_file should be an absolute path in some
// environment.
repository_path_list_file
//
= (full_root || './') + '_repository_path_list.txt',
//
matched = full_root.match(/^(.+?[\\\/])_for include[\\\/]$/),
//
node_fs = require('fs'),
//
CeL_path_list = node_fs.readFileSync(repository_path_list_file);
CeL_path_list = CeL_path_list && CeL_path_list.toString() || '';
if (matched) {
// `CeL_path_list` should not be "" here,
// but it doesn’t matter.
// 直接 require 函式庫下面的本檔案。 e.g.,
// require('path\\JS\\_for include\\_CeL.loader.nodejs.js');
// 如此可以不依靠 repository_path_list_file。
// ** 這時應該採用: require('path/to/node.loader.js');
CeL_path_list += '\n' + matched[1];
}
if (!CeL_path_list) {
console.error(
//
'Please set the absolute path list of CeL library in the file ['
//
+ repository_path_list_file + ']!');
return;
}
// ----------------------------------------------------------------------------
// Load CeJS library. For node.js loading.
// Copy/modified from "/_for include/_CeL.loader.nodejs.js".
function check_path(path) {
path = path.trim();
if (!path || path.charAt(0) === '#') {
// path is comments or blank line
return;
}
// console.log('Try path: ' + JSON.stringify(path));
try {
// old node.js has no method 'accessSync'.
// accessSync() added in: v0.11.15
if (node_fs.accessSync) {
// accessSync() throws if any accessibility checks fail,
// and does nothing otherwise.
node_fs.accessSync(path);
} else if (!node_fs.existsSync(path)) {
throw 'ENOENT';
}
if (!/[^\\\/]$/.test(path)) {
path += require('path').sep;
}
if (typeof global.CeL !== 'function'
//
&& (typeof global.CeL !== 'object' || !CeL)) {
global.CeL = {};
}
CeL.library_path = path + 'ce.js';
var loader = '/_for include/node.loader.js';
loader = path + (path.indexOf('/') !== -1 ? loader
//
: loader.replace(/\//g, '\\'));
// console.log('Try loader path: ' + loader);
require(loader);
return CeL.version;
} catch (e) {
// console.error(e);
// try next path
}
// Try the file below loader for relative path.
if (full_root && !/^(?:\/|[A-Z]:\\)/i.test(path)) {
return check_path(full_root + path);
}
}
CeL_path_list.split(CeL_path_list.indexOf('\n') === -1 ? '|' : /\r?\n/)
// 載入CeJS基礎泛用之功能。(例如非特殊目的使用的載入功能)
.some(check_path);
// If no latest version found, try to use cejs module instead.
if (typeof use_cejs_mudule === 'boolean'
// Set "global.use_cejs_mudule = true;" if you need to do so anyway.
&& use_cejs_mudule && typeof CeL !== 'function') {
try {
// 若有 CeJS 則用之。
require('cejs');
console.log('cejs loader: use npm, not the latest version!');
return;
} catch (e) {
// console.error(e);
}
console.error('Failed to load CeJS library!\n');
console.info('Please install CeJS library first.'
//
+ ' 請先安裝 CeJS library:\n' + 'npm install cejs\n\n'
//
+ 'Or you may trying the latest version:\n'
//
+ 'See https://github.com/kanasimi/CeJS');
throw 'No CeJS library';
}
if (typeof CeL !== 'function') {
console.error('Failed to load CeL!');
console.error('current working directory: ' + process.cwd());
console.error('main script: '
//
+ (require.main && require.main.filename));
console.error('loader path: ' + module.filename);
}
})();
// ----------------------------------------------------------------------------
// Load module.
// CeL.env.no_catch = true;
// CeL.set_debug(2);
// CeL.run([ 'data.code.compatibility' ]);