-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractal.js
92 lines (80 loc) · 2.35 KB
/
fractal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
const path = require('path');
const fractal = module.exports = require('@frctl/fractal').create();
const twigAdapter = require('frctl-twig');
const mandelbrot = require('@frctl/mandelbrot');
const defaultHandlePath = __dirname + '/site/fractal-handles.php';
/**
* Global Settings
*/
fractal.set('project.title', 'Living Component Library');
/**
* Webserver Settings
*/
const myCustomisedTheme = mandelbrot({
skin: 'black',
format: 'yaml',
nav: ["docs", "components", "assets"]
});
myCustomisedTheme.addLoadPath(__dirname + '/fractal/theme-overrides');
fractal.web.theme(myCustomisedTheme);
fractal.web.set('static.path', __dirname + '/site/templates');
fractal.web.set('builder.dest', __dirname + '/docs');
/**
* Components Settings
*/
fractal.components.set('path', __dirname + '/site/templates/views');
fractal.components.set('default.preview', '@preview');
fractal.components.engine(twigAdapter);
fractal.components.set('ext', '.twig');
/**
* Documentation Settings
*/
fractal.docs.set('path', __dirname + '/fractal/docs');
/**
* Assets Settings
*/
fractal.assets.set('path', __dirname);
/**
* Add build-handles command to fractal cli
*/
function buildPhpArray(args, done){
const app = fractal;
const fs = require('fs');
const filename = args && args.options.file
? args.options.file
: defaultHandlePath;
const location = path.dirname(filename);
let file = "<?php\n\nreturn array(\n";
app.components.flatten().toArray().concat(app.components.flattenDeep().toArray()).forEach(function(component){
let relativePath = path.relative(location, component.viewPath)
file += `\t"@${component.handle}"\n\t\t=> __DIR__ . "/${relativePath}",\n`;
});
file += `);\n`;
fs.writeFile(filename, file, function(err) {
if(err) return this.log(err);
done();
});
};
fractal.cli.command(
'build-handles',
buildPhpArray,
{
description: 'Lists components in the project',
options: [ ['-f, --file <path>', 'The path to save the config file to.'] ]
}
);
/**
* Bind build-handles command to some events
*/
function automatedExport() {
buildPhpArray(null, function(){});
}
// Export on component update while running the server with --sync
fractal.components.on('updated', function(source, eventData){
automatedExport();
});
// Export on static build (might not be necessary)
fractal.web.builder().on('end', function(){
automatedExport();
});