1 | #!/usr/bin/env node
|
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 |
|
15 | const loadNpmAsync = Promise.promisify(npm.load);
|
16 | const rimrafAsync = Promise.promisify(rimraf);
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
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 |
|
34 | var debug = require('debug');
|
35 | if (program.verbose) {
|
36 | debug.enable('firedoc:*');
|
37 | }
|
38 |
|
39 | debug = debug('firedoc:install');
|
40 |
|
41 |
|
42 |
|
43 | var name = program.alias();
|
44 | var remote = program.args[0];
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | if (/[a-z0-9_\-]+/i.test(remote)) {
|
50 |
|
51 |
|
52 |
|
53 |
|
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 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | if (!name) {
|
70 | var urlObj = url.parse(remote);
|
71 |
|
72 |
|
73 | name = path.basename(urlObj.path).replace('.git', '');
|
74 | }
|
75 |
|
76 | const themedir = path.join(__dirname, '../themes');
|
77 | const destdir = themedir + '/' + name;
|
78 |
|
79 |
|
80 |
|
81 | const cloneOption = {
|
82 | 'remoteCallbacks': {
|
83 |
|
84 | certificateCheck: function () { return 1; },
|
85 |
|
86 | credentials: function (url, user) {
|
87 | return git.Cred.sshKeyFromAgent(username);
|
88 | }
|
89 | }
|
90 |
|
91 |
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function errorHandler (e) {
|
100 | throw e;
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
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 |
|
122 |
|
123 | if (!installNpmPkgAsync) {
|
124 | installNpmPkgAsync = Promise.promisify(npm.commands.install);
|
125 | }
|
126 |
|
127 |
|
128 | return installNpmPkgAsync(destdir, pkg);
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
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 | });
|