2016-08-22 5 views
3

Я пытаюсь настроить кеширование пакетов npm. Это моя конфигурация плагина:Конфигурация nodeModulesDir в модуле gradle-node-plugin

node { 
    version = '4.5.0' 
    npmVersion = '3.10.6' 
    distBaseUrl = 'https://nodejs.org/dist' 
    download = true 

    workDir = file("$webAppSourceAbsolute/nodejs") 

    nodeModulesDir = file("$webAppSourceAbsolute/") 
} 

И это моя задача:

task npmCacheConfig(type: NpmTask) { 
    description = "Configure the NPM cache" 
    outputs.upToDateWhen { 
    false 
    } 
    def npmCacheDir = "${gradle.getGradleUserHomeDir()}/caches/npm" 
    outputs.files file(npmCacheDir) 
    args = ['config', 'set', 'cache', npmCacheDir] 
} 

Но когда я запускаю эту задачу, я получил ошибку:

:arm-bpa:nodeSetup UP-TO-DATE 
:arm-bpa:npmCacheConfig FAILED 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':myModule:npmCacheConfig'. 
> 
    Could not run npm command - local npm not found but requested in gradle node configuration. 
    A common reason for this is an npm-shrinkwrap.json file is present and un-installs npm. 
    To resolve this, add npm with version '3.10.6' to your package.json. 

я могу исправить это путь:

npmCacheConfig.doFirst { 
    this.project.node.nodeModulesDir = file("$webAppSourceAbsolute/nodejs/node-v4.5.0-linux-x64/lib/") 
} 

npmCacheConfig.doLast { 
    this.project.node.nodeModulesDir = file("$webAppSourceAbsolute/") 
} 

Есть ли способ исправить i t без этого hardcoding?

ответ

0

Я установил это так:

npmCacheConfig.doFirst { 
    def nodeJsDirectory = null 
    def nodeVersion = this.project.node.version 

    file("$webAppSourceAbsolute/nodejs/").traverse(
      type: FileType.DIRECTORIES, 
      nameFilter: ~"^node-v$nodeVersion.*", 
      postDir: { 
       return FileVisitResult.TERMINATE 
      } 
    ) { 
     nodeJsDirectory = it 
    } 

    if (nodeJsDirectory != null) { 
     this.project.node.nodeModulesDir = file("$nodeJsDirectory/lib/") 
    } else { 
     throw new IllegalStateException("nodejs is not installed") 
    } 
} 


npmCacheConfig.doLast { 
    this.project.node.nodeModulesDir = file("$webAppSourceAbsolute/") 
}