forked from liferay/liferay-js-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetaljs-portlet.ts
106 lines (90 loc) · 3.03 KB
/
metaljs-portlet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
import path from 'path';
import Generator from 'yeoman-generator';
import {Copier, promptWithConfig} from '../utils';
import ProjectAnalyzer from '../utils/ProjectAnalyzer';
import NpmbuildrcModifier from '../utils/modifier/npmbuildrc';
import NpmbundlerrcModifier from '../utils/modifier/npmbundlerrc';
import PkgJsonModifier from '../utils/modifier/package.json.js';
import * as standardTarget from '../utils/target/standard';
import dependenciesJson from './dependencies.json';
import importsJson from './imports.json';
/**
* Implementation of generation of Metal.js portlets.
*/
export default class extends Generator {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
answers: any;
namespace: string;
/**
* Standard Yeoman initialization function
*/
initializing() {
this.sourceRoot(path.join(__dirname, 'templates'));
this.namespace = 'target-metaljs-portlet';
}
/**
* Standard Yeoman prompt function
*/
async prompting() {
this.answers = await promptWithConfig(this, [
{
type: 'confirm',
name: 'importMetaljs',
message:
'Do you want to import Metal.js packages from Liferay DXP/Portal CE?\n' +
'\n' +
' 💡 If you answer yes Metal.js packages will be directly imported from the\n' +
' ones included in Liferay DXP/Portal CE instead of bundling them along\n' +
" this project's dependencies.\n" +
' 💡 Answer yes if you value more performance over isolation/control of your\n' +
" project's dependencies.\n" +
'\n',
default: true,
},
]);
await standardTarget.prompting(this);
}
/**
* Standard Yeoman generation function
*/
writing() {
const cp = new Copier(this);
const npmbuildrc = new NpmbuildrcModifier(this);
const npmbundlerrc = new NpmbundlerrcModifier(this);
const pkgJson = new PkgJsonModifier(this);
const projectAnalyzer = new ProjectAnalyzer(this);
const {importMetaljs, sampleWanted} = this.answers;
// Configure build
pkgJson.mergeDependencies(dependenciesJson);
pkgJson.addBuildStep('babel --source-maps -d build src');
cp.copyFile('.babelrc');
// Configure webpack
pkgJson.addDevDependency('babel-loader', '7.1.5');
npmbuildrc.addWebpackRule(/src\/.*\.js$/, 'babel-loader');
// Configure metal imports
if (importMetaljs) {
npmbundlerrc.mergeImports(importsJson);
npmbundlerrc.addExclusion('incremental-dom');
npmbundlerrc.addExclusion('incremental-dom-string');
}
// Prepare text labels
const labels = standardTarget.generateLabels(this);
// Prepare context
const context = standardTarget.generateContext(this, {
labels: labels[projectAnalyzer.hasLocalization ? 'jsx' : 'raw'],
});
// Copy JavaScript files
pkgJson.setMain('index.js');
cp.copyFile('src/index.js', {context});
// Generate sample contents
standardTarget.generateSamples(this, labels);
if (sampleWanted) {
cp.copyDir('src', {context});
}
}
}
module.exports = exports['default'];