UNPKG

2.06 kBJavaScriptView Raw
1/**
2 * This code is closed source and Confidential and Proprietary to
3 * Appcelerator, Inc. All Rights Reserved. This code MUST not be
4 * modified, copied or otherwise redistributed without express
5 * written permission of Appcelerator. This file is licensed as
6 * part of the Appcelerator Platform and governed under the terms
7 * of the Appcelerator license agreement.
8 */
9/**
10 * this file will attempt to locate the correct appcelerator module version
11 * and then will export its module.exports as its own
12 */
13var fs = require('fs'),
14 path = require('path'),
15 util = require('./lib/util'),
16 version;
17
18// see if we're being loaded from a parent module and if so, we need
19// to check and see if that module is requiring a specific version of
20// appc and if so, attempt to load it
21if (module.parent && module.parent.filename) {
22 // attempt to find the package.json for this file
23 var dir = path.dirname(module.parent.filename);
24 while (fs.existsSync(dir)) {
25 var pk = path.join(dir, 'package.json');
26 // if we found it, check and see if it has an appc-version key with a specific version
27 if (fs.existsSync(pk)) {
28 pk = require(pk);
29 version = pk['appc-version'];
30 break;
31 }
32 // else walk backwards
33 dir = path.join(dir, '..');
34 }
35}
36
37var bin = util.getInstallBinary(null,version);
38
39// if we didn't find the version, we need to bail
40if (!bin) {
41 // if we didn't specify a version and we couldn't find one. we don't have appc installed
42 if (!version) {
43 throw new Error("you must run `appc setup` before you can require this module");
44 }
45 // we specified a version but we don't have it installed. ideally we could auto-install
46 // but since module loading is synchronous we have to just bail and make the user do it
47 else {
48 throw new Error("you must run `appc use "+version+"` to install the required version");
49 }
50}
51
52// we found our appc module so we need to export our module impersonating
53// the module we're delegating to as if we had loaded it directly
54var pkgdir = path.join(path.dirname(bin),'..');
55module.exports = require(pkgdir);
56