@kopynator/cli 1.0.9 → 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 +29 -17
- package/package.json +1 -1
- package/src/commands/init.ts +36 -17
package/dist/index.js
CHANGED
|
@@ -98,17 +98,29 @@ 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
|
-
|
|
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");
|
|
106
117
|
if (!hasImport) {
|
|
107
|
-
|
|
108
|
-
` +
|
|
118
|
+
fileContent = `import { provideKopynator } from '@kopynator/angular';
|
|
119
|
+
` + fileContent;
|
|
109
120
|
}
|
|
110
|
-
if (
|
|
111
|
-
console.log(import_chalk.default.yellow("\u26A0\uFE0F provideKopynator is already present
|
|
121
|
+
if (fileContent.includes("provideKopynator(")) {
|
|
122
|
+
console.log(import_chalk.default.yellow("\u26A0\uFE0F provideKopynator is already present. Skipping injection."));
|
|
123
|
+
return;
|
|
112
124
|
} else {
|
|
113
125
|
const providerString = `
|
|
114
126
|
provideKopynator({
|
|
@@ -117,21 +129,21 @@ async function initCommand() {
|
|
|
117
129
|
mode: 'local',
|
|
118
130
|
languages: ${JSON.stringify(answers.languages)},
|
|
119
131
|
}),`;
|
|
120
|
-
const providersRegex = /(providers:\s*\[)/;
|
|
121
|
-
if (providersRegex.test(
|
|
122
|
-
|
|
123
|
-
import_fs.default.writeFileSync(
|
|
124
|
-
console.log(import_chalk.default.green(
|
|
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"}`));
|
|
125
137
|
console.log(import_chalk.default.yellow("\nNext steps:"));
|
|
126
138
|
console.log("1. Get your API Key from https://www.kopynator.com");
|
|
127
|
-
console.log(
|
|
139
|
+
console.log(`2. Update the apiKey in ${isModule ? "src/app/app.module.ts" : "src/app/app.config.ts"}`);
|
|
128
140
|
return;
|
|
129
141
|
} else {
|
|
130
|
-
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.`));
|
|
131
143
|
}
|
|
132
144
|
}
|
|
133
145
|
} else {
|
|
134
|
-
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."));
|
|
135
147
|
}
|
|
136
148
|
}
|
|
137
149
|
const configContent = {
|
package/package.json
CHANGED
package/src/commands/init.ts
CHANGED
|
@@ -71,22 +71,37 @@ 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
|
-
const hasImport =
|
|
95
|
+
const hasImport = fileContent.includes('@kopynator/angular');
|
|
82
96
|
if (!hasImport) {
|
|
83
|
-
|
|
97
|
+
fileContent = `import { provideKopynator } from '@kopynator/angular';\n` + fileContent;
|
|
84
98
|
}
|
|
85
99
|
|
|
86
100
|
// 2. Add Provider
|
|
87
|
-
// Check for the *usage* of the provider
|
|
88
|
-
if (
|
|
89
|
-
console.log(chalk.yellow('⚠️ provideKopynator is already present
|
|
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
|
|
90
105
|
} else {
|
|
91
106
|
const providerString = `
|
|
92
107
|
provideKopynator({
|
|
@@ -96,21 +111,25 @@ export async function initCommand() {
|
|
|
96
111
|
languages: ${JSON.stringify(answers.languages)},
|
|
97
112
|
}),`;
|
|
98
113
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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'}`));
|
|
104
123
|
console.log(chalk.yellow('\nNext steps:'));
|
|
105
124
|
console.log('1. Get your API Key from https://www.kopynator.com');
|
|
106
|
-
console.log(
|
|
125
|
+
console.log(`2. Update the apiKey in ${isModule ? 'src/app/app.module.ts' : 'src/app/app.config.ts'}`);
|
|
107
126
|
return; // Exit successfully without creating json
|
|
108
127
|
} else {
|
|
109
|
-
console.log(chalk.yellow(
|
|
128
|
+
console.log(chalk.yellow(`⚠️ Could not find "providers: []" array. Falling back to config file.`));
|
|
110
129
|
}
|
|
111
130
|
}
|
|
112
131
|
} else {
|
|
113
|
-
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.'));
|
|
114
133
|
}
|
|
115
134
|
}
|
|
116
135
|
|