2017-01-17 13:40:59 +01:00
|
|
|
/**
|
|
|
|
* Route Generator
|
|
|
|
*/
|
|
|
|
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-05-11 14:17:21 +02:00
|
|
|
|
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
|
|
|
|
const generateUpdatedFileContent = (data) => {
|
|
|
|
// Check if the file is existing or not
|
|
|
|
const routesFilesExists = fs.existsSync(routesFilePath);
|
|
|
|
|
|
|
|
// Read file
|
|
|
|
const fileContent = routesFilesExists ? JSON.parse(fs.readFileSync(routesFilePath, 'utf8')) : {};
|
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
|
|
|
|
const routesFilesExists = fs.existsSync(routesFilePath);
|
|
|
|
|
|
|
|
// Generate updated content
|
|
|
|
const updatedContent = generateUpdatedFileContent(data);
|
|
|
|
|
|
|
|
// Delete the file if existing
|
|
|
|
if (routesFilesExists) {
|
|
|
|
fs.unlinkSync(routesFilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the new file
|
|
|
|
fs.writeFileSync(routesFilePath, JSON.stringify(updatedContent, null, 2), 'utf8');
|
|
|
|
};
|
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
|
|
|
},
|
|
|
|
};
|