UNPKG

833 BJavaScriptView Raw
1'use strict';
2
3const path = require('path');
4const fs = require('fs');
5
6const config = {
7 dir: 'apps',
8 prefix: '@app:'
9};
10
11const exists = (...segments) => fs.existsSync(path.join(...segments));
12const isDjangoApp = it => {
13 if (!fs.lstatSync(it).isDirectory()) {
14 return false;
15 }
16 return exists(it, 'static') && exists(it, '__init__.py');
17};
18
19const djangoAppsAlias = (options = {}) => {
20 const { dir, prefix } = Object.assign(config, options);
21 if (!exists(dir)) return {};
22
23 return fs.readdirSync(dir).reduce((acc, name) => {
24 const appFullPath = path.join(dir, name);
25 if (!isDjangoApp(appFullPath)) return acc;
26
27 const appName = `${prefix}${name}`;
28 const appPath = `${appFullPath}/static/${name}/js/`;
29 return Object.assign(acc, { [appName]: appPath });
30 }, {});
31};
32
33module.exports = djangoAppsAlias;