@liferay/generator-js
Version:
Yeoman generators for Liferay DXP and Portal CE JavaScript projects.
270 lines (265 loc) • 10.2 kB
JavaScript
"use strict";
/**
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const js_toolkit_core_1 = require("@liferay/js-toolkit-core");
const path_1 = __importDefault(require("path"));
const yargs_1 = require("yargs");
const yeoman_generator_1 = __importDefault(require("yeoman-generator"));
const utils_1 = require("../utils");
const ProjectAnalyzer_1 = __importDefault(require("../utils/ProjectAnalyzer"));
const Language_properties_1 = __importDefault(require("../utils/modifier/features/localization/Language.properties"));
const gitignore_1 = __importDefault(require("../utils/modifier/gitignore"));
const npmbuildrc_1 = __importDefault(require("../utils/modifier/npmbuildrc"));
const npmbundlerrc_1 = __importDefault(require("../utils/modifier/npmbundlerrc"));
const package_json_1 = __importDefault(require("../utils/modifier/package.json"));
const { error, info, print, success, title, warn } = js_toolkit_core_1.format;
const project = new js_toolkit_core_1.Project('.');
const msg = {
angularCliDetected: [
success `
We have detected a project of type {angular-cli}
`,
],
createReactAppDetected: [
success `
We have detected a project of type {create-react-app}
`,
],
projectAdapted: [
``,
success `
Your project has been successfully adapted to {Liferay JS Toolkit}.
`,
info `
See http://bit.ly/js-toolkit-adapt for the full list of {npm} scripts
that may be used in your newly adapted project.
Nevertheless, you can start with {'npm run deploy:liferay'} to deploy it
to your Liferay server.
`,
],
questions: `
It will be tuned accordingly, so that you can deploy it to your Liferay
server.
But first we need you to answer some customization questions...
`,
unsupportedProjectType: [
error `
Oops! Your project type is not supported by {Liferay JS Toolkit} or
cannot be autodetected.
`,
info `
Please visit http://bit.ly/js-toolkit-adapt for the full list of
supported project types and how they are detected.
`,
],
vueCliDetected: [
success `
We have detected a project of type {vue-cli}
`,
],
warnAboutOverwrite: [
'',
warn `
Now your project files will be modified. As a consequence, {Yeoman} may
notify you about the existence of a conflict and prompt for permission
to overwrite your files.
Make sure to answer {'a'} or otherwise the adaptation to {Liferay JS
Toolkit} will fail.
`,
info `
Note that you can also avoid this conflict warning providing the
{'--force'} argument to Yeoman.
`,
],
welcome: [
title `
|👋 |Welcome to Liferay JS Toolkit project adapter
`,
],
};
// If --which parameter is given show path to generator and exit
if (yargs_1.argv.which) {
// eslint-disable-next-line no-console
console.log(require.resolve('./index'));
process.exit(0);
}
/**
* Generator to add deploy support to projects.
*/
class default_1 extends yeoman_generator_1.default {
/**
* Standard Yeoman constructor
*/
constructor(args, opts) {
super(args, opts);
}
/**
* Standard Yeoman initialization function
*/
initializing() {
this.sourceRoot(path_1.default.join(__dirname, 'templates'));
print(msg.welcome);
switch (project.probe.type) {
case js_toolkit_core_1.ProjectType.ANGULAR_CLI:
this._options = {
preset: 'angular-cli',
tuneProject: () => this._tuneAngularCliProject(),
};
print(msg.angularCliDetected);
break;
case js_toolkit_core_1.ProjectType.CREATE_REACT_APP:
this._options = {
preset: 'create-react-app',
tuneProject: () => this._tuneCreateReactAppProject(),
};
print(msg.createReactAppDetected);
break;
case js_toolkit_core_1.ProjectType.VUE_CLI:
this._options = {
preset: 'vue-cli',
tuneProject: () => this._tuneVueCliProject(),
};
print(msg.vueCliDetected);
break;
default:
print(msg.unsupportedProjectType);
process.exit(1);
}
}
/**
* Standard Yeoman prompt function
*/
async prompting() {
print(msg.questions);
this.answers = {};
if (project.pkgManager === null) {
const answers = await utils_1.promptWithConfig(this, 'adapt', [
{
choices: [
{ name: 'npm', value: 'npm' },
{ name: 'yarn', value: 'yarn' },
],
default: 'npm',
message: 'Which package manager are you using for the project?',
name: 'pkgManager',
type: 'list',
},
]);
// Set project's package manager internally using a hack
// eslint-disable-next-line @typescript-eslint/no-explicit-any
project._pkgManager = answers['pkgManager'];
}
Object.assign(this.answers, await utils_1.promptWithConfig(this, 'adapt', [
{
default: 'category.sample',
message: 'Under which category should your widget be listed?',
name: 'category',
type: 'input',
},
]));
const answers = await utils_1.promptWithConfig(this, 'adapt', [
{
default: true,
message: 'Do you have a local installation of Liferay for development?',
name: 'liferayPresent',
type: 'confirm',
},
]);
if (!answers['liferayPresent']) {
return;
}
Object.assign(this.answers, await utils_1.promptWithConfig(this, 'adapt', [
{
default: '/liferay',
message: 'Where is your local installation of Liferay placed?',
name: 'liferayDir',
type: 'input',
validate: utils_1.validateLiferayDir,
},
]));
}
/**
* Standard Yeoman generation function
*/
writing() {
print(msg.warnAboutOverwrite);
this._copyTemplates();
this._modifyTemplates();
this._options.tuneProject();
}
/**
* Standard Yeoman install function
*/
install() {
print(msg.projectAdapted);
const opts = {
bower: false,
npm: false,
skipInstall: this.options['skip-install'],
skipMessage: this.options['skip-install-message'],
yarn: false,
};
opts[project.pkgManager] = true;
this.installDependencies(opts);
}
_copyTemplates() {
const cp = new utils_1.Copier(this);
cp.copyDir('.');
}
_modifyTemplates() {
const gitignore = new gitignore_1.default(this);
const languagePropertiesModifier = new Language_properties_1.default(this);
const npmbuildrc = new npmbuildrc_1.default(this);
const npmbundlerrc = new npmbundlerrc_1.default(this);
const pkgJson = new package_json_1.default(this, 2);
const projectAnalyzer = new ProjectAnalyzer_1.default(this);
const portletName = utils_1.getPortletName(projectAnalyzer);
const { preset } = this._options;
// Git ignore build.liferay directory
gitignore.add('build.liferay');
// Configure deploy target
if (this.answers.liferayDir) {
npmbuildrc.setLiferayDir(this.answers.liferayDir);
}
// Add JS toolkit dependencies
pkgJson.addDevDependency('liferay-npm-build-support', utils_1.getSDKVersion('liferay-npm-build-support', { ignoreConfig: true }));
pkgJson.addDevDependency('liferay-npm-bundler', utils_1.getSDKVersion('liferay-npm-bundler', { ignoreConfig: true }));
pkgJson.addDevDependency(`liferay-npm-bundler-preset-${preset}`, utils_1.getSDKVersion(`liferay-npm-bundler-preset-${preset}`, {
ignoreConfig: true,
}));
// Configure preset
npmbundlerrc.setPreset(`liferay-npm-bundler-preset-${preset}`);
// Add npm scripts
pkgJson.addScript('build:liferay', 'lnbs-build');
pkgJson.addScript('deploy:liferay', `${project.pkgManager} run build:liferay && lnbs-deploy`);
// Add portlet section
pkgJson.addPortletProperty('com.liferay.portlet.display-category', this.answers.category);
pkgJson.addPortletProperty('javax.portlet.name', portletName);
pkgJson.addPortletProperty('javax.portlet.security-role-ref', 'power-user,user');
pkgJson.addPortletProperty('javax.portlet.resource-bundle', 'content.Language');
// Add localization key for portlet name
languagePropertiesModifier.addProperty(`javax.portlet.title.${portletName}`, projectAnalyzer.displayName);
}
_tuneAngularCliProject() {
const pkgJson = new package_json_1.default(this, 2);
const projectAnalyzer = new ProjectAnalyzer_1.default(this);
pkgJson.addPortletProperty('com.liferay.portlet.instanceable', false);
pkgJson.addPortletProperty('com.liferay.portlet.header-portlet-css', `/${projectAnalyzer.name}/styles.css`);
}
_tuneCreateReactAppProject() {
const pkgJson = new package_json_1.default(this, 2);
pkgJson.addPortletProperty('com.liferay.portlet.instanceable', true);
}
_tuneVueCliProject() {
const pkgJson = new package_json_1.default(this, 2);
pkgJson.addPortletProperty('com.liferay.portlet.instanceable', true);
}
}
exports.default = default_1;
module.exports = exports['default'];