@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
53 lines (45 loc) • 1.87 kB
JavaScript
const os = require('os')
const cds = require('../../../cds')
const { exists, copy, rimraf, fs:{promises:{ mkdtemp }}, path } = cds.utils, { join } = path
const cmd = require('../../../util/command')
const mvn = require('../../mvn')
const { bold, info, link } = require('../../../util/term')
const { URLS, OPTIONS: { NODEJS, JAVA }, COMMAND_ADD } = require('../../constants')
const { command, options } = cds.cli
module.exports = class JavaTemplate extends require('../../plugin') {
static help() {
return 'creates a Java-based project'
}
static hasInProduction() {
return exists('pom.xml') || options?.add?.has(JAVA)
}
async canRun() {
if (command === COMMAND_ADD) {
throw `You can't change the runtime of an existing project.`
}
if (options.add?.has(NODEJS)) {
throw `only one runtime per project is supported – specify either ${bold(JAVA)} or ${bold(NODEJS)}`
}
return true
}
async run() {
const projectName = path.basename(cds.root)
const { params, artifactId, archetypeVersion } = await mvn.init(projectName)
console.log(`using Maven archetype version ${archetypeVersion}`)
const temp = await mkdtemp(join(os.tmpdir(), `${path.basename(cds.root)}_`))
try {
await cmd.spawnCommand('mvn', params, { cwd: temp })
await copy(join(temp, artifactId)).to(cds.root)
} catch (err) {
if (err.code === 'ENOENT' && err.path === 'mvn') {
throw `Maven executable 'mvn' not found. Follow ${info(URLS.MAVEN_INSTALL_HELP)} and install Maven on your machine.`
}
throw err
} finally {
await rimraf(temp)
}
}
async finalize() {
console.log(`learn about next steps at ${link(URLS.CAPIRE)}`)
}
}