Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #49 from FormidableLabs/chore-global-local-installs
Browse files Browse the repository at this point in the history
Add local detect and switch for builder.
  • Loading branch information
ryan-roemer committed Dec 9, 2015
2 parents 2c0933b + e4d9899 commit 38c3e26
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ Like a global install of _any_ Node.js meta / task runner tool (e.g., `eslint`,
... so instead, we **strongly recommend** a local install described in the
next section!

To help you keep up with project-specific builder requirements, a globally-
installed `builder` will detect if a locally-installed version of `builder` is
available and switch to that instead:

```
$ /GLOBAL/PATH/TO/builder
[builder:local-detect] Switched to local builder at: ./node_modules/builder/bin/builder-core.js
... now using local builder! ...
```

#### Local Install

Expand Down
24 changes: 24 additions & 0 deletions bin/builder-core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

module.exports = function (callback) {
/*eslint-disable global-require*/
// Configuration
var Config = require("../lib/config");
var config = new Config();

// Set up environment
var Environment = require("../lib/environment");
var env = new Environment({
config: config
});

// Infer task to run
var Task = require("../lib/task");
var task = new Task({
config: config,
env: env
});

// Run the task
task.execute(callback);
};
34 changes: 18 additions & 16 deletions bin/builder.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
#!/usr/bin/env node
"use strict";

// Configuration
var Config = require("../lib/config");
var config = new Config();
var path = require("path");
var log = require("../lib/log");

// Set up environment
var Environment = require("../lib/environment");
var env = new Environment({
config: config
});
// Infer if we are global and there is a local version available.
var builderPath = require.resolve("./builder-core");
var localPath = path.resolve(process.cwd(), "node_modules/builder/bin/builder-core.js");

// Infer task to run
var Task = require("../lib/task");
var task = new Task({
config: config,
env: env
});
// Swap to local path if different.
if (builderPath !== localPath) {
try {
builderPath = require.resolve(localPath);
log.info("local-detect", "Switched to local builder at: " + localPath);
} catch (err) {
log.warn("local-detect", "Error importing local builder: " + err.message);
log.info("local-detect", "Using global builder at: " + builderPath);
}
}

// Run the task
task.execute(function (err) {
// Import and run.
var builder = require(builderPath);
builder(function (err) {
/*eslint-disable no-process-exit*/
process.exit(err ? err.code || 1 : 0);
});

0 comments on commit 38c3e26

Please sign in to comment.