1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const fs = require('fs');
|
5 |
|
6 | const config = {
|
7 | dir: 'apps',
|
8 | prefix: '@app:'
|
9 | };
|
10 |
|
11 | const exists = (...segments) => fs.existsSync(path.join(...segments));
|
12 | const isDjangoApp = it => {
|
13 | if (!fs.lstatSync(it).isDirectory()) {
|
14 | return false;
|
15 | }
|
16 | return exists(it, 'static') && exists(it, '__init__.py');
|
17 | };
|
18 |
|
19 | const 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 |
|
33 | module.exports = djangoAppsAlias;
|