@modern-js/upgrade-generator 2.4.16 → 2.4.18
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 +0 -4
- package/package.json +9 -9
- package/src/index.ts +195 -0
package/dist/index.js
CHANGED
|
@@ -52415,7 +52415,6 @@ var require_downloadPackage = __commonJSMin((exports) => {
|
|
|
52415
52415
|
if (response.status !== 200) {
|
|
52416
52416
|
throw new Error(`download tar package get bad status code: ${response.status}`);
|
|
52417
52417
|
}
|
|
52418
|
-
const contentLength = Number(response.headers["content-length"]);
|
|
52419
52418
|
const randomId = Math.floor(Math.random() * 1e4);
|
|
52420
52419
|
const tempTgzFilePath = `${_os.default.tmpdir()}/temp-${randomId}.tgz`;
|
|
52421
52420
|
const dest = _utils.fs.createWriteStream(tempTgzFilePath);
|
|
@@ -52428,9 +52427,6 @@ var require_downloadPackage = __commonJSMin((exports) => {
|
|
|
52428
52427
|
resolve();
|
|
52429
52428
|
});
|
|
52430
52429
|
});
|
|
52431
|
-
if ((await _utils.fs.stat(tempTgzFilePath)).size !== contentLength) {
|
|
52432
|
-
throw new Error("download tar package get bad content length");
|
|
52433
|
-
}
|
|
52434
52430
|
await new Promise((resolve, reject) => {
|
|
52435
52431
|
_utils.fs.createReadStream(tempTgzFilePath).pipe(_tar.default.x({
|
|
52436
52432
|
strip: 1,
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "2.4.
|
|
14
|
+
"version": "2.4.18",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"main": "./dist/index.js",
|
|
17
17
|
"files": [
|
|
@@ -20,18 +20,18 @@
|
|
|
20
20
|
],
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@babel/runtime": "^7.18.0",
|
|
23
|
-
"@modern-js/codesmith": "1.6.
|
|
24
|
-
"@modern-js/codesmith-api-app": "1.6.
|
|
25
|
-
"@modern-js/codesmith-api-json": "1.6.
|
|
23
|
+
"@modern-js/codesmith": "1.6.4",
|
|
24
|
+
"@modern-js/codesmith-api-app": "1.6.4",
|
|
25
|
+
"@modern-js/codesmith-api-json": "1.6.4",
|
|
26
26
|
"@types/jest": "^27",
|
|
27
27
|
"@types/node": "^14",
|
|
28
28
|
"jest": "^27",
|
|
29
29
|
"typescript": "^4",
|
|
30
|
-
"@modern-js/generator-common": "2.4.
|
|
31
|
-
"@modern-js/generator-utils": "2.4.
|
|
32
|
-
"@modern-js/plugin-i18n": "1.22.
|
|
33
|
-
"@scripts/build": "1.22.
|
|
34
|
-
"@scripts/jest-config": "1.22.
|
|
30
|
+
"@modern-js/generator-common": "2.4.18",
|
|
31
|
+
"@modern-js/generator-utils": "2.4.18",
|
|
32
|
+
"@modern-js/plugin-i18n": "1.22.7",
|
|
33
|
+
"@scripts/build": "1.22.7",
|
|
34
|
+
"@scripts/jest-config": "1.22.7"
|
|
35
35
|
},
|
|
36
36
|
"sideEffects": false,
|
|
37
37
|
"publishConfig": {
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
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
|
+
semver,
|
|
12
|
+
fs,
|
|
13
|
+
} from '@modern-js/generator-utils';
|
|
14
|
+
import {
|
|
15
|
+
PackageManager,
|
|
16
|
+
Solution,
|
|
17
|
+
SolutionText,
|
|
18
|
+
SolutionToolsMap,
|
|
19
|
+
} from '@modern-js/generator-common';
|
|
20
|
+
import { i18n, localeKeys } from './locale';
|
|
21
|
+
|
|
22
|
+
export const handleTemplateFile = async (
|
|
23
|
+
context: GeneratorContext,
|
|
24
|
+
generator: GeneratorCore,
|
|
25
|
+
appApi: AppAPI,
|
|
26
|
+
) => {
|
|
27
|
+
const jsonAPI = new JsonAPI(generator);
|
|
28
|
+
// get project solution type
|
|
29
|
+
const pkgInfo = await getPackageObj(context);
|
|
30
|
+
const deps = {
|
|
31
|
+
...pkgInfo.devDependencies,
|
|
32
|
+
...pkgInfo.dependencies,
|
|
33
|
+
};
|
|
34
|
+
const solutions = Object.keys(SolutionToolsMap).filter(
|
|
35
|
+
solution => deps[SolutionToolsMap[solution as Solution]],
|
|
36
|
+
);
|
|
37
|
+
if (solutions.length === 0) {
|
|
38
|
+
throw Error(i18n.t(localeKeys.tooltip.no_solution));
|
|
39
|
+
}
|
|
40
|
+
if (solutions.length >= 2) {
|
|
41
|
+
throw Error(i18n.t(localeKeys.tooltip.more_solution));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
generator.logger.info(
|
|
45
|
+
`[${i18n.t(localeKeys.projectType)}]: ${SolutionText[
|
|
46
|
+
solutions[0] as Solution
|
|
47
|
+
]()}`,
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
// get modern latest version
|
|
51
|
+
const modernVersion = await getModernVersion(
|
|
52
|
+
solutions[0] as Solution,
|
|
53
|
+
context.config.registry,
|
|
54
|
+
context.config.distTag,
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
generator.logger.info(
|
|
58
|
+
`[${i18n.t(localeKeys.modernVersion)}]: ${modernVersion}`,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// adjust Modern.js packages' version is latest?
|
|
62
|
+
if (
|
|
63
|
+
Object.keys(deps)
|
|
64
|
+
.filter(
|
|
65
|
+
dep => dep.startsWith('@modern-js') || dep.startsWith('@modern-js-app'),
|
|
66
|
+
)
|
|
67
|
+
.filter(dep => !dep.includes('electron'))
|
|
68
|
+
.filter(dep => !dep.includes('codesmith') && !dep.includes('easy-form'))
|
|
69
|
+
.filter(dep => !dep.startsWith('@modern-js-reduck'))
|
|
70
|
+
.every(dep => deps[dep] === modernVersion)
|
|
71
|
+
) {
|
|
72
|
+
generator.logger.info(
|
|
73
|
+
`[${i18n.t(localeKeys.alreadyLatest)}]: ${modernVersion}`,
|
|
74
|
+
);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const appDir = context.materials.default.basePath;
|
|
79
|
+
|
|
80
|
+
const packageManager = await getPackageManager(appDir);
|
|
81
|
+
context.config.packageManager = packageManager;
|
|
82
|
+
|
|
83
|
+
if (packageManager === PackageManager.Pnpm) {
|
|
84
|
+
const npmrcPath = path.join(generator.outputPath, '.npmrc');
|
|
85
|
+
if (fs.existsSync(npmrcPath)) {
|
|
86
|
+
const content = fs.readFileSync(npmrcPath, 'utf-8');
|
|
87
|
+
if (!content.includes('strict-peer-dependencies=false')) {
|
|
88
|
+
fs.appendFileSync(npmrcPath, '\nstrict-peer-dependencies=false\n');
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
fs.ensureFileSync(npmrcPath);
|
|
92
|
+
fs.writeFileSync(npmrcPath, 'strict-peer-dependencies=false');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const modernDeps = Object.keys(pkgInfo.dependencies || {}).filter(
|
|
97
|
+
dep => dep.startsWith('@modern-js') || dep.startsWith('@modern-js-app'),
|
|
98
|
+
);
|
|
99
|
+
const modernDevDeps = Object.keys(pkgInfo.devDependencies || {}).filter(
|
|
100
|
+
dep => dep.startsWith('@modern-js') || dep.startsWith('@modern-js-app'),
|
|
101
|
+
);
|
|
102
|
+
const updateInfo: Record<string, string> = {};
|
|
103
|
+
|
|
104
|
+
const spinner = ora('Loading...').start();
|
|
105
|
+
spinner.color = 'yellow';
|
|
106
|
+
|
|
107
|
+
await Promise.all(
|
|
108
|
+
modernDeps.map(
|
|
109
|
+
async dep =>
|
|
110
|
+
(updateInfo[`dependencies.${dep}`] = await getAvailableVersion(
|
|
111
|
+
dep,
|
|
112
|
+
modernVersion,
|
|
113
|
+
)),
|
|
114
|
+
),
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
await Promise.all(
|
|
118
|
+
modernDevDeps.map(
|
|
119
|
+
async dep =>
|
|
120
|
+
(updateInfo[`devDependencies.${dep}`] = await getAvailableVersion(
|
|
121
|
+
dep,
|
|
122
|
+
modernVersion,
|
|
123
|
+
)),
|
|
124
|
+
),
|
|
125
|
+
);
|
|
126
|
+
await jsonAPI.update(
|
|
127
|
+
context.materials.default.get(path.join(appDir, 'package.json')),
|
|
128
|
+
{
|
|
129
|
+
query: {},
|
|
130
|
+
update: {
|
|
131
|
+
$set: updateInfo,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
spinner.stop();
|
|
137
|
+
|
|
138
|
+
// update husky
|
|
139
|
+
const huskyVersion = deps.husky;
|
|
140
|
+
try {
|
|
141
|
+
if (huskyVersion && semver.lt(huskyVersion, '8.0.0')) {
|
|
142
|
+
generator.logger.info(`${i18n.t(localeKeys.updateHusky)}`);
|
|
143
|
+
await jsonAPI.update(
|
|
144
|
+
context.materials.default.get(path.join(appDir, 'package.json')),
|
|
145
|
+
{
|
|
146
|
+
query: {},
|
|
147
|
+
update: {
|
|
148
|
+
$set: {
|
|
149
|
+
'devDependencies.husky': '^8.0.0',
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const pkgPath = context.materials.default.get(
|
|
156
|
+
path.join(appDir, 'package.json'),
|
|
157
|
+
).filePath;
|
|
158
|
+
const pkgInfo = fs.readJSONSync(pkgPath, 'utf-8');
|
|
159
|
+
const { prepare } = pkgInfo.scripts;
|
|
160
|
+
if (!prepare) {
|
|
161
|
+
pkgInfo.scripts.prepare = 'husky install';
|
|
162
|
+
} else if (!prepare.includes('husky install')) {
|
|
163
|
+
pkgInfo.scripts.prepare = `${prepare} && husky install`;
|
|
164
|
+
}
|
|
165
|
+
pkgInfo.husky = undefined;
|
|
166
|
+
|
|
167
|
+
fs.writeJSONSync(pkgPath, pkgInfo, { spaces: 2 });
|
|
168
|
+
|
|
169
|
+
await appApi.forgeTemplate('templates/**/*');
|
|
170
|
+
fs.chmodSync(
|
|
171
|
+
path.join(generator.outputPath, '.husky', 'pre-commit'),
|
|
172
|
+
'755',
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
} catch (e) {}
|
|
176
|
+
|
|
177
|
+
await appApi.runInstall();
|
|
178
|
+
|
|
179
|
+
appApi.showSuccessInfo(i18n.t(localeKeys.success));
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
export default async (context: GeneratorContext, generator: GeneratorCore) => {
|
|
183
|
+
const appApi = new AppAPI(context, generator);
|
|
184
|
+
|
|
185
|
+
const { locale } = context.config;
|
|
186
|
+
appApi.i18n.changeLanguage({ locale });
|
|
187
|
+
|
|
188
|
+
generator.logger.debug(`start run @modern-js/upgrade-generator`);
|
|
189
|
+
generator.logger.debug(`context=${JSON.stringify(context)}`);
|
|
190
|
+
generator.logger.debug(`context.data=${JSON.stringify(context.data)}`);
|
|
191
|
+
|
|
192
|
+
await handleTemplateFile(context, generator, appApi);
|
|
193
|
+
|
|
194
|
+
generator.logger.debug(`forge @modern-js/upgrade-generator succeed `);
|
|
195
|
+
};
|