Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Commit

Permalink
Merge in 1.0.0 resolving conflicts.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffTomlinson committed Sep 23, 2016
2 parents 1850ba9 + c23a869 commit 64e6ffd
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 60 deletions.
2 changes: 2 additions & 0 deletions bin/aquifer.js
Expand Up @@ -20,6 +20,7 @@ const Console = require('../lib/console.api');
const Project = require('../lib/project.api');
const Build = require('../lib/build.api');
const Run = require('../lib/run.api');
const Npm = require('../lib/npm.api');
const Extension = require('../lib/extension.api');
const Environment = require('../lib/environment.api');
const Sync = require('../lib/sync.api');
Expand All @@ -29,6 +30,7 @@ AquiferAPI.prototype.api = {
project: Project,
build: Build,
run: Run,
npm: Npm,
extension: Extension,
environment: Environment,
sync: Sync
Expand Down
139 changes: 136 additions & 3 deletions lib/aquifer.api.js
Expand Up @@ -6,7 +6,11 @@
'use strict';

// Load dependencies.
const _ = require('lodash');
const fs = require('fs-extra');
const spawn = require('child_process').spawn;
const jsonFile = require('jsonfile');
const path = require('path');

/**
* Constructs a main Aquifer project.
Expand Down Expand Up @@ -76,18 +80,44 @@ class Aquifer {

// Traverse cwd, and find the aquifer project dir. If one does not exist
// in the current path, the framework is not initialized.
this.getProjectDir()
.then(() => {
if (this.projectDir) {
this.initialized = true;
}
return this.getConfig();
})
.then(() => {
return this.switchVersion();
})
.then(() => {
if (this.initialized) {
this.project = new this.api.project(this);
}
resolve();
})
.catch((reason) => {
reject(reason);
})
});
}

/**
* Find the current Aquifer project root.
* @returns {object} promise object.
*/
getProjectDir() {
return new Promise((resolve, reject) => {
// Traverse cwd, and find the aquifer project dir.
let dir = this.cwd;
while (this.projectDir === false && dir.length > 0) {
if (fs.existsSync(dir + '/aquifer.json')) {
this.initialized = true;
this.projectDir = dir;
this.project = new this.api.project(this);
}
else {
dir = dir.substring(0, dir.lastIndexOf('/'));
}
}

resolve();
});
}
Expand Down Expand Up @@ -156,6 +186,109 @@ class Aquifer {
resolve();
})
}

/**
* Get initial project configuration.
* @returns {object} promise object.
*/
getConfig() {
return new Promise((resolve, reject) => {
this.initialConfig = {};

// If this project is initialized, load the JSON path.
if (this.initialized) {
// Calculate paths to json, and source directory.
let jsonPath = path.join(this.projectDir, 'aquifer.json');
let localJsonPath = path.join(this.projectDir, 'aquifer.local.json');

this.initialConfig = jsonFile.readFileSync(jsonPath);

// Extend config with aquifer.local.json.
if (fs.existsSync(localJsonPath)) {
this.initialConfig = _.defaultsDeep(jsonFile.readFileSync(localJsonPath), this.initialConfig);
}
}

resolve();
})
}

