@ryziz/cli 0.0.46 → 0.0.48

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/package.json +1 -1
  2. package/src/build.js +13 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ryziz/cli",
3
- "version": "0.0.46",
3
+ "version": "0.0.48",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "ryziz": "./index.js",
package/src/build.js CHANGED
@@ -43,11 +43,16 @@ export default (isDev) => task('Building', {
43
43
  'Functions': async (c) => {
44
44
  const scanApiRoutes = () => {
45
45
  const files = existsSync(c.src) ? readdirSync(c.src).filter((f) => /^api\..+\.js$/.test(f)) : [];
46
- return files.map((file) => ({
47
- file,
48
- path: ('/' + file.slice(4, -3)).replace(/^\/index$/, '/').replace(/\./g, '/').replace(/\$/g, ':'),
49
- functionName: file.slice(0, -3).replace(/\./g, '-').replace(/\$/g, ''),
50
- }));
46
+ return files.map((file) => {
47
+ const content = readFileSync(join(c.src, file), 'utf8');
48
+ const hasConfig = /export\s+(const|let|var)\s+config\s*=/.test(content);
49
+ return {
50
+ file,
51
+ path: ('/api' + file.slice(3, -3)).replace(/^\/api\/index$/, '/api/').replace(/\./g, '/').replace(/\$/g, ':'),
52
+ functionName: file.slice(0, -3).replace(/\./g, '-').replace(/\$/g, ''),
53
+ hasConfig,
54
+ };
55
+ });
51
56
  };
52
57
 
53
58
  c.functionsBuilder = await context({
@@ -56,13 +61,14 @@ export default (isDev) => task('Building', {
56
61
  external: ['firebase-admin', 'firebase-functions', 'express'],
57
62
  plugins: [
58
63
  { name: 'log', setup: (b) => b.onEnd(() => { c.onFunctionsBuilt?.(); }) },
59
- { name: 'var', setup: (b) => { b.onResolve({ filter: /^virtual:api-routes$/ }, (a) => ({ path: a.path, namespace: 'var' })); b.onLoad({ filter: /.*/, namespace: 'var' }, () => { const routes = scanApiRoutes(); const requires = routes.map((r, i) => `const handler${i} = require('./src/${r.file}');`).join('\n'); const entries = routes.map((r, i) => `{path:'${r.path}',handler:handler${i}.default || handler${i},functionName:'${r.functionName}'}`).join(','); return { contents: `${requires}\nmodule.exports = [${entries}];`, loader: 'js', resolveDir: c.root, watchDirs: [c.src] }; }); } },
60
- { name: 'fb', setup: (b) => b.onEnd(() => { const routes = scanApiRoutes(); const cfg = JSON.parse(readFileSync(join(c.functionsPkg, 'firebase.json'))); writeFileSync(join(c.dist, 'firebase.json'), JSON.stringify({ ...cfg, hosting: { ...cfg.hosting, rewrites: [...routes.map((r) => ({ source: r.path, function: r.functionName })), { source: '**', destination: '/index.html' }] } }, null, 2)); }) },
64
+ { name: 'var', setup: (b) => { b.onResolve({ filter: /^virtual:api-routes$/ }, (a) => ({ path: a.path, namespace: 'var' })); b.onLoad({ filter: /.*/, namespace: 'var' }, () => { const routes = scanApiRoutes(); const requires = routes.map((r, i) => `const module${i} = require('./src/${r.file}');`).join('\n'); const entries = routes.map((r, i) => `{path:'${r.path}',handler:module${i}.default || module${i},config:module${i}.config,functionName:'${r.functionName}'}`).join(','); return { contents: `${requires}\nmodule.exports = [${entries}];`, loader: 'js', resolveDir: c.root, watchDirs: [c.src] }; }); } },
65
+ { name: 'fb', setup: (b) => b.onEnd(() => { const routes = scanApiRoutes(); const cfg = JSON.parse(readFileSync(join(c.functionsPkg, 'firebase.json'))); const rewrites = routes.map((r) => ({ source: r.path, function: r.hasConfig ? r.functionName : 'api' })); writeFileSync(join(c.dist, 'firebase.json'), JSON.stringify({ ...cfg, hosting: { ...cfg.hosting, rewrites: [...rewrites, { source: '**', destination: '/index.html' }] } }, null, 2)); }) },
61
66
  ],
62
67
  });
63
68
 
64
69
  await c.functionsBuilder.rebuild();
65
70
  cpSync(join(c.functionsPkg, 'package.json'), join(c.dist, 'functions', 'package.json'));
71
+ cpSync(join(c.functionsPkg, 'firestore.rules'), join(c.dist, 'firestore.rules'));
66
72
  if (!isDev) await c.functionsBuilder.dispose();
67
73
  },
68
74
  'Watching': async (c) => {