Skip to content

Commit

Permalink
feat: Read schema subfolders (#230). Thanks to @lksilva!
Browse files Browse the repository at this point in the history
* recursive read all files into folder, including files in subfolder

* add recursive read files into path

* resolve path

* fix lint problems
  • Loading branch information
lksilva authored and paveltiunov committed Oct 14, 2019
1 parent 1886d13 commit aa736b1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
62 changes: 41 additions & 21 deletions packages/cubejs-server-core/core/FileRepository.js
@@ -1,6 +1,6 @@
const path = require('path');
const fs = require('fs-extra');
const R = require('ramda');
const path = require("path");
const fs = require("fs-extra");
const R = require("ramda");

class FileRepository {
constructor(repositoryPath) {
Expand All @@ -11,13 +11,26 @@ class FileRepository {
return path.join(process.cwd(), this.repositoryPath);
}

async getFiles(dir, fileList = []) {
const files = await fs.readdir(path.join(this.localPath(), dir));
// eslint-disable-next-line no-restricted-syntax
for (const file of files) {
const stat = await fs.stat(path.join(this.localPath(), dir, file));
if (stat.isDirectory()) {
fileList = await this.getFiles(path.join(dir, file), fileList);
} else fileList.push(path.join(dir, file));
}
return fileList;
}

async dataSchemaFiles(includeDependencies) {
const self = this;
const files = await fs.readdir(this.localPath());
const files = await self.getFiles("");
let result = await Promise.all(
files.filter((file) => R.endsWith('.js', file))
.map(async (file) => {
const content = await fs.readFile(path.join(self.localPath(), file), "utf-8");
files
.filter(file => R.endsWith(".js", file))
.map(async file => {
const content = await fs.readFile(file, "utf-8");
return { fileName: file, content };
})
);
Expand All @@ -28,33 +41,40 @@ class FileRepository {
}

async readModules() {
const packageJson = JSON.parse(await fs.readFile('package.json', 'utf-8'));
const files = await Promise.all(Object.keys(packageJson.dependencies).map(async module => {
if (R.endsWith('-schema', module)) {
return this.readModuleFiles(path.join('node_modules', module));
}
return [];
}));
const packageJson = JSON.parse(await fs.readFile("package.json", "utf-8"));
const files = await Promise.all(
Object.keys(packageJson.dependencies).map(async module => {
if (R.endsWith("-schema", module)) {
return this.readModuleFiles(path.join("node_modules", module));
}
return [];
})
);
return files.reduce((a, b) => a.concat(b));
}

async readModuleFiles(modulePath) {
const files = await fs.readdir(modulePath);
return (await Promise.all(files
.map(async file => {
return (await Promise.all(
files.map(async file => {
const fileName = path.join(modulePath, file);
const stats = await fs.lstat(fileName);
if (stats.isDirectory()) {
return this.readModuleFiles(fileName);
} else if (R.endsWith('.js', file)) {
} else if (R.endsWith(".js", file)) {
const content = await fs.readFile(fileName, "utf-8");
return [{
fileName, content, readOnly: true
}];
return [
{
fileName,
content,
readOnly: true
}
];
} else {
return [];
}
}))).reduce((a, b) => a.concat(b), []);
})
)).reduce((a, b) => a.concat(b), []);
}
}

Expand Down
16 changes: 8 additions & 8 deletions packages/cubejs-server-core/yarn.lock
Expand Up @@ -18,10 +18,10 @@
esutils "^2.0.2"
js-tokens "^4.0.0"

"@cubejs-backend/api-gateway@^0.10.34":
version "0.10.34"
resolved "https://registry.yarnpkg.com/@cubejs-backend/api-gateway/-/api-gateway-0.10.34.tgz#ebec7fbe6412ebfa16e15d815c6332777f05c1f7"
integrity sha512-XBvBCw54AR0cEimOc10/RVgPMHYKBpmoug4vtEy1vfnZ0B0mBXlI+R+p7w0ZX2kEAqx0s/J8k/M2+Lsg+UBKRg==
"@cubejs-backend/api-gateway@^0.10.62":
version "0.10.62"
resolved "https://registry.yarnpkg.com/@cubejs-backend/api-gateway/-/api-gateway-0.10.62.tgz#83ad2894f7442e85d4fb00ee173070e53ff60a86"
integrity sha512-825CLVww9aOajKAwAE0S8vg4YWykMNZktA/pb9lASLNMmykm7om+gnVcphyIPc2c5W5iADbnH+X1uoqYKhkOQg==
dependencies:
"@hapi/joi" "^14.0.6"
chrono-node "^1.3.11"
Expand All @@ -37,10 +37,10 @@
ramda "^0.24.1"
redis "^2.8.0"

"@cubejs-backend/schema-compiler@^0.10.61":
version "0.10.61"
resolved "https://registry.yarnpkg.com/@cubejs-backend/schema-compiler/-/schema-compiler-0.10.61.tgz#77dd5a52d9c5b429795cb5cbbf5684771014b6dc"
integrity sha512-ICvKRvQdZ+g7q1YONHLc9GxdPjrI69s/n4FUkw26+Et+fgq1JG+wt6LnIFOA++ozf2SPdyPXUb+LeTDUfBvQfA==
"@cubejs-backend/schema-compiler@^0.10.62":
version "0.10.62"
resolved "https://registry.yarnpkg.com/@cubejs-backend/schema-compiler/-/schema-compiler-0.10.62.tgz#942beb89a02c7a22bc85de50d8916928193870d5"
integrity sha512-f/2rR6ZEs0y0DrprFcSAzb58bFk69o0QlRnGS9WWFtsmQ3NuboGx4IW9SRTSdba2RPijiOVXfnmduBIDqw8e0w==
dependencies:
"@hapi/joi" "^14.3.1"
babel-generator "^6.25.0"
Expand Down

0 comments on commit aa736b1

Please sign in to comment.