@lang-tag/cli 0.15.0 → 0.17.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.
Files changed (30) hide show
  1. package/README.md +23 -14
  2. package/algorithms/case-utils.d.ts +12 -0
  3. package/algorithms/collector/dictionary-collector.d.ts +2 -2
  4. package/algorithms/collector/index.d.ts +3 -3
  5. package/algorithms/collector/namespace-collector.d.ts +2 -2
  6. package/algorithms/collector/type.d.ts +2 -2
  7. package/algorithms/config-generation/config-keeper.d.ts +1 -1
  8. package/algorithms/config-generation/index.d.ts +3 -3
  9. package/algorithms/config-generation/path-based-config-generator.d.ts +4 -3
  10. package/algorithms/config-generation/prepend-namespace-to-path.d.ts +1 -1
  11. package/algorithms/import/flexible-import-algorithm.d.ts +232 -0
  12. package/algorithms/import/index.d.ts +2 -1
  13. package/algorithms/import/simple-mapping-import-algorithm.d.ts +120 -0
  14. package/algorithms/index.cjs +418 -26
  15. package/algorithms/index.d.ts +6 -3
  16. package/algorithms/index.js +420 -28
  17. package/chunks/namespace-collector.cjs +75 -0
  18. package/chunks/namespace-collector.js +76 -0
  19. package/index.cjs +1156 -743
  20. package/index.js +1316 -903
  21. package/logger.d.ts +1 -1
  22. package/package.json +1 -1
  23. package/templates/config/init-config.mustache +1 -0
  24. package/templates/import/imported-tag.mustache +14 -0
  25. package/{config.d.ts → type.d.ts} +41 -32
  26. package/namespace-collector-DCruv_PK.js +0 -95
  27. package/namespace-collector-DRnZvkDR.cjs +0 -94
  28. /package/{template → templates/tag}/base-app.mustache +0 -0
  29. /package/{template → templates/tag}/base-library.mustache +0 -0
  30. /package/{template → templates/tag}/placeholder.mustache +0 -0
