This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addons.js
125 lines (110 loc) · 3.75 KB
/
addons.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const { toCamelCase } = require('cna-cli/lib/helpers');
const { Options } = require('./docs');
const REACT_EXTENSIONS_BRANCH = process.env.REACT_EXTENSIONS_BRANCH || 'main';
const REACT_EXTENSIONS = 'https://github.com/Create-Node-App/react-extensions';
const DOCKER_EXTENSIONS = 'https://github.com/Create-Node-App/docker-extensions';
const ANDROID_TOOLS_EXTENSIONS = 'https://github.com/Create-Node-App/android-tools';
const CRA_TEMPLATE = `${REACT_EXTENSIONS}?branch=${REACT_EXTENSIONS_BRANCH}&subdir=addons/create-react-app/es`;
const CRA_TS_TEMPLATE = `${REACT_EXTENSIONS}?branch=${REACT_EXTENSIONS_BRANCH}&subdir=addons/create-react-app/ts`;
const NEXTJS_EXAMPLES =
'https://github.com/Create-Node-App/nextjs-extensions?branch=main&templatedir=&subdir=examples';
const GATSBY_ORG_URL = 'https://github.com/gatsbyjs';
const getCraTemplateUrl = (template) => {
try {
return new URL(template).toString();
} catch (error) {
return template;
}
};
const getGatsbyTemplateUrl = (template) => {
try {
return new URL(template).toString();
} catch (error) {
return `${GATSBY_ORG_URL}/${template}?branch=master&templatedir=`;
}
};
const getNextJsExampleUrl = (template) => {
try {
return new URL(template).toString();
} catch (error) {
return `${NEXTJS_EXAMPLES}/${template}`;
}
};
/**
* getExtensionUrl returns the extension URL based on selected backend
*
* @param {Options} options - User options to build setup
* @param {(string | undefined)} template - Template url or name specified for the user
* @returns {string} - extension url
*/
const getExtensionUrl = (options, template) => {
if (options.cra && typeof options.cra === 'string') {
return getCraTemplateUrl(template || options.cra);
}
if (options.next) {
return getNextJsExampleUrl(template || options.next);
}
if (options.gatsby) {
return getGatsbyTemplateUrl(template || options.gatsby);
}
return template;
};
/**
* get addons from user options
* @param {Options} options - Options specified by the user to create addons
* @returns {{ addon: string, ignorePackage?: boolean }[]}
*/
module.exports = (options) => {
const lang = options.typescript ? 'ts' : 'es';
const langAddons = [
'i18n',
'redux',
'redux-saga',
'recoil',
'ant-design',
'bootstrap',
'material-ui',
'semantic-ui',
];
// initialized with base template
let addons = [
{
addon: `${REACT_EXTENSIONS}?branch=${REACT_EXTENSIONS_BRANCH}&subdir=addons/webpack-base/common`,
},
{
addon: `${REACT_EXTENSIONS}?branch=${REACT_EXTENSIONS_BRANCH}&subdir=addons/webpack-base/${lang}`,
},
];
if (options.cra) {
addons = [
{
addon: options.typescript ? CRA_TS_TEMPLATE : CRA_TEMPLATE,
},
];
}
const baseTemplate = getExtensionUrl(options, options.template);
if (baseTemplate) {
addons = options.cra ? [...addons, { addon: baseTemplate }] : [{ addon: baseTemplate }];
}
langAddons.forEach((addon) => {
if (options[toCamelCase(addon)]) {
addons.push({ addon: `${REACT_EXTENSIONS}?branch=${REACT_EXTENSIONS_BRANCH}&subdir=addons/${addon}/common` });
addons.push({ addon: `${REACT_EXTENSIONS}?branch=${REACT_EXTENSIONS_BRANCH}&subdir=addons/${addon}/${lang}` });
}
});
if (options.ionic) {
addons.push({ addon: `${REACT_EXTENSIONS}?branch=${REACT_EXTENSIONS_BRANCH}&subdir=addons/ionic` });
}
if (options.androidTools) {
addons.push({ addon: `${ANDROID_TOOLS_EXTENSIONS}?branch=addon/docker/android` });
}
if (options.docker) {
addons.push({ addon: `${DOCKER_EXTENSIONS}?branch=addon/docker/web` });
}
if (options.extend) {
addons.push(
...options.extend.filter(Boolean).map((addon) => ({ addon: getExtensionUrl(options, addon) }))
);
}
return addons;
};