@e-mc/document 0.1.0 → 0.3.0

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.
package/index.js CHANGED
@@ -194,8 +194,8 @@ class Document extends core_1.Client {
194
194
  catch {
195
195
  }
196
196
  }
197
- static createSourceMap(code, uri) {
198
- return new transform_1.SourceMap(code, uri);
197
+ static createSourceMap(code, uri, remove) {
198
+ return new transform_1.SourceMap(code, uri, remove);
199
199
  }
200
200
  static updateGradle(source, namespaces, value, options) {
201
201
  const local = /^(\w+)\s*(=)?\s*(".+"|'.+'|\([\S\s]+\)||\[[\S\s]+\]|\S+)$/.exec(value = value.trim());
@@ -860,13 +860,13 @@ class Document extends core_1.Client {
860
860
  }
861
861
  const username = this.host?.username || '';
862
862
  const cache = CACHE_TEMPLATE[name] || (CACHE_TEMPLATE[name] = {});
863
- const cacheKey = username + core_1.Client.asHash(template + (compile ? core_1.Client.asString(compile) : ''), 64 /* VALUES.HASH_MINLENGTH */);
863
+ const cacheKey = username + core_1.Client.asHash(template + (compile ? core_1.Client.asString(compile) : ''));
864
864
  let result = '', render, valid;
865
865
  if (!(render = cache[cacheKey])) {
866
866
  render = await context.compile(template, compile); // eslint-disable-line @typescript-eslint/await-thenable
867
867
  cache[cacheKey] = render;
868
868
  if (!core_1.Client.enabled("memory.settings.users" /* KEY_NAME.MEMORY_SETTINGS_USERS */, username)) {
869
- setTimeout(() => delete cache[cacheKey], 60000 /* VALUES.TEMPLATE_TIMEOUT */);
869
+ setTimeout(() => delete cache[cacheKey], 60000 /* VALUES.TEMPLATE_EXPIRES */);
870
870
  }
871
871
  }
872
872
  for (let i = 0, j = 0, row; i < length; ++i) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/document",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Document constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,9 +20,9 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/core": "0.1.0",
24
- "@e-mc/db": "0.1.0",
25
- "@e-mc/types": "0.1.0",
23
+ "@e-mc/core": "0.3.0",
24
+ "@e-mc/db": "0.3.0",
25
+ "@e-mc/types": "0.3.0",
26
26
  "chalk": "4.1.2",
27
27
  "htmlparser2": "^8.0.1",
28
28
  "js-yaml": "^4.1.0",
package/parse/dom.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IGNORE_FLAG, DomWriterConstructor, HtmlElementConstructor } from './document';
1
+ import type { IGNORE_FLAG, DomWriterConstructor, HtmlElementConstructor } from './types';
2
2
 
3
3
  declare namespace dom {
4
4
  const IGNORE_FLAG: IGNORE_FLAG;
package/parse/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IGNORE_FLAG, XmlWriterConstructor, XmlElementConstructor } from './document';
1
+ import type { IGNORE_FLAG, XmlWriterConstructor, XmlElementConstructor } from './types';
2
2
 
3
3
  declare namespace parse {
4
4
  const IGNORE_FLAG: IGNORE_FLAG;
@@ -1,4 +1,4 @@
1
- import type { XmlTagNode as IXmlTagNode, TagAppend, TagData } from '../../types/lib/squared';
1
+ import type { XmlTagNode as IXmlTagNode, TagAppend, TagData } from '../../../types/lib/squared';
2
2
 
3
3
  // @ts-ignore
4
4
  import type { ParserOptions } from 'htmlparser2';
@@ -105,7 +105,10 @@ export class IXmlBase {
105
105
  }
106
106
 
107
107
  export class IXmlWriter extends IXmlBase {
108
+ modifyCount: number;
109
+ failCount: number;
108
110
  elements: XmlTagNode[];
111
+ errors: Error[];
109
112
  parser?: ParserOptions;
110
113
  ignoreTagName?: string;
111
114
  ignoreTagGroup?: string[];
@@ -127,7 +130,7 @@ export class IXmlWriter extends IXmlBase {
127
130
  renameTag(node: XmlTagNode, tagName: string): void;
128
131
  indexTag(tagName: string, append?: TagAppend, offset?: number): void;
129
132
  resetTag(tagName: string): void;
130
- ignoreTag(value: string): void;
133
+ ignoreTag(value: StringOfArray): void;
131
134
  resetPosition(startIndex?: number): void;
132
135
  getElementById(id: string, ignoreCase?: boolean, options?: TagNodeByIdOptions): Undef<SourceTagNode>;
133
136
  getElementsByTagName(tagName: string, ignoreCase?: boolean, options?: TagNodeOptions): SourceTagNode[];
@@ -180,7 +183,7 @@ export class IXmlElement extends IXmlBase {
180
183
  parseOuterXml(value?: string, tagVoid?: boolean): [string, string, boolean];
181
184
  getTagOffset(replacement?: string, options?: TagOffsetOptions): Undef<TagOffsetMap>;
182
185
  setAttribute(name: string, value: string): void;
183
- getAttribute(name: string): Optional<string>;
186
+ getAttribute(name: string): string;
184
187
  removeAttribute(...names: string[]): void;
185
188
  hasAttribute(name: string): boolean;
186
189
  hasModifiedContent(): boolean;
@@ -233,5 +236,6 @@ export interface DomWriterConstructor extends XmlWriterConstructor {
233
236
  export class IHtmlElement extends IXmlElement {}
234
237
 
235
238
  export interface HtmlElementConstructor extends XmlElementConstructor {
239
+ readonly prototype: IHtmlElement;
236
240
  new(documentName: string, node: XmlTagNode, attributes?: PlainObject, options?: ParserAction): IHtmlElement;
237
241
  }
@@ -248,16 +248,27 @@ class SourceMap {
248
248
  static isRaw(map) {
249
249
  return (0, types_1.isObject)(map) && (0, types_1.isString)(map.mappings);
250
250
  }
251
- constructor(code, uri) {
252
- this.code = code;
251
+ constructor(code, uri, remove) {
253
252
  this.output = new Map();
254
253
  this.sourceMappingURL = '';
255
254
  this[_d] = undefined;
256
- this[kCode] = code;
257
- const map = SourceMap.findSourceMap(code, uri);
255
+ if (typeof uri === 'boolean') {
256
+ remove = uri;
257
+ uri = undefined;
258
+ }
259
+ let map;
260
+ if (remove) {
261
+ const items = SourceMap.removeSourceMappingURL(code);
262
+ if (map = items[2]) {
263
+ code = items[0];
264
+ }
265
+ }
266
+ map || (map = SourceMap.findSourceMap(code, uri));
258
267
  if (SourceMap.isRaw(map)) {
259
268
  this.nextMap('unknown', code, map);
260
269
  }
270
+ this[kCode] = code;
271
+ this.code = code;
261
272
  }
262
273
  reset(restore) {
263
274
  if (restore) {
@@ -307,4 +318,4 @@ class SourceMap {
307
318
  }
308
319
  exports.SourceMap = SourceMap;
309
320
  _d = kMap;
310
- SourceMap.RE_SOURCE_MAPPING_URL = /\n*(?:(\/\/)|(\/\*))\s*[#@][ ]+sourceMappingURL=(data:[^,]+,)?(\S+)\s*(\*\/)?\n?/g;
321
+ SourceMap.RE_SOURCE_MAPPING_URL = /(?:\r\n|\n)*(?:(\/\/)|(\/\*))\s*[#@][ ]+sourceMappingURL=(data:[^,]+,)?(\S+)\s*(\*\/)?\r?\n?/g;
package/util.d.ts CHANGED
@@ -2,7 +2,7 @@ declare namespace util {
2
2
  function loadPlugins<T = unknown>(name: string | unknown[], plugins?: unknown[]): T[];
3
3
  function replaceAll(source: string, valueOf: (name: string) => string, opening?: string, closing?: string): string;
4
4
  function concatString(values: string[] | string | undefined, newline?: string): string;
5
- function splitEnclosing(value: string, pattern?: string | RegExp, trim?: boolean | number, opening?: string, closing?: string): string[];
5
+ function splitEnclosing(value: string, pattern?: string | RegExp, options?: { trim?: boolean, start?: number, startWith?: number, count?: number } | boolean | number, opening?: string, closing?: string): string[];
6
6
  function appendSuffix(filename: string, value: string, separator?: string): string;
7
7
  function getIndent(value: string, spaces?: number): string;
8
8
  function getNewline(value: string): string;
package/util.js CHANGED
@@ -38,7 +38,7 @@ function concatString(values, newline) {
38
38
  return typeof values === 'string' ? values : '';
39
39
  }
40
40
  exports.concatString = concatString;
41
- function splitEnclosing(value, pattern, trim, opening = '(', closing = ')') {
41
+ function splitEnclosing(value, pattern, options, opening = '(', closing = ')') {
42
42
  pattern || (pattern = opening);
43
43
  let position = 0, index = -1, end = 0, char;
44
44
  if (typeof pattern === 'string') {
@@ -70,18 +70,27 @@ function splitEnclosing(value, pattern, trim, opening = '(', closing = ')') {
70
70
  return -1;
71
71
  };
72
72
  complete: {
73
- let j = 0, k = 0;
74
- if (typeof trim === 'number') {
75
- if (trim < 0) {
76
- result.push(value.substring(0, position = trim * -1));
77
- }
78
- else if (trim < 1) {
79
- position = trim * length;
80
- }
81
- else {
82
- k = trim;
73
+ let j = 0, count = 0, trim;
74
+ if (options !== undefined) {
75
+ switch (typeof options) {
76
+ case 'number':
77
+ count = options;
78
+ break;
79
+ case 'boolean':
80
+ trim = options;
81
+ break;
82
+ default: {
83
+ let start, startWith;
84
+ ({ trim, start, startWith, count = 0 } = options);
85
+ if (startWith !== undefined) {
86
+ result.push(value.substring(0, position = startWith));
87
+ }
88
+ else if (start !== undefined) {
89
+ position = start;
90
+ }
91
+ break;
92
+ }
83
93
  }
84
- trim = false;
85
94
  }
86
95
  while ((index = nextIndex()) !== -1) {
87
96
  if (index !== position) {
@@ -106,7 +115,7 @@ function splitEnclosing(value, pattern, trim, opening = '(', closing = ')') {
106
115
  if (position === length) {
107
116
  return result;
108
117
  }
109
- if (++j === k) {
118
+ if (++j === count) {
110
119
  break complete;
111
120
  }
112
121
  break found;
@@ -1,66 +0,0 @@
1
- import type { FinalizedElement } from '../../types/lib/squared';
2
- import type { DocumentOutput } from '../../types/lib/android';
3
-
4
- import type { DocumentConstructor, IDocument, IFileManager } from '../../types/lib';
5
- import type { ExternalAsset } from '../../types/lib/asset';
6
- import type { DocumentDirectory as IDocumentDirectory, DocumentModule as IDocumentModule } from '../../types/lib/settings';
7
-
8
- interface DocumentDirectory extends IDocumentDirectory {
9
- template?: string;
10
- }
11
-
12
- interface AndroidDocumentSettings extends PlainObject {
13
- extensions?: {
14
- task?: {
15
- command?: string;
16
- };
17
- };
18
- language?: {
19
- gradle?: "java" | "kotlin" | "java+kotlin";
20
- };
21
- directory?: DocumentDirectory;
22
- }
23
-
24
- type LanguageType = "gradle";
25
- type DocumentProperties = "targetAPI" | "elements" | "extensionData" | "directories";
26
-
27
- export interface UserConfig extends Omit<DocumentOutput, DocumentProperties> {
28
- mainParentDir: string;
29
- mainSrcDir: string;
30
- mainActivityFile: string;
31
- javaVersion: number;
32
- dataBinding: boolean;
33
- }
34
-
35
- export interface DocumentModule extends IDocumentModule {
36
- settings?: AndroidDocumentSettings;
37
- }
38
-
39
- export interface TemplateData {
40
- localUri?: string;
41
- source?: string;
42
- existing?: boolean;
43
- kotlin?: boolean;
44
- language?: string;
45
- }
46
-
47
- export interface FindTemplateOptions {
48
- detect?: boolean;
49
- languageOf?: LanguageType;
50
- }
51
-
52
- export interface IAndroidDocument<T extends IFileManager<U>, U extends DocumentAsset = DocumentAsset> extends IDocument<T, U>, Pick<DocumentOutput, DocumentProperties> {
53
- config: UserConfig;
54
- elements: FinalizedElement[];
55
- extensionData: PlainObject;
56
- findTemplate(baseDir: string, filename: string, options?: FindTemplateOptions): TemplateData;
57
- detectKts(...paths: string[]): Null<boolean>;
58
- get settings(): AndroidDocumentSettings;
59
- }
60
-
61
- export interface AndroidDocumentConstructor<T extends IFileManager<U>, U extends DocumentAsset = DocumentAsset> extends DocumentConstructor<T, U> {
62
- readonly prototype: IAndroidDocument<T, U>;
63
- new(module?: DocumentModule, ...args: unknown[]): IAndroidDocument<T, U>;
64
- }
65
-
66
- export type DocumentAsset = ExternalAsset;
@@ -1,186 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const path = require("path");
4
- const htmlparser2 = require("htmlparser2");
5
- const domhandler = require("domhandler");
6
- const domutils = require("domutils");
7
- const domserializer = require("dom-serializer");
8
- const util_1 = require("../../../../util");
9
- const types_1 = require("../../../../../types");
10
- const Parser = htmlparser2.Parser;
11
- const DomHandler = domhandler.DomHandler;
12
- function finalize(instance) {
13
- const config = instance.config;
14
- if (!config.manifest) {
15
- return;
16
- }
17
- let { localUri, source, existing } = instance.findTemplate(path.join(this.baseDirectory, config.mainParentDir, config.mainSrcDir), "AndroidManifest.xml" /* MANIFEST.FILENAME */, { detect: !this.archiving });
18
- if (localUri && source && instance.canWrite(localUri, { ownPermissionOnly: true })) {
19
- const { package: manifestPackage, application = {} } = config.manifest;
20
- const { supportsRtl, label, theme, activity, metaData = [], activityName, fontProvider } = application;
21
- const profileable = config.profileable;
22
- let modified;
23
- if (!existing) {
24
- source = (0, util_1.replaceAll)(source.replace(manifestPackage ? '{{package}}' : ' package="{{package}}"', manifestPackage || ''), (name) => {
25
- switch (name) {
26
- case 'supportsRtl':
27
- return supportsRtl === false ? 'false' : 'true';
28
- case 'label':
29
- return '@string/' + (label || 'app_name');
30
- case 'theme':
31
- return theme ? '@style/' + theme : '';
32
- case 'activityName':
33
- return activityName || '';
34
- case 'profileable':
35
- return profileable ? 'true' : 'false';
36
- default:
37
- return '';
38
- }
39
- });
40
- }
41
- else if (manifestPackage || theme || activityName || activity || supportsRtl !== undefined) {
42
- new Parser(new DomHandler((err, dom) => {
43
- if (err) {
44
- instance.writeFail(['Unable to parse XML document', "AndroidManifest.xml" /* MANIFEST.FILENAME */], err, 0 /* LOG_TYPE.UNKNOWN */);
45
- return;
46
- }
47
- let target = null;
48
- if (manifestPackage && (target = domutils.findOne(elem => elem.tagName === 'manifest', dom, true))) {
49
- target.attribs['package'] = manifestPackage;
50
- modified = true;
51
- }
52
- if (target = domutils.findOne(elem => elem.tagName === 'application', dom, true)) {
53
- if (label) {
54
- target.attribs['android:label'] = '@string/' + label;
55
- modified = true;
56
- }
57
- if (theme) {
58
- target.attribs['android:theme'] = '@style/' + theme;
59
- modified = true;
60
- }
61
- if (supportsRtl !== undefined) {
62
- target.attribs['android:supportsRtl'] = supportsRtl.toString();
63
- modified = true;
64
- }
65
- if (activityName || activity) {
66
- const activities = domutils.getElementsByTagName('activity', target, true);
67
- if (activityName) {
68
- for (const item of activities) {
69
- const intentFilter = domutils.findAll(elem => elem.tagName === 'intent-filter', [item]);
70
- if (intentFilter.length) {
71
- const action = domutils.findOne(elem => elem.tagName === 'action' && elem.attribs['android:name'] === 'android.intent.action.MAIN', intentFilter);
72
- if (action) {
73
- const attribs = item.attribs;
74
- attribs['android:name'] = activityName;
75
- attribs['android:exported'] = 'true';
76
- modified = true;
77
- break;
78
- }
79
- }
80
- }
81
- }
82
- if (activity) {
83
- for (const name in activity) {
84
- const { layout: attribs } = activity[name];
85
- if (attribs) {
86
- let element = domutils.findOne(elem => elem.attribs['android:name'] === name, activities);
87
- if (!element) {
88
- domutils.appendChild(target, element = new domhandler.Element('activity', { 'android:name': name }));
89
- domutils.append(element, new domhandler.Text('\n'));
90
- }
91
- let layout = domutils.findOne(elem => elem.tagName === 'layout', [element]), found = true;
92
- if (!layout) {
93
- domutils.appendChild(element, layout = new domhandler.Element('layout', {}));
94
- domutils.append(layout, new domhandler.Text('\n'));
95
- found = false;
96
- }
97
- for (const attr in attribs) {
98
- switch (attr) {
99
- case 'defaultHeight':
100
- case 'defaultWidth':
101
- case 'minHeight':
102
- case 'minWidth':
103
- case 'gravity': {
104
- const value = attribs[attr];
105
- if (!found || layout.attribs['android:' + attr] !== value) {
106
- layout.attribs['android:' + attr] = value;
107
- modified = true;
108
- }
109
- break;
110
- }
111
- }
112
- }
113
- }
114
- }
115
- }
116
- }
117
- if (profileable !== undefined) {
118
- const attribs = profileable ? { 'android:shell': 'true', 'android:enabled': 'true' } : { 'android:enabled': 'false' };
119
- const element = domutils.findOne(elem => elem.tagName === 'profileable', [target]);
120
- if (element) {
121
- Object.assign(element.attribs, attribs);
122
- }
123
- else if (profileable) {
124
- domutils.appendChild(target, new domhandler.Element('profileable', attribs));
125
- domutils.appendChild(target, new domhandler.Text('\n'));
126
- }
127
- modified = true;
128
- }
129
- if (modified) {
130
- source = domserializer.default(dom, { xmlMode: true });
131
- }
132
- }
133
- }), { xmlMode: true, decodeEntities: false }).end(source);
134
- }
135
- if (fontProvider) {
136
- metaData.push({ name: 'preloaded_fonts', resource: '@array/' + fontProvider });
137
- }
138
- if ((0, types_1.isArray)(metaData)) {
139
- new Parser(new DomHandler((err, dom) => {
140
- if (err) {
141
- instance.writeFail(['Unable to parse XML document', "AndroidManifest.xml" /* MANIFEST.FILENAME */], err, 0 /* LOG_TYPE.UNKNOWN */);
142
- return;
143
- }
144
- const app = domutils.findOne(elem => elem.tagName === 'application', dom, true);
145
- if (app) {
146
- for (const { name, resource, value } of metaData) {
147
- if (name && (resource || value)) {
148
- let item = domutils.findOne(elem => elem.tagName === 'meta-data' && elem.attribs['android:name'] === name, app.childNodes);
149
- if (!item) {
150
- domutils.appendChild(app, item = new domhandler.Element('meta-data', { 'android:name': name }));
151
- domutils.append(item, new domhandler.Text('\n'));
152
- }
153
- else if (item.attribs['android:resource'] === resource || item.attribs['android:value'] === value) {
154
- continue;
155
- }
156
- if (resource) {
157
- item.attribs['android:resource'] = resource;
158
- }
159
- if (value) {
160
- item.attribs['android:value'] = value;
161
- }
162
- source = domserializer.default(dom, { xmlMode: true });
163
- modified = true;
164
- }
165
- }
166
- }
167
- }), { xmlMode: true, decodeEntities: false }).end(source);
168
- }
169
- if (modified || !existing) {
170
- try {
171
- if (instance.writeFile(localUri, source, { encoding: 'utf-8', ownPermissionOnly: true, throwsPermission: true }) && !existing) {
172
- this.add(localUri);
173
- }
174
- }
175
- catch (err) {
176
- this.writeFail(["Unable to write file" /* ERR_MESSAGE.WRITE_FILE */, path.basename(localUri)], err, 8192 /* LOG_TYPE.PERMISSION */);
177
- }
178
- }
179
- }
180
- }
181
- exports.default = finalize;
182
-
183
- if (exports.default) {
184
- module.exports = exports.default;
185
- module.exports.default = exports.default;
186
- }