ee-core 1.5.2-beta.1 → 2.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -2
  3. package/addon/window/index.js +91 -91
  4. package/bin/tools.js +18 -18
  5. package/config/config.default.js +287 -280
  6. package/core/index.js +12 -12
  7. package/core/lib/ee.js +218 -218
  8. package/core/lib/loader/context_loader.js +106 -106
  9. package/core/lib/loader/ee_loader.js +461 -457
  10. package/core/lib/loader/file_loader.js +325 -325
  11. package/core/lib/loader/mixin/addon.js +32 -32
  12. package/core/lib/loader/mixin/config.js +135 -135
  13. package/core/lib/loader/mixin/controller.js +125 -124
  14. package/core/lib/loader/mixin/service.js +28 -28
  15. package/core/lib/utils/base_context_class.js +34 -34
  16. package/core/lib/utils/function.js +30 -0
  17. package/core/lib/utils/index.js +127 -127
  18. package/core/lib/utils/sequencify.js +59 -59
  19. package/core/lib/utils/timing.js +77 -77
  20. package/index.js +49 -49
  21. package/lib/appLoader.js +48 -53
  22. package/lib/application.js +85 -84
  23. package/lib/baseApp.js +114 -131
  24. package/lib/eeApp.js +325 -359
  25. package/{lib/constant.js → module/const/index.js} +12 -9
  26. package/{lib/httpclient.js → module/httpclient/index.js} +170 -136
  27. package/module/jobs/child/forkProcess.js +99 -0
  28. package/module/jobs/child/index.js +33 -0
  29. package/module/jobs/index.js +55 -0
  30. package/module/jobs/renderer/index.js +140 -0
  31. package/module/jobs/renderer/loadView.js +40 -0
  32. package/module/loader/index.js +78 -0
  33. package/module/log/index.js +53 -0
  34. package/module/log/logger.js +61 -0
  35. package/module/message/index.js +13 -0
  36. package/module/message/ipcMain.js +160 -0
  37. package/module/message/ipcRender.js +0 -0
  38. package/{lib → module}/socket/httpServer.js +142 -142
  39. package/{lib → module}/socket/io.js +23 -23
  40. package/{lib → module}/socket/ipcServer.js +106 -108
  41. package/{lib → module}/socket/socketClient.js +51 -50
  42. package/{lib → module}/socket/socketServer.js +77 -76
  43. package/module/socket/start.js +22 -0
  44. package/{lib → module}/storage/index.js +35 -34
  45. package/module/storage/jsondb/adapters/Base.js +14 -0
  46. package/module/storage/jsondb/adapters/FileSync.js +32 -0
  47. package/{lib/storage/lowdb → module/storage/jsondb}/main.js +42 -46
  48. package/{lib/storage/lowdbStorage.js → module/storage/jsondbStorage.js} +98 -99
  49. package/{lib → module}/storage/sqliteStorage.js +123 -127
  50. package/module/utils/copyto.js +161 -0
  51. package/module/utils/helper.js +117 -0
  52. package/module/utils/index.js +120 -0
  53. package/module/utils/json.js +72 -0
  54. package/module/utils/ps.js +175 -0
  55. package/{utils → module/utils}/wrap.js +35 -37
  56. package/package.json +44 -48
  57. package/tools/encrypt.js +274 -274
  58. package/tools/replaceDist.js +61 -61
  59. package/utils/index.js +128 -246
  60. package/lib/logger.js +0 -47
  61. package/lib/socket/start.js +0 -22
  62. package/lib/storage/lowdb/adapters/Base.js +0 -15
  63. package/lib/storage/lowdb/adapters/FileAsync.js +0 -41
  64. package/lib/storage/lowdb/adapters/FileSync.js +0 -39
  65. package/lib/storage/lowdb/adapters/LocalStorage.js +0 -20
  66. package/lib/storage/lowdb/adapters/Memory.js +0 -8
  67. package/lib/storage/lowdb/adapters/_stringify.js +0 -4
  68. package/lib/storage/lowdb/common.js +0 -33
  69. package/lib/storage/lowdb/fp.js +0 -23
  70. package/lib/storage/lowdb/is-promise.js +0 -6
  71. package/lib/storage/lowdb/nano.js +0 -5
  72. package/utils/common.js +0 -91
