|
|
@ -38,8 +38,9 @@ buildScriptFiles() |
|
|
* Init server |
|
|
* Init server |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
let port = 3000; // This variable is used in *.hbs and it will be updated once BrowserSync is ready.
|
|
|
let port = 3000; // This variable is used in `*.hbs` and it will be updated once BrowserSync is ready.
|
|
|
let previewFrameUrl = `http://localhost:${port}`; // This variable is used in *.hbs and it will be updated once BrowserSync is ready.
|
|
|
let previewFrameUrl = `http://localhost:${port}`; // This variable is used in *.hbs and it will be updated once BrowserSync is ready.
|
|
|
|
|
|
const dataFiles = prepareListOfDataFiles(await fs.readdir('./data')); |
|
|
const app = express(); |
|
|
const app = express(); |
|
|
|
|
|
|
|
|
const hbs = create({ |
|
|
const hbs = create({ |
|
|
@ -61,7 +62,9 @@ app.engine('.hbs', hbs.engine); |
|
|
app.set('view engine', '.hbs'); |
|
|
app.set('view engine', '.hbs'); |
|
|
app.set('views', projectDir + 'layouts'); |
|
|
app.set('views', projectDir + 'layouts'); |
|
|
|
|
|
|
|
|
const dataFiles = prepareListOfDataFiles(await fs.readdir('./data')); |
|
|
//
|
|
|
|
|
|
// Routes
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
app.get('/', async (req, res) => { |
|
|
app.get('/', async (req, res) => { |
|
|
let jsonFileName = req.query.data ? req.query.data : 'default'; |
|
|
let jsonFileName = req.query.data ? req.query.data : 'default'; |
|
|
@ -149,24 +152,6 @@ app.get('/publish', async (req, res) => { |
|
|
await fs.unlink('./dist.zip'); |
|
|
await fs.unlink('./dist.zip'); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
function getListOfDesignPreviewFiles(jsonDataFileName, previewFiles) { |
|
|
|
|
|
return previewFiles |
|
|
|
|
|
.filter(fileName => { |
|
|
|
|
|
return fileName.startsWith(jsonDataFileName + '.'); |
|
|
|
|
|
}) |
|
|
|
|
|
.map(fileName => { |
|
|
|
|
|
const fileData = fileName.split('.'); |
|
|
|
|
|
const fileFormat = fileData.pop(); |
|
|
|
|
|
const previewSize = fileData.pop(); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
dataSource: jsonDataFileName, |
|
|
|
|
|
widthDimension: Number.parseInt(previewSize, 10), |
|
|
|
|
|
url: `/preview/${fileName}`, |
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
app.get('/data', async (req, res) => { |
|
|
app.get('/data', async (req, res) => { |
|
|
let jsonDataFileName = req.query.name ? req.query.name : 'default'; |
|
|
let jsonDataFileName = req.query.name ? req.query.name : 'default'; |
|
|
const data = await getBlockConfigs(jsonDataFileName); |
|
|
const data = await getBlockConfigs(jsonDataFileName); |
|
|
@ -181,6 +166,10 @@ app.get('/data', async (req, res) => { |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Errors handler
|
|
|
|
|
|
app.use(handleSyntaxErrors); |
|
|
|
|
|
|
|
|
|
|
|
// Static Files
|
|
|
app.use(express.static('src')); |
|
|
app.use(express.static('src')); |
|
|
app.use(express.static('design')); |
|
|
app.use(express.static('design')); |
|
|
app.use(express.static(projectDir + 'layouts')); |
|
|
app.use(express.static(projectDir + 'layouts')); |
|
|
@ -191,9 +180,28 @@ port = bsOptions.port; |
|
|
previewFrameUrl = bsOptions.previewFrameUrl; |
|
|
previewFrameUrl = bsOptions.previewFrameUrl; |
|
|
await open(bsOptions.devToolUrl); |
|
|
await open(bsOptions.devToolUrl); |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Functions |
|
|
//
|
|
|
*/ |
|
|
// Functions
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
function getListOfDesignPreviewFiles(jsonDataFileName, previewFiles) { |
|
|
|
|
|
return previewFiles |
|
|
|
|
|
.filter(fileName => { |
|
|
|
|
|
return fileName.startsWith(jsonDataFileName + '.'); |
|
|
|
|
|
}) |
|
|
|
|
|
.map(fileName => { |
|
|
|
|
|
const fileData = fileName.split('.'); |
|
|
|
|
|
const fileFormat = fileData.pop(); |
|
|
|
|
|
const previewSize = fileData.pop(); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
dataSource: jsonDataFileName, |
|
|
|
|
|
widthDimension: Number.parseInt(previewSize, 10), |
|
|
|
|
|
url: `/preview/${fileName}`, |
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function startBrowserSync() { |
|
|
function startBrowserSync() { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
@ -371,3 +379,17 @@ async function zipProject() { |
|
|
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
|
|
|
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
|
|
|
await archive.finalize(); |
|
|
await archive.finalize(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function handleSyntaxErrors(err, req, res, next) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
return res.render('error', { |
|
|
|
|
|
helpers: { |
|
|
|
|
|
include_partial: (path) => projectDir + path, |
|
|
|
|
|
}, |
|
|
|
|
|
err |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
next(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|