1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const config = require('config');
|
6 |
|
7 | const viewExcludes = {
|
8 | directories: [
|
9 | path.basename(config.get('nitro.viewDataDirectory')),
|
10 | path.basename(config.get('nitro.viewPartialsDirectory')),
|
11 | path.basename(config.get('nitro.viewLayoutsDirectory')),
|
12 | path.basename(config.get('nitro.placeholdersDirectory')),
|
13 | '.svn',
|
14 | ],
|
15 | files: [
|
16 | '.DS_Store',
|
17 | 'Thumbs.db',
|
18 | 'Desktop.ini',
|
19 | ],
|
20 | };
|
21 |
|
22 | const replaceAt = function replaceAt(string, index, character) {
|
23 | return string.substr(0, index) + character + string.substr(index + character.length);
|
24 | };
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function getViews(dir) {
|
38 | let results = [];
|
39 | const files = fs.readdirSync(dir);
|
40 |
|
41 | files.forEach((file) => {
|
42 | const filePath = `${dir}/${file}`;
|
43 | const stat = fs.statSync(filePath);
|
44 |
|
45 | if (file.substring(0, 1) === '.') {
|
46 |
|
47 | } else if (stat && stat.isDirectory() && viewExcludes.directories.indexOf(file) === -1) {
|
48 | results = results.concat(getViews(filePath));
|
49 | } else if (stat && stat.isFile() && path.extname(file) === `.${config.get('nitro.viewFileExtension')}` && viewExcludes.files.indexOf(file) === -1) {
|
50 | const relativePath = path.relative(config.get('nitro.basePath') + config.get('nitro.viewDirectory'), filePath);
|
51 | const ext = path.extname(filePath);
|
52 | const extReg = new RegExp(`${ext}$`);
|
53 | const name = relativePath.replace(extReg, '').replace(/\//g, ' ').replace(/\\/g, ' ').replace(/\b\w/g, (w) => {
|
54 | return w.toUpperCase();
|
55 | });
|
56 | const url = relativePath.replace(extReg, '').replace(/\//g, '-').replace(/\\/g, '-');
|
57 |
|
58 | results.push({ name, url });
|
59 | }
|
60 | });
|
61 |
|
62 | return results;
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function getViewCombinations(action) {
|
71 | const pathes = [action];
|
72 | const positions = [];
|
73 | let i;
|
74 | let j;
|
75 |
|
76 | for (i = 0; i < action.length; i++) {
|
77 | if (action[i] === '-') {
|
78 | positions.push(i);
|
79 | }
|
80 | }
|
81 |
|
82 | const len = positions.length;
|
83 | const combinations = [];
|
84 |
|
85 | for (i = 1; i < (1 << len); i++) {
|
86 | const c = [];
|
87 | for (j = 0; j < len; j++) {
|
88 | if (i & (1 << j)) {
|
89 | c.push(positions[j]);
|
90 | }
|
91 | }
|
92 | combinations.push(c);
|
93 | }
|
94 |
|
95 | combinations.forEach((combination) => {
|
96 | let combinationPath = action;
|
97 | combination.forEach((pos) => {
|
98 | combinationPath = replaceAt(combinationPath, pos, '/');
|
99 | });
|
100 | pathes.push(combinationPath);
|
101 | });
|
102 | return pathes;
|
103 | }
|
104 |
|
105 | module.exports = {
|
106 | getViews,
|
107 | getViewCombinations,
|
108 | };
|