1 | import { Application } from '../application';
|
2 | import * as fs from '../file-system';
|
3 | import { Trace } from '../trace';
|
4 | const cache = new Set();
|
5 | let initialized = false;
|
6 | function register(name, loader) {
|
7 | if (Trace.isEnabled()) {
|
8 | Trace.write(`[Compat] Register module: ${name}`, Trace.categories.ModuleNameResolver);
|
9 | }
|
10 | global.registerModule(name, loader);
|
11 | if (name.startsWith('tns_modules')) {
|
12 | const nonTnsModulesName = name.substr('tns_modules'.length + 1);
|
13 | if (Trace.isEnabled()) {
|
14 | Trace.write(`[Compat] Register module: ${nonTnsModulesName}`, Trace.categories.ModuleNameResolver);
|
15 | }
|
16 | global.registerModule(nonTnsModulesName, loader);
|
17 | }
|
18 | }
|
19 | function processFile(file) {
|
20 | const filePathRelativeToApp = file.path.substr(fs.knownFolders.currentApp().path.length + 1);
|
21 | const loadContent = () => file.readTextSync();
|
22 | switch (file.extension.toLocaleLowerCase()) {
|
23 | case '.js': {
|
24 | const noExtPath = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - '.js'.length);
|
25 | register(filePathRelativeToApp, function () {
|
26 | return global.require(file.path);
|
27 | });
|
28 | register(noExtPath, function () {
|
29 | return global.require(file.path);
|
30 | });
|
31 | break;
|
32 | }
|
33 | case '.css':
|
34 | register(filePathRelativeToApp, loadContent);
|
35 | break;
|
36 | case '.xml':
|
37 | register(filePathRelativeToApp, loadContent);
|
38 | break;
|
39 | }
|
40 | if (file.name === 'package.json') {
|
41 | const json = global.require(file.path);
|
42 | if (json.main) {
|
43 | const name = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - 'package.json'.length - 1);
|
44 | const requirePath = fs.path.join(file.parent.path, json.main);
|
45 | register(name, () => global.require(requirePath));
|
46 | }
|
47 | }
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function processFolder(path) {
|
54 | if (cache.has(path)) {
|
55 | return true;
|
56 | }
|
57 | cache.add(path);
|
58 | if (Trace.isEnabled()) {
|
59 | Trace.write(`[Compat] Processing folder: ${path}`, Trace.categories.ModuleNameResolver);
|
60 | }
|
61 | let folderEmpty = true;
|
62 | if (fs.Folder.exists(path)) {
|
63 | const folder = fs.Folder.fromPath(path);
|
64 | folder.eachEntity((file) => {
|
65 | if (file instanceof fs.File) {
|
66 | processFile(file);
|
67 | folderEmpty = false;
|
68 | }
|
69 | return true;
|
70 | });
|
71 | }
|
72 | return !folderEmpty;
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | export function registerModulesFromFileSystem(moduleName) {
|
80 | initialize();
|
81 | let folderProcessed = false;
|
82 | let parentFolderProcessed = false;
|
83 |
|
84 | const path = fs.path.join(fs.knownFolders.currentApp().path, moduleName);
|
85 | if (fs.Folder.exists(path)) {
|
86 | folderProcessed = processFolder(path);
|
87 | }
|
88 |
|
89 | const parentName = moduleName.substr(0, moduleName.lastIndexOf(fs.path.separator));
|
90 | const parentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, parentName);
|
91 | if (fs.Folder.exists(parentFolderPath)) {
|
92 | parentFolderProcessed = processFolder(parentFolderPath);
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 | if (folderProcessed || (parentFolderProcessed && parentName)) {
|
98 | return;
|
99 | }
|
100 |
|
101 | const tnsModulesPath = fs.path.join(fs.knownFolders.currentApp().path, 'tns_modules', moduleName);
|
102 | if (fs.Folder.exists(tnsModulesPath)) {
|
103 | processFolder(tnsModulesPath);
|
104 | }
|
105 |
|
106 | if (parentName) {
|
107 | const tnsParentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, 'tns_modules', parentName);
|
108 | if (fs.Folder.exists(tnsParentFolderPath)) {
|
109 | processFolder(tnsParentFolderPath);
|
110 | }
|
111 | }
|
112 | }
|
113 | function initialize() {
|
114 | if (!initialized) {
|
115 | Application.on('livesync', (args) => cache.clear());
|
116 | initialized = true;
|
117 | }
|
118 | }
|
119 |
|
\ | No newline at end of file |