@ryziz/cli 0.0.50 → 0.0.51

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 +12 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ryziz/cli",
3
- "version": "0.0.50",
3
+ "version": "0.0.51",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "ryziz": "./index.js",
package/src/build.js CHANGED
@@ -42,26 +42,26 @@ export default (isDev) => task('Building', {
42
42
  },
43
43
  'Functions': async (c) => {
44
44
  const scanApiRoutes = () => {
45
+ const getSortScore = (path) => {
46
+ const depth = path.split('/').length;
47
+ const hasParam = path.includes(':') ? 0 : 1;
48
+ const isCatchAll = path.endsWith('/') ? 0 : 1;
49
+ return (depth << 16) | (hasParam << 8) | isCatchAll;
50
+ };
51
+
45
52
  const files = existsSync(c.src) ? readdirSync(c.src).filter((f) => /^api\..+\.js$/.test(f)) : [];
46
53
  return files.map((file) => {
47
54
  const content = readFileSync(join(c.src, file), 'utf8');
48
- const hasConfig = /export\s+(const|let|var)\s+config\s*=/.test(content);
55
+ const path = ('/api/' + file.slice(4, -3)).replace(/^\/api\/index$/, '/api/').replace(/\./g, '/').replace(/\$/g, ':');
49
56
  return {
50
57
  file,
51
- path: ('/api/' + file.slice(4, -3)).replace(/^\/api\/index$/, '/api/').replace(/\./g, '/').replace(/\$/g, ':'),
58
+ path,
52
59
  functionName: file.slice(0, -3).replace(/\./g, '-').replace(/\$/g, ''),
53
- hasConfig,
60
+ hasConfig: /export\s+(const|let|var)\s+config\s*=/.test(content),
61
+ _score: getSortScore(path),
54
62
  };
55
63
  }).sort((a, b) => {
56
- const aDepth = a.path.split('/').length;
57
- const bDepth = b.path.split('/').length;
58
- const aHasParam = a.path.includes(':');
59
- const bHasParam = b.path.includes(':');
60
- const aIsCatchAll = a.path.endsWith('/');
61
- const bIsCatchAll = b.path.endsWith('/');
62
- if (aDepth !== bDepth) return bDepth - aDepth;
63
- if (aHasParam !== bHasParam) return aHasParam ? 1 : -1;
64
- if (aIsCatchAll !== bIsCatchAll) return aIsCatchAll ? 1 : -1;
64
+ if (a._score !== b._score) return b._score - a._score;
65
65
  return a.path.localeCompare(b.path);
66
66
  });
67
67
  };