@@ -0,0 +1,161 @@
1
+ /*!
2
+ * copy-to - index.js
3
+ * Copyright(c) 2014 dead_horse <dead_horse@qq.com>
4
+ * MIT Licensed
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ /**
10
+ * slice() reference.
11
+ */
12
+
13
+ var slice = Array.prototype.slice;
14
+
15
+ /**
16
+ * Expose copy
17
+ *
18
+ * ```
19
+ * copy({foo: 'nar', hello: 'copy'}).to({hello: 'world'});
20
+ * copy({foo: 'nar', hello: 'copy'}).toCover({hello: 'world'});
21
+ * ```
22
+ *
23
+ * @param {Object} src
24
+ * @return {Copy}
25
+ */
26
+
27
+ module.exports = Copy;
28
+
29
+
30
+ /**
31
+ * Copy
32
+ * @param {Object} src
33
+ * @param {Boolean} withAccess
34
+ */
35
+
36
+ function Copy(src, withAccess) {
37
+ if (!(this instanceof Copy)) return new Copy(src, withAccess);
38
+ this.src = src;
39
+ this._withAccess = withAccess;
40
+ }
41
+
42
+ /**
43
+ * copy properties include getter and setter
44
+ * @param {[type]} val [description]
45
+ * @return {[type]} [description]
46
+ */
47
+
48
+ Copy.prototype.withAccess = function (w) {
49
+ this._withAccess = w !== false;
50
+ return this;
51
+ };
52
+
53
+ /**
54
+ * pick keys in src
55
+ *
56
+ * @api: public
57
+ */
58
+
59
+ Copy.prototype.pick = function(keys) {
60
+ if (!Array.isArray(keys)) {
61
+ keys = slice.call(arguments);
62
+ }
63
+ if (keys.length) {
64
+ this.keys = keys;
65
+ }
66
+ return this;
67
+ };
68
+
69
+ /**
70
+ * copy src to target,
71
+ * do not cover any property target has
72
+ * @param {Object} to
73
+ *
74
+ * @api: public
75
+ */
76
+
77
+ Copy.prototype.to = function(to) {
78
+ to = to || {};
79
+
80
+ if (!this.src) return to;
81
+ var keys = this.keys || Object.keys(this.src);
82
+
83
+ if (!this._withAccess) {
84
+ for (var i = 0; i < keys.length; i++) {
85
+ var key = keys[i];
86
+ if (to[key] !== undefined) continue;
87
+ to[key] = this.src[key];
88
+ }
89
+ return to;
90
+ }
91
+
92
+ for (var i = 0; i < keys.length; i++) {
93
+ var key = keys[i];
94
+ if (!notDefined(to, key)) continue;
95
+ var getter = this.src.__lookupGetter__(key);
96
+ var setter = this.src.__lookupSetter__(key);
97
+ if (getter) to.__defineGetter__(key, getter);
98
+ if (setter) to.__defineSetter__(key, setter);
99
+
100
+ if (!getter && !setter) {
101
+ to[key] = this.src[key];
102
+ }
103
+ }
104
+ return to;
105
+ };
106
+
107
+ /**
108
+ * copy src to target,
109
+ * override any property target has
110
+ * @param {Object} to
111
+ *
112
+ * @api: public
113
+ */
114
+
115
+ Copy.prototype.toCover = function(to) {
116
+ var keys = this.keys || Object.keys(this.src);
117
+
118
+ for (var i = 0; i < keys.length; i++) {
119
+ var key = keys[i];
120
+ delete to[key];
121
+ var getter = this.src.__lookupGetter__(key);
122
+ var setter = this.src.__lookupSetter__(key);
123
+ if (getter) to.__defineGetter__(key, getter);
124
+ if (setter) to.__defineSetter__(key, setter);
125
+
126
+ if (!getter && !setter) {
127
+ to[key] = this.src[key];
128
+ }
129
+ }
130
+ };
131
+
132
+ Copy.prototype.override = Copy.prototype.toCover;
133
+
134
+ /**
135
+ * append another object to src
136
+ * @param {Obj} obj
137
+ * @return {Copy}
138
+ */
139
+
140
+ Copy.prototype.and = function (obj) {
141
+ var src = {};
142
+ this.to(src);
143
+ this.src = obj;
144
+ this.to(src);
145
+ this.src = src;
146
+
147
+ return this;
148
+ };
149
+
150
+ /**
151
+ * check obj[key] if not defiend
152
+ * @param {Object} obj
153
+ * @param {String} key
154
+ * @return {Boolean}
155
+ */
156
+
157
+ function notDefined(obj, key) {
158
+ return obj[key] === undefined
159
+ && obj.__lookupGetter__(key) === undefined
160
+ && obj.__lookupSetter__(key) === undefined;
161
+ }
@@ -0,0 +1,117 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const mkdirp = require('mkdirp');
4
+ const convert = require('koa-convert');
5
+ const is = require('is-type-of');
6
+ const co = require('co');
7
+
8
+ /**
9
+ * fnDebounce
10
+ *
11
+ * @param {Function} fn - 回调函数
12
+ * @param {Time} delayTime - 延迟时间(ms)
13
+ * @param {Boolean} isImediate - 是否需要立即调用
14
+ * @param {type} args - 回调函数传入参数
15
+ */
16
+ exports.fnDebounce = function() {
17
+ const fnObject = {};
18
+ let timer;
19
+
20
+ return (fn, delayTime, isImediate, args) => {
21
+ const setTimer = () => {
22
+ timer = setTimeout(() => {
23
+ fn(args);
24
+ clearTimeout(timer);
25
+ delete fnObject[fn];
26
+ }, delayTime);
27
+
28
+ fnObject[fn] = { delayTime, timer };
29
+ };
30
+
31
+ if (!delayTime || isImediate) return fn(args);
32
+
33
+ if (fnObject[fn]) {
34
+ clearTimeout(timer);
35
+ setTimer(fn, delayTime, args);
36
+ } else {
37
+ setTimer(fn, delayTime, args);
38
+ }
39
+ };
40
+ }
41
+
42
+ /**
43
+ * 随机10位字符串
44
+ */
45
+ exports.getRandomString = function() {
46
+ return Math.random().toString(36).substring(2);
47
+ };
48
+
49
+ /**
50
+ * 创建文件夹
51
+ */
52
+ exports.mkdir = function(filepath) {
53
+ mkdirp.sync(path.dirname(filepath));
54
+ return
55
+ }
56
+
57
+ /**
58
+ * 修改文件权限
59
+ */
60
+ exports.chmodPath = function(path, mode) {
61
+ let files = [];
62
+ if (fs.existsSync(path)) {
63
+ files = fs.readdirSync(path);
64
+ files.forEach((file, index) => {
65
+ const curPath = path + '/' + file;
66
+ if (fs.statSync(curPath).isDirectory()) {
67
+ this.chmodPath(curPath, mode); // 递归删除文件夹
68
+ } else {
69
+ fs.chmodSync(curPath, mode);
70
+ }
71
+ });
72
+ fs.chmodSync(path, mode);
73
+ }
74
+ };
75
+
76
+ /**
77
+ * 版本号比较
78
+ */
79
+ exports.compareVersion = function (v1, v2) {
80
+ v1 = v1.split('.')
81
+ v2 = v2.split('.')
82
+ const len = Math.max(v1.length, v2.length)
83
+
84
+ while (v1.length < len) {
85
+ v1.push('0')
86
+ }
87
+ while (v2.length < len) {
88
+ v2.push('0')
89
+ }
90
+
91
+ for (let i = 0; i < len; i++) {
92
+ const num1 = parseInt(v1[i])
93
+ const num2 = parseInt(v2[i])
94
+
95
+ if (num1 > num2) {
96
+ return 1
97
+ } else if (num1 < num2) {
98
+ return -1
99
+ }
100
+ }
101
+
102
+ return 0
103
+ }
104
+
105
+ /**
106
+ * 执行一个函数
107
+ */
108
+ exports.callFn = async function (fn, args, ctx) {
109
+ args = args || [];
110
+ if (!is.function(fn)) return;
111
+ if (is.generatorFunction(fn)) fn = co.wrap(fn);
112
+ return ctx ? fn.call(ctx, ...args) : fn(...args);
113
+ }
114
+
115
+ exports.middleware = function (fn) {
116
+ return is.generatorFunction(fn) ? convert(fn) : fn;
117
+ }
@@ -0,0 +1,120 @@
1
+ const path = require('path');
2
+ const eis = require('electron-is');
3
+ const Storage = require('../storage');
4
+ const Constants = require('../const');
5
+ const Ps = require('./ps');
6
+ const Helper = require('./helper');
7
+ const UtilsJson = require('./json');
8
+ const Copy = require('./copyto');
9
+
10
+ /**
11
+ * other module
12
+ */
13
+ Copy(Ps)
14
+ .and(Helper)
15
+ .to(exports);
16
+
17
+ /**
18
+ * 获取项目根目录package.json
19
+ */
20
+ exports.getPackage = function() {
21
+ const json = UtilsJson.readSync(path.join(this.getHomeDir(), 'package.json'));
22
+
23
+ return json;
24
+ };
25
+
26
+ /**
27
+ * 获取 coredb
28
+ */
29
+ exports.getCoreDB = function() {
30
+ const coreDB = Storage.connection('system');
31
+ return coreDB;
32
+ }
33
+
34
+ /**
35
+ * 获取 ee配置
36
+ */
37
+ exports.getEeConfig = function() {
38
+ const cdb = this.getCoreDB();
39
+ const config = cdb.getItem('config');
40
+
41
+ return config;
42
+ }
43
+
44
+ /**
45
+ * 获取 app version
46
+ */
47
+ exports.getAppVersion = function() {
48
+ const cdb = this.getCoreDB();
49
+ const v = cdb.getItem('config').appVersion;
50
+ return v;
51
+ }
52
+
53
+ /**
54
+ * 获取 插件配置
55
+ */
56
+ exports.getAddonConfig = function() {
57
+ const cdb = this.getCoreDB();
58
+ const cfg = cdb.getItem('config').addons;
59
+ return cfg;
60
+ }
61
+
62
+ /**
63
+ * 获取 mainServer配置
64
+ */
65
+ exports.getMainServerConfig = function() {
66
+ const cdb = this.getCoreDB();
67
+ const cfg = cdb.getItem('config').mainServer;
68
+ return cfg;
69
+ }
70
+
71
+ /**
72
+ * 获取 httpServer配置
73
+ */
74
+ exports.getHttpServerConfig = function() {
75
+ const cdb = this.getCoreDB();
76
+ const cfg = cdb.getItem('config').httpServer;
77
+ return cfg;
78
+ }
79
+
80
+ /**
81
+ * 获取 socketServer配置
82
+ */
83
+ exports.getSocketServerConfig = function() {
84
+ const cdb = this.getCoreDB();
85
+ const cfg = cdb.getItem('config').socketServer;
86
+ return cfg;
87
+ }
88
+
89
+ /**
90
+ * 获取 socket channel
91
+ */
92
+ exports.getSocketChannel = function() {
93
+ return Constants.socketIo.channel;
94
+ }
95
+
96
+ /**
97
+ * 获取 额外资源目录
98
+ */
99
+ exports.getExtraResourcesDir = function() {
100
+ const execDir = this.getExecDir();
101
+ const isPackaged = this.isPackaged();
102
+
103
+
104
+ // 资源路径不同
105
+ let dir = '';
106
+ if (isPackaged) {
107
+ // 打包后 execDir为 应用程序 exe\dmg\dep软件所在目录;打包前该值是项目根目录
108
+ // windows和MacOs不一样
109
+ dir = path.join(execDir, "resources", "extraResources");
110
+ if (eis.macOS()) {
111
+ dir = path.join(execDir, "..", "Resources", "extraResources");
112
+ }
113
+ } else {
114
+ // 打包前
115
+ dir = path.join(execDir, "build", "extraResources");
116
+ }
117
+ return dir;
118
+ }
119
+
120
+
@@ -0,0 +1,72 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const mkdirp = require('mkdirp');
4
+
5
+ exports.strictParse = function (str) {
6
+ const obj = JSON.parse(str);
7
+ if (!obj || typeof obj !== 'object') {
8
+ throw new Error('JSON string is not object');
9
+ }
10
+ return obj;
11
+ };
12
+
13
+ exports.readSync = function(filepath) {
14
+ if (!fs.existsSync(filepath)) {
15
+ throw new Error(filepath + ' is not found');
16
+ }
17
+ return JSON.parse(fs.readFileSync(filepath));
18
+ };
19
+
20
+ exports.writeSync = function(filepath, str, options) {
21
+ options = options || {};
22
+ if (!('space' in options)) {
23
+ options.space = 2;
24
+ }
25
+
26
+ mkdirp.sync(path.dirname(filepath));
27
+ if (typeof str === 'object') {
28
+ str = JSON.stringify(str, options.replacer, options.space) + '\n';
29
+ }
30
+
31
+ fs.writeFileSync(filepath, str);
32
+ };
33
+
34
+ exports.read = function(filepath) {
35
+ return fs.stat(filepath)
36
+ .then(function(stats) {
37
+ if (!stats.isFile()) {
38
+ throw new Error(filepath + ' is not found');
39
+ }
40
+ return fs.readFile(filepath);
41
+ })
42
+ .then(function(buf) {
43
+ return JSON.parse(buf);
44
+ });
45
+ };
46
+
47
+ exports.write = function(filepath, str, options) {
48
+ options = options || {};
49
+ if (!('space' in options)) {
50
+ options.space = 2;
51
+ }
52
+
53
+ if (typeof str === 'object') {
54
+ str = JSON.stringify(str, options.replacer, options.space) + '\n';
55
+ }
56
+
57
+ return mkdir(path.dirname(filepath))
58
+ .then(function() {
59
+ return fs.writeFile(filepath, str);
60
+ });
61
+ };
62
+
63
+ function mkdir(dir) {
64
+ return new Promise(function(resolve, reject) {
65
+ mkdirp(dir, function(err) {
66
+ if (err) {
67
+ return reject(err);
68
+ }
69
+ resolve();
70
+ });
71
+ });
72
+ }
@@ -0,0 +1,175 @@
1
+ const path = require('path');
2
+
3
+ /**
4
+ * 当前进程的所有env
5
+ */
6
+ exports.allEnv = function() {
7
+ return process.env;
8
+ }
9
+
10
+ /**
11
+ * 当前环境 - local | prod
12
+ */
13
+ exports.env = function() {
14
+ return process.env.EE_SERVER_ENV;
15
+ }
16
+
17
+ /**
18
+ * 获取 当前环境
19
+ */
20
+ exports.getEnv = this.env
21
+
22
+ /**
23
+ * 是否为开发环境
24
+ */
25
+ exports.isDev = function() {
26
+ if ( process.env.EE_SERVER_ENV === 'development' ||
27
+ process.env.EE_SERVER_ENV === 'dev' ||
28
+ process.env.EE_SERVER_ENV === 'local'
29
+ ) {
30
+ return true;
31
+ }
32
+
33
+ if ( process.env.NODE_ENV === 'development' ||
34
+ process.env.NODE_ENV === 'dev' ||
35
+ process.env.NODE_ENV === 'local'
36
+ ) {
37
+ return true;
38
+ }
39
+
40
+ return false;
41
+ };
42
+
43
+ /**
44
+ * 是否为渲染进程
45
+ */
46
+ exports.isRenderer = function() {
47
+ return (typeof process === 'undefined' ||
48
+ !process ||
49
+ process.type === 'renderer');
50
+ };
51
+
52
+ /**
53
+ * 是否为主进程
54
+ */
55
+ exports.isMain = function() {
56
+ return ( typeof process !== 'undefined' &&
57
+ process.type === 'browser');
58
+ };
59
+
60
+ /**
61
+ * 是否为node子进程
62
+ */
63
+ exports.isForkedChild = function() {
64
+ return (Number(process.env.ELECTRON_RUN_AS_NODE) === 1);
65
+ };
66
+
67
+ /**
68
+ * 当前进程类型
69
+ */
70
+ exports.processType = function() {
71
+ let type = '';
72
+ if (this.isMain()) {
73
+ type = 'browser';
74
+ } else if (this.isRenderer()) {
75
+ type = 'renderer';
76
+ } else if (this.isForkedChild()) {
77
+ type = 'child';
78
+ }
79
+
80
+ return type;
81
+ };
82
+
83
+ /**
84
+ * 获取数据存储路径
85
+ */
86
+ exports.getHomeDir = function () {
87
+ return process.env.EE_HOME;
88
+ }
89
+
90
+ /**
91
+ * 获取数据存储路径
92
+ */
93
+ exports.getStorageDir = function () {
94
+ const storageDir = path.join(this.getRootDir(), 'data');
95
+ return storageDir;
96
+ }
97
+
98
+ /**
99
+ * 获取日志存储路径
100
+ */
101
+ exports.getLogDir = function () {
102
+ const dir = path.join(this.getRootDir(), 'logs');
103
+ return dir;
104
+ }
105
+
106
+ /**
107
+ * 获取root目录 (dev-项目根目录,pro-app user data目录)
108
+ */
109
+ exports.getRootDir = function () {
110
+ const appDir = this.isDev() ? process.env.EE_HOME : process.env.EE_APP_USER_DATA;
111
+ return appDir;
112
+ }
113
+
114
+ /**
115
+ * 获取 base目录
116
+ */
117
+ exports.getBaseDir = function() {
118
+ return process.env.EE_BASE_DIR;
119
+ }
120
+
121
+ /**
122
+ * 获取 appUserData目录
123
+ */
124
+ exports.getAppUserDataDir = function() {
125
+ return process.env.EE_APP_USER_DATA;
126
+ }
127
+
128
+ /**
129
+ * 获取数据存储路径
130
+ */
131
+ exports.getHomeDir = function () {
132
+ return process.env.EE_HOME;
133
+ }
134
+
135
+ /**
136
+ * 获取 exec目录
137
+ */
138
+ exports.getExecDir = function() {
139
+ return process.env.EE_EXEC_DIR;
140
+ }
141
+
142
+ /**
143
+ * 获取操作系统用户目录
144
+ */
145
+ exports.getUserHomeDir = function () {
146
+ return process.env.EE_USER_HOME;
147
+ }
148
+
149
+ /**
150
+ * 获取主进程端口
151
+ */
152
+ exports.getMainPort = function () {
153
+ return process.env.EE_MAIN_PORT;
154
+ }
155
+
156
+ /**
157
+ * 获取内置socket端口
158
+ */
159
+ exports.getSocketPort = function () {
160
+ return process.env.EE_SOCKET_PORT;
161
+ }
162
+
163
+ /**
164
+ * 获取内置http端口
165
+ */
166
+ exports.getHttpPort = function () {
167
+ return process.env.EE_HTTP_PORT;
168
+ }
169
+
170
+ /**
171
+ * 是否打包
172
+ */
173
+ exports.isPackaged = function () {
174
+ return process.env.EE_IS_PACKAGED;
175
+ }
@@ -1,38 +1,36 @@
1
- 'use strict';
2
-
3
- const assert = require('assert');
4
- const is = require('is-type-of');
5
-
6
- exports.getProperties = (filepath, { caseStyle }) => {
7
- // if caseStyle is function, return the result of function
8
- if (is.function(caseStyle)) {
9
- const result = caseStyle(filepath);
10
- assert(is.array(result), `caseStyle expect an array, but got ${result}`);
11
- return result;
12
- }
13
- // use default camelize
14
- return this.defaultCamelize(filepath, caseStyle);
15
- }
16
-
17
- exports.defaultCamelize = (filepath, caseStyle) => {
18
- const properties = filepath.substring(0, filepath.lastIndexOf('.')).split('/');
19
- return properties.map(property => {
20
- if (!/^[a-z][a-z0-9_-]*$/i.test(property)) {
21
- throw new Error(`${property} is not match 'a-z0-9_-' in ${filepath}`);
22
- }
23
-
24
- property = property.replace(/[_-][a-z]/ig, s => s.substring(1).toUpperCase());
25
- let first = property[0];
26
- switch (caseStyle) {
27
- case 'lower':
28
- first = first.toLowerCase();
29
- break;
30
- case 'upper':
31
- first = first.toUpperCase();
32
- break;
33
- case 'camel':
34
- default:
35
- }
36
- return first + property.substring(1);
37
- });
1
+ const assert = require('assert');
2
+ const is = require('is-type-of');
3
+
4
+ exports.getProperties = (filepath, { caseStyle }) => {
5
+ // if caseStyle is function, return the result of function
6
+ if (is.function(caseStyle)) {
7
+ const result = caseStyle(filepath);
8
+ assert(is.array(result), `caseStyle expect an array, but got ${result}`);
9
+ return result;
10
+ }
11
+ // use default camelize
12
+ return this.defaultCamelize(filepath, caseStyle);
13
+ }
14
+
15
+ exports.defaultCamelize = (filepath, caseStyle) => {
16
+ const properties = filepath.substring(0, filepath.lastIndexOf('.')).split('/');
17
+ return properties.map(property => {
18
+ if (!/^[a-z][a-z0-9_-]*$/i.test(property)) {
19
+ throw new Error(`${property} is not match 'a-z0-9_-' in ${filepath}`);
20
+ }
21
+
22
+ property = property.replace(/[_-][a-z]/ig, s => s.substring(1).toUpperCase());
23
+ let first = property[0];
24
+ switch (caseStyle) {
25
+ case 'lower':
26
+ first = first.toLowerCase();
27
+ break;
28
+ case 'upper':
29
+ first = first.toUpperCase();
30
+ break;
31
+ case 'camel':
32
+ default:
33
+ }
34
+ return first + property.substring(1);
35
+ });
38
36
  }