@jnxplus/nx-maven
Version:
[](https://badge.fury.io/js/@jnxplus%2Fnx-maven)
204 lines • 8.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.initGenerator = initGenerator;
const tslib_1 = require("tslib");
const common_1 = require("@jnxplus/common");
const devkit_1 = require("@nx/devkit");
const path = require("path");
function normalizeOptions(tree, options) {
const dot = '.';
return Object.assign(Object.assign({}, options), { dot,
springBootVersion: common_1.springBootVersion,
quarkusVersion: common_1.quarkusVersion,
micronautVersion: common_1.micronautVersion });
}
function addFiles(tree, options) {
const templateOptions = Object.assign(Object.assign({}, options), { offsetFromRoot: (0, devkit_1.offsetFromRoot)(tree.root), template: '' });
if (!options.skipWrapper) {
(0, devkit_1.generateFiles)(tree, path.join(__dirname, 'files', 'maven', 'wrapper'), options.mavenRootDirectory, templateOptions);
}
(0, devkit_1.generateFiles)(tree, path.join(__dirname, 'files', 'maven', 'config'), options.mavenRootDirectory, templateOptions);
}
exports.default = initGenerator;
function initGenerator(tree, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const nxMavenVersion = (0, common_1.getPluginVersion)(__dirname);
devkit_1.logger.info(`Using version v${nxMavenVersion} of nx-maven`);
const normalizedOptions = normalizeOptions(tree, options);
if (options.mavenRootDirectory) {
const projectConfiguration = {
root: normalizedOptions.mavenRootDirectory,
targets: {
build: {
executor: '@jnxplus/nx-maven:run-task',
outputs: ['{options.outputDirLocalRepo}'],
options: {
task: 'install -N',
},
},
},
};
(0, devkit_1.addProjectConfiguration)(tree, normalizedOptions.aggregatorProjectName, projectConfiguration);
}
addFiles(tree, normalizedOptions);
updateNxJson(tree, normalizedOptions);
(0, common_1.updateNxJsonConfiguration)(tree);
updateGitIgnore(tree, normalizedOptions);
if (options.formatter === 'prettier') {
addPrettierToPackageJson(tree);
addOrUpdatePrettierRc(tree);
addOrUpdatePrettierIgnore(tree, normalizedOptions);
}
addOrUpdateGitattributes(tree);
if (!options.skipWrapper) {
tree.changePermissions((0, devkit_1.joinPathFragments)(normalizedOptions.mavenRootDirectory, 'mvnw'), '755');
tree.changePermissions((0, devkit_1.joinPathFragments)(normalizedOptions.mavenRootDirectory, 'mvnw.cmd'), '755');
}
if (!options.skipFormat) {
yield (0, devkit_1.formatFiles)(tree);
}
return () => {
(0, devkit_1.installPackagesTask)(tree);
};
});
}
function updateNxJson(tree, options) {
let pluginOptions = {};
if (options.mavenRootDirectory) {
pluginOptions = Object.assign(Object.assign({}, pluginOptions), { mavenRootDirectory: options.mavenRootDirectory });
}
if (options.localRepoRelativePath) {
pluginOptions = Object.assign(Object.assign({}, pluginOptions), { localRepoRelativePath: options.localRepoRelativePath });
}
if (options.buildTargetName && options.buildTargetName !== 'build') {
pluginOptions = Object.assign(Object.assign({}, pluginOptions), { buildTargetName: options.buildTargetName });
}
let plugin;
if (pluginOptions) {
plugin = {
plugin: '@jnxplus/nx-maven',
options: pluginOptions,
};
}
else {
plugin = '@jnxplus/nx-maven';
}
(0, devkit_1.updateJson)(tree, 'nx.json', (nxJson) => {
var _a;
nxJson.targetDefaults = Object.assign(Object.assign({}, nxJson.targetDefaults), { [options.buildTargetName]: {
cache: true,
dependsOn: [`^${options.buildTargetName}`],
inputs: ['production', '^production'],
} });
// if plugins is undefined, set it to an empty array
nxJson.plugins = (_a = nxJson.plugins) !== null && _a !== void 0 ? _a : [];
// add plugin
nxJson.plugins.push(plugin);
// return modified JSON object
return nxJson;
});
}
function updateGitIgnore(tree, options) {
var _a;
const filePath = '.gitignore';
const contents = (_a = tree.read(filePath, 'utf-8')) !== null && _a !== void 0 ? _a : '';
const mavenIgnores = [
'\n',
'\n# Maven',
'\ntarget/',
'\n!**/src/main/**/target/',
'\n!**/src/test/**/target/',
];
if (!options.skipWrapper) {
mavenIgnores.push('\n!.mvn/wrapper/maven-wrapper.jar');
}
if (options.localRepoRelativePath) {
if (options.mavenRootDirectory) {
mavenIgnores.push(`\n${options.mavenRootDirectory}/${options.localRepoRelativePath}`);
}
else {
mavenIgnores.push(`\n${options.localRepoRelativePath}`);
}
}
const newContents = contents.concat(mavenIgnores.join(''));
tree.write(filePath, newContents);
}
function addPrettierToPackageJson(tree) {
(0, devkit_1.addDependenciesToPackageJson)(tree, {}, {
prettier: common_1.prettierVersion,
'@prettier/plugin-xml': common_1.prettierPluginXmlVersion,
'prettier-plugin-java': common_1.prettierPluginJavaVersion,
});
}
function addOrUpdatePrettierRc(tree) {
const prettierRcPath = '.prettierrc';
if (tree.exists(prettierRcPath)) {
(0, devkit_1.updateJson)(tree, prettierRcPath, (prettierRcJson) => {
var _a;
prettierRcJson.xmlWhitespaceSensitivity = 'ignore';
prettierRcJson.plugins = (_a = prettierRcJson.plugins) !== null && _a !== void 0 ? _a : [];
if (!prettierRcJson.plugins.includes('@prettier/plugin-xml')) {
prettierRcJson.plugins.push('@prettier/plugin-xml');
}
if (!prettierRcJson.plugins.includes('prettier-plugin-java')) {
prettierRcJson.plugins.push('prettier-plugin-java');
}
// return modified JSON object
return prettierRcJson;
});
}
else if (common_1.prettierrcNameOptions.every((name) => !tree.exists(name))) {
(0, devkit_1.writeJson)(tree, prettierRcPath, {
xmlWhitespaceSensitivity: 'ignore',
plugins: ['@prettier/plugin-xml', 'prettier-plugin-java'],
});
}
else {
devkit_1.logger.warn('Please add xmlWhitespaceSensitivity with ignore value, @prettier/plugin-xml and prettier-plugin-java plugins to your prettier config file');
}
}
function addOrUpdatePrettierIgnore(tree, options) {
var _a;
const prettierIgnorePath = '.prettierignore';
const mavenPrettierIgnores = ['# Maven target', '\ntarget/'];
if (options.localRepoRelativePath) {
if (options.mavenRootDirectory) {
mavenPrettierIgnores.push(`\n${options.mavenRootDirectory}/${options.localRepoRelativePath}`);
}
else {
mavenPrettierIgnores.push(`\n${options.localRepoRelativePath}`);
}
}
if (tree.exists(prettierIgnorePath)) {
const prettierIgnoreOldContent = (_a = tree.read(prettierIgnorePath, 'utf-8')) !== null && _a !== void 0 ? _a : '';
mavenPrettierIgnores.unshift('\n\n');
const prettierIgnoreContent = prettierIgnoreOldContent.concat(mavenPrettierIgnores.join(''));
tree.write(prettierIgnorePath, prettierIgnoreContent);
}
else {
tree.write(prettierIgnorePath, mavenPrettierIgnores.join(''));
}
}
function addOrUpdateGitattributes(tree) {
var _a;
const gitattributesPath = '.gitattributes';
const attributes = [
'#',
'\n# https://help.github.com/articles/dealing-with-line-endings/',
'\n#',
'\n# Linux start script should use lf',
'\nmvnw text eol=lf',
'\n# Windows script files should use crlf',
'\nmvnw.cmd text eol=crlf',
];
if (tree.exists(gitattributesPath)) {
const gitattributesOldContent = (_a = tree.read(gitattributesPath, 'utf-8')) !== null && _a !== void 0 ? _a : '';
attributes.unshift('\n\n');
const gitattributesContent = gitattributesOldContent.concat(attributes.join(''));
tree.write(gitattributesPath, gitattributesContent);
}
else {
tree.write(gitattributesPath, attributes.join(''));
}
}
//# sourceMappingURL=generator.js.map