@cabloy/cli 3.0.10 → 3.0.12

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.
@@ -31,15 +31,11 @@ export class LocalCommon {
31
31
  if (realFile === typeFile) {
32
32
  needCreate = false;
33
33
  }
34
- else {
35
- await fse.remove(moduleTypeFile);
36
- }
37
- }
38
- catch (_err) {
39
- await fse.remove(moduleTypeFile);
40
34
  }
35
+ catch (_err) { }
41
36
  }
42
37
  if (needCreate) {
38
+ await fse.remove(moduleTypeFile);
43
39
  await fse.ensureSymlink(typeFile, moduleTypeFile);
44
40
  }
45
41
  }
@@ -114,6 +114,7 @@ export declare class LocalHelper {
114
114
  safeSplit(str: string, sep?: string): string[];
115
115
  removeGitkeep(parentPath: string): Promise<void>;
116
116
  combineModuleNameAndResource(moduleName: string, resourceName: string): string;
117
+ importDynamic(fileName: string): Promise<any>;
117
118
  importDynamic<RESULT>(fileName: string, fn: (instance: any) => Promise<RESULT>): Promise<RESULT>;
118
119
  requireDynamic(file: string): any;
119
120
  private _requireDynamic_getFileTime;
@@ -245,6 +245,8 @@ export class LocalHelper {
245
245
  async importDynamic(fileName, fn) {
246
246
  // load
247
247
  const instance = await import(__rewriteRelativeImportExtension(this.pathToHref(fileName)));
248
+ if (!fn)
249
+ return instance;
248
250
  return await fn(instance);
249
251
  }
250
252
  requireDynamic(file) {
@@ -98,6 +98,12 @@ export declare class LocalTemplate {
98
98
  ast: any;
99
99
  snippet: any;
100
100
  };
101
+ getInitData(targetFile: string): {
102
+ brandName: import("@cabloy/module-info").TypeBrandName;
103
+ argv: import("../index.ts").ICommandArgv;
104
+ cli: BeanCliBase;
105
+ targetFile: string;
106
+ };
101
107
  applySnippets(targetDir: string, snippetsDir: string): Promise<void>;
102
108
  _loadSnippetInstance(snippetTemplatePath: string, fn: (snippet: ISnippet) => Promise<void>): Promise<void>;
103
109
  applySnippet(targetFile: string, snippet: ISnippet): Promise<void>;
@@ -182,6 +182,13 @@ export class LocalTemplate {
182
182
  ...this.context,
183
183
  };
184
184
  }
185
+ getInitData(targetFile) {
186
+ return {
187
+ cli: this.cli,
188
+ targetFile,
189
+ ...this.context,
190
+ };
191
+ }
185
192
  async applySnippets(targetDir, snippetsDir) {
186
193
  // snippets
187
194
  let files = await globby('*.{cjs,ts}', {
@@ -233,7 +240,14 @@ export class LocalTemplate {
233
240
  if (!snippet.init) {
234
241
  throw new Error(`should provider init content for: ${targetFile}`);
235
242
  }
236
- sourceCode = await this.renderContent({ content: snippet.init });
243
+ let content;
244
+ if (typeof snippet.init === 'function') {
245
+ content = await snippet.init(this.getInitData(targetFile));
246
+ }
247
+ else {
248
+ content = snippet.init;
249
+ }
250
+ sourceCode = await this.renderContent({ content });
237
251
  }
238
252
  // language
239
253
  const language = snippet.language;
@@ -21,13 +21,17 @@ export interface IAstData<LANGUAGE extends TypeParseLanguage> extends ICommandCo
21
21
  ast: TypeParseResult<LANGUAGE>;
22
22
  snippet: ISnippet<LANGUAGE>;
23
23
  }
24
+ export interface IInitData extends ICommandContext {
25
+ cli: BeanCliBase;
26
+ targetFile: string;
27
+ }
24
28
  export type TypeParseLanguage = 'plain' | 'json' | 'gogo' | '';
25
29
  export type TypeParseResult<LANGUAGE extends TypeParseLanguage> = LANGUAGE extends 'plain' ? string : LANGUAGE extends 'json' ? any : GoGoAST;
26
30
  export type TypeParseOptions<LANGUAGE extends TypeParseLanguage> = LANGUAGE extends 'plain' ? never : LANGUAGE extends 'json' ? never : ParserOptions;
27
31
  export interface ISnippet<LANGUAGE extends TypeParseLanguage = ''> {
28
32
  language: LANGUAGE;
29
33
  file: string | ((ejsData: IEjsData) => string);
30
- init?: string;
34
+ init?: string | ((initData: IInitData) => Promise<string>);
31
35
  parseOptions?: TypeParseOptions<LANGUAGE>;
32
36
  transform: (astData: IAstData<LANGUAGE>) => Promise<TypeParseResult<LANGUAGE>>;
33
37
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cabloy/cli",
3
3
  "type": "module",
4
- "version": "3.0.10",
4
+ "version": "3.0.12",
5
5
  "description": "@cabloy/cli",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -27,7 +27,7 @@
27
27
  ],
28
28
  "dependencies": {
29
29
  "@babel/parser": "^7.26.5",
30
- "@cabloy/word-utils": "^2.0.0",
30
+ "@cabloy/word-utils": "^2.0.1",
31
31
  "@npmcli/config": "^8.3.4",
32
32
  "@zhennann/common-bin": "^4.0.0",
33
33
  "@zhennann/ejs": "^3.0.1",
@@ -44,9 +44,9 @@
44
44
  "tmp": "^0.2.3",
45
45
  "urllib": "^3.24.0",
46
46
  "@cabloy/module-glob": "^5.2.1",
47
- "@cabloy/module-info": "^1.3.6",
47
+ "@cabloy/module-info": "^1.3.8",
48
48
  "@cabloy/process-helper": "^2.0.4",
49
- "@cabloy/utils": "^1.0.11"
49
+ "@cabloy/utils": "^1.0.15"
50
50
  },
51
51
  "gitHead": "2d40c063c115b230fdbc89f5a78b4412ebd0dfc9"
52
52
  }