UNPKG

4.43 kBJavaScriptView Raw
1/*
2 * Copyright 2014-2016 Guy Bedford (http://guybedford.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16var EventEmitter = require('events').EventEmitter;
17var API = module.exports = new EventEmitter();
18
19var install = require('./lib/install');
20var bundle = require('./lib/bundle');
21var link = require('./lib/link');
22var core = require('./lib/core');
23var ui = require('./lib/ui');
24var SystemJSLoader = require('systemjs').constructor;
25var config = require('./lib/config');
26var path = require('path');
27
28API.version = config.version;
29
30API.setPackagePath = function(packagePath) {
31 if (config.loaded && process.env.jspmConfigPath !== path.resolve(packagePath, 'package.json'))
32 throw new Error('Configuration has already been loaded. Call setPackagePath before using other APIs.');
33 process.env.jspmConfigPath = path.resolve(packagePath, 'package.json');
34};
35API.setPackagePath('.');
36
37/*
38 * jspm.on('log', function(type, msg) { console.log(msg); });
39 * jspm.on('prompt', function(prompt, callback) {
40 * if (prompt.type == 'confirm')
41 * callback({ confirm: true });
42 * if (prompt.type == 'input')
43 * callback({ input: value });
44 * });
45 *
46 * Prompt as defined in https://github.com/SBoudrias/Inquirer.js/tree/master#question
47 * Callback answer defined in https://github.com/SBoudrias/Inquirer.js/tree/master#answers
48 */
49ui.setResolver(API);
50ui.useDefaults();
51
52API.promptDefaults = function(_useDefaults) {
53 ui.useDefaults(_useDefaults);
54};
55
56API.version = require('./package.json').version;
57
58/*
59 * Loader API
60 */
61var apiLoader;
62API.normalize = function(name, parentName) {
63 apiLoader = apiLoader || new API.Loader();
64 return apiLoader.normalize(name, parentName);
65};
66
67API.import = function(name, parentName) {
68 apiLoader = apiLoader || new API.Loader();
69 return apiLoader.import(name, parentName);
70};
71
72API.Loader = function() {
73 config.loadSync();
74
75 var cfg = config.getLoaderConfig();
76
77 var loader = new SystemJSLoader();
78 loader.config(cfg);
79
80 return loader;
81};
82
83/*
84 * Builder API
85 */
86API.Builder = bundle.Builder;
87
88// deprecated:
89function builderDeprecationMsg(method) {
90 return Promise.reject(new TypeError('jspm.' + method + ' is deprecated for the Builder API:\n\tvar Builder = require(\'jspm\').Builder;\n\tvar builder = new Builder();\n\tbuilder.' + method + '(...).then(...)'));
91}
92
93
94API.bundle = builderDeprecationMsg.bind(API, 'bundle');
95API.build = builderDeprecationMsg.bind(API, 'build');
96API.bundleSFX = builderDeprecationMsg.bind(API, 'bundleSFX');
97API.unbundle = function() {
98 return bundle.unbundle();
99};
100
101/*
102 * Package Management API
103 *
104
105/*
106 * Installs a library in the current folder
107 * returns a promise
108 *
109 * jspm.install('jquery')
110 * jspm.install('jquery', 'github:components/jquery@^2.0.0')
111 * jspm.install('jquery', '2')
112 * jspm.install('jquery', 'github:components/jquery')
113 * jspm.install('jquery', { force: true });
114 * jspm.install({ jquery: '1.2.3' }, { force: true })
115 * jspm.install(true, options) // install from package.json
116 *
117 */
118API.install = function(name, target, options) {
119 return install.install(name, target, options);
120};
121
122/* Uninstalls a library in the current folder.
123 * returns a promise
124 *
125 * jspm.uninstall('jquery')
126 * jspm.uninstall(['jquery', 'handlebars'])
127 *
128 */
129API.uninstall = function(names) {
130 return install.uninstall(names);
131};
132
133API.dlLoader = function(transpiler) {
134 return core.checkDlLoader(transpiler);
135};
136
137/**
138 * Link a local folder as an installable package
139 * @param {string} linkTargetDir Filesystem path to package source directory
140 * @param {string} linkName Uncanonical package name used to register the link target (the version before map/path rules are applied)
141 * @param {object} options
142 * @param {boolean} options.force
143 * @param {boolean} options.quick
144 * @returns {Promise}
145 */
146API.link = function(linkTargetDir, linkName, options) {
147 return link.link(linkTargetDir, linkName, options || {});
148};