Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
feat: added build cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrantsberg committed Mar 4, 2020
1 parent 4845a63 commit 941c082
Show file tree
Hide file tree
Showing 4 changed files with 3,817 additions and 242 deletions.
50 changes: 50 additions & 0 deletions cli/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
const Services = require('../lib/services')
const _ = require('lodash')
const servicesCache = require('@labshare/services-cache').Middleware
const path = require('path');
const yargs = require('yargs');
const {buildService, getBuildDate} = require('../lib/cli/build-service');

exports.usage = [
'lsc services start - Start up LabShare API services.',
'lsc services build - Build LabShare API services.',
''
]

Expand All @@ -26,3 +30,49 @@ exports.start = async function () {
}
await services.start()
}

exports.build = async function () {
this.log.info('Building LabShare services...');
const distPath = path.join('dist', `service.${getBuildDate()}`);
const options = getBuildOptions({defaultDestination: distPath});
await buildService(options);
}


/**
* @description Gets the common build options
* @param {String} defaultDestination
* @param {object} extendedOptions
* @returns {debug, buildVersion, npmCache}
* @private
*/
function getBuildOptions({defaultDestination}, extendedOptions = {}) {
return yargs.options(_.extend({
debug: {
describe: 'Build unminified version',
type: 'boolean',
default: false
},
buildVersion: {
describe: 'Customize the build version',
default: getBuildDate()
},
npmCache: {
type: 'string',
describe: 'Overrides global npm cache for npm install',
default: null
},
source: {
type: 'string',
describe: 'Set the project root directory',
default: process.cwd()
},
destination: {
alias: ['dest', 'dist'],
describe: 'The path to the build destination',
type: 'string',
default: defaultDestination
}
}, extendedOptions)).argv;
}

87 changes: 87 additions & 0 deletions lib/cli/build-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const path = require('path')
const gulp = require('gulp')
const util = require('util');
const del = require('del');
const pipeline = util.promisify(require('stream').pipeline);
const log = require('fancy-log');
const {execSync} = require('child_process');
const {writeFileSync} = require('fs');
const _ = require('lodash')

async function buildService({
source,
destination,
buildVersion,
npmCache = null
}) {
const localCache = npmCache || path.join(destination, 'local-npm-cache')
const envFilePath = path.join(destination, '.env');
await cleanDir(destination);

log('Copy server files to distribution...');
await copyServerFiles({destination, cwd: source});

const cmd = `npm i -progress=false --cache=${localCache}`;

log(`Installing server dependencies in ${destination}...`);
log(`Command: ${cmd}`);

execSync(cmd, {cwd: destination});

// Remove dev dependencies
log('Removing extraneous dependencies with "npm prune"...');
execSync(`npm prune`, {cwd: destination});

// Store build version in the .env file
writeFileSync(envFilePath, `LABSHARE_BUILD_VERSION=${buildVersion}`);

// Remove local npm caches.
log(`Removing local cache directories in ${localCache} and ${path.join(destination, path.basename(localCache))}...`);
await Promise.all([
cleanDir(localCache),
cleanDir(path.join(destination, path.basename(localCache)))
]);

return destination;
}

/**
*
* @returns {string} A build date formatted as 'v<year>.<month><day>'. For example: 'v17.1127' for November, 27th 2017.
*/
function getBuildDate() {
const today = new Date()
const year = today.getFullYear().toString().slice(2)
const month = padLeft((today.getMonth() + 1).toString())
const day = padLeft(today.getDate().toString())
return `v${year}.${padLeft(month + day)}`
}

function padLeft(dateValue) {
return _.padStart(dateValue, 2, '0')
}

/**
* @description
* @param {string} dist - The directory to copy the LabShare service source files to
* @param {String} cwd - The current working directory
* @returns {*}
* @private
*/
async function copyServerFiles({destination, cwd = process.cwd()}) {
const source = gulp.src([
'./*.*',
'./!(node_modules|packages|docs|dist|test)/**/*',
'!./node_modules/*/test/**/*',
'!package-lock.json'
], {follow: true, base: cwd, cwd})
return await pipeline(source, gulp.dest(destination));
}

async function cleanDir(directory) {
return del([
path.join(directory, '**/*')
], {cwd: process.cwd(), force: true});
}

module.exports = {buildService, getBuildDate};
Loading

0 comments on commit 941c082

Please sign in to comment.