@ryziz/cli 0.0.38 → 0.0.41
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/index.js +2 -66
- package/package.json +1 -1
- package/src/build.js +75 -0
package/index.js
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command } from 'commander';
|
|
3
|
-
import { readFileSync, readdirSync, mkdirSync, existsSync, cpSync, watch as fsWatch } from 'node:fs';
|
|
4
|
-
import { join, resolve, dirname } from 'node:path';
|
|
5
|
-
import { fileURLToPath } from 'node:url';
|
|
6
3
|
import { createRequire } from 'node:module';
|
|
7
|
-
import
|
|
8
|
-
import { task } from '@ryziz/flow';
|
|
4
|
+
import runBuild from './src/build.js';
|
|
9
5
|
|
|
10
|
-
const
|
|
11
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
12
|
-
const pkg = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf-8'));
|
|
6
|
+
const pkg = createRequire(import.meta.url)('./package.json');
|
|
13
7
|
|
|
14
8
|
const program = new Command();
|
|
15
9
|
program.name('ryziz').version(pkg.version);
|
|
@@ -18,61 +12,3 @@ program.command('dev').action(() => runBuild(true));
|
|
|
18
12
|
program.command('build').action(() => runBuild(false));
|
|
19
13
|
|
|
20
14
|
program.parse();
|
|
21
|
-
|
|
22
|
-
function runBuild(watch) {
|
|
23
|
-
task(watch ? 'Serving' : 'Bundling', {
|
|
24
|
-
'Configuring': (c) => {
|
|
25
|
-
c.root = process.cwd();
|
|
26
|
-
c.out = 'dist';
|
|
27
|
-
c.pub = join(c.root, 'public');
|
|
28
|
-
c.pkg = require.resolve('@ryziz/router/package.json');
|
|
29
|
-
if (!existsSync(c.out)) mkdirSync(c.out, { recursive: true });
|
|
30
|
-
},
|
|
31
|
-
'Scanning': (c) => {
|
|
32
|
-
const src = resolve(c.root, 'src');
|
|
33
|
-
if (existsSync(src)) {
|
|
34
|
-
const files = readdirSync(src).filter((f) => f.startsWith('pages.') && f.endsWith('.jsx'));
|
|
35
|
-
const maps = files.map((f, i) => ({
|
|
36
|
-
n: `R${i}`,
|
|
37
|
-
p: join(src, f).replace(/\\/g, '/'),
|
|
38
|
-
r: ('/' + f.slice(6, -4)).replace(/^\/index$/, '/').replace(/\./g, '/').replace(/\$/g, ':'),
|
|
39
|
-
}));
|
|
40
|
-
c.imp = maps.map((m) => `import ${m.n} from '${m.p}';`).join('\n');
|
|
41
|
-
c.rts = `[${maps.map((m) => `{path:'${m.r}',element:<${m.n}/>}`).join(',')}]`;
|
|
42
|
-
} else {
|
|
43
|
-
c.imp = ''; c.rts = '[]';
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
'Building': async (c) => {
|
|
47
|
-
const cp = () => existsSync(c.pub) && cpSync(c.pub, c.out, { recursive: true });
|
|
48
|
-
|
|
49
|
-
await build({
|
|
50
|
-
entryPoints: [join(dirname(c.pkg), 'entry.jsx')],
|
|
51
|
-
bundle: true,
|
|
52
|
-
outfile: join(c.out, 'assets/index.js'),
|
|
53
|
-
plugins: [{
|
|
54
|
-
name: 'vr',
|
|
55
|
-
setup(b) {
|
|
56
|
-
b.onResolve({ filter: /^virtual:routes$/ }, (a) => ({ path: a.path, namespace: 'vr' }));
|
|
57
|
-
b.onLoad({ filter: /.*/, namespace: 'vr' }, () => ({
|
|
58
|
-
contents: `import React from 'react';\n${c.imp}\nexport default ${c.rts};`,
|
|
59
|
-
loader: 'jsx',
|
|
60
|
-
resolveDir: c.root,
|
|
61
|
-
}));
|
|
62
|
-
},
|
|
63
|
-
}],
|
|
64
|
-
loader: { '.js': 'jsx', '.jsx': 'jsx' },
|
|
65
|
-
jsx: 'automatic',
|
|
66
|
-
watch: watch && {
|
|
67
|
-
onRebuild(e) { !e && (console.log('Reloaded'), cp()); },
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
cp();
|
|
72
|
-
if (watch) {
|
|
73
|
-
if (existsSync(c.pub)) fsWatch(c.pub, { recursive: true }, cp);
|
|
74
|
-
await new Promise(() => { });
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
}
|
package/package.json
CHANGED
package/src/build.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { context } from 'esbuild';
|
|
2
|
+
import { existsSync, readdirSync, mkdirSync, cpSync, watch, rmSync } from 'node:fs';
|
|
3
|
+
import { join, resolve, dirname } from 'node:path';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { task } from '@ryziz/flow';
|
|
6
|
+
|
|
7
|
+
export default (isDev) => task('Building', {
|
|
8
|
+
'Preparing': (c) => {
|
|
9
|
+
c.root = process.cwd(); c.dist = 'dist'; c.public = join(c.root, 'public');
|
|
10
|
+
if (existsSync(c.dist)) rmSync(c.dist, { recursive: true, force: true });
|
|
11
|
+
mkdirSync(c.dist, { recursive: true });
|
|
12
|
+
mkdirSync(join(c.dist, 'functions'), { recursive: true });
|
|
13
|
+
},
|
|
14
|
+
'Hosting': async (c) => {
|
|
15
|
+
const getRoutes = () => {
|
|
16
|
+
const src = resolve(c.root, 'src'), files = existsSync(src) ? readdirSync(src).filter((f) => /^pages\..+\.jsx$/.test(f)) : [];
|
|
17
|
+
const imports = files.map((file, index) => `import R${index} from './src/${file}';`).join('\n');
|
|
18
|
+
const exports = files.map((file, index) => `{path:'${('/' + file.slice(6, -4)).replace(/^\/index$/, '/').replace(/\./g, '/').replace(/\$/g, ':')}',element:<R${index}/>}`).join(',');
|
|
19
|
+
return { contents: `import React from 'react';\n${imports}\nexport default [${exports}];`, loader: 'jsx', resolveDir: c.root, watchDirs: [src] };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
c.hostingBuilder = await context({
|
|
23
|
+
entryPoints: { index: join(dirname(createRequire(import.meta.url).resolve('@ryziz/router/package.json')), 'entry.jsx') },
|
|
24
|
+
bundle: true, splitting: true, format: 'esm', outdir: join(c.dist, 'assets'), loader: { '.js': 'jsx' }, jsx: 'automatic', nodePaths: [join(c.root, 'node_modules')],
|
|
25
|
+
plugins: [
|
|
26
|
+
{ name: 'log', setup: (b) => b.onEnd(() => { c.onHostingBuilt?.(); }) },
|
|
27
|
+
{ name: 'vr', setup: (b) => { b.onResolve({ filter: /^virtual:routes$/ }, (a) => ({ path: a.path, namespace: 'vr' })); b.onLoad({ filter: /.*/, namespace: 'vr' }, getRoutes); } },
|
|
28
|
+
],
|
|
29
|
+
alias: {
|
|
30
|
+
'react': join(c.root, 'node_modules/react'),
|
|
31
|
+
'react-dom': join(c.root, 'node_modules/react-dom'),
|
|
32
|
+
'react-router': join(c.root, 'node_modules/react-router'),
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
await c.hostingBuilder.rebuild();
|
|
37
|
+
if (existsSync(c.public)) cpSync(c.public, c.dist, { recursive: true });
|
|
38
|
+
if (!isDev) await c.hostingBuilder.dispose();
|
|
39
|
+
},
|
|
40
|
+
'Functions': async (c) => {
|
|
41
|
+
const getApiRoutes = () => {
|
|
42
|
+
const src = resolve(c.root, 'src'), files = existsSync(src) ? readdirSync(src).filter((f) => /^api\..+\.js$/.test(f)) : [];
|
|
43
|
+
const imports = files.map((file, index) => `import handler${index} from './src/${file}';`).join('\n');
|
|
44
|
+
const routes = files.map((file, index) => {
|
|
45
|
+
const path = ('/' + file.slice(4, -3)).replace(/^\/index$/, '/').replace(/\./g, '/').replace(/\$/g, ':');
|
|
46
|
+
const functionName = file.slice(0, -3).replace(/\./g, '-').replace(/\$/g, '');
|
|
47
|
+
return `{path:'${path}',handler:handler${index},functionName:'${functionName}'}`;
|
|
48
|
+
}).join(',');
|
|
49
|
+
return { contents: `${imports}\nexport default [${routes}];`, loader: 'js', resolveDir: c.root, watchDirs: [src] };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
c.functionsBuilder = await context({
|
|
53
|
+
entryPoints: { index: join(dirname(createRequire(import.meta.url).resolve('@ryziz/functions/package.json')), 'entry.js') },
|
|
54
|
+
bundle: true, format: 'cjs', platform: 'node', outfile: join(c.dist, 'functions', 'index.js'), minify: !isDev,
|
|
55
|
+
external: ['firebase-admin', 'firebase-functions', 'express'],
|
|
56
|
+
plugins: [
|
|
57
|
+
{ name: 'log', setup: (b) => b.onEnd(() => { c.onFunctionsBuilt?.(); }) },
|
|
58
|
+
{ name: 'var', setup: (b) => { b.onResolve({ filter: /^virtual:api-routes$/ }, (a) => ({ path: a.path, namespace: 'var' })); b.onLoad({ filter: /.*/, namespace: 'var' }, getApiRoutes); } },
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
await c.functionsBuilder.rebuild();
|
|
63
|
+
if (!isDev) await c.functionsBuilder.dispose();
|
|
64
|
+
},
|
|
65
|
+
'Watching': async (c) => {
|
|
66
|
+
if (!isDev) c.skip();
|
|
67
|
+
let isFirstHosting = true, isFirstFunctions = true;
|
|
68
|
+
c.onHostingBuilt = () => isFirstHosting ? (isFirstHosting = false) : console.log('Hosting build updated');
|
|
69
|
+
c.onFunctionsBuilt = () => isFirstFunctions ? (isFirstFunctions = false) : console.log('Functions build updated');
|
|
70
|
+
if (existsSync(c.public)) watch(c.public, { recursive: true }, () => { console.log('Public assets updated'); cpSync(c.public, c.dist, { recursive: true }); });
|
|
71
|
+
await c.hostingBuilder.watch();
|
|
72
|
+
await c.functionsBuilder.watch();
|
|
73
|
+
await new Promise(() => { });
|
|
74
|
+
},
|
|
75
|
+
});
|