UNPKG

boilerplate.js

Version:

Development Tools

82 lines (63 loc) 2.78 kB
function initPage(param, jsLocation) { var pathname = url(param).pathname; var createComponent = false; var sendToComponent = pathname.split('~'); if (pathname.split('~').length > 1) { createComponent = true; pathname = pathname.split('~')[0]; }; var arrayCC = (toCamelCase(pathname)).split('/'); var arrayOriginal = pathname.split('/'); arrayOriginal = arrayOriginal.slice(1, arrayOriginal.length); var toCheck = []; if (param[0] === '~') { require('./initPage/createGlobalComponent').createGlobalComponent(param); return; } !fs.existsSync(jsLocation + '/page') && fs.mkdirSync(jsLocation + '/page'); // append data to file fs.readFile(global.jsInit, function (err, data) { if (err) throw err; // console.log('Updating init.js'); // (data.indexOf('function ' + arrayCC[0]) < 0) ? fs.appendFileSync(global.jsInit, '\r\nfunction ' + arrayCC[0] + '() {}'): ''; arrayCC.map((x, index) => { if (!x) return; toCheck.push((x)); var FilePath = jsLocation + '/page/' + toCheck.join('/'); var initFile = path.resolve(FilePath + '/_init.js'); // if (data.indexOf('page.' + toCheck.join('.')) < 0) { // fs.appendFileSync(global.jsInit, '\r\npage.' + toCheck.join('.') + ' = {}; '); // toCheck.join('.') !== arrayCC[0] ? fs.appendFileSync(global.jsInit, toCheck.join('.') + ' = {}; ') : ''; // fs.appendFileSync(global.jsInit, toCheck.join('.') + '.components = {};'); // } !fs.existsSync(FilePath) && fs.mkdirSync(FilePath); if (!fs.existsSync(initFile)) { fs.writeFileSync(initFile, template(toCheck, index + 1, arrayOriginal)); console.log('Created: ', initFile); } else { console.log('File Already Exists', initFile); } }); }); if (createComponent) require('./initPage/createPageComponent').createPageComponent(sendToComponent); function template(data, index, pathname) { pathname = pathname.slice(0, index); return `page('${data.join('.')}.init', () => { // Code here also executes in all subpages // Executes on this page only thisPageOnly('/${pathname.join('/')}', function () { // Your Code Here }); });`; } function templateComponent(data) { return `// How To Use -> ${data.join('.')}.components.exampleComponent(); ${data.join('.')}.components.exampleComponent = function() { // Example component for this page }; ` } } module.exports = { initPage };