UNPKG

cloudux-starter-kit

Version:

Starter kit for UX developers in MediaCentral - NPM package

72 lines (69 loc) 2.91 kB
const fs = require('fs-extra'); const path = require('path'); const constants = require('./constants'); const config = JSON.parse(fs.readFileSync(constants.templateCoreProjectAct, 'utf8')); const createDirectoryContents = (templatePath, newProjectPath, path) => { const filesToCreate = fs.readdirSync(templatePath); filesToCreate.forEach(file => { const origFilePath = `${templatePath}/${file}`; let writePath = `${path}/${newProjectPath}/${file}`; // get stats about the current file const stats = fs.statSync(origFilePath); if (stats.isFile()) { const contents = fs.readFileSync(origFilePath, 'utf8'); // Rename if (file === '.npmignore') { file = '.gitignore'; writePath = `${path}/${newProjectPath}/${file}`; } fs.writeFileSync(writePath, contents, 'utf8'); } else if (stats.isDirectory()) { if (!fs.existsSync(writePath)) { fs.mkdirSync(writePath); } // recursive call createDirectoryContents(`${templatePath}/${file}`, `${newProjectPath}/${file}`, path); } }); }; const copyProjectTemplate = (projectName, projectChoice, path) => { const corePath = `${constants.toolkitPath}/templates/core`; const pluginPath = `${constants.toolkitPath}/templates/${projectChoice}`; try { fs.ensureDirSync(`${path}/${projectName}`); } catch (err) { throw new Error(err.message.split(":")[1].split(",")[0].substring(1)); } if (projectChoice === 'react') { // Create core directory with vanilla JS createDirectoryContents(corePath, projectName, path); // Override files and directories with plugin ones createDirectoryContents(pluginPath, projectName, path); // Remove vanilla JS src/app/index because react creates index.jsx fs.unlink(`${path}/${projectName}/src/app/index.js`, () => {}); } else { createDirectoryContents(corePath, projectName, path); } }; const writeUserConfig = (projectName, projectDescription, projectHostIp, projectHostPort, configPath, srcPackagePath) => { let srcPackage = require(srcPackagePath); srcPackage.identity.appName = projectName; srcPackage.avid.features.provides.apps[0].name = projectName; srcPackage.avid.features.provides.apps[0].config.id = projectName; srcPackage.avid.features.provides.apps[0].config.title = { en: projectName }; srcPackage.avid.features.provides.apps[0].config.preload = false; srcPackage.avid.features.provides.apps[0].config.allowedContext = 'none'; config.identity.description = projectDescription; config.connection.hostIp = projectHostIp; config.connection.hostPort = projectHostPort; config.connection.proxyPort = '443'; fs.writeFileSync(configPath, JSON.stringify(config, null, ' ')); fs.writeFileSync(srcPackagePath, JSON.stringify(srcPackage, null, ' ')); }; module.exports = { writeUserConfig, copyProjectTemplate, createDirectoryContents };