Skip to content

Commit

Permalink
feat: add support for zeit pkg builds (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
from-the-river-to-the-sea committed Jan 18, 2018
1 parent 54baa0e commit acb168c
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 30 deletions.
1 change: 1 addition & 0 deletions .idea/nodecg.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions build/src/nodecg-api.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build/src/nodecg-api.min.js.map

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions index.js
Expand Up @@ -3,8 +3,12 @@
process.title = 'NodeCG';
global.exitOnUncaught = true;

if (process.cwd() !== __dirname) {
console.warn('[nodecg] process.cwd is %s, expected %s', process.cwd(), __dirname);
const cwd = process.cwd();
global.isZeitPkg = __dirname.startsWith('/snapshot/') || __dirname.toLowerCase().startsWith('c:\\snapshot\\');
if (global.isZeitPkg) {
console.info('[nodecg] Detected that NodeCG is running inside a ZEIT pkg (https://github.com/zeit/pkg)');
} else if (cwd !== __dirname) {
console.warn('[nodecg] process.cwd is %s, expected %s', cwd, __dirname);
process.chdir(__dirname);
console.info('[nodecg] Changed process.cwd to %s', __dirname);
}
Expand Down
21 changes: 10 additions & 11 deletions lib/bundle-manager.js
Expand Up @@ -29,9 +29,9 @@ const watcher = chokidar.watch([

const emitter = new EventEmitter();
const bundles = [];
let bundlesPath;
let log;
let root;
let _cfgPath;
let _bundlesPath;
let backoffTimer = null;
let hasChanged = {};
let initialized = false;
Expand Down Expand Up @@ -63,25 +63,24 @@ module.exports = emitter;

/**
* Constructs a bundle-manager.
* @param rootPath {String} - The directory where NodeCG's "bundles" and "cfg" folders can be found.
* @param bundlesPath {String} - The path to NodeCG's "bundles" folder
* @param cfgPath {String} - The path to NodeCG's "cfg" folder.
* @param nodecgVersion {String} - The value of "version" in NodeCG's package.json.
* @param nodecgConfig {Object} - The global NodeCG config.
* @param Logger {Function} - A preconfigured @nodecg/logger constructor.
* @return {Object} - A bundle-manager instance.
*/
module.exports.init = function (rootPath, nodecgVersion, nodecgConfig, Logger) {
module.exports.init = function (bundlesPath, cfgPath, nodecgVersion, nodecgConfig, Logger) {
if (initialized) {
throw new Error('Cannot initialize when already initialized');
}

initialized = true;

root = rootPath;
_bundlesPath = bundlesPath;
_cfgPath = cfgPath;
log = new Logger('nodecg/lib/bundles');
log.trace('Loading bundles');

bundlesPath = path.join(rootPath, '/bundles');

// Create the "bundles" dir if it does not exist.
/* istanbul ignore if: We know this code works and testing it is tedious, so we don't bother to test it. */
if (!fs.existsSync(bundlesPath)) {
Expand Down Expand Up @@ -155,7 +154,7 @@ module.exports.init = function (rootPath, nodecgVersion, nodecgConfig, Logger) {

// Parse each bundle and push the result onto the bundles array
let bundle;
const bundleCfgPath = path.join(rootPath, '/cfg/', bundleFolderName + '.json');
const bundleCfgPath = path.join(cfgPath, `${bundleFolderName}.json`);
if (fs.existsSync(bundleCfgPath)) {
bundle = parseBundle(bundlePath, bundleCfgPath);
} else {
Expand Down Expand Up @@ -286,7 +285,7 @@ function _handleChange(bundleName) {
resetBackoffTimer();

let reparsedBundle;
const bundleCfgPath = path.join(root, '/cfg/', bundleName + '.json');
const bundleCfgPath = path.join(_cfgPath, `${bundleName}.json`);
if (fs.existsSync(bundleCfgPath)) {
reparsedBundle = parseBundle(bundle.dir, bundleCfgPath);
} else {
Expand Down Expand Up @@ -325,7 +324,7 @@ function resetBackoffTimer() {
* @private
*/
function extractBundleName(filePath) {
const parts = filePath.replace(bundlesPath, '').split(path.sep);
const parts = filePath.replace(_bundlesPath, '').split(path.sep);
return parts[1];
}

Expand Down
10 changes: 6 additions & 4 deletions lib/bundle-parser/git.js
Expand Up @@ -5,17 +5,19 @@ const git = require('git-rev-sync');
module.exports = function (bundle) {
const gitData = {};
const workingDir = process.cwd();
process.chdir(bundle.dir); // Needed for git-rev-sync to work.

try {
// These will error if bundle.dir is not a git repo
gitData.branch = git.branch(bundle.dir);
gitData.hash = git.long(bundle.dir);
gitData.shortHash = git.short(bundle.dir);

// These will error if bundle.dir is not a git repo and if `git` is not in $PATH
gitData.date = git.date(bundle.dir);
gitData.message = git.message(bundle.dir);
// Needed for the below commands to work.
process.chdir(bundle.dir);

// These will error if bundle.dir is not a git repo and if `git` is not in $PATH.
gitData.date = git.date();
gitData.message = git.message();
} catch (e) {}

process.chdir(workingDir);
Expand Down
4 changes: 3 additions & 1 deletion lib/replicant/replicant.js
Expand Up @@ -84,7 +84,9 @@ class Replicant extends EventEmitter {
if (opts.schemaPath) {
const absoluteSchemaPath = path.isAbsolute(opts.schemaPath) ?
opts.schemaPath :
path.join(process.env.NODECG_ROOT, opts.schemaPath);
global.isZeitPkg ?
path.resolve(__dirname, '../..', opts.schemaPath) :
path.join(process.env.NODECG_ROOT, opts.schemaPath);
if (fs.existsSync(absoluteSchemaPath)) {
try {
const schema = $RefParser.readSync(absoluteSchemaPath);
Expand Down
7 changes: 6 additions & 1 deletion lib/server/index.js
Expand Up @@ -22,6 +22,7 @@ if (config.sentry && config.sentry.enabled) {
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const EventEmitter = require('events').EventEmitter;
const authors = require('parse-authors');
const tokens = require('../login/tokens');
Expand Down Expand Up @@ -95,7 +96,11 @@ module.exports.start = function () {
});
}

bundleManager.init(process.env.NODECG_ROOT, require('../../package.json').version, config, Logger);
const bundlesPath = global.isZeitPkg ?
path.resolve(__dirname, '../../bundles') :
path.join(process.env.NODECG_ROOT, 'bundles');
const cfgPath = path.join(process.env.NODECG_ROOT, 'cfg');
bundleManager.init(bundlesPath, cfgPath, require('../../package.json').version, config, Logger);

io.on('error', err => {
if (global.sentryEnabled) {
Expand Down
10 changes: 9 additions & 1 deletion package.json
Expand Up @@ -66,7 +66,7 @@
"scripts": {
"start": "node index.js",
"pretest": "npm run build && npm run instrument",
"test": "nyc ava --reporter=html",
"test": "nyc ava test --reporter=html",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"static": "eslint index.js lib/**/*.js src/**/*.js test/**/*.js",
"build": "npm run static && npm run docs:build && gulp",
Expand Down Expand Up @@ -96,6 +96,14 @@
"index.js",
"lib/"
],
"pkg": {
"assets": [
"bower_components/**/*",
"src/**/*",
"build/**/*",
"bundles/**/*"
]
},
"devDependencies": {
"aliasify": "^2.1.0",
"ava": "^0.24.0",
Expand Down
8 changes: 7 additions & 1 deletion test/bundle-manager.js
Expand Up @@ -38,7 +38,13 @@ test.cb.before(t => {
};

bundleManager = require('../lib/bundle-manager');
bundleManager.init(tempFolder, '0.7.0', nodecgConfig, Logger);
bundleManager.init(
path.join(tempFolder, 'bundles'),
path.join(tempFolder, 'cfg'),
'0.7.0',
nodecgConfig,
Logger
);

// Needs a little extra wait time for some reason.
// Without this, tests randomly fail.
Expand Down

0 comments on commit acb168c

Please sign in to comment.