@kopynator/cli 1.0.8 → 1.0.10

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/dist/index.js CHANGED
@@ -98,39 +98,52 @@ async function initCommand() {
98
98
  console.log(import_chalk.default.green(`
99
99
  \u2705 Setting up for ${answers.framework}...`));
100
100
  if (answers.framework === "Angular") {
101
- const configPath = import_path.default.join(process.cwd(), "src/app/app.config.ts");
102
- if (import_fs.default.existsSync(configPath)) {
103
- console.log(import_chalk.default.blue("\u2139\uFE0F Detected Angular project structure. Attempting to modify app.config.ts..."));
104
- let appConfigContent = import_fs.default.readFileSync(configPath, "utf-8");
105
- if (!appConfigContent.includes("@kopynator/angular")) {
106
- appConfigContent = `import { provideKopynator } from '@kopynator/angular';
107
- ` + appConfigContent;
101
+ const appConfigPath = import_path.default.join(process.cwd(), "src/app/app.config.ts");
102
+ const appModulePath = import_path.default.join(process.cwd(), "src/app/app.module.ts");
103
+ let targetPath = null;
104
+ let isModule = false;
105
+ if (import_fs.default.existsSync(appConfigPath)) {
106
+ targetPath = appConfigPath;
107
+ isModule = false;
108
+ console.log(import_chalk.default.blue("\u2139\uFE0F Detected Angular standalone project (app.config.ts)"));
109
+ } else if (import_fs.default.existsSync(appModulePath)) {
110
+ targetPath = appModulePath;
111
+ isModule = true;
112
+ console.log(import_chalk.default.blue("\u2139\uFE0F Detected Angular NgModule project (app.module.ts)"));
113
+ }
114
+ if (targetPath) {
115
+ let fileContent = import_fs.default.readFileSync(targetPath, "utf-8");
116
+ const hasImport = fileContent.includes("@kopynator/angular");
117
+ if (!hasImport) {
118
+ fileContent = `import { provideKopynator } from '@kopynator/angular';
119
+ ` + fileContent;
108
120
  }
109
- const providerString = `
121
+ if (fileContent.includes("provideKopynator(")) {
122
+ console.log(import_chalk.default.yellow("\u26A0\uFE0F provideKopynator is already present. Skipping injection."));
123
+ return;
124
+ } else {
125
+ const providerString = `
110
126
  provideKopynator({
111
127
  apiKey: 'YOUR_API_KEY_HERE',
112
128
  defaultLocale: '${answers.defaultLocale}',
113
129
  mode: 'local',
114
130
  languages: ${JSON.stringify(answers.languages)},
115
131
  }),`;
116
- if (appConfigContent.includes("provideKopynator")) {
117
- console.log(import_chalk.default.yellow("\u26A0\uFE0F provideKopynator is already present in app.config.ts. Skipping injection."));
118
- } else {
119
- const providersRegex = /(providers:\s*\[)/;
120
- if (providersRegex.test(appConfigContent)) {
121
- appConfigContent = appConfigContent.replace(providersRegex, `$1${providerString}`);
122
- import_fs.default.writeFileSync(configPath, appConfigContent);
123
- console.log(import_chalk.default.green("\u2705 Successfully injected configuration into src/app/app.config.ts"));
132
+ const providersRegex = isModule ? /(providers:\s*\[)/ : /(providers:\s*\[)/;
133
+ if (providersRegex.test(fileContent)) {
134
+ fileContent = fileContent.replace(providersRegex, `$1${providerString}`);
135
+ import_fs.default.writeFileSync(targetPath, fileContent);
136
+ console.log(import_chalk.default.green(`\u2705 Successfully injected configuration into ${isModule ? "app.module.ts" : "app.config.ts"}`));
124
137
  console.log(import_chalk.default.yellow("\nNext steps:"));
125
138
  console.log("1. Get your API Key from https://www.kopynator.com");
126
- console.log("2. Update the apiKey in src/app/app.config.ts");
139
+ console.log(`2. Update the apiKey in ${isModule ? "src/app/app.module.ts" : "src/app/app.config.ts"}`);
127
140
  return;
128
141
  } else {
129
- console.log(import_chalk.default.yellow('\u26A0\uFE0F Could not find "providers: []" array in app.config.ts. Falling back to config file.'));
142
+ console.log(import_chalk.default.yellow(`\u26A0\uFE0F Could not find "providers: []" array. Falling back to config file.`));
130
143
  }
131
144
  }
132
145
  } else {
133
- console.log(import_chalk.default.yellow("\u26A0\uFE0F Could not find src/app/app.config.ts. Falling back to config file."));
146
+ console.log(import_chalk.default.yellow("\u26A0\uFE0F Could not find app.config.ts or app.module.ts. Falling back to config file."));
134
147
  }
135
148
  }
136
149
  const configContent = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kopynator/cli",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "CLI tool for Kopynator - The i18n management solution",
5
5
  "bin": {
6
6
  "kopynator": "dist/index.js"
@@ -71,22 +71,39 @@ export async function initCommand() {
71
71
  console.log(chalk.green(`\n✅ Setting up for ${answers.framework}...`));
72
72
 
73
73
  if (answers.framework === 'Angular') {
74
- const configPath = path.join(process.cwd(), 'src/app/app.config.ts');
74
+ // Try app.config.ts first (Angular 17+ standalone)
75
+ const appConfigPath = path.join(process.cwd(), 'src/app/app.config.ts');
76
+ const appModulePath = path.join(process.cwd(), 'src/app/app.module.ts');
75
77
 
76
- if (fs.existsSync(configPath)) {
77
- console.log(chalk.blue('ℹ️ Detected Angular project structure. Attempting to modify app.config.ts...'));
78
- let appConfigContent = fs.readFileSync(configPath, 'utf-8');
78
+ let targetPath: string | null = null;
79
+ let isModule = false;
80
+
81
+ if (fs.existsSync(appConfigPath)) {
82
+ targetPath = appConfigPath;
83
+ isModule = false;
84
+ console.log(chalk.blue('ℹ️ Detected Angular standalone project (app.config.ts)'));
85
+ } else if (fs.existsSync(appModulePath)) {
86
+ targetPath = appModulePath;
87
+ isModule = true;
88
+ console.log(chalk.blue('ℹ️ Detected Angular NgModule project (app.module.ts)'));
89
+ }
90
+
91
+ if (targetPath) {
92
+ let fileContent = fs.readFileSync(targetPath, 'utf-8');
79
93
 
80
94
  // 1. Add Import if missing
81
- if (!appConfigContent.includes('@kopynator/angular')) {
82
- appConfigContent = `import { provideKopynator } from '@kopynator/angular';\n` + appConfigContent;
95
+ const hasImport = fileContent.includes('@kopynator/angular');
96
+ if (!hasImport) {
97
+ fileContent = `import { provideKopynator } from '@kopynator/angular';\n` + fileContent;
83
98
  }
84
99
 
85
100
  // 2. Add Provider
86
- // Simple regex to find the providers array. This is a heuristic and might need AST for robust handling,
87
- // but standard regex works for most standard Angular CLI generated files.
88
- // We look for "providers: [" and insert our provider right after.
89
- const providerString = `
101
+ // Check for the *usage* of the provider
102
+ if (fileContent.includes('provideKopynator(')) {
103
+ console.log(chalk.yellow('⚠️ provideKopynator is already present. Skipping injection.'));
104
+ return; // Exit successfully, assuming manual config is sufficient
105
+ } else {
106
+ const providerString = `
90
107
  provideKopynator({
91
108
  apiKey: 'YOUR_API_KEY_HERE',
92
109
  defaultLocale: '${answers.defaultLocale}',
@@ -94,24 +111,25 @@ export async function initCommand() {
94
111
  languages: ${JSON.stringify(answers.languages)},
95
112
  }),`;
96
113
 
97
- if (appConfigContent.includes('provideKopynator')) {
98
- console.log(chalk.yellow('⚠️ provideKopynator is already present in app.config.ts. Skipping injection.'));
99
- } else {
100
- const providersRegex = /(providers:\s*\[)/;
101
- if (providersRegex.test(appConfigContent)) {
102
- appConfigContent = appConfigContent.replace(providersRegex, `$1${providerString}`);
103
- fs.writeFileSync(configPath, appConfigContent);
104
- console.log(chalk.green('✅ Successfully injected configuration into src/app/app.config.ts'));
114
+ // Different regex for NgModule vs standalone config
115
+ const providersRegex = isModule
116
+ ? /(providers:\s*\[)/ // NgModule: providers: [ in @NgModule decorator
117
+ : /(providers:\s*\[)/; // Standalone: providers: [ in ApplicationConfig
118
+
119
+ if (providersRegex.test(fileContent)) {
120
+ fileContent = fileContent.replace(providersRegex, `$1${providerString}`);
121
+ fs.writeFileSync(targetPath, fileContent);
122
+ console.log(chalk.green(`✅ Successfully injected configuration into ${isModule ? 'app.module.ts' : 'app.config.ts'}`));
105
123
  console.log(chalk.yellow('\nNext steps:'));
106
124
  console.log('1. Get your API Key from https://www.kopynator.com');
107
- console.log('2. Update the apiKey in src/app/app.config.ts');
125
+ console.log(`2. Update the apiKey in ${isModule ? 'src/app/app.module.ts' : 'src/app/app.config.ts'}`);
108
126
  return; // Exit successfully without creating json
109
127
  } else {
110
- console.log(chalk.yellow('⚠️ Could not find "providers: []" array in app.config.ts. Falling back to config file.'));
128
+ console.log(chalk.yellow(`⚠️ Could not find "providers: []" array. Falling back to config file.`));
111
129
  }
112
130
  }
113
131
  } else {
114
- console.log(chalk.yellow('⚠️ Could not find src/app/app.config.ts. Falling back to config file.'));
132
+ console.log(chalk.yellow('⚠️ Could not find app.config.ts or app.module.ts. Falling back to config file.'));
115
133
  }
116
134
  }
117
135