Skip to content

Commit

Permalink
Merge pull request #67 from artemv/custom-commands
Browse files Browse the repository at this point in the history
feat: allow to use `yarn add folder` as currentModuleInstall
  • Loading branch information
artemv authored Mar 21, 2018
2 parents c2aecc7 + a00ce9c commit 2497c41
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 47 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,16 @@ By default it equals to "test" command.
### Current module installation method

To test dependent package dont-break installs current module inside the dependent package directory. By default it uses
`cd $DEPENDENT_PACKAGE_DIR && npm install $CURRENT_MODULE_DIR`. Other options are
[npm-link](https://docs.npmjs.com/cli/link) and [yarn-link](https://yarnpkg.com/lang/en/docs/cli/link/). They can be
helpful in some cases, e.g. if you need to use `npm install` or `yarn` in postinstall command. To use `npm link` method
specify {"currentModuleInstall": "npm-link"}:
`npm install $CURRENT_MODULE_DIR`. You can enter your command there, e.g. `yarn add $CURRENT_MODULE_DIR`. There are
also pre-configured option options [npm-link](https://docs.npmjs.com/cli/link) and
[yarn-link](https://yarnpkg.com/lang/en/docs/cli/link/). They can be helpful in some cases, e.g. if you need to use
`npm install` or `yarn` in postinstall command. To use `npm link` method specify {"currentModuleInstall": "npm-link"}:
```
{
"currentModuleInstall": "npm-link",
"projects": ["packageA", "packageB"]
}
```
Default value is {"currentModuleInstall": "npm-install"}.

### Env vars exported to called scripts
Following env vars are available for use in scripts called by executed steps:
Expand Down
91 changes: 49 additions & 42 deletions src/dont-break.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,34 +134,6 @@ var linkCurrentModule = _.memoize(function (thisFolder, linkCmd) {
})
})

function installCurrentModuleToDependent (sourceFolder, dependentFolder, currentModuleInstallMethod) {
la(check.unemptyString(dependentFolder), 'expected dependent folder', dependentFolder)

debug('testing the current module in %s', dependentFolder)
debug('current module folder %s', sourceFolder)

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)
.then(function () {
return runInFolder(dependentFolder, `${linkCmd} ${pkgName}`, {
success: `linked ${pkgName}`,
failure: `linking ${pkgName} failed`
})
})
.finally(chdir.from)
.then(function () {
return dependentFolder
})
}
}

function getDependencyName (dependent) {
if (isRepoUrl(dependent)) {
debug('dependent is git repo url %s', dependent)
Expand All @@ -182,18 +154,6 @@ function getDependentVersion (pkg, name) {
}
}

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

function testDependent (options, dependent, config) {
var moduleTestCommand
var modulePostinstallCommand
Expand All @@ -203,7 +163,7 @@ function testDependent (options, dependent, config) {
dependent = {name: dependent.trim()}
}

dependent = Object.assign({pretest: true, currentModuleInstall: 'npm-install'}, config, dependent)
dependent = Object.assign({pretest: true, currentModuleInstall: 'npm install $CURRENT_MODULE_DIR'}, config, dependent)
moduleTestCommand = dependent.test
modulePostinstallCommand = dependent.postinstall || 'npm install'
testWithPreviousVersion = dependent.pretest
Expand Down Expand Up @@ -238,6 +198,53 @@ function testDependent (options, dependent, config) {
process.env.CURRENT_MODULE_NAME = pkg.name
process.env.CURRENT_MODULE_DIR = cwd

function expandCommandVars (command) {
command = command.replace('$CURRENT_MODULE_DIR', cwd)
command = command.replace('$CURRENT_MODULE_NAME', pkg.name)
return command
}

function postInstallInFolder (dependentFolder, command) {
if (command) {
command = expandCommandVars(command)
return runInFolder(dependentFolder, command, {
success: 'postinstall succeeded',
failure: 'postinstall did not work'
})
} else {
return dependentFolder
}
}

function installCurrentModuleToDependent (sourceFolder, dependentFolder, currentModuleInstallMethod) {
la(check.unemptyString(dependentFolder), 'expected dependent folder', dependentFolder)

debug('testing the current module in %s', dependentFolder)
debug('current module folder %s', sourceFolder)

var pkgName = currentPackageName()
if (_.includes(['yarn-link', 'npm-link'], currentModuleInstallMethod)) {
var linkCmd = currentModuleInstallMethod.replace('-', ' ')
return linkCurrentModule(sourceFolder, linkCmd)
.then(function () {
return runInFolder(dependentFolder, `${linkCmd} ${pkgName}`, {
success: `linked ${pkgName}`,
failure: `linking ${pkgName} failed`
})
})
.finally(chdir.from)
.then(function () {
return dependentFolder
})
} else {
currentModuleInstallMethod = expandCommandVars(currentModuleInstallMethod)
return runInFolder(dependentFolder, `${currentModuleInstallMethod}`, {
success: `installed ${pkgName}`,
failure: `installing ${pkgName} failed`
})
}
}

var depName = pkg.name + '-v' + pkg.version + '-against-' + moduleName
var safeName = _.kebabCase(_.deburr(depName))
debug('original name "%s", safe "%s"', depName, safeName)
Expand All @@ -253,7 +260,7 @@ function testDependent (options, dependent, config) {
cmd: dependentInstall
}

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

var res = install(installOptions)
.timeout(timeoutSeconds * 1000, 'install timed out for ' + moduleName)
Expand Down

0 comments on commit 2497c41

Please sign in to comment.