@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 +32 -19
- package/package.json +1 -1
- package/src/commands/init.ts +39 -21
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
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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(
|
|
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(
|
|
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
|
|
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
package/src/commands/init.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
82
|
-
|
|
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
|
-
//
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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(
|
|
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(
|
|
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
|
|
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
|
|