@heybox/hb-sdk 0.3.3 → 0.4.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 (35) hide show
  1. package/README.md +63 -28
  2. package/dist/cli-chunks/browser-RAy8e8cV.cjs +635 -0
  3. package/dist/cli-chunks/create-Ds8A82lV.cjs +1376 -0
  4. package/dist/cli-chunks/deploy-D4uxwB2W.cjs +3730 -0
  5. package/dist/cli-chunks/dev-v-tcA7mM.cjs +955 -0
  6. package/dist/cli-chunks/doctor-C8NP7bow.cjs +186 -0
  7. package/dist/cli-chunks/index-BWrMUHh9.cjs +64023 -0
  8. package/dist/cli-chunks/index-DDqd9qAR.cjs +13348 -0
  9. package/dist/cli-chunks/login-BJVOo-hq.cjs +193 -0
  10. package/dist/cli-chunks/session-Iyxc2AGl.cjs +3040 -0
  11. package/dist/cli.cjs +19 -77707
  12. package/dist/devtools/mock-host/index.html +62 -2
  13. package/dist/devtools/mock-host/main.js +3246 -20
  14. package/dist/index.cjs.js +20 -0
  15. package/dist/index.esm.js +20 -0
  16. package/dist/miniapp-publish.cjs.js +2810 -4
  17. package/dist/miniapp-publish.esm.js +2809 -4
  18. package/dist/protocol.cjs.js +15 -0
  19. package/dist/protocol.esm.js +15 -1
  20. package/dist/templates/vue3-vite-ts/README.md.ejs +6 -2
  21. package/dist/vite.cjs.js +2814 -14
  22. package/dist/vite.esm.js +2814 -14
  23. package/package.json +6 -1
  24. package/skill/SKILL.md +19 -13
  25. package/skill/references/api-protocol.md +7 -2
  26. package/skill/references/api-root.md +24 -13
  27. package/skill/references/cli.md +339 -104
  28. package/skill/scripts/sync-references.mjs +17 -1
  29. package/skill/skill.json +4 -4
  30. package/types/index.d.ts +1 -1
  31. package/types/miniapp-manifest/schema.d.ts +2 -1
  32. package/types/miniapp-publish/index.d.ts +2 -1
  33. package/types/modules/viewport/index.d.ts +33 -0
  34. package/types/protocol/capabilities.d.ts +17 -2
  35. package/types/protocol.d.ts +2 -2
