107 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-01-17 13:40:59 +01:00
/**
* Route Generator
*/
2018-03-14 22:20:39 +01:00
/* eslint-disable no-console */
2017-01-17 13:40:59 +01:00
const fs = require('fs');
2017-08-14 18:25:48 +02:00
const path = require('path');
const routesFilePath = path.resolve(process.cwd(), 'admin', 'src', 'routes.json');
2017-01-17 13:40:59 +01:00
const componentExists = require('../utils/componentExists');
2017-08-14 18:25:48 +02:00
// Generate the update file content
2017-08-16 19:14:39 +02:00
const generateUpdatedFileContent = (data, existingContent) => {
const fileContent = existingContent || {};
2017-01-17 13:40:59 +01:00
2017-08-14 18:25:48 +02:00
// Add new route
const updatedFileContent = fileContent;
updatedFileContent[data.path] = {
container: data.container,
};
2017-01-17 13:40:59 +01:00
2017-08-14 18:25:48 +02:00
return updatedFileContent;
};
2017-01-17 13:40:59 +01:00
module.exports = {
description: 'Add a route',
prompts: [{
type: 'input',
2017-08-14 18:25:48 +02:00
name: 'container',
message: 'Which container should the route show?',
2017-01-17 13:40:59 +01:00
validate: (value) => {
if ((/.+/).test(value)) {
return componentExists(value) ? true : `"${value}" doesn't exist.`;
}
return 'The path is required';
},
}, {
type: 'input',
name: 'path',
message: 'Enter the path of the route.',
default: '/about',
validate: (value) => {
if ((/.+/).test(value)) {
return true;
}
return 'path is required';
},
}],
actions: (data) => {
2017-08-14 18:25:48 +02:00
const replaceFile = () => {
// Check if the file is existing or not
2017-08-16 19:14:39 +02:00
let routesFilesStats;
try {
routesFilesStats = fs.statSync(routesFilePath);
} catch (error) {
routesFilesStats = false;
}
const routesFilesExists = routesFilesStats && routesFilesStats.isFile();
// Read the file content
let existingContent = {};
if (routesFilesExists) {
try {
existingContent = fs.readFileSync(routesFilePath, 'utf8');
} catch (error) {
existingContent = false;
console.log('Unable to read existing `admin/src/routes.json` file content.');
}
try {
existingContent = JSON.parse(existingContent);
} catch (error) {
existingContent = false;
console.log('Unable to parse existing `admin/src/routes.json` file content.');
}
}
2017-08-14 18:25:48 +02:00
// Generate updated content
2017-08-16 19:14:39 +02:00
const updatedContent = generateUpdatedFileContent(data, existingContent || {});
2017-08-14 18:25:48 +02:00
// Delete the file if existing
if (routesFilesExists) {
2017-08-16 19:14:39 +02:00
try {
fs.unlinkSync(routesFilePath);
} catch (error) {
console.log('Unable to remove `admin/src/routes.json` file.');
throw error;
}
2017-08-14 18:25:48 +02:00
}
// Write the new file
2017-08-16 19:14:39 +02:00
try {
fs.writeFileSync(routesFilePath, JSON.stringify(updatedContent, null, 2), 'utf8');
console.log('File `admin/src/routes.json` successfully written.');
} catch (error) {
console.log('Unable to write `admin/src/routes.json` file.');
throw error;
}
2017-08-14 18:25:48 +02:00
};
2017-01-17 13:40:59 +01:00
2017-08-14 18:25:48 +02:00
return [replaceFile];
2017-01-17 13:40:59 +01:00
},
};