Skip to content

Commit

Permalink
Merge pull request #66 from artemv/custom-commands
Browse files Browse the repository at this point in the history
tweaks & fixes re custom commands
  • Loading branch information
artemv authored Feb 28, 2018
2 parents d376dda + fc8220b commit e15522d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 70 deletions.
63 changes: 31 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ overriden on project level as in case of "project-c" here.
### Execution flow overview
Dont-break performs folowing steps for each dependent project:
* Install the dependent project into temporary dir using the [specified command](#install-command)
* Install the dependent project's dependencies using the [specified command](#install-command)
* [Pre-test](#pre-testing-with-previous-package-version) it if this is not disabled
* Run [post-install](#post-install-command) command if [pre-test](#pre-testing-with-previous-package-version) is not disabled
* [Pre-test](#pre-testing-with-previous-package-version) the dependent project if this is not disabled
* [Install current module](#current-module-installation-method) into the dependent project
* Run [post-install](#post-install-command) command if needed
* Run [post-install](#post-install-command) command
* [Test](#test-command) the dependent project

Sections below describe how you can customize these steps.
Expand All @@ -182,30 +182,43 @@ but default command for module `bar-name`, list in `.dont-break.json` the follow
### Install command

You can specify a custom install command per dependent module. By default it's `npm install`. For example, this will use
`yarn` for `foo-module-name`, but keep default `npm install` for module `bar-name`:
`yarn add` for `foo-module-name`, but keep default `npm install` for module `bar-name`:
```
[
{
"name": "foo-module-name",
"install": "yarn"
"install": "yarn add"
},
"bar-name"
]
```
This command is used in 2 steps: first, when dependent project is installed to temporary dir
(e.g. `yarn add my-module`), and second, when dependent project's dependencies are installed (e.g. `yarn`). If you use
smth else than yarn or npm, customize the 'add' part of `yarn add my-module` version with "installAddWord" option like
this:
The name of dependent module will be added to given command, e.g. for above it will run `yarn add foo-module-name`.

### Post-install command

Before testing the dependent package dont-break installs its dev dependencies via `npm install` command run from the
dependency directory. If you need something more you can specify it via "postinstall" config parameter like this:
```
[
{
"name": "foo-module-name",
"install": "npm-install-retry --wait 500 --attempts 10",
"installAddWord": "--"
"name": "packageA",
"postinstall": "npm run update"
}, {
"name": "packageB"
}
]
```
If specified this command will run first before pretesting the old version of lib (if pretest isn't disabled), then
after installing current version of lib to dependent package. You can use $CURRENT_MODULE_DIR variable here which
will be replaced with a path to current module:
```
[
{
"name": "packageA",
"postinstall": "$CURRENT_MODULE_DIR/install-all-deps.sh",
}
]
```
This will result in `npm-install-retry --wait 500 --attempts 10 -- my-module` for first step.

### Pre-testing with previous package version
By default dont-break first tests dependent module with its published version of current module, to make sure that it
Expand Down Expand Up @@ -247,26 +260,12 @@ specify {"currentModuleInstall": "npm-link"}:
"projects": ["packageA", "packageB"]
}
```
Default value is {"currentModuleInstall": "npm-install"}.

### Post-install command
Default value is {"currentModuleInstall": "npm-install"}.

Before testing the dependent package dont-break installs its dev dependencies via `npm install` command run from the
dependency directory. If you need something more you can specify it via "postinstall" config parameter like this:
```
[
{
"name": "packageA",
"postinstall": "npm run update",
"test": "dont-break-tests-with-my-package.sh"
}, {
"name": "packageB",
"test": "dont-break-tests-with-my-package.sh"
}
]
```
If specified this command will run first before pretesting the old version of lib (if pretest isn't disabled), then
after installing current version of lib to dependent package.
### Env vars exported to called scripts
Following env vars are available for use in scripts called by executed steps:
* `$CURRENT_MODULE_DIR` - directory of current module
* `$CURRENT_MODULE_NAME` - name of current module as stated in its package.json

### Installation timeout
You can specify a longer installation time out, in seconds, using CLI option
Expand Down
55 changes: 19 additions & 36 deletions src/dont-break.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ function installCurrentModuleToDependent (sourceFolder, dependentFolder, current
debug('testing the current module in %s', dependentFolder)
debug('current module folder %s', sourceFolder)

if (_.indexOf(['npm-link', 'yarn-link'], currentModuleInstallMethod) >= 0) {
if (currentModuleInstallMethod === 'npm-install') {
return install({ prefix: dependentFolder, name: sourceFolder })
.then(function () {
return dependentFolder
})
} else {
var pkgName = currentPackageName()
var linkCmd = currentModuleInstallMethod.replace('-', ' ')
return linkCurrentModule(sourceFolder, linkCmd)
Expand All @@ -154,11 +159,6 @@ function installCurrentModuleToDependent (sourceFolder, dependentFolder, current
.then(function () {
return dependentFolder
})
} else {
return install({ prefix: dependentFolder, name: sourceFolder })
.then(function () {
return dependentFolder
})
}
}

Expand All @@ -182,14 +182,15 @@ function getDependentVersion (pkg, name) {
}
}

function postInstallInFolder (command, folder) {
function postInstallInFolder (dependentFolder, command, sourceFolder) {
if (command) {
return runInFolder(folder, command, {
command = command.replace('$CURRENT_MODULE_DIR', sourceFolder)
return runInFolder(dependentFolder, command, {
success: 'postinstall succeeded',
failure: 'postinstall did not work'
})
} else {
return folder
return dependentFolder
}
}

Expand All @@ -204,15 +205,10 @@ function testDependent (options, dependent, config) {

dependent = Object.assign({pretest: true, currentModuleInstall: 'npm-install'}, config, dependent)
moduleTestCommand = dependent.test
modulePostinstallCommand = dependent.postinstall
modulePostinstallCommand = dependent.postinstall || 'npm install'
testWithPreviousVersion = dependent.pretest
currentModuleInstallMethod = dependent.currentModuleInstall
var dependentInstall = dependent.install
var installAddWord = dependent.installAddWord
if (dependentInstall === 'yarn') {
installAddWord = installAddWord || 'add'
}
installAddWord = installAddWord || ''

dependent = dependent.name

Expand All @@ -226,12 +222,7 @@ function testDependent (options, dependent, config) {
// simple repo installation
return toFolder
} else {
// it was NPM install
var parts = [toFolder]
if (!dependentInstall) {
parts.push('lib')
}
return join.apply(null, parts.concat(['node_modules', moduleName]))
return join(toFolder, 'node_modules', moduleName)
}
}

Expand All @@ -242,7 +233,11 @@ function testDependent (options, dependent, config) {
moduleTestCommand = moduleTestCommand || DEFAULT_TEST_COMMAND
var testModuleInFolder = _.partial(testInFolder, moduleTestCommand)

var pkg = require(join(process.cwd(), 'package.json'))
var cwd = process.cwd()
var pkg = require(join(cwd, 'package.json'))
process.env.CURRENT_MODULE_NAME = pkg.name
process.env.CURRENT_MODULE_DIR = cwd

var depName = pkg.name + '-v' + pkg.version + '-against-' + moduleName
var safeName = _.kebabCase(_.deburr(depName))
debug('original name "%s", safe "%s"', depName, safeName)
Expand All @@ -255,13 +250,11 @@ function testDependent (options, dependent, config) {
var installOptions = {
name: moduleName,
prefix: toFolder,
cmd: dependentInstall,
installAddWord: installAddWord
cmd: dependentInstall
}

var postInstallModuleInFolder = _.partial(postInstallInFolder, modulePostinstallCommand)
var postInstallModuleInFolder = _.partialRight(postInstallInFolder, modulePostinstallCommand, cwd)

var cwd = process.cwd()
var res = install(installOptions)
.timeout(timeoutSeconds * 1000, 'install timed out for ' + moduleName)
.then(formFullFolderName)
Expand All @@ -283,16 +276,6 @@ function testDependent (options, dependent, config) {
'\nwill test', pkg.name + '@' + pkg.version)
return folder
})
.then(function installDependencies (folder) {
console.log('installing dev dependencies', folder)
return install({cmd: installOptions.cmd, prefix: folder}).then(function () {
return folder
}, function (err) {
console.error('Could not install dependencies in', folder)
console.error(err)
throw err
})
})

if (testWithPreviousVersion) {
var modulePretestCommand
Expand Down
3 changes: 1 addition & 2 deletions src/install-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ function install (options) {
mkdirp.sync(options.prefix)
}
var cmd = options.cmd || 'npm install'
options.installAddWord = options.installAddWord || ''
if (options.name) {
cmd = `${cmd} ${options.installAddWord} ${options.name}`
cmd = `${cmd} ${options.name}`
}
console.log('running "%s" install command in %s', cmd, options.prefix)
return runInFolder(options.prefix, cmd, {
Expand Down
1 change: 1 addition & 0 deletions src/run-in-folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function runInFolder (folder, command, options) {

return chdir.to(folder)
.then(function () {
console.log(`running "${command}" from ${folder}`)
return npmTest(command)
})
.then(function () {
Expand Down

0 comments on commit e15522d

Please sign in to comment.