-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #819 from electron-userland/prune-via-js-only
Drop packageManager support & refactor pruning to be part of the copy filter
- Loading branch information
Showing
16 changed files
with
178 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,69 @@ | ||
'use strict' | ||
|
||
const child = require('mz/child_process') | ||
const debug = require('debug')('electron-packager') | ||
const Walker = require('pruner').Walker | ||
|
||
const knownPackageManagers = ['npm', 'cnpm', 'yarn'] | ||
|
||
function pruneCommand (packageManager) { | ||
switch (packageManager) { | ||
case 'npm': | ||
case 'cnpm': | ||
return `${packageManager} prune --production` | ||
case 'yarn': | ||
return `${packageManager} install --production --no-bin-links --no-lockfile` | ||
const common = require('./common') | ||
const galactus = require('galactus') | ||
const fs = require('fs-extra') | ||
const path = require('path') | ||
|
||
const ELECTRON_MODULES = [ | ||
'electron', | ||
'electron-prebuilt', | ||
'electron-prebuilt-compile' | ||
] | ||
|
||
class Pruner { | ||
constructor (dir) { | ||
this.baseDir = common.normalizePath(dir) | ||
this.galactus = new galactus.DestroyerOfModules({ | ||
rootDirectory: dir, | ||
shouldKeepModuleTest: (module, isDevDep) => this.shouldKeepModule(module, isDevDep) | ||
}) | ||
this.walkedTree = false | ||
} | ||
} | ||
|
||
function pruneModules (opts, appPath) { | ||
if (opts.packageManager === false) { | ||
const walker = new Walker(appPath) | ||
return walker.prune() | ||
} else { | ||
const packageManager = opts.packageManager || 'npm' | ||
setModuleMap (moduleMap) { | ||
this.moduleMap = new Map() | ||
// destructured assignments are in Node 6 | ||
for (const modulePair of moduleMap) { | ||
const modulePath = modulePair[0] | ||
const module = modulePair[1] | ||
this.moduleMap.set(`/${common.normalizePath(modulePath)}`, module) | ||
} | ||
this.walkedTree = true | ||
} | ||
|
||
/* istanbul ignore if */ | ||
if (packageManager === 'cnpm' && process.platform === 'win32') { | ||
return Promise.reject(new Error('cnpm support does not currently work with Windows, see: https://github.com/electron-userland/electron-packager/issues/515#issuecomment-297604044')) | ||
pruneModule (name) { | ||
if (this.walkedTree) { | ||
return this.isProductionModule(name) | ||
} else { | ||
return this.galactus.collectKeptModules({ relativePaths: true }) | ||
.then(moduleMap => this.setModuleMap(moduleMap)) | ||
.then(() => this.isProductionModule(name)) | ||
} | ||
} | ||
|
||
const command = pruneCommand(packageManager) | ||
shouldKeepModule (module, isDevDep) { | ||
if (isDevDep || module.depType === galactus.DepType.ROOT) { | ||
return false | ||
} | ||
|
||
if (command) { | ||
debug(`Pruning modules via: ${command}`) | ||
return child.exec(command, { cwd: appPath }) | ||
} else { | ||
return Promise.reject(new Error(`Unknown package manager "${packageManager}". Known package managers: ${knownPackageManagers.join(', ')}`)) | ||
// Node 6 has Array.prototype.includes | ||
if (ELECTRON_MODULES.indexOf(module.name) !== -1) { | ||
common.warning(`Found '${module.name}' but not as a devDependency, pruning anyway`) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
isProductionModule (name) { | ||
return !!this.moduleMap.get(name) | ||
} | ||
} | ||
|
||
module.exports = { | ||
pruneCommand: pruneCommand, | ||
pruneModules: pruneModules | ||
isModule: function isModule (pathToCheck) { | ||
return fs.pathExists(path.join(pathToCheck, 'package.json')) | ||
}, | ||
Pruner: Pruner | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,9 @@ case "$TRAVIS_OS_NAME" in | |
# Not using Trusty containers because it can't install wine1.6(-i386), | ||
# see: https://github.com/travis-ci/travis-ci/issues/6460 | ||
sudo rm /etc/apt/sources.list.d/google-chrome.list | ||
sudo apt-key adv --fetch-keys http://dl.yarnpkg.com/debian/pubkey.gpg | ||
echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list | ||
sudo dpkg --add-architecture i386 | ||
sudo apt-get update | ||
sudo apt-get install -y wine1.6 yarn | ||
sudo apt-get install -y wine1.6 | ||
;; | ||
"osx") | ||
# Create CA | ||
|
@@ -31,9 +29,5 @@ case "$TRAVIS_OS_NAME" in | |
npm install [email protected] | ||
# Setup ~/.wine by running a command | ||
./node_modules/.bin/wine hostname | ||
# Install yarn | ||
npm install -g yarn | ||
;; | ||
esac | ||
|
||
npm install -g cnpm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body>Hello, world!</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
'use strict' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"main": "main.js", | ||
"productName": "MainJS", | ||
"description": "Removing electron from dependencies", | ||
"dependencies": { | ||
"electron": "1.3.1" | ||
} | ||
} |
Oops, something went wrong.