@entrinsik/vite-plugin-informer 2.0.0 → 2.1.0

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/bin/init.js CHANGED
@@ -232,12 +232,18 @@ async function init() {
232
232
  console.log('Added @entrinsik/vite-plugin-informer to devDependencies');
233
233
  }
234
234
 
235
- // 6. Add deploy script if not present
235
+ // 6. Add scripts if not present
236
236
  if (!pkg.scripts) pkg.scripts = {};
237
237
  if (!pkg.scripts.deploy) {
238
238
  pkg.scripts.deploy = 'npm run build && informer-deploy';
239
239
  console.log('Added "deploy" script to package.json');
240
240
  }
241
+ if (!pkg.scripts['workspace:init']) {
242
+ pkg.scripts['workspace:init'] = 'informer-workspace init';
243
+ pkg.scripts['workspace:migrate'] = 'informer-workspace migrate';
244
+ pkg.scripts['workspace:reset'] = 'informer-workspace reset';
245
+ console.log('Added workspace scripts to package.json');
246
+ }
241
247
 
242
248
  await writeFile(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
243
249
  console.log(`Updated package.json with informer.name="${reportName}"`);
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env node
2
+
3
+ import dotenv from 'dotenv';
4
+ import { readFile } from 'node:fs/promises';
5
+ import { resolve } from 'node:path';
6
+ import { createClient } from '../src/client.js';
7
+ import { init, migrate, reset } from '../src/workspace.js';
8
+
9
+ dotenv.config();
10
+
11
+ const command = process.argv[2];
12
+
13
+ if (!command || !['init', 'migrate', 'reset'].includes(command)) {
14
+ console.error('Usage: informer-workspace <init|migrate|reset>');
15
+ console.error('');
16
+ console.error('Commands:');
17
+ console.error(' init Create a dev workspace datasource and run migrations');
18
+ console.error(' migrate Run pending migrations against the dev workspace');
19
+ console.error(' reset Drop all tables and re-run all migrations');
20
+ process.exit(1);
21
+ }
22
+
23
+ const baseUrl = process.env.INFORMER_URL;
24
+ const apiKey = process.env.INFORMER_API_KEY;
25
+ const user = process.env.INFORMER_USER;
26
+ const pass = process.env.INFORMER_PASS;
27
+
28
+ if (!baseUrl || (!apiKey && (!user || !pass))) {
29
+ console.error('Missing required environment variables.');
30
+ console.error('Set INFORMER_URL and either INFORMER_API_KEY or INFORMER_USER/INFORMER_PASS in .env');
31
+ process.exit(1);
32
+ }
33
+
34
+ const api = createClient({ baseUrl, apiKey, user, pass });
35
+ const migrationsDir = resolve('migrations');
36
+ const envPath = resolve('.env');
37
+
38
+ try {
39
+ if (command === 'init') {
40
+ // Check if already initialized
41
+ if (process.env.INFORMER_DEV_WORKSPACE) {
42
+ console.error(`Workspace already initialized: ${process.env.INFORMER_DEV_WORKSPACE}`);
43
+ console.error('Run workspace:reset to start fresh, or remove INFORMER_DEV_WORKSPACE from .env to re-initialize.');
44
+ process.exit(1);
45
+ }
46
+
47
+ // Read slug from package.json
48
+ const pkgPath = resolve('package.json');
49
+ let pkg;
50
+ try {
51
+ pkg = JSON.parse(await readFile(pkgPath, 'utf8'));
52
+ } catch {
53
+ console.error('Could not read package.json in current directory.');
54
+ process.exit(1);
55
+ }
56
+
57
+ let slug = pkg.name;
58
+ if (slug && slug.startsWith('@') && slug.includes('/')) {
59
+ slug = slug.split('/')[1];
60
+ }
61
+ if (!slug) {
62
+ console.error('package.json must have a "name" field.');
63
+ process.exit(1);
64
+ }
65
+
66
+ await init({ api, slug, migrationsDir, envPath });
67
+
68
+ } else if (command === 'migrate') {
69
+ const workspaceId = process.env.INFORMER_DEV_WORKSPACE;
70
+ if (!workspaceId) {
71
+ console.error('No dev workspace found. Run workspace:init first.');
72
+ process.exit(1);
73
+ }
74
+ await migrate({ api, workspaceId, migrationsDir });
75
+
76
+ } else if (command === 'reset') {
77
+ const workspaceId = process.env.INFORMER_DEV_WORKSPACE;
78
+ if (!workspaceId) {
79
+ console.error('No dev workspace found. Run workspace:init first.');
80
+ process.exit(1);
81
+ }
82
+ await reset({ api, workspaceId, migrationsDir });
83
+ }
84
+ } catch (err) {
85
+ console.error(`workspace:${command} failed:`, err.message);
86
+ process.exit(1);
87
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@entrinsik/vite-plugin-informer",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Vite plugin and publish tool for local Magic Report development",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,11 +23,13 @@
23
23
  "types": "./index.d.ts",
24
24
  "default": "./src/index.js"
25
25
  },
26
- "./deploy": "./src/deploy.js"
26
+ "./deploy": "./src/deploy.js",
27
+ "./workspace": "./src/workspace.js"
27
28
  },
