@searkin/next 0.1.0 → 0.1.1
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/README.md +1 -1
- package/dist/cli.js +49 -3
- package/dist/shared.d.ts +1 -1
- package/dist/shared.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,12 +7,12 @@ Compatible avec Next.js 13.4 à 16, en App Router comme en Pages Router.
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
npm install @searkin/next
|
|
11
10
|
npx @searkin/next init --key sks_…
|
|
12
11
|
```
|
|
13
12
|
|
|
14
13
|
`init` commence par détecter si le projet utilise l'App Router ou le Pages Router, et s'il a un dossier `src/`. Il effectue ensuite les opérations suivantes :
|
|
15
14
|
|
|
15
|
+
- il ajoute `@searkin/next` aux dépendances du site, avec le gestionnaire de paquets du projet (npm, pnpm, yarn ou bun, d'après son fichier de verrouillage). L'option `--no-install` l'en dispense ;
|
|
16
16
|
- il écrit `SEARKIN_KEY` dans `.env.local`, ainsi que `SEARKIN_SITE_URL` s'il peut la déduire de `NEXT_PUBLIC_SERVER_URL` / `NEXT_PUBLIC_SITE_URL` (ou de l'option `--site-url`) ;
|
|
17
17
|
- il crée la route `app/api/searkin/[...searkin]/route.ts`, ou `pages/api/searkin/[...searkin].ts` en Pages Router ;
|
|
18
18
|
- il crée `middleware.ts` (`proxy.ts` à partir de Next 16). Si ce fichier existe déjà, il n'y touche pas et affiche les deux lignes à ajouter ;
|
package/dist/cli.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* npx @searkin/next init --key sks_… [--dir <projet>] [--site-url https://…] [--dry-run]
|
|
3
|
+
* npx @searkin/next init --key sks_… [--dir <projet>] [--site-url https://…] [--dry-run] [--no-install]
|
|
4
4
|
*/
|
|
5
|
+
import { spawnSync } from 'node:child_process';
|
|
5
6
|
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
6
7
|
import { dirname, join, relative, resolve } from 'node:path';
|
|
7
8
|
import { VERSION } from './shared.js';
|
|
8
9
|
function parseArgs(argv) {
|
|
9
|
-
const a = { dir: process.cwd(), dryRun: false };
|
|
10
|
+
const a = { dir: process.cwd(), dryRun: false, install: true };
|
|
10
11
|
for (let i = 0; i < argv.length; i++) {
|
|
11
12
|
const v = argv[i];
|
|
12
13
|
if (v === '--key')
|
|
@@ -19,6 +20,8 @@ function parseArgs(argv) {
|
|
|
19
20
|
a.dir = resolve(argv[++i] || '.');
|
|
20
21
|
else if (v === '--dry-run')
|
|
21
22
|
a.dryRun = true;
|
|
23
|
+
else if (v === '--no-install')
|
|
24
|
+
a.install = false;
|
|
22
25
|
else if (v === '--version' || v === '-v')
|
|
23
26
|
a.cmd = 'version';
|
|
24
27
|
else if (!v.startsWith('-') && !a.cmd)
|
|
@@ -133,6 +136,46 @@ export async function getServerSideProps({ resolvedUrl }) {
|
|
|
133
136
|
export default function Page({ seo }) {
|
|
134
137
|
return <><SearkinHead seo={seo} />…</>
|
|
135
138
|
}`;
|
|
139
|
+
/** Gestionnaire de paquets du projet, d'après son fichier de verrouillage. */
|
|
140
|
+
function packageManager(root) {
|
|
141
|
+
if (existsSync(join(root, 'pnpm-lock.yaml')))
|
|
142
|
+
return { cmd: 'pnpm', add: ['add'] };
|
|
143
|
+
if (existsSync(join(root, 'yarn.lock')))
|
|
144
|
+
return { cmd: 'yarn', add: ['add'] };
|
|
145
|
+
if (existsSync(join(root, 'bun.lock')) || existsSync(join(root, 'bun.lockb')))
|
|
146
|
+
return { cmd: 'bun', add: ['add'] };
|
|
147
|
+
return { cmd: 'npm', add: ['install'] };
|
|
148
|
+
}
|
|
149
|
+
/** Les fichiers créés importent `@searkin/next` : le paquet doit être une
|
|
150
|
+
* dépendance du site (lancé par `npx`, il ne l'est pas encore). */
|
|
151
|
+
function ensureDependency(root, args) {
|
|
152
|
+
let pkg;
|
|
153
|
+
try {
|
|
154
|
+
pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
pkg = {};
|
|
158
|
+
}
|
|
159
|
+
if (pkg.dependencies?.['@searkin/next'] || pkg.devDependencies?.['@searkin/next']) {
|
|
160
|
+
log('Dépendance @searkin/next : déjà présente.');
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
const pm = packageManager(root);
|
|
164
|
+
const cmd = [pm.cmd, ...pm.add, `@searkin/next@^${VERSION}`];
|
|
165
|
+
if (args.dryRun || !args.install) {
|
|
166
|
+
log(`Dépendance @searkin/next : à installer avec « ${cmd.join(' ')} ».`);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
log(`Installation : ${cmd.join(' ')}`);
|
|
170
|
+
const res = spawnSync(cmd[0], cmd.slice(1), {
|
|
171
|
+
cwd: root, stdio: 'inherit', shell: process.platform === 'win32',
|
|
172
|
+
});
|
|
173
|
+
if (res.status !== 0) {
|
|
174
|
+
log(`Échec de l'installation : lancez « ${cmd.join(' ')} » à la racine du projet, puis relancez init.`);
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
136
179
|
function init(args) {
|
|
137
180
|
const root = args.dir;
|
|
138
181
|
log(`@searkin/next ${VERSION} — init${args.dryRun ? ' (simulation)' : ''}`);
|
|
@@ -150,6 +193,9 @@ function init(args) {
|
|
|
150
193
|
log(`Projet : ${hasApp ? 'App Router' : ''}${hasApp && hasPages ? ' + ' : ''}${hasPages ? 'Pages Router' : ''}${!hasApp && !hasPages ? 'aucun dossier app/ ni pages/' : ''}${base !== root ? ' (src/)' : ''}, Next ${major}${isExport ? ', output: export' : ''}`);
|
|
151
194
|
if (!hasApp && !hasPages)
|
|
152
195
|
return 1;
|
|
196
|
+
// 0. Dépendance (les fichiers ci-dessous l'importent).
|
|
197
|
+
if (!ensureDependency(root, args))
|
|
198
|
+
return 1;
|
|
153
199
|
// 1. Variables d'environnement.
|
|
154
200
|
if (args.key) {
|
|
155
201
|
if (!/^sks_[A-Za-z0-9_-]{16,}$/.test(args.key))
|
|
@@ -225,7 +271,7 @@ function main() {
|
|
|
225
271
|
}
|
|
226
272
|
if (!args.cmd || args.cmd === 'init')
|
|
227
273
|
return init(args);
|
|
228
|
-
log('Usage : npx @searkin/next init --key sks_… [--site-url https://…] [--dir <projet>] [--dry-run]');
|
|
274
|
+
log('Usage : npx @searkin/next init --key sks_… [--site-url https://…] [--dir <projet>] [--dry-run] [--no-install]');
|
|
229
275
|
return 1;
|
|
230
276
|
}
|
|
231
277
|
process.exitCode = main();
|
package/dist/shared.d.ts
CHANGED
package/dist/shared.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Code commun, sans dépendance Node : utilisable côté serveur, en Edge (middleware) et au build.
|
|
2
|
-
export const VERSION = '0.1.
|
|
2
|
+
export const VERSION = '0.1.1';
|
|
3
3
|
export const CACHE_TAG = 'searkin';
|
|
4
4
|
const DEFAULT_API = 'https://app.searkin.com/api/proxy';
|
|
5
5
|
const env = (name) => {
|
package/package.json
CHANGED