');
+ // Find the docker-compose container for materia-web
+ // 1. lists all containers
+ // 2. filter for materia-web image and named xxxx_phpfpm_1 name
+ // 3. pick the first line
+ // 4. pick the container name
+ let targetImage = execSync('docker ps -a --format "{{.Image}} {{.Names}}" | grep -e ".*materia:.* docker[-_]app[-_].*" | head -n 1 | cut -d" " -f2');
+ if(!targetImage){
+ console.log(`Couldn't find docker container`)
+ throw "MWDK Couldn't find a docker container."
+ }
+ targetImage = targetImage.toString().trim();
+ console.log(`Using Docker image '${targetImage}' to install widgets`)
+ res.write(`> Using Docker image '${targetImage}' to install widgets `);
+
+ // get the image information
+ let containerInfo = execSync(`docker inspect ${targetImage}`);
+ containerInfo = JSON.parse(containerInfo.toString());
+
+ // Find mounted volume that will tell us where materia is on the host system
+ let found = containerInfo[0].Mounts.filter(m => m.Destination === '/var/www/html')
+ if(!found){
+ console.error('MWDK Couldnt find the Materia mount on the host system')
+ res.write(`
Cant Find Materia
`);
+ throw `MWDK Couldn't find the Materia mount on the host system'`
+ }
+ let materiaPath = found[0].Source.replace(/^\/host_mnt/, '') // depending on your Docker version, host_mnt may be prepended to the directory path
+ let serverWidgetPath = `${materiaPath}/fuel/app/tmp/widget_packages`
+
+ // make sure the dir exists
+ console.log(`Checking if ${materiaPath}/fuel/app/tmp/widget_packages exists`)
+ if(!fs.existsSync(serverWidgetPath)){
+ console.log(`Making directory ${materiaPath}/fuel/app/tmp/widget_packages`)
+ fs.mkdirSync(serverWidgetPath);
+ }
- // Display index page
- app.get('/', (req, res) => {
- res.locals = Object.assign(res.locals, {template: 'index', title: getWidgetTitle()})
- res.render(res.locals.template)
- });
+ // Build!
+ console.log('Building widget')
+ res.write(`> Building widget `);
+ let { widgetPath, widgetData } = buildWidget()
- // ============= MWDK ROUTES =======================
+ // create a file name with a timestamp in it
+ console.log(`Creating ${widgetData.clean_name}-${new Date().getTime()}.wigt`)
+ const filename = `${widgetData.clean_name}-${new Date().getTime()}.wigt`;
- app.get('/mwdk/my-widgets', (req, res) => {
- res.redirect('/')
- });
+ // get the widget I just built
+ let widgetPacket = fs.readFileSync(widgetPath)
- app.get('/mwdk/icons', (req, res) => {
- const sizes = [
- {size: 394, x2: 394*2, canGenerateLarge: false, canGenerateSmall: true},
- {size: 275, x2: 275*2, canGenerateLarge: true, canGenerateSmall: true},
- {size: 92, x2: 92*2, canGenerateLarge: true, canGenerateSmall: true},
- {size: 60, x2: 60*2, canGenerateLarge: true, canGenerateSmall: true}
- ];
- res.locals = Object.assign(res.locals, { template: 'icons', sizes: sizes, timestamp: new Date().getTime()})
- res.render(res.locals.template)
- });
+ // write the built widget to that path
+ let target = path.join(serverWidgetPath, filename)
+ console.log(`> Writing to ${target} `)
+ res.write(`> Writing to ${target} `);
+ fs.writeFileSync(target, widgetPacket);
+ // run the install command
+ console.log(`Running > cd ${materiaPath}/docker/ && ./run_widgets_install.sh ${filename}`)
+ res.write(`Running > cd ${materiaPath}/docker/ && ./run_widgets_install.sh ${filename}`);
+ try {
+ let run = require('child_process').spawn(`./run_widgets_install.sh`, [`${filename}`], {cwd: `${materiaPath}/docker/`})
+
+ run.stdout.on('data', function(data) {
+ console.log('stdout: ' + data.toString());
+ res.write(data.toString());
+ })
+ run.stderr.on('data', function(data) {
+ console.error('stderr: ' + data.toString());
+ res.write(data.toString());
+ })
+ run.on('close', function(code) {
+ if (code == 0) {
+ res.write("
SUCCESS!");
+ } else {
+ res.write("
Something failed!");
+ }
+ res.write('child process exited with code ' + code.toString());
+ console.log(`ps process exited with code ${code}`);
- app.get('/mwdk/auto-icon/:size/:double?', (req, res) => {
- let regularSizes = [60, 92, 275, 394]
- let doubleSizes = [60, 92, 275]
+ res.write(' ');
+ res.end()
+ })
+ }
+ catch (err) {
+ throw err;
+ res.write("