Browse Source

All development block files moved to `blocks` directory.

pull/1/head
Roman Axelrod 3 years ago
parent
commit
c48420249b
  1. 5
      .gitignore
  2. 2
      package.json
  3. 51
      server.js

5
.gitignore

@ -10,9 +10,4 @@ deploy-*.sh
# Custom
blocks
config
data
src
exports
block.json
design

2
package.json

@ -8,7 +8,7 @@
},
"scripts": {
"start": "component-dev",
"dev": "NODE_ENV=development node server.js",
"dev": "NODE_ENV=development NODE_CONFIG_DIR=blocks/team/config BLOCK_NAME=team node server.js",
"build": "rollup --config rollup.config.js",
"build-platform": "NODE_ENV=development node ./build.js",
"build-platform-cli": "component-build",

51
server.js

@ -1,5 +1,6 @@
#!/usr/bin/env node
import path from 'path';
import fetch from "node-fetch";
import express from 'express';
import {create} from 'express-handlebars';
@ -24,10 +25,12 @@ import archiver from 'archiver';
* Constants
*/
const isDev = process.env.NODE_ENV === 'development' || (config.has('isDev') && config.get('isDev')); // Check README file in case you get "missing files" error.
const isDev = process.env.NODE_ENV === 'development'; // Check README file in case you get "missing files" error.
const blocksRegistry = isDev ? 'http://localhost:3020' : 'https://axe-web-blocks-registry.captain.devdevdev.life';
const modulePath = isDev ? '' : 'node_modules/block-dev-tool/';
const projectDir = modulePath;
const modulesPath = isDev ? '' : 'node_modules/block-dev-tool';
const developmentBlockName = process.env.BLOCK_NAME;
const projectPath = isDev ? path.join('blocks', developmentBlockName) : '';
const sass = gulpSass(dartSass);
@ -39,8 +42,8 @@ buildScriptFiles()
*/
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.
const dataFiles = prepareListOfDataFiles(await fs.readdir('./data'));
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(path.join(projectPath, 'data')));
const app = express();
const hbs = create({
@ -60,7 +63,7 @@ const hbs = create({
app.engine('.hbs', hbs.engine);
app.set('view engine', '.hbs');
app.set('views', projectDir + 'layouts');
app.set('views', path.join(modulesPath, 'layouts'));
//
// Routes
@ -78,7 +81,7 @@ app.get('/', async (req, res) => {
data.helpers = {
port,
include_partial: (path) => projectDir + path,
include_partial: (filesPath) => path.join(modulesPath, filesPath),
baseView,
previewFrameUrl: `${previewFrameUrl}/${baseViewUrl}`,
}
@ -93,11 +96,11 @@ app.get('/view/:baseView', async (req, res) => {
return res.send(data.errorMessage);
}
const blockName = config.has('blockName') ? config.get('blockName') : 'development';
const blockName = config.has('blockName') ? config.get('blockName') : developmentBlockName;
data.helpers = {
include_partial: (path) => projectDir + path,
include_block_template: (path) => `src/${blockName}.template`,
include_partial: (filesPath) => path.join(modulesPath, filesPath),
include_block_template: () => path.join(projectPath, 'src', `${blockName}.template`),
section_class: `${blockName}--${jsonFileName}`,
base_url: '/'
}
@ -108,7 +111,7 @@ app.get('/view/:baseView', async (req, res) => {
});
app.get('/publish', async (req, res) => {
const data = await readJSONFile('./block.json');
const data = await readJSONFile(path.join(projectPath, `block.json`));
let responseData;
try {
@ -131,7 +134,7 @@ app.get('/publish', async (req, res) => {
if (responseData.uploadUrl) {
await zipProject();
const body = await fs.readFile('./dist.zip');
const body = await fs.readFile(path.join(projectPath, 'dist.zip'));
const response = await fetch(`${responseData.uploadUrl}`, {
method: 'PUT',
body,
@ -141,7 +144,7 @@ app.get('/publish', async (req, res) => {
if (response.status !== 200) {
res.json({success: false, message: "Can't upload the archive, permissions error."});
// TODO: Need to update the registry server.
await fs.unlink('./dist.zip');
await fs.unlink(path.join(projectPath, 'dist.zip'));
return;
}
}
@ -149,15 +152,15 @@ app.get('/publish', async (req, res) => {
res.json({success: true});
await fs.unlink('./dist.zip');
await fs.unlink(path.join(projectPath, 'dist.zip'));
});
app.get('/data', async (req, res) => {
let jsonDataFileName = req.query.name ? req.query.name : 'default';
const data = await getBlockConfigs(jsonDataFileName);
const dataFiles = prepareListOfDataFiles(await fs.readdir('./data'));
const designPreviewFiles = getListOfDesignPreviewFiles(jsonDataFileName, await fs.readdir('./design/preview'));
const dataFiles = prepareListOfDataFiles(await fs.readdir(path.join(projectPath, 'data')));
const designPreviewFiles = getListOfDesignPreviewFiles(jsonDataFileName, await fs.readdir(path.join(projectPath, 'design', 'preview')));
return res.json({
dataOptions: dataFiles,
@ -170,9 +173,9 @@ app.get('/data', async (req, res) => {
app.use(handleSyntaxErrors);
// Static Files
app.use(express.static('src'));
app.use(express.static('design'));
app.use(express.static(projectDir + 'layouts'));
app.use(express.static(path.join(projectPath, 'src')));
app.use(express.static(path.join(projectPath, 'design')));
app.use(express.static(path.join(modulesPath, 'layouts')));
// BrowserSync
const bsOptions = await startBrowserSync();
@ -307,7 +310,7 @@ async function readJSONFile(jsonFile) {
}
async function getBlockConfigs(jsonFileName = 'default', {includeConfigs} = {}) {
let data = await readJSONFile(`./data/${jsonFileName}.json`);
let data = await readJSONFile(path.join(projectPath, 'data', `${jsonFileName}.json`));
if (data.error) {
return data;
}
@ -316,7 +319,7 @@ async function getBlockConfigs(jsonFileName = 'default', {includeConfigs} = {})
Object.assign(data, {
config: Object.assign(JSON.parse(JSON.stringify(config)), // The entire config object.
{
projectDir, activeDataFile: jsonFileName, dataFiles: dataFiles.map((name) => {
projectDir: modulesPath, activeDataFile: jsonFileName, dataFiles: dataFiles.map((name) => {
return {
name, active: jsonFileName === name,
};
@ -372,8 +375,8 @@ async function zipProject() {
// pipe archive data to the file
archive.pipe(output);
// append files from a sub-directory, putting its contents at the root of archive
archive.directory('src/', false);
// append files from a subdirectory, putting its contents at the root of archive
archive.directory(path.join(projectPath, 'src', '/'), false);
// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
@ -384,7 +387,7 @@ function handleSyntaxErrors(err, req, res, next) {
if (err) {
return res.render('error', {
helpers: {
include_partial: (path) => projectDir + path,
include_partial: (filesPath) => path.join(modulesPath, filesPath),
},
err
});

Loading…
Cancel
Save