package/logger.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LangTagCLIConflict } from './config.ts';
1
+ import { LangTagCLIConflict } from './type';
2
2
  export interface LangTagCLILogger {
3
3
  info(message: string, params?: Record<string, any>): void;
4
4
  success(message: string, params?: Record<string, any>): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lang-tag/cli",
3
- "version": "0.15.0",
3
+ "version": "0.17.0",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -0,0 +1 @@
1
+ -
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Imported library translations
3
+ *
4
+ * You can modify these translations as needed. On next import:
5
+ * - Your changes will be preserved ( for now only translations values are preserved )
6
+ * - New translations will be added
7
+ * - Unused translations will be commented out
8
+ */
9
+ {{tagImportPath}}
10
+
11
+ {{#exports}}
12
+ export const {{name}} = {{config.tagName}}({{parameter1}}{{#hasParameter2}}, {{parameter2}}{{/hasParameter2}});
13
+
14
+ {{/exports}}
@@ -1,6 +1,6 @@
1
1
  import { LangTagTranslationsConfig } from 'lang-tag';
2
- import { LangTagCLILogger } from './logger.ts';
3
- import { TranslationsCollector } from './algorithms/collector/type.ts';
2
+ import { TranslationsCollector } from './algorithms/collector/type';
3
+ import { LangTagCLILogger } from './logger';
4
4
  export interface LangTagCLIConfig {
5
5
  /**
6
6
  * Tag name used to mark translations in code.
@@ -121,7 +121,7 @@ export interface LangTagCLIConfig {
121
121
  * A function to customize the generated file name and export name for imported library tags.
122
122
  * Allows controlling how imported tags are organized and named within the generated files.
123
123
  */
124
- onImport: (params: LangTagCLIOnImportParams, actions: LangTagCLIOnImportActions) => void;
124
+ onImport: (event: LangTagCLIImportEvent) => void;
125
125
  /**
126
126
  * A function called after all lang-tags were imported
127
127
  */
@@ -136,34 +136,6 @@ export interface LangTagCLIConfig {
136
136
  translationArgPosition: 1 | 2;
137
137
  debug?: boolean;
138
138
  }
139
- /**
140
- * Parameters passed to the `onImport` configuration function.
141
- */
142
- export interface LangTagCLIOnImportParams {
143
- /** The name of the package from which the tag is being imported. */
144
- packageName: string;
145
- /** The relative path to the source file within the imported package. */
146
- importedRelativePath: string;
147
- /** The original variable name assigned to the lang tag in the source library file, if any. */
148
- originalExportName: string | undefined;
149
- /** Parsed JSON translation object from the imported tag. */
150
- translations: Record<string, any>;
151
- /** Configuration object associated with the imported tag. */
152
- config: LangTagTranslationsConfig;
153
- /** A mutable object that can be used to pass data between multiple `onImport` calls for the same generated file. */
154
- fileGenerationData: any;
155
- }
156
- /**
157
- * Actions that can be performed within the onImport callback.
158
- */
159
- export interface LangTagCLIOnImportActions {
160
- /** Sets the desired file for the generated import. */
161
- setFile: (file: string) => void;
162
- /** Sets the desired export name for the imported tag. */
163
- setExportName: (name: string) => void;
164
- /** Sets the configuration for the currently imported tag. */
165
- setConfig: (config: LangTagTranslationsConfig) => void;
166
- }
167
139
  type Validity = 'ok' | 'invalid-param-1' | 'invalid-param-2' | 'translations-not-found';
168
140
  export interface LangTagCLIProcessedTag {
169
141
  fullMatch: string;
@@ -191,6 +163,43 @@ export interface LangTagCLIConflict {
191
163
  tagB: LangTagCLITagConflictInfo;
192
164
  conflictType: 'path_overwrite' | 'type_mismatch';
193
165
  }
166
+ export interface LangTagCLIImportManager {
167
+ importTag(pathRelativeToImportDir: string, tag: LangTagCLIImportedTag): void;
168
+ getImportedFiles(): LangTagCLIImportedTagsFile[];
169
+ getImportedFilesCount(): number;
170
+ hasImportedFiles(): boolean;
171
+ }
172
+ export interface LangTagCLIImportedTag {
173
+ variableName: string;
174
+ translations: any;
175
+ config: any | null;
176
+ }
177
+ export interface LangTagCLIImportedTagsFile {
178
+ pathRelativeToImportDir: string;
179
+ tags: LangTagCLIImportedTag[];
180
+ }
181
+ export interface LangTagCLIExportData {
182
+ baseLanguageCode: string;
183
+ files: LangTagCLIExportDataFile[];
184
+ }
185
+ export interface LangTagCLIExportDataFile {
186
+ relativeFilePath: string;
187
+ tags: LangTagCLIExportDataTag[];
188
+ }
189
+ export interface LangTagCLIExportDataTag {
190
+ variableName: string | undefined;
191
+ translations: object;
192
+ config: object | undefined;
193
+ }
194
+ export interface LangTagCLIImportEvent {
195
+ exports: {
196
+ packageJSON: any;
197
+ exportData: LangTagCLIExportData;
198
+ }[];
199
+ langTagConfig: LangTagCLIConfig;
200
+ logger: LangTagCLILogger;
201
+ importManager: LangTagCLIImportManager;
202
+ }
194
203
  export interface LangTagCLIConfigGenerationEvent {
195
204
  /** The absolute path to the source file being processed. */
196
205
  readonly absolutePath: string;
@@ -205,6 +214,7 @@ export interface LangTagCLIConfigGenerationEvent {
205
214
  * To update the configuration, use the `save()` method with a new configuration object.
206
215
  */
207
216
  readonly config: Readonly<LangTagTranslationsConfig> | undefined;
217
+ readonly logger: LangTagCLILogger;
208
218
  readonly langTagConfig: LangTagCLIConfig;
209
219
  /**
210
220
  * Indicates whether the `save()` method has been called during this event.
@@ -241,5 +251,4 @@ export interface LangTagCLICollectFinishEvent {
241
251
  /** Breaks translation collection process */
242
252
  exit(): void;
243
253
  }
244
- export declare const LANG_TAG_DEFAULT_CONFIG: LangTagCLIConfig;
245
254
  export {};
@@ -1,95 +0,0 @@
1
- import { mkdir, writeFile, readFile, rm } from "fs/promises";
2
- import { dirname, resolve } from "path";
3
- import path, { resolve as resolve$1 } from "pathe";
4
- import process__default from "node:process";
5
- class TranslationsCollector {
6
- config;
7
- logger;
8
- }
9
- async function $LT_EnsureDirectoryExists(filePath) {
10
- await mkdir(filePath, { recursive: true });
11
- }
12
- async function $LT_RemoveDirectory(dirPath) {
13
- try {
14
- await rm(dirPath, { recursive: true, force: true });
15
- } catch (error) {
16
- }
17
- }
18
- async function $LT_RemoveFile(filePath) {
19
- try {
20
- await rm(filePath, { force: true });
21
- } catch (error) {
22
- }
23
- }
24
- async function $LT_WriteJSON(filePath, data) {
25
- await writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
26
- }
27
- async function $LT_ReadJSON(filePath) {
28
- const content = await readFile(filePath, "utf-8");
29
- return JSON.parse(content);
30
- }
31
- async function $LT_WriteFileWithDirs(filePath, content) {
32
- const dir = dirname(filePath);
33
- try {
34
- await mkdir(dir, { recursive: true });
35
- } catch (error) {
36
- }
37
- await writeFile(filePath, content, "utf-8");
38
- }
39
- async function $LT_ReadFileContent(relativeFilePath) {
40
- const cwd = process.cwd();
41
- const absolutePath = resolve(cwd, relativeFilePath);
42
- return await readFile(absolutePath, "utf-8");
43
- }
44
- class NamespaceCollector extends TranslationsCollector {
45
- clean;
46
- languageDirectory;
47
- aggregateCollection(namespace) {
48
- return namespace;
49
- }
50
- transformTag(tag) {
51
- return tag;
52
- }
53
- async preWrite(clean) {
54
- this.clean = clean;
55
- this.languageDirectory = path.join(this.config.localesDirectory, this.config.baseLanguageCode);
56
- if (clean) {
57
- this.logger.info("Cleaning output directory...");
58
- await $LT_RemoveDirectory(this.languageDirectory);
59
- }
60
- await $LT_EnsureDirectoryExists(this.languageDirectory);
61
- }
62
- async resolveCollectionFilePath(collectionName) {
63
- return resolve$1(
64
- process__default.cwd(),
65
- this.languageDirectory,
66
- collectionName + ".json"
67
- );
68
- }
69
- async onMissingCollection(collectionName) {
70
- if (!this.clean) {
71
- this.logger.warn(`Original namespace file "{namespace}.json" not found. A new one will be created.`, { namespace: collectionName });
72
- }
73
- }
74
- async postWrite(changedCollections) {
75
- if (!changedCollections?.length) {
76
- this.logger.info("No changes were made based on the current configuration and files");
77
- return;
78
- }
79
- const n = changedCollections.map((n2) => `"${n2}.json"`).join(", ");
80
- this.logger.success("Updated namespaces {outputDir} ({namespaces})", {
81
- outputDir: this.config.localesDirectory,
82
- namespaces: n
83
- });
84
- }
85
- }
86
- export {
87
- $LT_ReadFileContent as $,
88
- NamespaceCollector as N,
89
- TranslationsCollector as T,
90
- $LT_ReadJSON as a,
91
- $LT_WriteJSON as b,
92
- $LT_EnsureDirectoryExists as c,
93
- $LT_WriteFileWithDirs as d,
94
- $LT_RemoveFile as e
95
- };
@@ -1,94 +0,0 @@
1
- "use strict";
2
- const promises = require("fs/promises");
3
- const path = require("path");
4
- const path$1 = require("pathe");
5
- const process$1 = require("node:process");
6
- class TranslationsCollector {
7
- config;
8
- logger;
9
- }
10
- async function $LT_EnsureDirectoryExists(filePath) {
11
- await promises.mkdir(filePath, { recursive: true });
12
- }
13
- async function $LT_RemoveDirectory(dirPath) {
14
- try {
15
- await promises.rm(dirPath, { recursive: true, force: true });
16
- } catch (error) {
17
- }
18
- }
19
- async function $LT_RemoveFile(filePath) {
20
- try {
21
- await promises.rm(filePath, { force: true });
22
- } catch (error) {
23
- }
24
- }
25
- async function $LT_WriteJSON(filePath, data) {
26
- await promises.writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
27
- }
28
- async function $LT_ReadJSON(filePath) {
29
- const content = await promises.readFile(filePath, "utf-8");
30
- return JSON.parse(content);
31
- }
32
- async function $LT_WriteFileWithDirs(filePath, content) {
33
- const dir = path.dirname(filePath);
34
- try {
35
- await promises.mkdir(dir, { recursive: true });
36
- } catch (error) {
37
- }
38
- await promises.writeFile(filePath, content, "utf-8");
39
- }
40
- async function $LT_ReadFileContent(relativeFilePath) {
41
- const cwd = process.cwd();
42
- const absolutePath = path.resolve(cwd, relativeFilePath);
43
- return await promises.readFile(absolutePath, "utf-8");
44
- }
45
- class NamespaceCollector extends TranslationsCollector {
46
- clean;
47
- languageDirectory;
48
- aggregateCollection(namespace) {
49
- return namespace;
50
- }
51
- transformTag(tag) {
52
- return tag;
53
- }
54
- async preWrite(clean) {
55
- this.clean = clean;
56
- this.languageDirectory = path$1.join(this.config.localesDirectory, this.config.baseLanguageCode);
57
- if (clean) {
58
- this.logger.info("Cleaning output directory...");
59
- await $LT_RemoveDirectory(this.languageDirectory);
60
- }
61
- await $LT_EnsureDirectoryExists(this.languageDirectory);
62
- }
63
- async resolveCollectionFilePath(collectionName) {
64
- return path$1.resolve(
65
- process$1.cwd(),
66
- this.languageDirectory,
67
- collectionName + ".json"
68
- );
69
- }
70
- async onMissingCollection(collectionName) {
71
- if (!this.clean) {
72
- this.logger.warn(`Original namespace file "{namespace}.json" not found. A new one will be created.`, { namespace: collectionName });
73
- }
74
- }
75
- async postWrite(changedCollections) {
76
- if (!changedCollections?.length) {
77
- this.logger.info("No changes were made based on the current configuration and files");
78
- return;
79
- }
80
- const n = changedCollections.map((n2) => `"${n2}.json"`).join(", ");
81
- this.logger.success("Updated namespaces {outputDir} ({namespaces})", {
82
- outputDir: this.config.localesDirectory,
83
- namespaces: n
84
- });
85
- }
86
- }
87
- exports.$LT_EnsureDirectoryExists = $LT_EnsureDirectoryExists;
88
- exports.$LT_ReadFileContent = $LT_ReadFileContent;
89
- exports.$LT_ReadJSON = $LT_ReadJSON;
90
- exports.$LT_RemoveFile = $LT_RemoveFile;
91
- exports.$LT_WriteFileWithDirs = $LT_WriteFileWithDirs;
92
- exports.$LT_WriteJSON = $LT_WriteJSON;
93
- exports.NamespaceCollector = NamespaceCollector;
94
- exports.TranslationsCollector = TranslationsCollector;
File without changes