/**
* Redirects execution to the proper version of aquifer.
* @returns {object} promise object.
*/
switchVersion() {
return new Promise((resolve, reject) => {
// No need to switch aquifer versions if we don't have an aquifer project, yet.
if (!this.initialized) {
resolve();
return;
}

if (!this.initialConfig.hasOwnProperty('version')) {
this.console.log('You have not specified an aquifer version in aquifer.json. Using the default installed version: ' + this.version, 'warning');
resolve();
return;
}

if (this.initialConfig.version === this.version) {
resolve();
return;
}

let source = this.initialConfig.source || 'aquifer@' + this.initialConfig.version;

let newAquifer = new this.api.npm(this, 'aquifer', source);

if (newAquifer.installed) {
let newAquiferDir = fs.realpathSync(path.join(newAquifer.path));
let aquiferDir = fs.realpathSync(path.join(path.dirname(module.parent.filename), '..'));

// Since the child aquifer module is installed, if the child module and
// this current instance of aquifer have the same directory, then we are
// running a chained instance of aquifer. If that chained instance
// doesn't have a matching version property, we have a problem.
if (aquiferDir === newAquiferDir && this.initialConfig.version !== this.version) {
reject('You have specified a version of aquifer that does not match the version that was installed. You probably have misconfigured source or version properties in your aquifer.json file.');
return;
}
}

// Install the right version of Aquifer.
let installAquifer = new Promise((installResolve, installReject) => {
// Return early if Aquifer is already installed.
if (newAquifer.installed) {
installResolve();
return;
}

// Let the user know we'll be installing the specified version of aquifer now.
this.console.log('Aquifer ' + this.initialConfig.version + ' is not installed. Installing now...');

newAquifer.install()
.then(() => {
installResolve();
})
.catch((reason) => {
installReject(reason);
})
});

installAquifer.then(() => {
let command = path.join(newAquifer.path, '../.bin/aquifer');

// Execute aquifer with the proper version.
let p = spawn(command, process.argv.slice(2), {stdio: 'inherit'});

p.on('close', () => {
process.exit();
})
})
.catch((reason) => {
reject(reason);
});
});
}
}

module.exports = Aquifer;
52 changes: 17 additions & 35 deletions lib/extension.api.js
Expand Up @@ -43,6 +43,9 @@ class Extension {
this.source = source || this.name;
}

// Construct the npm module instance.
this.npmModule = new this.aquifer.api.npm(this.aquifer, this.name, this.source);

// Use the source value to determine whether the extension is local.
this.isLocal = this.pathExists(this.source);
}
Expand All @@ -54,7 +57,7 @@ class Extension {
install() {
return new Promise((resolve, reject) => {
// Download this extension.
this.download()
this.npmModule.install()

// Update extensions object in aquifer.json.
.then(() => {
Expand All @@ -67,6 +70,9 @@ class Extension {
this.loadConfig();
resolve();
})
.catch((reason) => {
reject(reason);
});
})
}

Expand All @@ -76,13 +82,10 @@ class Extension {
*/
uninstall() {
return new Promise((resolve, reject) => {
// npm install extension.
let p = exec('cd ' + path.join(this.aquifer.project.directory, '.aquifer') + ' && npm uninstall --save ' + this.name, (error, stdout, stderr) => {
if (error) {
reject('Could not remove ' + this.name + ': ' + error);
return;
}
// npm uninstall extension.
this.npmModule.uninstall()

.then(() => {
// Remove configuration from aquifer.json.
let extensions = this.aquifer.project.config.extensions;
delete extensions[this.name];
Expand All @@ -93,9 +96,6 @@ class Extension {
this.loadConfig();
resolve();
});

p.stdout.pipe(process.stdout)
p.stderr.pipe(process.stdout);
})
}

Expand All @@ -118,7 +118,7 @@ class Extension {
return;
}

let extension = require(this.path)(this.aquifer, this.config) || false;
let extension = this.npmModule.load([this.aquifer, this.config]) || false;
resolve(extension);
})
};
Expand All @@ -129,32 +129,14 @@ class Extension {
*/
download() {
return new Promise((resolve, reject) => {
// Set command to link if local.
let command = this.isLocal ? 'link' : 'install';
let vendor = path.join(this.aquifer.project.directory, '.aquifer');

// If the .aquifer directory does not exist yet, create it.
if (!this.pathExists(vendor)) {
// Make the directory.
fs.mkdirSync(vendor);

// Copy src/package.json into .aquifer/package.json.
fs.copySync(path.join(this.aquifer.project.srcDir, 'package.json'), path.join(vendor, 'package.json'));
}

// Download the extensions.
let p = exec('cd ' + vendor + ' && npm ' + command + ' --save ' + this.source, (error, stdout, stderr) => {
if (error) {
reject('Could not download ' + this.name + ': ' + error);
return;
}
this.npmModule.install()

// Reload config.
.then(() => {
this.loadConfig();
resolve();
});

p.stdout.pipe(process.stdout)
p.stderr.pipe(process.stdout);
})
})
});
}

/**
Expand Down

0 comments on commit 64e6ffd

Please sign in to comment.