-
-
Notifications
You must be signed in to change notification settings - Fork 21
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 #618 from hrkt/add-package-manager-option
Add package-manager option
- Loading branch information
Showing
11 changed files
with
170 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict' | ||
|
||
const child = require('child_process') | ||
const debug = require('debug')('electron-packager') | ||
|
||
const knownPackageManagers = ['npm', 'cnpm', 'yarn'] | ||
|
||
function pruneCommand (packageManager) { | ||
switch (packageManager) { | ||
case 'npm': | ||
case 'cnpm': | ||
return `${packageManager} prune --production` | ||
case 'yarn': | ||
return `${packageManager} install --production` | ||
} | ||
} | ||
|
||
function pruneModules (opts, appPath, cb) { | ||
const packageManager = opts.packageManager || 'npm' | ||
|
||
if (packageManager === 'cnpm' && process.platform === 'win32') { | ||
return cb(new Error('cnpm support does not currently work with Windows, see: https://github.com/electron-userland/electron-packager/issues/515#issuecomment-297604044')) | ||
} | ||
|
||
const command = pruneCommand(packageManager) | ||
|
||
if (command) { | ||
debug(`Pruning modules via: ${command}`) | ||
child.exec(command, { cwd: appPath }, cb) | ||
} else { | ||
cb(new Error(`Unknown package manager "${packageManager}". Known package managers: ${knownPackageManagers.join(', ')}`)) | ||
} | ||
} | ||
|
||
module.exports = { | ||
pruneCommand: pruneCommand, | ||
pruneModules: pruneModules | ||
} |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
'use strict' | ||
|
||
const config = require('./config.json') | ||
const fs = require('fs-extra') | ||
const packager = require('..') | ||
const path = require('path') | ||
const prune = require('../prune') | ||
const test = require('tape') | ||
const util = require('./util') | ||
const waterfall = require('run-waterfall') | ||
const which = require('which') | ||
|
||
function createPruneOptionTest (baseOpts, prune, testMessage) { | ||
return (t) => { | ||
t.timeoutAfter(config.timeout) | ||
|
||
let opts = Object.create(baseOpts) | ||
opts.name = 'basicTest' | ||
opts.dir = path.join(__dirname, 'fixtures', 'basic') | ||
opts.prune = prune | ||
|
||
let finalPath | ||
let resourcesPath | ||
|
||
waterfall([ | ||
(cb) => { | ||
packager(opts, cb) | ||
}, (paths, cb) => { | ||
finalPath = paths[0] | ||
fs.stat(finalPath, cb) | ||
}, (stats, cb) => { | ||
t.true(stats.isDirectory(), 'The expected output directory should exist') | ||
resourcesPath = path.join(finalPath, util.generateResourcesPath(opts)) | ||
fs.stat(resourcesPath, cb) | ||
}, (stats, cb) => { | ||
t.true(stats.isDirectory(), 'The output directory should contain the expected resources subdirectory') | ||
fs.stat(path.join(resourcesPath, 'app', 'node_modules', 'run-series'), cb) | ||
}, (stats, cb) => { | ||
t.true(stats.isDirectory(), 'package.json dependency should exist under app/node_modules') | ||
fs.exists(path.join(resourcesPath, 'app', 'node_modules', 'run-waterfall'), (exists) => { | ||
t.equal(!prune, exists, testMessage) | ||
cb() | ||
}) | ||
} | ||
], (err) => { | ||
t.end(err) | ||
}) | ||
} | ||
} | ||
|
||
test('pruneCommand returns the correct command when passing a known package manager', (t) => { | ||
t.equal(prune.pruneCommand('npm'), 'npm prune --production', 'passing npm gives the npm prune command') | ||
t.equal(prune.pruneCommand('cnpm'), 'cnpm prune --production', 'passing cnpm gives the cnpm prune command') | ||
t.equal(prune.pruneCommand('yarn'), 'yarn install --production', 'passing yarn gives the yarn "prune command"') | ||
t.end() | ||
}) | ||
|
||
test('pruneCommand returns null when the package manager is unknown', (t) => { | ||
t.notOk(prune.pruneCommand('unknown-package-manager')) | ||
t.end() | ||
}) | ||
|
||
test('pruneModules returns an error when the package manager is unknown', (t) => { | ||
prune.pruneModules({packageManager: 'unknown-package-manager'}, '/tmp/app-path', (err) => { | ||
t.ok(err, 'error returned') | ||
t.end() | ||
}) | ||
}) | ||
|
||
if (process.platform === 'win32') { | ||
test('pruneModules returns an error when trying to use cnpm on Windows', (t) => { | ||
prune.pruneModules({packageManager: 'cnpm'}, '/tmp/app-path', (err) => { | ||
t.ok(err, 'error returned') | ||
t.end() | ||
}) | ||
}) | ||
} | ||
|
||
// This is not in the below loop so that it tests the default packageManager option. | ||
util.testSinglePlatform('prune test with npm', (baseOpts) => { | ||
return createPruneOptionTest(baseOpts, true, 'package.json devDependency should NOT exist under app/node_modules') | ||
}) | ||
|
||
for (const packageManager of ['cnpm', 'yarn']) { | ||
which(packageManager, (err, resolvedPath) => { | ||
if (err) return | ||
|
||
util.testSinglePlatform(`prune test with ${packageManager}`, (baseOpts) => { | ||
const opts = Object.assign({packageManager: packageManager}, baseOpts) | ||
return createPruneOptionTest(opts, true, 'package.json devDependency should NOT exist under app/node_modules') | ||
}) | ||
}) | ||
} | ||
|
||
util.testSinglePlatform('prune=false test', (baseOpts) => { | ||
return createPruneOptionTest(baseOpts, false, 'npm devDependency should exist under app/node_modules') | ||
}) |
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