28
29
  "bin": {
29
30
  "informer-deploy": "./bin/deploy.js",
30
31
  "informer-init": "./bin/init.js",
32
+ "informer-workspace": "./bin/workspace.js",
31
33
  "create-magic-report": "./bin/init.js"
32
34
  },
33
35
  "files": [
package/src/deploy.js CHANGED
@@ -11,7 +11,7 @@ const TEXT_EXTENSIONS = new Set([
11
11
  const CHUNK_THRESHOLD = 512 * 1024; // 512KB
12
12
 
13
13
  // Config files to upload from project root (if they exist)
14
- const ROOT_CONFIG_FILES = ['data-access.yaml'];
14
+ const ROOT_CONFIG_FILES = ['informer.yaml', 'data-access.yaml'];
15
15
 
16
16
  /**
17
17
  * Deploy a built Vite project to Informer as an App (or legacy Magic Report).
@@ -59,7 +59,7 @@ export async function deploy({ baseUrl, apiKey, user, pass, distDir, name, descr
59
59
 
60
60
  try {
61
61
  console.log(`Creating new app "${name}"...`);
62
- entity = await api.post('apps', { ...payload, type: 'report' });
62
+ entity = await api.post('apps', { ...payload, type: 'app' });
63
63
  apiPrefix = 'apps';
64
64
  } catch (e) {
65
65
  // If apps endpoint doesn't exist (404), fall back to legacy reports
@@ -168,8 +168,40 @@ export async function deploy({ baseUrl, apiKey, user, pass, distDir, name, descr
168
168
  }
169
169
  }
170
170
 
171
- // 8. Print URL and return UUID for saving
172
- const totalFiles = entries.length + configCount;
171
+ // 8. Upload migrations/ directory from project root (if it exists)
172
+ const migrationsDir = join(projectRoot, 'migrations');
173
+ let migrationsCount = 0;
174
+ try {
175
+ await access(migrationsDir);
176
+ const migrationFiles = await walkDir(migrationsDir);
177
+ for (const filePath of migrationFiles) {
178
+ const relPath = posix.normalize(relative(projectRoot, filePath).split('\\').join('/'));
179
+ const content = await readFile(filePath, 'utf8');
180
+ await api.put(`${entityPath}/contents/${relPath}`, {
181
+ content,
182
+ encoding: 'utf8'
183
+ });
184
+ console.log(` ${relPath} (from project root)`);
185
+ migrationsCount++;
186
+ }
187
+ } catch {
188
+ // No migrations directory, skip
189
+ }
190
+
191
+ // 9. Run migrations (if the server supports it)
192
+ if (apiPrefix === 'apps') {
193
+ try {
194
+ const result = await api.post(`${entityPath}/_migrate`);
195
+ if (result && result.migrated && result.migrated.length > 0) {
196
+ console.log(`Ran ${result.migrated.length} migration(s): ${result.migrated.join(', ')}`);
197
+ }
198
+ } catch {
199
+ // Server may not support _migrate yet — ignore
200
+ }
201
+ }
202
+
203
+ // 9. Print URL and return UUID for saving
204
+ const totalFiles = entries.length + configCount + migrationsCount;
173
205
  const base = baseUrl.replace(/\/+$/, '');
174
206
  const entityUrl = apiPrefix === 'apps'
175
207
  ? `${base}/api/apps/${naturalId}/view`
package/src/index.js CHANGED
@@ -4,6 +4,7 @@ import dotenv from 'dotenv';
4
4
  * Vite plugin for local Magic Report development.
5
5
  *
6
6
  * - Proxies /api requests to the Informer server with Basic auth
7
+ * - Rewrites /api/_query to the dev workspace's _sql route (if configured)
7
8
  * - Injects window.__INFORMER__ context mock in dev mode
8
9
  * - Sets base to './' so built assets use relative paths
9
10
  *
@@ -12,6 +13,9 @@ import dotenv from 'dotenv';
12
13
  */
13
14
  export default function informer(options = {}) {
14
15
  let isDev = false;
16
+ let authHeader = null;
17
+ let serverOrigin = null;
18
+ let devWorkspaceId = null;
15
19
 
16
20
  return {
17
21
  name: 'vite-plugin-informer',
@@ -31,8 +35,11 @@ export default function informer(options = {}) {
31
35
  const user = process.env.INFORMER_USER;
32
36
  const pass = process.env.INFORMER_PASS;
33
37
 
38
+ devWorkspaceId = process.env.INFORMER_DEV_WORKSPACE || null;
39
+
34
40
  if (baseUrl) {
35
- const auth = apiKey
41
+ serverOrigin = baseUrl.replace(/\/+$/, '');
42
+ authHeader = apiKey
36
43
  ? 'Bearer ' + apiKey
37
44
  : 'Basic ' + Buffer.from(`${user}:${pass}`).toString('base64');
38
45
 
@@ -42,7 +49,7 @@ export default function informer(options = {}) {
42
49
  target: baseUrl,
43
50
  changeOrigin: true,
44
51
  headers: {
45
- Authorization: auth
52
+ Authorization: authHeader
46
53
  }
47
54
  }
48
55
  }
@@ -53,6 +60,45 @@ export default function informer(options = {}) {
53
60
  return cfg;
54
61
  },
55
62
 
63
+ configureServer(server) {
64
+ if (!isDev || !devWorkspaceId || !serverOrigin) return;
65
+
66
+ // Intercept POST /api/_query and proxy to the dev workspace's _sql route.
67
+ // This middleware runs before the generic /api proxy, so the rewrite takes effect.
68
+ server.middlewares.use('/api/_query', async (req, res, next) => {
69
+ if (req.method !== 'POST') return next();
70
+
71
+ // Read request body
72
+ const chunks = [];
73
+ for await (const chunk of req) {
74
+ chunks.push(chunk);
75
+ }
76
+ const body = Buffer.concat(chunks).toString('utf8');
77
+
78
+ // Forward to workspace _sql endpoint
79
+ const url = `${serverOrigin}/api/datasources/${devWorkspaceId}/_sql`;
80
+ try {
81
+ const upstream = await fetch(url, {
82
+ method: 'POST',
83
+ headers: {
84
+ 'Content-Type': 'application/json',
85
+ Authorization: authHeader
86
+ },
87
+ body
88
+ });
89
+
90
+ res.statusCode = upstream.status;
91
+ res.setHeader('Content-Type', 'application/json');
92
+ const text = await upstream.text();
93
+ res.end(text);
94
+ } catch (err) {
95
+ res.statusCode = 502;
96
+ res.setHeader('Content-Type', 'application/json');
97
+ res.end(JSON.stringify({ error: err.message }));
98
+ }
99
+ });
100
+ },
101
+
56
102
  transformIndexHtml: {
57
103
  order: 'pre',
58
104
  handler(html) {
@@ -64,6 +110,7 @@ export default function informer(options = {}) {
64
110
  name: 'Local Development'
65
111
  },
66
112
  theme: 'light',
113
+ roles: [],
67
114
  ...options.mock
68
115
  };
69
116
 
@@ -0,0 +1,159 @@
1
+ import { readdir, readFile, writeFile, access } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+
4
+ /**
5
+ * Execute raw SQL against a workspace datasource via the _sql route.
6
+ */
7
+ async function execSql(api, workspaceId, sql, params = []) {
8
+ return api.post(`datasources/${workspaceId}/_sql`, { sql, params });
9
+ }
10
+
11
+ /**
12
+ * Ensure the _migrations tracking table exists in the workspace.
13
+ */
14
+ async function ensureMigrationsTable(api, workspaceId) {
15
+ await execSql(api, workspaceId, `
16
+ CREATE TABLE IF NOT EXISTS _migrations (
17
+ name TEXT PRIMARY KEY,
18
+ executed_at TIMESTAMPTZ DEFAULT NOW()
19
+ )
20
+ `);
21
+ }
22
+
23
+ /**
24
+ * Read local migration files sorted alphabetically.
25
+ * @returns {Promise<string[]>} Sorted filenames (e.g., ['001-create-orders.sql', '002-add-index.sql'])
26
+ */
27
+ async function readMigrationFiles(migrationsDir) {
28
+ try {
29
+ await access(migrationsDir);
30
+ } catch {
31
+ return [];
32
+ }
33
+
34
+ const entries = await readdir(migrationsDir);
35
+ return entries
36
+ .filter(f => f.endsWith('.sql'))
37
+ .sort();
38
+ }
39
+
40
+ /**
41
+ * Get the set of already-executed migration names.
42
+ */
43
+ async function getCompletedMigrations(api, workspaceId) {
44
+ const result = await execSql(api, workspaceId, 'SELECT name FROM _migrations');
45
+ return new Set(result.rows.map(r => r.name));
46
+ }
47
+
48
+ /**
49
+ * Run pending migrations against a workspace.
50
+ */
51
+ async function runMigrations(api, workspaceId, migrationsDir) {
52
+ await ensureMigrationsTable(api, workspaceId);
53
+
54
+ const files = await readMigrationFiles(migrationsDir);
55
+ if (files.length === 0) {
56
+ console.log('No migration files found in migrations/');
57
+ return { migrated: [], total: 0 };
58
+ }
59
+
60
+ const completed = await getCompletedMigrations(api, workspaceId);
61
+ const pending = files.filter(f => !completed.has(f));
62
+
63
+ if (pending.length === 0) {
64
+ console.log(`All ${files.length} migration(s) already applied.`);
65
+ return { migrated: [], total: files.length };
66
+ }
67
+
68
+ const migrated = [];
69
+ for (const file of pending) {
70
+ const sql = await readFile(join(migrationsDir, file), 'utf8');
71
+ console.log(` Running ${file}...`);
72
+ await execSql(api, workspaceId, sql);
73
+ await execSql(api, workspaceId, 'INSERT INTO _migrations (name) VALUES ($1)', [file]);
74
+ migrated.push(file);
75
+ }
76
+
77
+ console.log(`Applied ${migrated.length} migration(s). Total: ${files.length}`);
78
+ return { migrated, total: files.length };
79
+ }
80
+
81
+ /**
82
+ * Initialize a dev workspace datasource.
83
+ *
84
+ * Creates a new workspace datasource named "{slug}-dev", runs all
85
+ * migrations, and saves the workspace ID to .env.
86
+ */
87
+ export async function init({ api, slug, migrationsDir, envPath }) {
88
+ const name = `${slug}-dev`;
89
+ console.log(`Creating workspace datasource "${name}"...`);
90
+
91
+ const datasource = await api.post('datasources', {
92
+ name,
93
+ type: 'workspace'
94
+ });
95
+
96
+ if (!datasource || !datasource.naturalId) {
97
+ throw new Error('Failed to create workspace datasource');
98
+ }
99
+
100
+ const workspaceId = datasource.naturalId;
101
+ console.log(`Created: ${workspaceId}`);
102
+
103
+ // Run all migrations
104
+ const result = await runMigrations(api, workspaceId, migrationsDir);
105
+
106
+ // Save workspace ID to .env
107
+ let envContent = '';
108
+ try {
109
+ envContent = await readFile(envPath, 'utf8');
110
+ } catch {
111
+ // .env doesn't exist yet
112
+ }
113
+
114
+ if (envContent.includes('INFORMER_DEV_WORKSPACE=')) {
115
+ envContent = envContent.replace(
116
+ /INFORMER_DEV_WORKSPACE=.*/,
117
+ `INFORMER_DEV_WORKSPACE=${workspaceId}`
118
+ );
119
+ } else {
120
+ const nl = envContent.length > 0 && !envContent.endsWith('\n') ? '\n' : '';
121
+ envContent += `${nl}\n# Dev workspace (created by workspace:init)\nINFORMER_DEV_WORKSPACE=${workspaceId}\n`;
122
+ }
123
+
124
+ await writeFile(envPath, envContent);
125
+ console.log(`Saved INFORMER_DEV_WORKSPACE=${workspaceId} to .env`);
126
+
127
+ return { workspaceId, ...result };
128
+ }
129
+
130
+ /**
131
+ * Run pending migrations against the dev workspace.
132
+ */
133
+ export async function migrate({ api, workspaceId, migrationsDir }) {
134
+ console.log(`Migrating workspace ${workspaceId}...`);
135
+ return runMigrations(api, workspaceId, migrationsDir);
136
+ }
137
+
138
+ /**
139
+ * Reset the dev workspace: drop all tables, then re-run all migrations.
140
+ */
141
+ export async function reset({ api, workspaceId, migrationsDir }) {
142
+ console.log(`Resetting workspace ${workspaceId}...`);
143
+
144
+ // Drop all tables in the workspace schema
145
+ await execSql(api, workspaceId, `
146
+ DO $$
147
+ DECLARE r RECORD;
148
+ BEGIN
149
+ FOR r IN SELECT tablename FROM pg_tables WHERE schemaname = current_schema()
150
+ LOOP
151
+ EXECUTE 'DROP TABLE IF EXISTS "' || r.tablename || '" CASCADE';
152
+ END LOOP;
153
+ END $$
154
+ `);
155
+ console.log('Dropped all tables.');
156
+
157
+ // Re-run all migrations from scratch
158
+ return runMigrations(api, workspaceId, migrationsDir);
159
+ }