@@ -0,0 +1,1376 @@
1
+ 'use strict';
2
+
3
+ var fs$1 = require('node:fs');
4
+ var fs = require('node:fs/promises');
5
+ var path = require('node:path');
6
+ var require$$0 = require('fs');
7
+ var require$$1 = require('path');
8
+ var index = require('./index-DDqd9qAR.cjs');
9
+ require('node:module');
10
+ require('os');
11
+ require('readline');
12
+ require('tty');
13
+ require('assert');
14
+ require('events');
15
+ require('stream');
16
+ require('buffer');
17
+ require('util');
18
+
19
+ var ejs = {};
20
+
21
+ var utils = {};
22
+
23
+ /*
24
+ * EJS Embedded JavaScript templates
25
+ * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
26
+ *
27
+ * Licensed under the Apache License, Version 2.0 (the "License");
28
+ * you may not use this file except in compliance with the License.
29
+ * You may obtain a copy of the License at
30
+ *
31
+ * http://www.apache.org/licenses/LICENSE-2.0
32
+ *
33
+ * Unless required by applicable law or agreed to in writing, software
34
+ * distributed under the License is distributed on an "AS IS" BASIS,
35
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36
+ * See the License for the specific language governing permissions and
37
+ * limitations under the License.
38
+ *
39
+ */
40
+
41
+ var hasRequiredUtils;
42
+
43
+ function requireUtils () {
44
+ if (hasRequiredUtils) return utils;
45
+ hasRequiredUtils = 1;
46
+ (function (exports) {
47
+
48
+ var regExpChars = /[|\\{}()[\]^$+*?.]/g;
49
+ var hasOwnProperty = Object.prototype.hasOwnProperty;
50
+ var hasOwn = function (obj, key) { return hasOwnProperty.apply(obj, [key]); };
51
+
52
+ /**
53
+ * Escape characters reserved in regular expressions.
54
+ *
55
+ * If `string` is `undefined` or `null`, the empty string is returned.
56
+ *
57
+ * @param {String} string Input string
58
+ * @return {String} Escaped string
59
+ * @static
60
+ * @private
61
+ */
62
+ exports.escapeRegExpChars = function (string) {
63
+ // istanbul ignore if
64
+ if (!string) {
65
+ return '';
66
+ }
67
+ return String(string).replace(regExpChars, '\\$&');
68
+ };
69
+
70
+ var _ENCODE_HTML_RULES = {
71
+ '&': '&',
72
+ '<': '&lt;',
73
+ '>': '&gt;',
74
+ '"': '&#34;',
75
+ "'": '&#39;'
76
+ };
77
+ var _MATCH_HTML = /[&<>'"]/g;
78
+
79
+ function encode_char(c) {
80
+ return _ENCODE_HTML_RULES[c] || c;
81
+ }
82
+
83
+ /**
84
+ * Stringified version of constants used by {@link module:utils.escapeXML}.
85
+ *
86
+ * It is used in the process of generating {@link ClientFunction}s.
87
+ *
88
+ * @readonly
89
+ * @type {String}
90
+ */
91
+
92
+ var escapeFuncStr =
93
+ 'var _ENCODE_HTML_RULES = {\n'
94
+ + ' "&": "&amp;"\n'
95
+ + ' , "<": "&lt;"\n'
96
+ + ' , ">": "&gt;"\n'
97
+ + ' , \'"\': "&#34;"\n'
98
+ + ' , "\'": "&#39;"\n'
99
+ + ' }\n'
100
+ + ' , _MATCH_HTML = /[&<>\'"]/g;\n'
101
+ + 'function encode_char(c) {\n'
102
+ + ' return _ENCODE_HTML_RULES[c] || c;\n'
103
+ + '};\n';
104
+
105
+ /**
106
+ * Escape characters reserved in XML.
107
+ *
108
+ * If `markup` is `undefined` or `null`, the empty string is returned.
109
+ *
110
+ * @implements {EscapeCallback}
111
+ * @param {String} markup Input string
112
+ * @return {String} Escaped string
113
+ * @static
114
+ * @private
115
+ */
116
+
117
+ exports.escapeXML = function (markup) {
118
+ return markup == undefined
119
+ ? ''
120
+ : String(markup)
121
+ .replace(_MATCH_HTML, encode_char);
122
+ };
123
+
124
+ function escapeXMLToString() {
125
+ return Function.prototype.toString.call(this) + ';\n' + escapeFuncStr;
126
+ }
127
+
128
+ try {
129
+ if (typeof Object.defineProperty === 'function') {
130
+ // If the Function prototype is frozen, the "toString" property is non-writable. This means that any objects which inherit this property
131
+ // cannot have the property changed using an assignment. If using strict mode, attempting that will cause an error. If not using strict
132
+ // mode, attempting that will be silently ignored.
133
+ // However, we can still explicitly shadow the prototype's "toString" property by defining a new "toString" property on this object.
134
+ Object.defineProperty(exports.escapeXML, 'toString', { value: escapeXMLToString });
135
+ } else {
136
+ // If Object.defineProperty() doesn't exist, attempt to shadow this property using the assignment operator.
137
+ exports.escapeXML.toString = escapeXMLToString;
138
+ }
139
+ } catch (err) {
140
+ console.warn('Unable to set escapeXML.toString (is the Function prototype frozen?)');
141
+ }
142
+
143
+ /**
144
+ * Naive copy of properties from one object to another.
145
+ * Does not recurse into non-scalar properties
146
+ * Does not check to see if the property has a value before copying
147
+ *
148
+ * @param {Object} to Destination object
149
+ * @param {Object} from Source object
150
+ * @return {Object} Destination object
151
+ * @static
152
+ * @private
153
+ */
154
+ exports.shallowCopy = function (to, from) {
155
+ from = from || {};
156
+ if ((to !== null) && (to !== undefined)) {
157
+ for (var p in from) {
158
+ if (!hasOwn(from, p)) {
159
+ continue;
160
+ }
161
+ if (p === '__proto__' || p === 'constructor') {
162
+ continue;
163
+ }
164
+ to[p] = from[p];
165
+ }
166
+ }
167
+ return to;
168
+ };
169
+
170
+ /**
171
+ * Naive copy of a list of key names, from one object to another.
172
+ * Only copies property if it is actually defined
173
+ * Does not recurse into non-scalar properties
174
+ *
175
+ * @param {Object} to Destination object
176
+ * @param {Object} from Source object
177
+ * @param {Array} list List of properties to copy
178
+ * @return {Object} Destination object
179
+ * @static
180
+ * @private
181
+ */
182
+ exports.shallowCopyFromList = function (to, from, list) {
183
+ list = list || [];
184
+ from = from || {};
185
+ if ((to !== null) && (to !== undefined)) {
186
+ for (var i = 0; i < list.length; i++) {
187
+ var p = list[i];
188
+ if (typeof from[p] != 'undefined') {
189
+ if (!hasOwn(from, p)) {
190
+ continue;
191
+ }
192
+ if (p === '__proto__' || p === 'constructor') {
193
+ continue;
194
+ }
195
+ to[p] = from[p];
196
+ }
197
+ }
198
+ }
199
+ return to;
200
+ };
201
+
202
+ /**
203
+ * Simple in-process cache implementation. Does not implement limits of any
204
+ * sort.
205
+ *
206
+ * @implements {Cache}
207
+ * @static
208
+ * @private
209
+ */
210
+ exports.cache = {
211
+ _data: {},
212
+ set: function (key, val) {
213
+ this._data[key] = val;
214
+ },
215
+ get: function (key) {
216
+ return this._data[key];
217
+ },
218
+ remove: function (key) {
219
+ delete this._data[key];
220
+ },
221
+ reset: function () {
222
+ this._data = {};
223
+ }
224
+ };
225
+
226
+ /**
227
+ * Transforms hyphen case variable into camel case.
228
+ *
229
+ * @param {String} string Hyphen case string
230
+ * @return {String} Camel case string
231
+ * @static
232
+ * @private
233
+ */
234
+ exports.hyphenToCamel = function (str) {
235
+ return str.replace(/-[a-z]/g, function (match) { return match[1].toUpperCase(); });
236
+ };
237
+
238
+ /**
239
+ * Returns a null-prototype object in runtimes that support it
240
+ *
241
+ * @return {Object} Object, prototype will be set to null where possible
242
+ * @static
243
+ * @private
244
+ */
245
+ exports.createNullProtoObjWherePossible = (function () {
246
+ if (typeof Object.create == 'function') {
247
+ return function () {
248
+ return Object.create(null);
249
+ };
250
+ }
251
+ if (!({__proto__: null} instanceof Object)) {
252
+ return function () {
253
+ return {__proto__: null};
254
+ };
255
+ }
256
+ // Not possible, just pass through
257
+ return function () {
258
+ return {};
259
+ };
260
+ })();
261
+
262
+ exports.hasOwnOnlyObject = function (obj) {
263
+ var o = exports.createNullProtoObjWherePossible();
264
+ for (var p in obj) {
265
+ if (hasOwn(obj, p)) {
266
+ o[p] = obj[p];
267
+ }
268
+ }
269
+ return o;
270
+ };
271
+ } (utils));
272
+ return utils;
273
+ }
274
+
275
+ var version = "3.1.10";
276
+ var require$$3 = {
277
+ version: version};
278
+
279
+ /*
280
+ * EJS Embedded JavaScript templates
281
+ * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
282
+ *
283
+ * Licensed under the Apache License, Version 2.0 (the "License");
284
+ * you may not use this file except in compliance with the License.
285
+ * You may obtain a copy of the License at
286
+ *
287
+ * http://www.apache.org/licenses/LICENSE-2.0
288
+ *
289
+ * Unless required by applicable law or agreed to in writing, software
290
+ * distributed under the License is distributed on an "AS IS" BASIS,
291
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
292
+ * See the License for the specific language governing permissions and
293
+ * limitations under the License.
294
+ *
295
+ */
296
+
297
+ var hasRequiredEjs;
298
+
299
+ function requireEjs () {
300
+ if (hasRequiredEjs) return ejs;
301
+ hasRequiredEjs = 1;
302
+ (function (exports) {
303
+
304
+ /**
305
+ * @file Embedded JavaScript templating engine. {@link http://ejs.co}
306
+ * @author Matthew Eernisse <mde@fleegix.org>
307
+ * @author Tiancheng "Timothy" Gu <timothygu99@gmail.com>
308
+ * @project EJS
309
+ * @license {@link http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0}
310
+ */
311
+
312
+ /**
313
+ * EJS internal functions.
314
+ *
315
+ * Technically this "module" lies in the same file as {@link module:ejs}, for
316
+ * the sake of organization all the private functions re grouped into this
317
+ * module.
318
+ *
319
+ * @module ejs-internal
320
+ * @private
321
+ */
322
+
323
+ /**
324
+ * Embedded JavaScript templating engine.
325
+ *
326
+ * @module ejs
327
+ * @public
328
+ */
329
+
330
+
331
+ var fs = require$$0;
332
+ var path = require$$1;
333
+ var utils = requireUtils();
334
+
335
+ var scopeOptionWarned = false;
336
+ /** @type {string} */
337
+ var _VERSION_STRING = require$$3.version;
338
+ var _DEFAULT_OPEN_DELIMITER = '<';
339
+ var _DEFAULT_CLOSE_DELIMITER = '>';
340
+ var _DEFAULT_DELIMITER = '%';
341
+ var _DEFAULT_LOCALS_NAME = 'locals';
342
+ var _NAME = 'ejs';
343
+ var _REGEX_STRING = '(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
344
+ var _OPTS_PASSABLE_WITH_DATA = ['delimiter', 'scope', 'context', 'debug', 'compileDebug',
345
+ 'client', '_with', 'rmWhitespace', 'strict', 'filename', 'async'];
346
+ // We don't allow 'cache' option to be passed in the data obj for
347
+ // the normal `render` call, but this is where Express 2 & 3 put it
348
+ // so we make an exception for `renderFile`
349
+ var _OPTS_PASSABLE_WITH_DATA_EXPRESS = _OPTS_PASSABLE_WITH_DATA.concat('cache');
350
+ var _BOM = /^\uFEFF/;
351
+ var _JS_IDENTIFIER = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
352
+
353
+ /**
354
+ * EJS template function cache. This can be a LRU object from lru-cache NPM
355
+ * module. By default, it is {@link module:utils.cache}, a simple in-process
356
+ * cache that grows continuously.
357
+ *
358
+ * @type {Cache}
359
+ */
360
+
361
+ exports.cache = utils.cache;
362
+
363
+ /**
364
+ * Custom file loader. Useful for template preprocessing or restricting access
365
+ * to a certain part of the filesystem.
366
+ *
367
+ * @type {fileLoader}
368
+ */
369
+
370
+ exports.fileLoader = fs.readFileSync;
371
+
372
+ /**
373
+ * Name of the object containing the locals.
374
+ *
375
+ * This variable is overridden by {@link Options}`.localsName` if it is not
376
+ * `undefined`.
377
+ *
378
+ * @type {String}
379
+ * @public
380
+ */
381
+
382
+ exports.localsName = _DEFAULT_LOCALS_NAME;
383
+
384
+ /**
385
+ * Promise implementation -- defaults to the native implementation if available
386
+ * This is mostly just for testability
387
+ *
388
+ * @type {PromiseConstructorLike}
389
+ * @public
390
+ */
391
+
392
+ exports.promiseImpl = (new Function('return this;'))().Promise;
393
+
394
+ /**
395
+ * Get the path to the included file from the parent file path and the
396
+ * specified path.
397
+ *
398
+ * @param {String} name specified path
399
+ * @param {String} filename parent file path
400
+ * @param {Boolean} [isDir=false] whether the parent file path is a directory
401
+ * @return {String}
402
+ */
403
+ exports.resolveInclude = function(name, filename, isDir) {
404
+ var dirname = path.dirname;
405
+ var extname = path.extname;
406
+ var resolve = path.resolve;
407
+ var includePath = resolve(isDir ? filename : dirname(filename), name);
408
+ var ext = extname(name);
409
+ if (!ext) {
410
+ includePath += '.ejs';
411
+ }
412
+ return includePath;
413
+ };
414
+
415
+ /**
416
+ * Try to resolve file path on multiple directories
417
+ *
418
+ * @param {String} name specified path
419
+ * @param {Array<String>} paths list of possible parent directory paths
420
+ * @return {String}
421
+ */
422
+ function resolvePaths(name, paths) {
423
+ var filePath;
424
+ if (paths.some(function (v) {
425
+ filePath = exports.resolveInclude(name, v, true);
426
+ return fs.existsSync(filePath);
427
+ })) {
428
+ return filePath;
429
+ }
430
+ }
431
+
432
+ /**
433
+ * Get the path to the included file by Options
434
+ *
435
+ * @param {String} path specified path
436
+ * @param {Options} options compilation options
437
+ * @return {String}
438
+ */
439
+ function getIncludePath(path, options) {
440
+ var includePath;
441
+ var filePath;
442
+ var views = options.views;
443
+ var match = /^[A-Za-z]+:\\|^\//.exec(path);
444
+
445
+ // Abs path
446
+ if (match && match.length) {
447
+ path = path.replace(/^\/*/, '');
448
+ if (Array.isArray(options.root)) {
449
+ includePath = resolvePaths(path, options.root);
450
+ } else {
451
+ includePath = exports.resolveInclude(path, options.root || '/', true);
452
+ }
453
+ }
454
+ // Relative paths
455
+ else {
456
+ // Look relative to a passed filename first
457
+ if (options.filename) {
458
+ filePath = exports.resolveInclude(path, options.filename);
459
+ if (fs.existsSync(filePath)) {
460
+ includePath = filePath;
461
+ }
462
+ }
463
+ // Then look in any views directories
464
+ if (!includePath && Array.isArray(views)) {
465
+ includePath = resolvePaths(path, views);
466
+ }
467
+ if (!includePath && typeof options.includer !== 'function') {
468
+ throw new Error('Could not find the include file "' +
469
+ options.escapeFunction(path) + '"');
470
+ }
471
+ }
472
+ return includePath;
473
+ }
474
+
475
+ /**
476
+ * Get the template from a string or a file, either compiled on-the-fly or
477
+ * read from cache (if enabled), and cache the template if needed.
478
+ *
479
+ * If `template` is not set, the file specified in `options.filename` will be
480
+ * read.
481
+ *
482
+ * If `options.cache` is true, this function reads the file from
483
+ * `options.filename` so it must be set prior to calling this function.
484
+ *
485
+ * @memberof module:ejs-internal
486
+ * @param {Options} options compilation options
487
+ * @param {String} [template] template source
488
+ * @return {(TemplateFunction|ClientFunction)}
489
+ * Depending on the value of `options.client`, either type might be returned.
490
+ * @static
491
+ */
492
+
493
+ function handleCache(options, template) {
494
+ var func;
495
+ var filename = options.filename;
496
+ var hasTemplate = arguments.length > 1;
497
+
498
+ if (options.cache) {
499
+ if (!filename) {
500
+ throw new Error('cache option requires a filename');
501
+ }
502
+ func = exports.cache.get(filename);
503
+ if (func) {
504
+ return func;
505
+ }
506
+ if (!hasTemplate) {
507
+ template = fileLoader(filename).toString().replace(_BOM, '');
508
+ }
509
+ }
510
+ else if (!hasTemplate) {
511
+ // istanbul ignore if: should not happen at all
512
+ if (!filename) {
513
+ throw new Error('Internal EJS error: no file name or template '
514
+ + 'provided');
515
+ }
516
+ template = fileLoader(filename).toString().replace(_BOM, '');
517
+ }
518
+ func = exports.compile(template, options);
519
+ if (options.cache) {
520
+ exports.cache.set(filename, func);
521
+ }
522
+ return func;
523
+ }
524
+
525
+ /**
526
+ * Try calling handleCache with the given options and data and call the
527
+ * callback with the result. If an error occurs, call the callback with
528
+ * the error. Used by renderFile().
529
+ *
530
+ * @memberof module:ejs-internal
531
+ * @param {Options} options compilation options
532
+ * @param {Object} data template data
533
+ * @param {RenderFileCallback} cb callback
534
+ * @static
535
+ */
536
+
537
+ function tryHandleCache(options, data, cb) {
538
+ var result;
539
+ if (!cb) {
540
+ if (typeof exports.promiseImpl == 'function') {
541
+ return new exports.promiseImpl(function (resolve, reject) {
542
+ try {
543
+ result = handleCache(options)(data);
544
+ resolve(result);
545
+ }
546
+ catch (err) {
547
+ reject(err);
548
+ }
549
+ });
550
+ }
551
+ else {
552
+ throw new Error('Please provide a callback function');
553
+ }
554
+ }
555
+ else {
556
+ try {
557
+ result = handleCache(options)(data);
558
+ }
559
+ catch (err) {
560
+ return cb(err);
561
+ }
562
+
563
+ cb(null, result);
564
+ }
565
+ }
566
+
567
+ /**
568
+ * fileLoader is independent
569
+ *
570
+ * @param {String} filePath ejs file path.
571
+ * @return {String} The contents of the specified file.
572
+ * @static
573
+ */
574
+
575
+ function fileLoader(filePath){
576
+ return exports.fileLoader(filePath);
577
+ }
578
+
579
+ /**
580
+ * Get the template function.
581
+ *
582
+ * If `options.cache` is `true`, then the template is cached.
583
+ *
584
+ * @memberof module:ejs-internal
585
+ * @param {String} path path for the specified file
586
+ * @param {Options} options compilation options
587
+ * @return {(TemplateFunction|ClientFunction)}
588
+ * Depending on the value of `options.client`, either type might be returned
589
+ * @static
590
+ */
591
+
592
+ function includeFile(path, options) {
593
+ var opts = utils.shallowCopy(utils.createNullProtoObjWherePossible(), options);
594
+ opts.filename = getIncludePath(path, opts);
595
+ if (typeof options.includer === 'function') {
596
+ var includerResult = options.includer(path, opts.filename);
597
+ if (includerResult) {
598
+ if (includerResult.filename) {
599
+ opts.filename = includerResult.filename;
600
+ }
601
+ if (includerResult.template) {
602
+ return handleCache(opts, includerResult.template);
603
+ }
604
+ }
605
+ }
606
+ return handleCache(opts);
607
+ }
608
+
609
+ /**
610
+ * Re-throw the given `err` in context to the `str` of ejs, `filename`, and
611
+ * `lineno`.
612
+ *
613
+ * @implements {RethrowCallback}
614
+ * @memberof module:ejs-internal
615
+ * @param {Error} err Error object
616
+ * @param {String} str EJS source
617
+ * @param {String} flnm file name of the EJS file
618
+ * @param {Number} lineno line number of the error
619
+ * @param {EscapeCallback} esc
620
+ * @static
621
+ */
622
+
623
+ function rethrow(err, str, flnm, lineno, esc) {
624
+ var lines = str.split('\n');
625
+ var start = Math.max(lineno - 3, 0);
626
+ var end = Math.min(lines.length, lineno + 3);
627
+ var filename = esc(flnm);
628
+ // Error context
629
+ var context = lines.slice(start, end).map(function (line, i){
630
+ var curr = i + start + 1;
631
+ return (curr == lineno ? ' >> ' : ' ')
632
+ + curr
633
+ + '| '
634
+ + line;
635
+ }).join('\n');
636
+
637
+ // Alter exception message
638
+ err.path = filename;
639
+ err.message = (filename || 'ejs') + ':'
640
+ + lineno + '\n'
641
+ + context + '\n\n'
642
+ + err.message;
643
+
644
+ throw err;
645
+ }
646
+
647
+ function stripSemi(str){
648
+ return str.replace(/;(\s*$)/, '$1');
649
+ }
650
+
651
+ /**
652
+ * Compile the given `str` of ejs into a template function.
653
+ *
654
+ * @param {String} template EJS template
655
+ *
656
+ * @param {Options} [opts] compilation options
657
+ *
658
+ * @return {(TemplateFunction|ClientFunction)}
659
+ * Depending on the value of `opts.client`, either type might be returned.
660
+ * Note that the return type of the function also depends on the value of `opts.async`.
661
+ * @public
662
+ */
663
+
664
+ exports.compile = function compile(template, opts) {
665
+ var templ;
666
+
667
+ // v1 compat
668
+ // 'scope' is 'context'
669
+ // FIXME: Remove this in a future version
670
+ if (opts && opts.scope) {
671
+ if (!scopeOptionWarned){
672
+ console.warn('`scope` option is deprecated and will be removed in EJS 3');
673
+ scopeOptionWarned = true;
674
+ }
675
+ if (!opts.context) {
676
+ opts.context = opts.scope;
677
+ }
678
+ delete opts.scope;
679
+ }
680
+ templ = new Template(template, opts);
681
+ return templ.compile();
682
+ };
683
+
684
+ /**
685
+ * Render the given `template` of ejs.
686
+ *
687
+ * If you would like to include options but not data, you need to explicitly
688
+ * call this function with `data` being an empty object or `null`.
689
+ *
690
+ * @param {String} template EJS template
691
+ * @param {Object} [data={}] template data
692
+ * @param {Options} [opts={}] compilation and rendering options
693
+ * @return {(String|Promise<String>)}
694
+ * Return value type depends on `opts.async`.
695
+ * @public
696
+ */
697
+
698
+ exports.render = function (template, d, o) {
699
+ var data = d || utils.createNullProtoObjWherePossible();
700
+ var opts = o || utils.createNullProtoObjWherePossible();
701
+
702
+ // No options object -- if there are optiony names
703
+ // in the data, copy them to options
704
+ if (arguments.length == 2) {
705
+ utils.shallowCopyFromList(opts, data, _OPTS_PASSABLE_WITH_DATA);
706
+ }
707
+
708
+ return handleCache(opts, template)(data);
709
+ };
710
+
711
+ /**
712
+ * Render an EJS file at the given `path` and callback `cb(err, str)`.
713
+ *
714
+ * If you would like to include options but not data, you need to explicitly
715
+ * call this function with `data` being an empty object or `null`.
716
+ *
717
+ * @param {String} path path to the EJS file
718
+ * @param {Object} [data={}] template data
719
+ * @param {Options} [opts={}] compilation and rendering options
720
+ * @param {RenderFileCallback} cb callback
721
+ * @public
722
+ */
723
+
724
+ exports.renderFile = function () {
725
+ var args = Array.prototype.slice.call(arguments);
726
+ var filename = args.shift();
727
+ var cb;
728
+ var opts = {filename: filename};
729
+ var data;
730
+ var viewOpts;
731
+
732
+ // Do we have a callback?
733
+ if (typeof arguments[arguments.length - 1] == 'function') {
734
+ cb = args.pop();
735
+ }
736
+ // Do we have data/opts?
737
+ if (args.length) {
738
+ // Should always have data obj
739
+ data = args.shift();
740
+ // Normal passed opts (data obj + opts obj)
741
+ if (args.length) {
742
+ // Use shallowCopy so we don't pollute passed in opts obj with new vals
743
+ utils.shallowCopy(opts, args.pop());
744
+ }
745
+ // Special casing for Express (settings + opts-in-data)
746
+ else {
747
+ // Express 3 and 4
748
+ if (data.settings) {
749
+ // Pull a few things from known locations
750
+ if (data.settings.views) {
751
+ opts.views = data.settings.views;
752
+ }
753
+ if (data.settings['view cache']) {
754
+ opts.cache = true;
755
+ }
756
+ // Undocumented after Express 2, but still usable, esp. for
757
+ // items that are unsafe to be passed along with data, like `root`
758
+ viewOpts = data.settings['view options'];
759
+ if (viewOpts) {
760
+ utils.shallowCopy(opts, viewOpts);
761
+ }
762
+ }
763
+ // Express 2 and lower, values set in app.locals, or people who just
764
+ // want to pass options in their data. NOTE: These values will override
765
+ // anything previously set in settings or settings['view options']
766
+ utils.shallowCopyFromList(opts, data, _OPTS_PASSABLE_WITH_DATA_EXPRESS);
767
+ }
768
+ opts.filename = filename;
769
+ }
770
+ else {
771
+ data = utils.createNullProtoObjWherePossible();
772
+ }
773
+
774
+ return tryHandleCache(opts, data, cb);
775
+ };
776
+
777
+ /**
778
+ * Clear intermediate JavaScript cache. Calls {@link Cache#reset}.
779
+ * @public
780
+ */
781
+
782
+ /**
783
+ * EJS template class
784
+ * @public
785
+ */
786
+ exports.Template = Template;
787
+
788
+ exports.clearCache = function () {
789
+ exports.cache.reset();
790
+ };
791
+
792
+ function Template(text, optsParam) {
793
+ var opts = utils.hasOwnOnlyObject(optsParam);
794
+ var options = utils.createNullProtoObjWherePossible();
795
+ this.templateText = text;
796
+ /** @type {string | null} */
797
+ this.mode = null;
798
+ this.truncate = false;
799
+ this.currentLine = 1;
800
+ this.source = '';
801
+ options.client = opts.client || false;
802
+ options.escapeFunction = opts.escape || opts.escapeFunction || utils.escapeXML;
803
+ options.compileDebug = opts.compileDebug !== false;
804
+ options.debug = !!opts.debug;
805
+ options.filename = opts.filename;
806
+ options.openDelimiter = opts.openDelimiter || exports.openDelimiter || _DEFAULT_OPEN_DELIMITER;
807
+ options.closeDelimiter = opts.closeDelimiter || exports.closeDelimiter || _DEFAULT_CLOSE_DELIMITER;
808
+ options.delimiter = opts.delimiter || exports.delimiter || _DEFAULT_DELIMITER;
809
+ options.strict = opts.strict || false;
810
+ options.context = opts.context;
811
+ options.cache = opts.cache || false;
812
+ options.rmWhitespace = opts.rmWhitespace;
813
+ options.root = opts.root;
814
+ options.includer = opts.includer;
815
+ options.outputFunctionName = opts.outputFunctionName;
816
+ options.localsName = opts.localsName || exports.localsName || _DEFAULT_LOCALS_NAME;
817
+ options.views = opts.views;
818
+ options.async = opts.async;
819
+ options.destructuredLocals = opts.destructuredLocals;
820
+ options.legacyInclude = typeof opts.legacyInclude != 'undefined' ? !!opts.legacyInclude : true;
821
+
822
+ if (options.strict) {
823
+ options._with = false;
824
+ }
825
+ else {
826
+ options._with = typeof opts._with != 'undefined' ? opts._with : true;
827
+ }
828
+
829
+ this.opts = options;
830
+
831
+ this.regex = this.createRegex();
832
+ }
833
+
834
+ Template.modes = {
835
+ EVAL: 'eval',
836
+ ESCAPED: 'escaped',
837
+ RAW: 'raw',
838
+ COMMENT: 'comment',
839
+ LITERAL: 'literal'
840
+ };
841
+
842
+ Template.prototype = {
843
+ createRegex: function () {
844
+ var str = _REGEX_STRING;
845
+ var delim = utils.escapeRegExpChars(this.opts.delimiter);
846
+ var open = utils.escapeRegExpChars(this.opts.openDelimiter);
847
+ var close = utils.escapeRegExpChars(this.opts.closeDelimiter);
848
+ str = str.replace(/%/g, delim)
849
+ .replace(/</g, open)
850
+ .replace(/>/g, close);
851
+ return new RegExp(str);
852
+ },
853
+
854
+ compile: function () {
855
+ /** @type {string} */
856
+ var src;
857
+ /** @type {ClientFunction} */
858
+ var fn;
859
+ var opts = this.opts;
860
+ var prepended = '';
861
+ var appended = '';
862
+ /** @type {EscapeCallback} */
863
+ var escapeFn = opts.escapeFunction;
864
+ /** @type {FunctionConstructor} */
865
+ var ctor;
866
+ /** @type {string} */
867
+ var sanitizedFilename = opts.filename ? JSON.stringify(opts.filename) : 'undefined';
868
+
869
+ if (!this.source) {
870
+ this.generateSource();
871
+ prepended +=
872
+ ' var __output = "";\n' +
873
+ ' function __append(s) { if (s !== undefined && s !== null) __output += s }\n';
874
+ if (opts.outputFunctionName) {
875
+ if (!_JS_IDENTIFIER.test(opts.outputFunctionName)) {
876
+ throw new Error('outputFunctionName is not a valid JS identifier.');
877
+ }
878
+ prepended += ' var ' + opts.outputFunctionName + ' = __append;' + '\n';
879
+ }
880
+ if (opts.localsName && !_JS_IDENTIFIER.test(opts.localsName)) {
881
+ throw new Error('localsName is not a valid JS identifier.');
882
+ }
883
+ if (opts.destructuredLocals && opts.destructuredLocals.length) {
884
+ var destructuring = ' var __locals = (' + opts.localsName + ' || {}),\n';
885
+ for (var i = 0; i < opts.destructuredLocals.length; i++) {
886
+ var name = opts.destructuredLocals[i];
887
+ if (!_JS_IDENTIFIER.test(name)) {
888
+ throw new Error('destructuredLocals[' + i + '] is not a valid JS identifier.');
889
+ }
890
+ if (i > 0) {
891
+ destructuring += ',\n ';
892
+ }
893
+ destructuring += name + ' = __locals.' + name;
894
+ }
895
+ prepended += destructuring + ';\n';
896
+ }
897
+ if (opts._with !== false) {
898
+ prepended += ' with (' + opts.localsName + ' || {}) {' + '\n';
899
+ appended += ' }' + '\n';
900
+ }
901
+ appended += ' return __output;' + '\n';
902
+ this.source = prepended + this.source + appended;
903
+ }
904
+
905
+ if (opts.compileDebug) {
906
+ src = 'var __line = 1' + '\n'
907
+ + ' , __lines = ' + JSON.stringify(this.templateText) + '\n'
908
+ + ' , __filename = ' + sanitizedFilename + ';' + '\n'
909
+ + 'try {' + '\n'
910
+ + this.source
911
+ + '} catch (e) {' + '\n'
912
+ + ' rethrow(e, __lines, __filename, __line, escapeFn);' + '\n'
913
+ + '}' + '\n';
914
+ }
915
+ else {
916
+ src = this.source;
917
+ }
918
+
919
+ if (opts.client) {
920
+ src = 'escapeFn = escapeFn || ' + escapeFn.toString() + ';' + '\n' + src;
921
+ if (opts.compileDebug) {
922
+ src = 'rethrow = rethrow || ' + rethrow.toString() + ';' + '\n' + src;
923
+ }
924
+ }
925
+
926
+ if (opts.strict) {
927
+ src = '"use strict";\n' + src;
928
+ }
929
+ if (opts.debug) {
930
+ console.log(src);
931
+ }
932
+ if (opts.compileDebug && opts.filename) {
933
+ src = src + '\n'
934
+ + '//# sourceURL=' + sanitizedFilename + '\n';
935
+ }
936
+
937
+ try {
938
+ if (opts.async) {
939
+ // Have to use generated function for this, since in envs without support,
940
+ // it breaks in parsing
941
+ try {
942
+ ctor = (new Function('return (async function(){}).constructor;'))();
943
+ }
944
+ catch(e) {
945
+ if (e instanceof SyntaxError) {
946
+ throw new Error('This environment does not support async/await');
947
+ }
948
+ else {
949
+ throw e;
950
+ }
951
+ }
952
+ }
953
+ else {
954
+ ctor = Function;
955
+ }
956
+ fn = new ctor(opts.localsName + ', escapeFn, include, rethrow', src);
957
+ }
958
+ catch(e) {
959
+ // istanbul ignore else
960
+ if (e instanceof SyntaxError) {
961
+ if (opts.filename) {
962
+ e.message += ' in ' + opts.filename;
963
+ }
964
+ e.message += ' while compiling ejs\n\n';
965
+ e.message += 'If the above error is not helpful, you may want to try EJS-Lint:\n';
966
+ e.message += 'https://github.com/RyanZim/EJS-Lint';
967
+ if (!opts.async) {
968
+ e.message += '\n';
969
+ e.message += 'Or, if you meant to create an async function, pass `async: true` as an option.';
970
+ }
971
+ }
972
+ throw e;
973
+ }
974
+
975
+ // Return a callable function which will execute the function
976
+ // created by the source-code, with the passed data as locals
977
+ // Adds a local `include` function which allows full recursive include
978
+ var returnedFn = opts.client ? fn : function anonymous(data) {
979
+ var include = function (path, includeData) {
980
+ var d = utils.shallowCopy(utils.createNullProtoObjWherePossible(), data);
981
+ if (includeData) {
982
+ d = utils.shallowCopy(d, includeData);
983
+ }
984
+ return includeFile(path, opts)(d);
985
+ };
986
+ return fn.apply(opts.context,
987
+ [data || utils.createNullProtoObjWherePossible(), escapeFn, include, rethrow]);
988
+ };
989
+ if (opts.filename && typeof Object.defineProperty === 'function') {
990
+ var filename = opts.filename;
991
+ var basename = path.basename(filename, path.extname(filename));
992
+ try {
993
+ Object.defineProperty(returnedFn, 'name', {
994
+ value: basename,
995
+ writable: false,
996
+ enumerable: false,
997
+ configurable: true
998
+ });
999
+ } catch (e) {/* ignore */}
1000
+ }
1001
+ return returnedFn;
1002
+ },
1003
+
1004
+ generateSource: function () {
1005
+ var opts = this.opts;
1006
+
1007
+ if (opts.rmWhitespace) {
1008
+ // Have to use two separate replace here as `^` and `$` operators don't
1009
+ // work well with `\r` and empty lines don't work well with the `m` flag.
1010
+ this.templateText =
1011
+ this.templateText.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
1012
+ }
1013
+
1014
+ // Slurp spaces and tabs before <%_ and after _%>
1015
+ this.templateText =
1016
+ this.templateText.replace(/[ \t]*<%_/gm, '<%_').replace(/_%>[ \t]*/gm, '_%>');
1017
+
1018
+ var self = this;
1019
+ var matches = this.parseTemplateText();
1020
+ var d = this.opts.delimiter;
1021
+ var o = this.opts.openDelimiter;
1022
+ var c = this.opts.closeDelimiter;
1023
+
1024
+ if (matches && matches.length) {
1025
+ matches.forEach(function (line, index) {
1026
+ var closing;
1027
+ // If this is an opening tag, check for closing tags
1028
+ // FIXME: May end up with some false positives here
1029
+ // Better to store modes as k/v with openDelimiter + delimiter as key
1030
+ // Then this can simply check against the map
1031
+ if ( line.indexOf(o + d) === 0 // If it is a tag
1032
+ && line.indexOf(o + d + d) !== 0) { // and is not escaped
1033
+ closing = matches[index + 2];
1034
+ if (!(closing == d + c || closing == '-' + d + c || closing == '_' + d + c)) {
1035
+ throw new Error('Could not find matching close tag for "' + line + '".');
1036
+ }
1037
+ }
1038
+ self.scanLine(line);
1039
+ });
1040
+ }
1041
+
1042
+ },
1043
+
1044
+ parseTemplateText: function () {
1045
+ var str = this.templateText;
1046
+ var pat = this.regex;
1047
+ var result = pat.exec(str);
1048
+ var arr = [];
1049
+ var firstPos;
1050
+
1051
+ while (result) {
1052
+ firstPos = result.index;
1053
+
1054
+ if (firstPos !== 0) {
1055
+ arr.push(str.substring(0, firstPos));
1056
+ str = str.slice(firstPos);
1057
+ }
1058
+
1059
+ arr.push(result[0]);
1060
+ str = str.slice(result[0].length);
1061
+ result = pat.exec(str);
1062
+ }
1063
+
1064
+ if (str) {
1065
+ arr.push(str);
1066
+ }
1067
+
1068
+ return arr;
1069
+ },
1070
+
1071
+ _addOutput: function (line) {
1072
+ if (this.truncate) {
1073
+ // Only replace single leading linebreak in the line after
1074
+ // -%> tag -- this is the single, trailing linebreak
1075
+ // after the tag that the truncation mode replaces
1076
+ // Handle Win / Unix / old Mac linebreaks -- do the \r\n
1077
+ // combo first in the regex-or
1078
+ line = line.replace(/^(?:\r\n|\r|\n)/, '');
1079
+ this.truncate = false;
1080
+ }
1081
+ if (!line) {
1082
+ return line;
1083
+ }
1084
+
1085
+ // Preserve literal slashes
1086
+ line = line.replace(/\\/g, '\\\\');
1087
+
1088
+ // Convert linebreaks
1089
+ line = line.replace(/\n/g, '\\n');
1090
+ line = line.replace(/\r/g, '\\r');
1091
+
1092
+ // Escape double-quotes
1093
+ // - this will be the delimiter during execution
1094
+ line = line.replace(/"/g, '\\"');
1095
+ this.source += ' ; __append("' + line + '")' + '\n';
1096
+ },
1097
+
1098
+ scanLine: function (line) {
1099
+ var self = this;
1100
+ var d = this.opts.delimiter;
1101
+ var o = this.opts.openDelimiter;
1102
+ var c = this.opts.closeDelimiter;
1103
+ var newLineCount = 0;
1104
+
1105
+ newLineCount = (line.split('\n').length - 1);
1106
+
1107
+ switch (line) {
1108
+ case o + d:
1109
+ case o + d + '_':
1110
+ this.mode = Template.modes.EVAL;
1111
+ break;
1112
+ case o + d + '=':
1113
+ this.mode = Template.modes.ESCAPED;
1114
+ break;
1115
+ case o + d + '-':
1116
+ this.mode = Template.modes.RAW;
1117
+ break;
1118
+ case o + d + '#':
1119
+ this.mode = Template.modes.COMMENT;
1120
+ break;
1121
+ case o + d + d:
1122
+ this.mode = Template.modes.LITERAL;
1123
+ this.source += ' ; __append("' + line.replace(o + d + d, o + d) + '")' + '\n';
1124
+ break;
1125
+ case d + d + c:
1126
+ this.mode = Template.modes.LITERAL;
1127
+ this.source += ' ; __append("' + line.replace(d + d + c, d + c) + '")' + '\n';
1128
+ break;
1129
+ case d + c:
1130
+ case '-' + d + c:
1131
+ case '_' + d + c:
1132
+ if (this.mode == Template.modes.LITERAL) {
1133
+ this._addOutput(line);
1134
+ }
1135
+
1136
+ this.mode = null;
1137
+ this.truncate = line.indexOf('-') === 0 || line.indexOf('_') === 0;
1138
+ break;
1139
+ default:
1140
+ // In script mode, depends on type of tag
1141
+ if (this.mode) {
1142
+ // If '//' is found without a line break, add a line break.
1143
+ switch (this.mode) {
1144
+ case Template.modes.EVAL:
1145
+ case Template.modes.ESCAPED:
1146
+ case Template.modes.RAW:
1147
+ if (line.lastIndexOf('//') > line.lastIndexOf('\n')) {
1148
+ line += '\n';
1149
+ }
1150
+ }
1151
+ switch (this.mode) {
1152
+ // Just executing code
1153
+ case Template.modes.EVAL:
1154
+ this.source += ' ; ' + line + '\n';
1155
+ break;
1156
+ // Exec, esc, and output
1157
+ case Template.modes.ESCAPED:
1158
+ this.source += ' ; __append(escapeFn(' + stripSemi(line) + '))' + '\n';
1159
+ break;
1160
+ // Exec and output
1161
+ case Template.modes.RAW:
1162
+ this.source += ' ; __append(' + stripSemi(line) + ')' + '\n';
1163
+ break;
1164
+ case Template.modes.COMMENT:
1165
+ // Do nothing
1166
+ break;
1167
+ // Literal <%% mode, append as raw output
1168
+ case Template.modes.LITERAL:
1169
+ this._addOutput(line);
1170
+ break;
1171
+ }
1172
+ }
1173
+ // In string mode, just add the output
1174
+ else {
1175
+ this._addOutput(line);
1176
+ }
1177
+ }
1178
+
1179
+ if (self.opts.compileDebug && newLineCount) {
1180
+ this.currentLine += newLineCount;
1181
+ this.source += ' ; __line = ' + this.currentLine + '\n';
1182
+ }
1183
+ }
1184
+ };
1185
+
1186
+ /**
1187
+ * Escape characters reserved in XML.
1188
+ *
1189
+ * This is simply an export of {@link module:utils.escapeXML}.
1190
+ *
1191
+ * If `markup` is `undefined` or `null`, the empty string is returned.
1192
+ *
1193
+ * @param {String} markup Input string
1194
+ * @return {String} Escaped string
1195
+ * @public
1196
+ * @func
1197
+ * */
1198
+ exports.escapeXML = utils.escapeXML;
1199
+
1200
+ /**
1201
+ * Express.js support.
1202
+ *
1203
+ * This is an alias for {@link module:ejs.renderFile}, in order to support
1204
+ * Express.js out-of-the-box.
1205
+ *
1206
+ * @func
1207
+ */
1208
+
1209
+ exports.__express = exports.renderFile;
1210
+
1211
+ /**
1212
+ * Version of EJS.
1213
+ *
1214
+ * @readonly
1215
+ * @type {String}
1216
+ * @public
1217
+ */
1218
+
1219
+ exports.VERSION = _VERSION_STRING;
1220
+
1221
+ /**
1222
+ * Name for detection of EJS.
1223
+ *
1224
+ * @readonly
1225
+ * @type {String}
1226
+ * @public
1227
+ */
1228
+
1229
+ exports.name = _NAME;
1230
+
1231
+ /* istanbul ignore if */
1232
+ if (typeof window != 'undefined') {
1233
+ window.ejs = exports;
1234
+ }
1235
+ } (ejs));
1236
+ return ejs;
1237
+ }
1238
+
1239
+ var ejsExports = requireEjs();
1240
+
1241
+ const TEMPLATE_NAME = 'vue3-vite-ts';
1242
+ const TEMPLATE_ROOT_CANDIDATES = [
1243
+ path.resolve(__dirname, 'templates', TEMPLATE_NAME),
1244
+ path.resolve(__dirname, '..', 'templates', TEMPLATE_NAME),
1245
+ path.resolve(__dirname, '..', 'src', 'cli', 'templates', TEMPLATE_NAME),
1246
+ ];
1247
+ function createPackageJsonCandidates(baseDir = __dirname) {
1248
+ return [
1249
+ path.resolve(baseDir, '..', 'package.json'),
1250
+ path.resolve(baseDir, '..', '..', 'package.json'),
1251
+ path.resolve(baseDir, '..', '..', '..', 'package.json'),
1252
+ ];
1253
+ }
1254
+ const PACKAGE_JSON_CANDIDATES = createPackageJsonCandidates();
1255
+ const UNSCOPED_PACKAGE_NAME_PATTERN = /^(?![._])[a-z0-9._-]+$/;
1256
+ async function runCreateCommand(projectName, options = {}) {
1257
+ const cwd = options.cwd ?? process.cwd();
1258
+ const targetDir = path.resolve(cwd, projectName);
1259
+ const packageName = readPackageName(projectName, targetDir);
1260
+ await assertTargetDirectoryWritable(targetDir);
1261
+ const templateRoot = await resolveTemplateRoot(options.templateRoots);
1262
+ const data = {
1263
+ packageName,
1264
+ projectName: packageName,
1265
+ sdkVersion: await readCurrentSdkVersion(options.packageJsonFiles),
1266
+ };
1267
+ await renderTemplateDirectory(templateRoot, targetDir, data);
1268
+ printCreateResult(projectName, targetDir, {
1269
+ cwd,
1270
+ logger: options.logger,
1271
+ });
1272
+ }
1273
+ function readPackageName(projectName, targetDir) {
1274
+ if (projectName.trim().startsWith('@') || projectName.split(/[\\/]/).some((segment) => segment.startsWith('@'))) {
1275
+ throw new Error('暂不支持 scoped package name,请使用 my-miniapp 这类普通目录名');
1276
+ }
1277
+ const packageName = path.basename(targetDir);
1278
+ if (!UNSCOPED_PACKAGE_NAME_PATTERN.test(packageName)) {
1279
+ throw new Error(`项目名必须是合法 npm 包名,请使用 kebab-case,例如 my-miniapp。当前值: ${packageName}`);
1280
+ }
1281
+ if (packageName.length > 214) {
1282
+ throw new Error('项目名过长,npm package name 不能超过 214 个字符');
1283
+ }
1284
+ return packageName;
1285
+ }
1286
+ async function assertTargetDirectoryWritable(targetDir) {
1287
+ if (!(await pathExists(targetDir))) {
1288
+ await fs.mkdir(targetDir, { recursive: true });
1289
+ return;
1290
+ }
1291
+ const targetStat = await fs.stat(targetDir);
1292
+ if (!targetStat.isDirectory()) {
1293
+ throw new Error(`目标路径已存在但不是目录: ${targetDir}`);
1294
+ }
1295
+ const entries = await fs.readdir(targetDir);
1296
+ if (entries.length > 0) {
1297
+ throw new Error(`目标目录非空,已拒绝覆盖: ${targetDir}`);
1298
+ }
1299
+ }
1300
+ async function resolveTemplateRoot(templateRootCandidates = TEMPLATE_ROOT_CANDIDATES) {
1301
+ for (const candidate of templateRootCandidates) {
1302
+ if (await pathExists(candidate)) {
1303
+ return candidate;
1304
+ }
1305
+ }
1306
+ throw new Error(`未找到 hb-sdk create 模板: ${TEMPLATE_NAME}。请先执行 @heybox/hb-sdk 的 build:templates。`);
1307
+ }
1308
+ async function readCurrentSdkVersion(packageJsonCandidates = PACKAGE_JSON_CANDIDATES) {
1309
+ const packageJsonPath = await resolvePackageJsonPath(packageJsonCandidates);
1310
+ const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
1311
+ if (typeof packageJson.version !== 'string' || !packageJson.version) {
1312
+ throw new Error('未能读取 @heybox/hb-sdk 当前版本号');
1313
+ }
1314
+ return packageJson.version;
1315
+ }
1316
+ async function resolvePackageJsonPath(packageJsonCandidates = PACKAGE_JSON_CANDIDATES) {
1317
+ for (const candidate of packageJsonCandidates) {
1318
+ if (await pathExists(candidate)) {
1319
+ return candidate;
1320
+ }
1321
+ }
1322
+ throw new Error('未找到 @heybox/hb-sdk package.json');
1323
+ }
1324
+ async function renderTemplateDirectory(sourceDir, targetDir, data) {
1325
+ const entries = await fs.readdir(sourceDir, { withFileTypes: true });
1326
+ for (const entry of entries) {
1327
+ const sourcePath = path.join(sourceDir, entry.name);
1328
+ const outputName = toOutputFileName(entry.name);
1329
+ const targetPath = path.join(targetDir, outputName);
1330
+ if (entry.isDirectory()) {
1331
+ await fs.mkdir(targetPath, { recursive: true });
1332
+ await renderTemplateDirectory(sourcePath, targetPath, data);
1333
+ continue;
1334
+ }
1335
+ if (!entry.isFile()) {
1336
+ continue;
1337
+ }
1338
+ const content = await fs.readFile(sourcePath, 'utf8');
1339
+ const rendered = ejsExports.render(content, data, {
1340
+ filename: sourcePath,
1341
+ });
1342
+ await fs.mkdir(path.dirname(targetPath), { recursive: true });
1343
+ await fs.writeFile(targetPath, rendered, 'utf8');
1344
+ }
1345
+ }
1346
+ function toOutputFileName(fileName) {
1347
+ return fileName.endsWith('.ejs') ? fileName.slice(0, -'.ejs'.length) : fileName;
1348
+ }
1349
+ async function pathExists(filePath) {
1350
+ try {
1351
+ await fs.access(filePath, fs$1.constants.F_OK);
1352
+ return true;
1353
+ }
1354
+ catch {
1355
+ return false;
1356
+ }
1357
+ }
1358
+ function printCreateResult(inputPath, targetDir, options) {
1359
+ const logger = options.logger ?? index.createCliLogger();
1360
+ const cwd = options.cwd;
1361
+ const displayPath = path.isAbsolute(inputPath) ? targetDir : path.relative(cwd, targetDir) || '.';
1362
+ logger.success(`已创建外部小程序模板:${targetDir}`);
1363
+ logger.section('下一步');
1364
+ logger.info(`cd ${quoteShellPath(displayPath)}`);
1365
+ logger.info('npm install');
1366
+ logger.info('npm run dev');
1367
+ }
1368
+ function quoteShellPath(value) {
1369
+ if (!/[\s'"`$\\]/.test(value)) {
1370
+ return value;
1371
+ }
1372
+ return `'${value.replace(/'/g, "'\\''")}'`;
1373
+ }
1374
+
1375
+ exports.createPackageJsonCandidates = createPackageJsonCandidates;
1376
+ exports.runCreateCommand = runCreateCommand;