1 |
|
2 |
|
3 | const _ = require('underscore');
|
4 | const fs = require('fs');
|
5 | const path = require('path');
|
6 | const url = require('url');
|
7 | const npm = require('npm');
|
8 | const exec = require('child_process').execSync;
|
9 | const rimraf = require('rimraf');
|
10 | const semver = require('semver');
|
11 | const program = require('commander');
|
12 | const Promise = require('bluebird');
|
13 |
|
14 | // Promisify some methods
|
15 | const loadNpmAsync = Promise.promisify(npm.load);
|
16 | const rimrafAsync = Promise.promisify(rimraf);
|
17 |
|
18 | // We can't initilize the `npm.commands` before getting the callback from
|
19 | // `npm.load`.
|
20 | //
|
21 | // https://github.com/npm/npm/blob/master/lib/npm.js#L175-L180
|
22 | // NPM wrote a getter on `npm.commands` to detect this, because of the reason
|
23 | // from NPM, we have to put a `var` and update it later.
|
24 | var installNpmPkgAsync;
|
25 |
|
26 | program
|
27 | .option('-a --alias <name>', 'specify the theme/plugin name')
|
28 | .option('-t --theme', 'install package as theme')
|
29 | .option('-p --plugin', 'install package as plugin')
|
30 | .option('-v --verbose', 'print all verbose information')
|
31 | .parse(process.argv);
|
32 |
|
33 | // Make variablize is because we should rewrite this label.
|
34 | var debug = require('debug');
|
35 | if (program.verbose) {
|
36 | debug.enable('firedoc:*');
|
37 | }
|
38 | // Here we rewrite the `debug` to enuse it.
|
39 | debug = debug('firedoc:install');
|
40 |
|
41 | // The user can input the --alias to forcely set
|
42 | // the `name` value
|
43 | var name = program.alias();
|
44 | var remote = program.args[0];
|
45 |
|
46 | // Commonly, if remote is not a url but a pure name, FireDoc
|
47 | // should prepend it with `github.com/fireball-x` and make it
|
48 | // to be a valid URI.
|
49 | if (/[a-z0-9_\-]+/i.test(remote)) {
|
50 | // Check the remote does start from `firedoc-theme-`, this
|
51 | // allows the pure theme name like:
|
52 | //
|
53 | // notab` -> `firedoc-theme-notab`
|
54 | //
|
55 | if (remote.slice(0, 14) !== 'firedoc-theme-') {
|
56 | remote = 'firedoc-theme-' + remote;
|
57 | }
|
58 | remote = 'https://github.com/fireball-x/' + remote;
|
59 | }
|
60 |
|
61 | // By default, we should get the name from the url, like if
|
62 | // we get the following remote:
|
63 | //
|
64 | // https://github.com/fireball-x/firedoc-theme-notab
|
65 | //
|
66 | // So we should extract the name to: firedoc-theme-notab
|
67 | // so far.
|
68 | //
|
69 | if (!name) {
|
70 | var urlObj = url.parse(remote);
|
71 | // If the user inputs a url end with a `.git`, we should
|
72 | // not put the extension name to the `name` variable.
|
73 | name = path.basename(urlObj.path).replace('.git', '');
|
74 | }
|
75 |
|
76 | const themedir = path.join(__dirname, '../themes');
|
77 | const destdir = themedir + '/' + name;
|
78 |
|
79 | // This option is used for calling `git.Clone`, that basically
|
80 | // is for authorization of SSH/HTTPS from Github/Bitbucket.
|
81 | const cloneOption = {
|
82 | 'remoteCallbacks': {
|
83 | // set a function to return 1 always to skip the authentication
|
84 | certificateCheck: function () { return 1; },
|
85 | // this is for SSH agent
|
86 | credentials: function (url, user) {
|
87 | return git.Cred.sshKeyFromAgent(username);
|
88 | }
|
89 | }
|
90 | // Check http://www.nodegit.org/guides/cloning for more details
|
91 | // about NODEGIT
|
92 | };
|
93 |
|
94 | // TODO(Yorkie): This function to be extended in the future, just quite
|
95 | // not sure the error report for now.
|
96 | //
|
97 | // #PR-WELCOME
|
98 | //
|
99 | function errorHandler (e) {
|
100 | throw e;
|
101 | }
|
102 |
|
103 | // Build the installing package string. It supports the following styles:
|
104 | //
|
105 | // - [<@scope>/]<pkg>
|
106 | // - [<@scope>/]<pkg>@<tag>
|
107 | // - [<@scope>/]<pkg>@<version>
|
108 | // - [<@scope>/]<pkg>@<version range> (Very Important)
|
109 | // - <git:// url>
|
110 | // - etc.
|
111 | //
|
112 | // Thanks to the powerful support from libnpm, `firedoc-install(1)` also
|
113 | // supports the above installation ways.
|
114 | function buildNpmPackageName (version, name) {
|
115 | debug('Validing the package(semver): ' + name);
|
116 | return name + '@' + version;
|
117 | }
|
118 |
|
119 | function installNpmPackage (pkg) {
|
120 | debug('Installing the dependence: ' + pkg);
|
121 | // Rewrite the file-scoped `installNpmPkgAsync`, promisify the
|
122 | // `npm.commands.install` once here, and free to use forever.
|
123 | if (!installNpmPkgAsync) {
|
124 | installNpmPkgAsync = Promise.promisify(npm.commands.install);
|
125 | }
|
126 | // call the promisified `npm.commands.install` and set the theme dir
|
127 | // to where the package would be installed.
|
128 | return installNpmPkgAsync(destdir, pkg);
|
129 | }
|
130 |
|
131 | // This line is the start line in real runtime logic, the entire logic
|
132 | // of installing theme would inclued the following steps:
|
133 | //
|
134 | // - clean the original/installed theme directory (#PR-WELCOME, remove this?)
|
135 | // - clone the project the theme directory
|
136 | // - install npm package
|
137 | // ? install bower(#PR-WELCOME, do we really need to support bower progress?)
|
138 | //
|
139 |
|
140 | debug('cleaning the original folder: ' + name);
|
141 | rimrafAsync(destdir)
|
142 | .then(function () {
|
143 | debug('Start cloning "' + remote +
|
144 | '" into "' + path.relative(process.cwd(), destdir) + '"');
|
145 | return exec(`git clone ${remote} ${destdir}`);
|
146 | })
|
147 | .then(function () {
|
148 | debug('Start installing the dependencies');
|
149 | return _.map(
|
150 | require(destdir + '/package.json').dependencies,
|
151 | buildNpmPackageName
|
152 | );
|
153 | })
|
154 | .then(function (deps) {
|
155 | return loadNpmAsync({}).then(function () {
|
156 | return Promise.map(deps, installNpmPackage);
|
157 | });
|
158 | })
|
159 | .catch(errorHandler)
|
160 | .done(function () {
|
161 | debug('Installed ' + name);
|
162 | });
|