#! /usr/bin/env node
var Commander =require('commander')
var Shared =require('placeholder-shared')
var Store = require('../apis/app.js')
const path = require('path');

const open = require('open');

const express = require('express');
const app = express();

Commander.program.command('edit').description('run structure editor').action(async (cmd, options)=> {
    process.env.STRUCTURE_MODE='commandLine'
    //start backend
    const apis = Store.getAPIs()
    app.use('/api',apis)

    //start editor
    const currentBuildPath = "../build"
    console.log("opening editor in web browser...");
    const staticEditorFiles = require.main.path + '/../build'

    app.use(express.static(staticEditorFiles));
    app.get('/*', function (req, res) {
        const indexFile = path.resolve(staticEditorFiles, 'index.html')
	       console.log("serve index")
        res.sendFile(indexFile);
      });
    app.listen(3000, () => {})

    await open("http://localhost:3000/app/editor")
    return;
});
Commander.program.parse(process.argv);
