smoonb 1.0.7 → 1.0.9

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.
@@ -1,122 +1,95 @@
1
- const chalk = require('chalk');
2
- const path = require('path');
3
- const fs = require('fs').promises;
4
- const { execSync } = require('child_process');
5
- const { copyDirectoryRecursive } = require('../utils');
6
- const { t } = require('../../../i18n');
7
-
8
- /**
9
- * Etapa 4: Restaurar Edge Functions via supabase functions deploy
10
- */
11
- module.exports = async ({ backupPath, targetProject }) => {
12
- try {
13
- const getT = global.smoonbI18n?.t || t;
14
- const edgeFunctionsDir = path.join(backupPath, 'edge-functions');
15
-
16
- if (!await fs.access(edgeFunctionsDir).then(() => true).catch(() => false)) {
17
- console.log(chalk.yellow(` ⚠️ ${getT('restore.steps.edgeFunctions.notFound')}`));
18
- return { success: false, functions_count: 0, success_count: 0 };
19
- }
20
-
21
- const items = await fs.readdir(edgeFunctionsDir);
22
- const functions = [];
23
-
24
- for (const item of items) {
25
- const itemPath = path.join(edgeFunctionsDir, item);
26
- const stats = await fs.stat(itemPath);
27
- if (stats.isDirectory()) {
28
- functions.push(item);
29
- }
30
- }
31
-
32
- if (functions.length === 0) {
33
- console.log(chalk.yellow(` ⚠️ ${getT('restore.steps.edgeFunctions.notFound')}`));
34
- return { success: false, functions_count: 0, success_count: 0 };
35
- }
36
-
37
- console.log(chalk.white(` - ${getT('restore.steps.edgeFunctions.found', { count: functions.length })}`));
38
-
39
- // COPIAR Edge Functions de backups/backup-XXX/edge-functions para supabase/functions
40
- const supabaseFunctionsDir = path.join(process.cwd(), 'supabase', 'functions');
41
-
42
- // Criar diretório supabase/functions se não existir
43
- await fs.mkdir(supabaseFunctionsDir, { recursive: true });
44
-
45
- // Limpar supabase/functions antes de copiar (necessário para garantir ambiente limpo)
46
- console.log(chalk.cyan(` - ${getT('restore.steps.edgeFunctions.cleaningBefore')}`));
47
- try {
48
- await fs.rm(supabaseFunctionsDir, { recursive: true, force: true });
49
- await fs.mkdir(supabaseFunctionsDir, { recursive: true });
50
- } catch {
51
- // Ignorar erro de limpeza se não existir
52
- }
53
-
54
- // Copiar cada Edge Function para supabase/functions
55
- for (const funcName of functions) {
56
- const backupFuncPath = path.join(edgeFunctionsDir, funcName);
57
- const targetFuncPath = path.join(supabaseFunctionsDir, funcName);
58
-
59
- console.log(chalk.white(` - ${getT('restore.steps.edgeFunctions.copying', { funcName })}`));
60
-
61
- // Copiar recursivamente
62
- await copyDirectoryRecursive(backupFuncPath, targetFuncPath);
63
- }
64
-
65
- console.log(chalk.white(` - ${getT('restore.steps.edgeFunctions.linking', { projectId: targetProject.targetProjectId })}`));
66
-
67
- // Linkar com o projeto destino
68
- try {
69
- execSync(`supabase link --project-ref ${targetProject.targetProjectId}`, {
70
- stdio: 'pipe',
71
- encoding: 'utf8',
72
- timeout: 10000,
73
- env: { ...process.env, SUPABASE_ACCESS_TOKEN: targetProject.targetAccessToken || '' }
74
- });
75
- } catch {
76
- console.log(chalk.yellow(` ⚠️ ${getT('restore.steps.edgeFunctions.linkMayExist')}`));
77
- }
78
-
79
- // Deploy das Edge Functions
80
- let successCount = 0;
81
- for (const funcName of functions) {
82
- console.log(chalk.white(` - ${getT('restore.steps.edgeFunctions.deploying', { funcName })}`));
83
-
84
- try {
85
- execSync(`supabase functions deploy ${funcName}`, {
86
- cwd: process.cwd(),
87
- stdio: 'pipe',
88
- encoding: 'utf8',
89
- timeout: 120000,
90
- env: { ...process.env, SUPABASE_ACCESS_TOKEN: targetProject.targetAccessToken || '' }
91
- });
92
-
93
- console.log(chalk.green(` ✅ ${getT('restore.steps.edgeFunctions.deployed', { funcName })}`));
94
- successCount++;
95
- } catch (deployError) {
96
- console.log(chalk.yellow(` ⚠️ ${getT('restore.steps.edgeFunctions.deployFailed', { funcName, message: deployError.message })}`));
97
- }
98
- }
99
-
100
- // Limpar supabase/functions após deploy (arquivos temporários não são mais necessários)
101
- console.log(chalk.cyan(` - ${getT('restore.steps.edgeFunctions.cleaningAfter')}`));
102
- try {
103
- await fs.rm(supabaseFunctionsDir, { recursive: true, force: true });
104
- } catch {
105
- // Ignorar erro de limpeza
106
- }
107
-
108
- console.log(chalk.green(` ✅ ${getT('restore.steps.edgeFunctions.success')}`));
109
-
110
- return {
111
- success: true,
112
- functions_count: functions.length,
113
- success_count: successCount
114
- };
115
-
116
- } catch (error) {
117
- const getT = global.smoonbI18n?.t || t;
118
- console.error(chalk.red(` ❌ ${getT('restore.steps.edgeFunctions.error', { message: error.message })}`));
119
- throw error;
120
- }
121
- };
122
-
1
+ const fs = require('fs').promises;
2
+ const os = require('os');
3
+ const path = require('path');
4
+ const { runSupabase } = require('../../../utils/supabaseCli');
5
+ const { management } = require('../management');
6
+ const { copyDirectoryRecursive } = require('../utils');
7
+ module.exports = async (
8
+ { backupPath, targetProject },
9
+ { deploy = runSupabase, request = management } = {},
10
+ ) => {
11
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'smoonb-functions-'));
12
+ try {
13
+ const source = path.join(backupPath, 'edge-functions');
14
+ const names = (await fs.readdir(source, { withFileTypes: true }))
15
+ .filter((e) => e.isDirectory() && e.name !== '_shared')
16
+ .map((e) => e.name);
17
+ if (!names.length)
18
+ return {
19
+ success: true,
20
+ verified: true,
21
+ functions_count: 0,
22
+ success_count: 0,
23
+ };
24
+ if (names.some((n) => !/^[-a-zA-Z0-9_]+$/.test(n)))
25
+ throw new Error('Invalid function slug in backup');
26
+ await copyDirectoryRecursive(
27
+ source,
28
+ path.join(root, 'supabase', 'functions'),
29
+ );
30
+ const manifest = JSON.parse(
31
+ await fs.readFile(path.join(backupPath, 'backup-manifest.json'), 'utf8'),
32
+ );
33
+ const metadata = manifest.components?.edge_functions?.functions || [];
34
+ const settings = names.map((name) => ({
35
+ name,
36
+ verify_jwt:
37
+ metadata.find((f) => (f.slug || f.name) === name)?.verify_jwt ?? true,
38
+ }));
39
+ await fs.writeFile(
40
+ path.join(root, 'supabase', 'config.toml'),
41
+ `project_id = "smoonb-restore"\n` +
42
+ settings
43
+ .map((f) => `[functions.${f.name}]\nverify_jwt = ${f.verify_jwt}\n`)
44
+ .join('\n'),
45
+ );
46
+ for (const f of settings) {
47
+ const args = [
48
+ 'functions',
49
+ 'deploy',
50
+ f.name,
51
+ '--project-ref',
52
+ targetProject.targetProjectId,
53
+ '--workdir',
54
+ root,
55
+ '--use-api',
56
+ ];
57
+ if (!f.verify_jwt) args.push('--no-verify-jwt');
58
+ try {
59
+ deploy('supabase', args, {
60
+ cwd: root,
61
+ env: {
62
+ ...process.env,
63
+ SUPABASE_ACCESS_TOKEN: targetProject.targetAccessToken,
64
+ },
65
+ stdio: 'pipe',
66
+ timeout: 180000,
67
+ windowsHide: true,
68
+ });
69
+ } catch {
70
+ throw new Error(`Edge Function deployment failed: ${f.name}`);
71
+ }
72
+ }
73
+ const remote = await request(targetProject, 'functions');
74
+ for (const f of settings)
75
+ if (
76
+ !remote.some(
77
+ (r) =>
78
+ r.slug === f.name &&
79
+ r.status === 'ACTIVE' &&
80
+ r.verify_jwt === f.verify_jwt,
81
+ )
82
+ )
83
+ throw new Error(`Edge Function verification failed: ${f.name}`);
84
+ return {
85
+ success: true,
86
+ verified: true,
87
+ functions_count: names.length,
88
+ success_count: names.length,
89
+ local_sources_preserved: true,
90
+ };
91
+ } finally {
92
+ // Only the directory freshly allocated by mkdtemp is removed.
93
+ await fs.rm(root, { recursive: true, force: true });
94
+ }
95
+ };
@@ -1,54 +1,15 @@
1
- const chalk = require('chalk');
2
- const path = require('path');
3
- const fs = require('fs');
4
- const inquirer = require('inquirer');
5
- const { t } = require('../../../i18n');
6
-
7
- /**
8
- * Etapa 5: Restaurar Auth Settings (interativo - exibir URL e valores)
9
- */
10
- module.exports = async ({ backupPath, targetProject }) => {
11
-
12
- try {
13
- const getT = global.smoonbI18n?.t || t;
14
- const authSettingsPath = path.join(backupPath, 'auth-settings.json');
15
-
16
- if (!fs.existsSync(authSettingsPath)) {
17
- console.log(chalk.yellow(` ⚠️ ${getT('restore.steps.auth.notFound')}`));
18
- return;
19
- }
20
-
21
- const authSettings = JSON.parse(fs.readFileSync(authSettingsPath, 'utf8'));
22
- const dashboardUrl = `https://supabase.com/dashboard/project/${targetProject.targetProjectId}/auth/url-config`;
23
-
24
- console.log(chalk.green(`\n ✅ ${getT('restore.steps.auth.urlTitle')}`));
25
- console.log(chalk.cyan(` ${dashboardUrl}`));
26
- console.log(chalk.yellow(`\n 📋 ${getT('restore.steps.auth.configureTitle')}`));
27
-
28
- if (authSettings.settings?.auth_url_config) {
29
- Object.entries(authSettings.settings.auth_url_config).forEach(([key, value]) => {
30
- console.log(chalk.white(` - ${key}: ${value}`));
31
- });
32
- } else if (authSettings.auth_url_config) {
33
- Object.entries(authSettings.auth_url_config).forEach(([key, value]) => {
34
- console.log(chalk.white(` - ${key}: ${value}`));
35
- });
36
- }
37
-
38
- console.log(chalk.yellow(`\n ⚠️ ${getT('restore.steps.auth.pressEnter')}`));
39
-
40
- await inquirer.prompt([{
41
- type: 'input',
42
- name: 'continue',
43
- message: getT('restore.steps.auth.pressEnter'),
44
- prefix: ''
45
- }]);
46
-
47
- console.log(chalk.green(` ✅ ${getT('restore.steps.auth.success')}`));
48
-
49
- } catch (error) {
50
- const getT = global.smoonbI18n?.t || t;
51
- console.error(chalk.red(` ❌ ${getT('restore.steps.auth.error', { message: error.message })}`));
52
- }
53
- };
54
-
1
+ const { applyConfig } = require('../management');
2
+ const { prepareAuth } = require('../auth');
3
+ const allowed = require('../auth-fields.json');
4
+ module.exports = async (context, { request } = {}) => {
5
+ const { desired, writeOnly } =
6
+ context.authConfig || (await prepareAuth(context));
7
+ return applyConfig(
8
+ context.targetProject,
9
+ 'config/auth',
10
+ desired,
11
+ allowed,
12
+ request,
13
+ { writeOnly },
14
+ );
15
+ };