1 | #!/usr/bin/env node
|
2 | const fs = require('fs');
|
3 | const path = require('path');
|
4 | const child_process = require('child_process');
|
5 | const _ = require('lodash');
|
6 |
|
7 | const exec = function (cmd, args) {
|
8 | return new Promise(function (resolve, reject) {
|
9 |
|
10 | const child = child_process.exec(cmd, {
|
11 | cwd: process.cwd(),
|
12 | env: process.env,
|
13 | });
|
14 |
|
15 |
|
16 | child.stdout.on('data', function (data) {
|
17 | process.stdout.write(data.toString());
|
18 | });
|
19 | child.stderr.on('data', function (data) {
|
20 | process.stderr.write(data.toString());
|
21 | });
|
22 |
|
23 | child.on('exit', function (code) {
|
24 | if (code) reject(code);
|
25 | else resolve();
|
26 | });
|
27 | child.on('error', reject);
|
28 | });
|
29 | };
|
30 |
|
31 | const CWD = process.cwd();
|
32 | const POSTINSTALL_BUILD_CWD = process.env.POSTINSTALL_BUILD_CWD;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | if (POSTINSTALL_BUILD_CWD !== CWD) {
|
43 | const BUILD_ARTIFACT = process.argv[2];
|
44 | const BUILD_COMMAND = process.argv[3];
|
45 |
|
46 | fs.stat(BUILD_ARTIFACT, function (err, stats) {
|
47 | if (err || !(stats.isFile() || stats.isDirectory())) {
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | process.env.POSTINSTALL_BUILD_CWD = CWD;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | const pkgJson = require(path.join(CWD, 'package.json'));
|
58 | const devDeps = pkgJson.devDependencies;
|
59 |
|
60 |
|
61 | const buildDependencies = _.pick(devDeps, pkgJson.buildDependencies);
|
62 |
|
63 |
|
64 | if (!_.isEmpty(buildDependencies)) {
|
65 | const opts = { env: process.env, stdio: 'inherit' };
|
66 |
|
67 | console.log('Building Knex.js');
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | const installArgs = _(buildDependencies)
|
73 | .pickBy(function (semver, dep) {
|
74 |
|
75 | try {
|
76 | require(dep);
|
77 | return false;
|
78 | } catch (err) {
|
79 | return true;
|
80 | }
|
81 | })
|
82 | .map(function (semver, dep) {
|
83 |
|
84 | return dep + '@' + semver;
|
85 | })
|
86 | .value()
|
87 | .join(' ');
|
88 | const needsDepInstallation = !_.isEmpty(installArgs);
|
89 | const dependenciesInstalledQ = needsDepInstallation
|
90 | ? exec('npm install ' + installArgs, opts)
|
91 | : Promise.resolve();
|
92 | dependenciesInstalledQ
|
93 | .then(function () {
|
94 | console.log('✓');
|
95 |
|
96 |
|
97 |
|
98 | process.env.POSTINSTALL_BUILD_CWD = POSTINSTALL_BUILD_CWD;
|
99 | console.log('Building compiled files (' + BUILD_COMMAND + ')');
|
100 | return exec(BUILD_COMMAND, opts);
|
101 | })
|
102 | .catch(function (err) {
|
103 | console.error(err);
|
104 | process.exit(1);
|
105 | })
|
106 | .then(function () {
|
107 | if (process.env.NODE_ENV === 'production') {
|
108 | console.log('✓');
|
109 | console.log('Pruning dev dependencies for production build');
|
110 | return exec('npm prune --production', opts);
|
111 | } else {
|
112 | console.log('Skipping npm prune');
|
113 | }
|
114 | })
|
115 | .then(function () {
|
116 | console.log('✓');
|
117 | })
|
118 | .catch(function (err) {
|
119 | console.error(err);
|
120 | process.exit(1);
|
121 | });
|
122 | }
|
123 | }
|
124 | });
|
125 | }
|