@modern-js/upgrade-generator 3.0.3 → 3.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1836 -1621
- package/package.json +6 -6
- package/src/index.ts +214 -0
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "3.0.
|
|
14
|
+
"version": "3.0.5",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"main": "./dist/index.js",
|
|
17
17
|
"files": [
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"@types/node": "^14",
|
|
28
28
|
"jest": "^27",
|
|
29
29
|
"typescript": "^4",
|
|
30
|
-
"@modern-js/generator-
|
|
31
|
-
"@modern-js/generator-
|
|
32
|
-
"@modern-js/plugin-i18n": "2.
|
|
33
|
-
"@scripts/jest-config": "2.
|
|
34
|
-
"@scripts/build": "2.
|
|
30
|
+
"@modern-js/generator-common": "3.0.5",
|
|
31
|
+
"@modern-js/generator-utils": "3.0.5",
|
|
32
|
+
"@modern-js/plugin-i18n": "2.3.0",
|
|
33
|
+
"@scripts/jest-config": "2.3.0",
|
|
34
|
+
"@scripts/build": "2.3.0"
|
|
35
35
|
},
|
|
36
36
|
"sideEffects": false,
|
|
37
37
|
"publishConfig": {
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { GeneratorContext, GeneratorCore } from '@modern-js/codesmith';
|
|
3
|
+
import { AppAPI } from '@modern-js/codesmith-api-app';
|
|
4
|
+
import { JsonAPI } from '@modern-js/codesmith-api-json';
|
|
5
|
+
import {
|
|
6
|
+
ora,
|
|
7
|
+
getAvailableVersion,
|
|
8
|
+
getModernVersion,
|
|
9
|
+
getPackageManager,
|
|
10
|
+
getPackageObj,
|
|
11
|
+
execa,
|
|
12
|
+
semver,
|
|
13
|
+
fs,
|
|
14
|
+
} from '@modern-js/generator-utils';
|
|
15
|
+
import {
|
|
16
|
+
PackageManager,
|
|
17
|
+
Solution,
|
|
18
|
+
SolutionText,
|
|
19
|
+
SolutionToolsMap,
|
|
20
|
+
} from '@modern-js/generator-common';
|
|
21
|
+
import { i18n, localeKeys } from './locale';
|
|
22
|
+
|
|
23
|
+
export const handleTemplateFile = async (
|
|
24
|
+
context: GeneratorContext,
|
|
25
|
+
generator: GeneratorCore,
|
|
26
|
+
appApi: AppAPI,
|
|
27
|
+
) => {
|
|
28
|
+
const jsonAPI = new JsonAPI(generator);
|
|
29
|
+
// get project solution type
|
|
30
|
+
const pkgInfo = await getPackageObj(context);
|
|
31
|
+
const deps = {
|
|
32
|
+
...pkgInfo.devDependencies,
|
|
33
|
+
...pkgInfo.dependencies,
|
|
34
|
+
};
|
|
35
|
+
const solutions = Object.keys(SolutionToolsMap).filter(
|
|
36
|
+
solution => deps[SolutionToolsMap[solution as Solution]],
|
|
37
|
+
);
|
|
38
|
+
if (solutions.length === 0) {
|
|
39
|
+
throw Error(i18n.t(localeKeys.tooltip.no_solution));
|
|
40
|
+
}
|
|
41
|
+
if (solutions.length >= 2) {
|
|
42
|
+
throw Error(i18n.t(localeKeys.tooltip.more_solution));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
generator.logger.info(
|
|
46
|
+
`[${i18n.t(localeKeys.projectType)}]: ${SolutionText[
|
|
47
|
+
solutions[0] as Solution
|
|
48
|
+
]()}`,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
// get modern latest version
|
|
52
|
+
const modernVersion = await getModernVersion(
|
|
53
|
+
solutions[0] as Solution,
|
|
54
|
+
context.config.registry,
|
|
55
|
+
context.config.distTag,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
generator.logger.info(
|
|
59
|
+
`[${i18n.t(localeKeys.modernVersion)}]: ${modernVersion}`,
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
// adjust Modern.js packages' version is latest?
|
|
63
|
+
if (
|
|
64
|
+
Object.keys(deps)
|
|
65
|
+
.filter(
|
|
66
|
+
dep => dep.startsWith('@modern-js') || dep.startsWith('@modern-js-app'),
|
|
67
|
+
)
|
|
68
|
+
.filter(dep => !dep.includes('electron'))
|
|
69
|
+
.filter(dep => !dep.includes('codesmith') && !dep.includes('easy-form'))
|
|
70
|
+
.filter(dep => !dep.startsWith('@modern-js-reduck'))
|
|
71
|
+
.every(dep => deps[dep] === modernVersion)
|
|
72
|
+
) {
|
|
73
|
+
generator.logger.info(
|
|
74
|
+
`[${i18n.t(localeKeys.alreadyLatest)}]: ${modernVersion}`,
|
|
75
|
+
);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const appDir = context.materials.default.basePath;
|
|
80
|
+
|
|
81
|
+
const packageManager = await getPackageManager(appDir);
|
|
82
|
+
context.config.packageManager = packageManager;
|
|
83
|
+
|
|
84
|
+
if (packageManager === PackageManager.Pnpm) {
|
|
85
|
+
const npmrcPath = path.join(generator.outputPath, '.npmrc');
|
|
86
|
+
if (fs.existsSync(npmrcPath)) {
|
|
87
|
+
const content = fs.readFileSync(npmrcPath, 'utf-8');
|
|
88
|
+
if (!content.includes('strict-peer-dependencies=false')) {
|
|
89
|
+
fs.appendFileSync(npmrcPath, '\nstrict-peer-dependencies=false\n');
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
fs.ensureFileSync(npmrcPath);
|
|
93
|
+
fs.writeFileSync(npmrcPath, 'strict-peer-dependencies=false');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (
|
|
98
|
+
solutions[0] === Solution.Monorepo &&
|
|
99
|
+
packageManager === PackageManager.Pnpm
|
|
100
|
+
) {
|
|
101
|
+
await execa(
|
|
102
|
+
'pnpm',
|
|
103
|
+
['update', '@modern-js/*', '@modern-js-app/*', '--recursive', '--latest'],
|
|
104
|
+
{
|
|
105
|
+
stdin: 'inherit',
|
|
106
|
+
stdout: 'inherit',
|
|
107
|
+
stderr: 'inherit',
|
|
108
|
+
},
|
|
109
|
+
);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const modernDeps = Object.keys(pkgInfo.dependencies || {}).filter(
|
|
114
|
+
dep => dep.startsWith('@modern-js') || dep.startsWith('@modern-js-app'),
|
|
115
|
+
);
|
|
116
|
+
const modernDevDeps = Object.keys(pkgInfo.devDependencies || {}).filter(
|
|
117
|
+
dep => dep.startsWith('@modern-js') || dep.startsWith('@modern-js-app'),
|
|
118
|
+
);
|
|
119
|
+
const updateInfo: Record<string, string> = {};
|
|
120
|
+
|
|
121
|
+
const spinner = ora({
|
|
122
|
+
text: 'Load Generator...',
|
|
123
|
+
spinner: 'runner',
|
|
124
|
+
}).start();
|
|
125
|
+
|
|
126
|
+
await Promise.all(
|
|
127
|
+
modernDeps.map(
|
|
128
|
+
async dep =>
|
|
129
|
+
(updateInfo[`dependencies.${dep}`] = await getAvailableVersion(
|
|
130
|
+
dep,
|
|
131
|
+
modernVersion,
|
|
132
|
+
)),
|
|
133
|
+
),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
await Promise.all(
|
|
137
|
+
modernDevDeps.map(
|
|
138
|
+
async dep =>
|
|
139
|
+
(updateInfo[`devDependencies.${dep}`] = await getAvailableVersion(
|
|
140
|
+
dep,
|
|
141
|
+
modernVersion,
|
|
142
|
+
)),
|
|
143
|
+
),
|
|
144
|
+
);
|
|
145
|
+
await jsonAPI.update(
|
|
146
|
+
context.materials.default.get(path.join(appDir, 'package.json')),
|
|
147
|
+
{
|
|
148
|
+
query: {},
|
|
149
|
+
update: {
|
|
150
|
+
$set: updateInfo,
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
spinner.stop();
|
|
156
|
+
|
|
157
|
+
// update husky
|
|
158
|
+
const huskyVersion = deps.husky;
|
|
159
|
+
try {
|
|
160
|
+
if (huskyVersion && semver.lt(huskyVersion, '8.0.0')) {
|
|
161
|
+
generator.logger.info(`${i18n.t(localeKeys.updateHusky)}`);
|
|
162
|
+
await jsonAPI.update(
|
|
163
|
+
context.materials.default.get(path.join(appDir, 'package.json')),
|
|
164
|
+
{
|
|
165
|
+
query: {},
|
|
166
|
+
update: {
|
|
167
|
+
$set: {
|
|
168
|
+
'devDependencies.husky': '^8.0.0',
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
const pkgPath = context.materials.default.get(
|
|
175
|
+
path.join(appDir, 'package.json'),
|
|
176
|
+
).filePath;
|
|
177
|
+
const pkgInfo = fs.readJSONSync(pkgPath, 'utf-8');
|
|
178
|
+
const { prepare } = pkgInfo.scripts;
|
|
179
|
+
if (!prepare) {
|
|
180
|
+
pkgInfo.scripts.prepare = 'husky install';
|
|
181
|
+
} else if (!prepare.includes('husky install')) {
|
|
182
|
+
pkgInfo.scripts.prepare = `${prepare} && husky install`;
|
|
183
|
+
}
|
|
184
|
+
pkgInfo.husky = undefined;
|
|
185
|
+
|
|
186
|
+
fs.writeJSONSync(pkgPath, pkgInfo, { spaces: 2 });
|
|
187
|
+
|
|
188
|
+
await appApi.forgeTemplate('templates/**/*');
|
|
189
|
+
fs.chmodSync(
|
|
190
|
+
path.join(generator.outputPath, '.husky', 'pre-commit'),
|
|
191
|
+
'755',
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
} catch (e) {}
|
|
195
|
+
|
|
196
|
+
await appApi.runInstall();
|
|
197
|
+
|
|
198
|
+
appApi.showSuccessInfo(i18n.t(localeKeys.success));
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
export default async (context: GeneratorContext, generator: GeneratorCore) => {
|
|
202
|
+
const appApi = new AppAPI(context, generator);
|
|
203
|
+
|
|
204
|
+
const { locale } = context.config;
|
|
205
|
+
appApi.i18n.changeLanguage({ locale });
|
|
206
|
+
|
|
207
|
+
generator.logger.debug(`start run @modern-js/upgrade-generator`);
|
|
208
|
+
generator.logger.debug(`context=${JSON.stringify(context)}`);
|
|
209
|
+
generator.logger.debug(`context.data=${JSON.stringify(context.data)}`);
|
|
210
|
+
|
|
211
|
+
await handleTemplateFile(context, generator, appApi);
|
|
212
|
+
|
|
213
|
+
generator.logger.debug(`forge @modern-js/upgrade-generator succeed `);
|
|
214
|
+
};
|