oneentry 1.0.140 → 1.0.142

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 (2) hide show
  1. package/configure.js +107 -15
  2. package/package.json +1 -1
package/configure.js CHANGED
@@ -4,11 +4,13 @@
4
4
  * OneEntry SDK Configuration Tool
5
5
  *
6
6
  * Interactive CLI tool for initial SDK setup. Prompts the user for:
7
+ * - Project type (Node.js or Next.js)
7
8
  * - Project URL (must include https://)
8
9
  * - Authentication token
9
10
  *
10
- * Generates an example.ts file with a TypeScript interface template
11
- * containing the provided configuration values.
11
+ * Node.js: Generates an example.mjs file with a ready-to-use SDK setup.
12
+ * Next.js: Initializes a new Next.js project via create-next-app and adds
13
+ * oneentry configuration in lib/oneentry.ts and .env.local.
12
14
  *
13
15
  * Usage:
14
16
  * npx oneentry
@@ -22,6 +24,7 @@
22
24
  /* eslint-disable @typescript-eslint/no-require-imports */
23
25
 
24
26
  const fs = require('fs');
27
+ const path = require('path');
25
28
  const readline = require('readline');
26
29
  const { spawn } = require('child_process');
27
30
 
@@ -30,26 +33,41 @@ const rl = readline.createInterface({
30
33
  output: process.stdout,
31
34
  });
32
35
 
33
- rl.question('Enter project name with https://... : ', (url) => {
34
- rl.question('Enter token: ', (token) => {
35
- rl.question('Run example after creating? (y/n): ', (answer) => {
36
- rl.close();
37
- createInterface(url, token, answer.trim().toLowerCase() === 'y');
36
+ console.log('\nOneEntry SDK setup\n');
37
+ console.log('1) Node.js');
38
+ console.log('2) Next.js\n');
39
+
40
+ rl.question('Choose project type (1/2): ', (type) => {
41
+ const isNext = type.trim() === '2';
42
+
43
+ if (isNext) {
44
+ rl.question('Enter Next.js project name: ', (projectName) => {
45
+ rl.question('Enter project URL with https://... : ', (url) => {
46
+ rl.question('Enter token: ', (token) => {
47
+ rl.close();
48
+ initNextProject(projectName.trim(), url.trim(), token.trim());
49
+ });
50
+ });
38
51
  });
39
- });
52
+ } else {
53
+ rl.question('Enter project URL with https://... : ', (url) => {
54
+ rl.question('Enter token: ', (token) => {
55
+ rl.question('Run example after creating? (y/n): ', (answer) => {
56
+ rl.close();
57
+ createNodeExample(url.trim(), token.trim(), answer.trim().toLowerCase() === 'y');
58
+ });
59
+ });
60
+ });
61
+ }
40
62
  });
41
63
 
42
64
  /**
43
65
  * Creates an example JavaScript file with SDK initialization and sample requests
44
- *
45
- * Generates an example.mjs file with a ready-to-use defineOneEntry call
46
- * that fetches admins, pages and products and prints them to the console.
47
- * Run with: node example.mjs
48
- * @param {string} url - Project URL with https:// (e.g., "https://example.oneentry.cloud")
49
- * @param {string} token - Authentication token for API access
66
+ * @param {string} url - Project URL with https://
67
+ * @param {string} token - Authentication token
50
68
  * @param {boolean} run - Whether to run the generated file immediately
51
69
  */
52
- function createInterface(url, token, run) {
70
+ function createNodeExample(url, token, run) {
53
71
  const filePath = 'example.mjs';
54
72
  const tokenLine = token ? `\n token: '${token}',` : '';
55
73
  fs.writeFile(
@@ -88,3 +106,77 @@ console.log('Products:', JSON.stringify(products, null, 2));
88
106
  },
89
107
  );
90
108
  }
109
+
110
+ /**
111
+ * Initializes a Next.js project and adds OneEntry SDK configuration
112
+ * @param {string} projectName - Directory name for the new Next.js project
113
+ * @param {string} url - Project URL with https://
114
+ * @param {string} token - Authentication token
115
+ */
116
+ function initNextProject(projectName, url, token) {
117
+ console.log(`\nRunning create-next-app for "${projectName}"...\n`);
118
+
119
+ const child = spawn(
120
+ 'npx',
121
+ ['create-next-app@latest', projectName, '--ts', '--app', '--eslint', '--no-tailwind', '--src-dir', '--import-alias', '@/*'],
122
+ { stdio: 'inherit', shell: true },
123
+ );
124
+
125
+ child.on('error', (e) => {
126
+ console.error('Failed to run create-next-app:', e.message);
127
+ });
128
+
129
+ child.on('close', (code) => {
130
+ if (code !== 0) {
131
+ console.error(`create-next-app exited with code ${code}`);
132
+ return;
133
+ }
134
+ addOneEntryConfig(projectName, url, token);
135
+ });
136
+ }
137
+
138
+ /**
139
+ * Writes oneentry config files into the newly created Next.js project
140
+ * @param {string} projectName - Project directory name
141
+ * @param {string} url - Project URL with https://
142
+ * @param {string} token - Authentication token
143
+ */
144
+ function addOneEntryConfig(projectName, url, token) {
145
+ const libDir = path.join(projectName, 'src', 'lib');
146
+ fs.mkdirSync(libDir, { recursive: true });
147
+
148
+ const tokenLine = token ? `\n token: process.env.ONEENTRY_TOKEN,` : '';
149
+ const libContent = `import { defineOneEntry } from 'oneentry';
150
+
151
+ const { Admins, Pages, Products } = defineOneEntry(
152
+ process.env.NEXT_PUBLIC_ONEENTRY_URL ?? '',
153
+ {${tokenLine}
154
+ langCode: 'en_US',
155
+ },
156
+ );
157
+
158
+ export { Admins, Pages, Products };
159
+ `;
160
+
161
+ const envContent = `NEXT_PUBLIC_ONEENTRY_URL=${url}\n${token ? `ONEENTRY_TOKEN=${token}\n` : ''}`;
162
+
163
+ fs.writeFile(path.join(libDir, 'oneentry.ts'), libContent, (err) => {
164
+ if (err) {
165
+ console.error('Failed to create lib/oneentry.ts:', err.message);
166
+ return;
167
+ }
168
+ console.log(`Created ${projectName}/src/lib/oneentry.ts`);
169
+ });
170
+
171
+ fs.writeFile(path.join(projectName, '.env.local'), envContent, (err) => {
172
+ if (err) {
173
+ console.error('Failed to create .env.local:', err.message);
174
+ return;
175
+ }
176
+ console.log(`Created ${projectName}/.env.local`);
177
+ console.log(`\nNext steps:`);
178
+ console.log(` cd ${projectName}`);
179
+ console.log(` npm install oneentry`);
180
+ console.log(` npm run dev`);
181
+ });
182
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oneentry",
3
- "version": "1.0.140",
3
+ "version": "1.0.142",
4
4
  "description": "OneEntry NPM package",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",