UNPKG

nodejs-tpkg-builder

Version:

a build tool for node apps using tpkg (http://tpkg.github.io/)

103 lines (74 loc) 3.97 kB
'use strict'; var path = require('path'); var shell = require('shelljs'); var setupFiles = function ( config, directories ) { var buildSubDir = path.join(directories.buildDir, 'build'); var projectDir = path.join(buildSubDir, 'root', config.directory.project); var binDir = path.join(buildSubDir, 'root', config.directory.bin); var appDir = path.join(projectDir, config.build.appFolder); var etcDir = path.join(buildSubDir, 'root', config.directory.etc); var cronDir = path.join(buildSubDir, 'root', config.directory.cron); var initDir = path.join(buildSubDir, 'root', config.directory.init); var sysconfigDir = path.join(buildSubDir, 'root', config.directory.sysconfig); // setup build location shell.mkdir('-p', projectDir); shell.mkdir('-p', cronDir); shell.mkdir('-p', initDir); shell.mkdir('-p', sysconfigDir); // copy node app files to build location shell.cp('-r', path.join(directories.projectDir, config.build.appFolder, '*'), appDir); shell.cp(path.join(directories.projectDir, 'package.json'), projectDir); shell.cp(path.join(directories.projectDir, 'README.md'), projectDir); if (shell.test('-d', path.join(directories.projectDir, 'bin'))) { shell.cp('-r', path.join(directories.projectDir, 'bin'), projectDir); } if (shell.test('-d', path.join(directories.projectDir, 'config'))) { shell.mkdir('-p', path.join(projectDir, 'config')); shell.cp('-r', path.join(directories.projectDir, 'config/*'), path.join(projectDir, 'config')); } if (shell.test('-d', path.join(directories.projectDir, 'scripts'))) { shell.mkdir('-p', path.join(projectDir, 'scripts')); shell.cp('-r', path.join(directories.projectDir, 'scripts/*.js'), path.join(projectDir, 'scripts')); } if (shell.test('-f', './newrelic.js')) { shell.cp(path.join(directories.projectDir, 'newrelic.js'), projectDir); } if (shell.test('-f', path.join(directories.buildDir, 'copytomogwai.conf'))) { shell.mv(path.join( directories.buildDir, 'copytomogwai.conf' ), path.join( etcDir, config.project.name + '-copytomogwai.conf')); } // move tpkg files to build location shell.mv(path.join(directories.buildDir, 'tpkg.yml'), path.join(buildSubDir, 'tpkg.yml')); shell.mv(path.join(directories.buildDir, 'postinstall'), path.join(buildSubDir, 'postinstall')); shell.mv(path.join(directories.buildDir, 'postremove'), path.join(buildSubDir, 'postremove')); shell.mv(path.join(directories.buildDir, 'crontab'), path.join(cronDir, config.project.name + '.crontab')); shell.mv(path.join(directories.buildDir, 'logrotate.conf'), path.join(cronDir, config.project.name + '-logrotate.conf')); shell.mv(path.join(directories.buildDir, 'init.sh'), path.join(initDir, config.project.name + '-init.sh')); shell.mv(path.join(directories.buildDir, 'config.sh'), path.join(sysconfigDir, config.project.name + '-config.sh')); // add execute permissions to tpkg files shell.chmod('-R', 755, buildSubDir); console.log('success - tpkg build directory setup complete') }; var makeTpkg = function (config, buildDir) { var tpkgBuildDir = path.join(buildDir, 'build'); var output = shell.exec('tpkg --make ' + tpkgBuildDir).output; if (output.indexOf('tpkg: command not found') !== -1) { console.error('error - unable to create tpkg file because the tpkg client is not installed'); return false; } var expectedFile = output.match(/\/(.*)?\.tpkg$/gmi); //var expectedFile = path.join(buildDir, config.project.name + '-' + config.project.version + '-*.tpkg'); if ( !expectedFile || !shell.test('-e', expectedFile[0]) ) { console.error('error - failed to create the expected tpkg file'); return false; } return true; }; var cleanBuildDir = function (buildDir) { shell.rm('-rf', buildDir); shell.mkdir('-p', buildDir); }; module.exports = { setupFiles: setupFiles, makeTpkg: makeTpkg, cleanBuildDir: cleanBuildDir };