Skip to content

Commit 83ab54a

Browse files
committed
Clean ourselves after install, heavily based (copied) from nodegit
1 parent 84c8d31 commit 83ab54a

5 files changed

Lines changed: 120 additions & 2 deletions

File tree

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"space-in-parens": [2, "always", { "exceptions": ["{}"] }]
4141
},
4242
"env": {
43-
"node": true
43+
"node": true,
44+
"es6": true
4445
},
4546
"extends": "eslint:recommended"
4647
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
},
2121
"main": "./index.js",
2222
"scripts": {
23-
"install": "node-pre-gyp install --fallback-to-build",
2423
"test": "mocha test --reporter spec",
2524
"docs": "jsdoc2md lib/*.js > api.md",
25+
"postinstall": "node scripts/postinstall",
2626
"pregyp": "node-pre-gyp"
2727
},
2828
"binary": {
@@ -36,6 +36,7 @@
3636
],
3737
"dependencies": {
3838
"debug": "^2.2",
39+
"fs-extra": "^4.0.1",
3940
"nan": "~2.5.0",
4041
"node-gyp": "~3.6.2",
4142
"node-pre-gyp": "~0.6",

scripts/buildFlags.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var fs = require( 'fs' );
2+
var path = require( 'path' );
3+
4+
var isGitRepo;
5+
6+
// Proudly copied from https://github.com/nodegit/nodegit/blob/288ab93/lifecycleScripts/buildFlags.js
7+
8+
try {
9+
fs.statSync( path.join( __dirname, '..', '.git' ) );
10+
isGitRepo = true;
11+
} catch ( e ) {
12+
isGitRepo = false;
13+
}
14+
15+
module.exports = {
16+
debugBuild: !!process.env.BUILD_DEBUG,
17+
isElectron: process.env.npm_config_runtime === 'electron',
18+
isGitRepo: isGitRepo,
19+
isNwjs: process.env.npm_config_runtime === 'node-webkit',
20+
mustBuild: !!( isGitRepo || process.env.BUILD_DEBUG || process.env.BUILD_ONLY )
21+
};

scripts/execPromise.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Proudly copied from https://github.com/nodegit/nodegit/blob/288ab93/lifecycleScripts/execPromise.js
2+
var cp = require( 'child_process' );
3+
4+
// We have to manually promisify this because at this is required in lifecycle
5+
// methods and we are not guaranteed that any 3rd party packages are installed
6+
// at this point
7+
module.exports = function ( command, opts ) {
8+
return new Promise( function ( resolve, reject ) {
9+
return cp.exec( command, opts, function ( err, result ) {
10+
if ( err ) {
11+
reject( err );
12+
} else {
13+
resolve( result );
14+
}
15+
});
16+
});
17+
};

scripts/postinstall.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Mostly copied from https://github.com/nodegit/nodegit/blob/288ab93/lifecycleScripts/postinstall.js
2+
var fse = require( 'fs-extra' );
3+
var path = require( 'path' );
4+
var log = require( 'npmlog' );
5+
6+
var buildFlags = require( './buildFlags' );
7+
var exec = require( './execPromise' );
8+
9+
log.heading = 'node-libcurl';
10+
11+
var rootPath = path.join( __dirname, '..' );
12+
13+
function printStandardLibError() {
14+
log.error( '', 'the latest libstdc++ is missing on your system!' );
15+
log.error( 'On Ubuntu you can install it using:' );
16+
log.error( '$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test' );
17+
log.error( '$ sudo apt-get update' );
18+
log.error( '$ sudo apt-get install libstdc++-4.9-dev' );
19+
}
20+
21+
module.exports = function install() {
22+
if ( buildFlags.isGitRepo ) {
23+
// If we're building NodeGit from a git repo we aren't going to do any
24+
// cleaning up
25+
return Promise.resolve();
26+
}
27+
28+
if ( buildFlags.isElectron || buildFlags.isNWjs ) {
29+
// If we're building for electron or NWjs, we're unable to require the
30+
// built library so we have to just assume success, unfortunately.
31+
return Promise.resolve();
32+
}
33+
34+
return exec( 'node "' + path.join( rootPath, 'lib/Curl.js"' ) )
35+
.catch( function( e ) {
36+
if ( ~e.toString().indexOf( 'Module version mismatch' ) ) {
37+
log.warn(
38+
'', 'NodeGit was built for a different version of node.'
39+
);
40+
log.warn(
41+
'', 'If you are building NodeGit for electron/nwjs you can ignore this warning.'
42+
);
43+
} else {
44+
throw e;
45+
}
46+
}).then( function() {
47+
// Is we're using node-libcurl from a package manager then let's clean up after
48+
// ourselves when we install successfully.
49+
if ( !buildFlags.mustBuild ) {
50+
// We can't remove the source files yet because apparently the
51+
// "standard workflow" for native node modules in Electron/nwjs is to
52+
// build them for node and then nah eff that noise let's rebuild them
53+
// again for the actual platform! Hurray!!! When that madness is dead
54+
// we can clean up the source which is a serious amount of data.
55+
// fse.removeSync(path.join(rootPath, "vendor"));
56+
// fse.removeSync(path.join(rootPath, "src"));
57+
// fse.removeSync(path.join(rootPath, "include"));
58+
fse.removeSync( path.join( rootPath, 'build' ) );
59+
if ( fse.pathExists( path.join( rootPath, 'curl-for-windows' ) ) ) {
60+
fse.removeSync( path.join( rootPath, 'curl-for-windows' ) );
61+
}
62+
}
63+
});
64+
};
65+
66+
// Called on the command line
67+
if ( require.main === module ) {
68+
module.exports()
69+
.catch( function( e ) {
70+
log.warn( '', 'Could not finish postinstall' );
71+
72+
if ( process.platform === 'linux' && ~e.toString().indexOf( 'libstdc++' ) ) {
73+
printStandardLibError();
74+
} else {
75+
log.error( e );
76+
}
77+
});
78+
}

0 commit comments

Comments
 (0)