Skip to content

Commit b194d8f

Browse files
author
雅影
committed
feat: publish support async
1 parent a679ec4 commit b194d8f

4 files changed

Lines changed: 133 additions & 121 deletions

File tree

src/built-in-plugins/command-build/plugin/build.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { runWebpack } from '../../../utils/webpack';
1616
import { getStaticHtmlPaths } from './generate-static-html';
1717
import { IOpts } from './interface';
1818
import { tsPlusBabel } from '../../../utils/ts-plus-babel';
19+
import { PackageInfo } from '../../../utils/define';
1920

2021
export const buildProject = async (opts: IOpts = {}) => {
2122
const result = await spinner('Analyse project', async () => {
@@ -72,7 +73,7 @@ export const buildProject = async (opts: IOpts = {}) => {
7273
});
7374
};
7475

75-
export const buildComponent = async () => {
76+
export const buildComponent = async (packageInfo: PackageInfo = null) => {
7677
// FIXME:
7778
// Do not minimize in cloud build(def envirenment), because commnets will lead to
7879
// build error in cloud build.
@@ -112,8 +113,8 @@ export const buildComponent = async () => {
112113
// );
113114

114115
// TODO:
115-
await spinner(`build source files`, async () => {
116-
await tsPlusBabel(false);
116+
await spinner(`build source files ${(packageInfo && packageInfo.name) || ''}`, async () => {
117+
await tsPlusBabel(false, false, packageInfo);
117118
});
118119

119120
// TODO: add back after upgrade to webpack5

src/built-in-plugins/command-publish/plugin/run-publish.ts

Lines changed: 55 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import { pri, tempPath, declarationPath } from '../../../node';
99
import { buildComponent } from '../../command-build/plugin/build';
1010
import { commandBundle } from '../../command-bundle/plugin/command-bundle';
1111
import { isWorkingTreeClean, getCurrentBranchName } from '../../../utils/git-operate';
12-
import { logFatal, logInfo, spinner } from '../../../utils/log';
12+
import { logInfo, spinner } from '../../../utils/log';
1313
import { getMonoAndNpmDepsOnce, DepMap } from '../../../utils/packages';
1414
import { PackageInfo } from '../../../utils/define';
1515
import {
1616
buildDeclaration,
1717
generateVersion,
1818
addTagAndPush,
1919
moveSourceFilesToTempFolderAndPublish,
20-
cleanWorkingTree,
2120
upgradePackageVersionAndPush,
2221
generateTag,
22+
prePareParamsBeforePublish,
23+
checkEnvBeforePublish,
2324
} from './utils.js';
2425

2526
export const publish = async (options: PublishOption) => {
@@ -58,8 +59,8 @@ export const publish = async (options: PublishOption) => {
5859

5960
await buildDeclaration();
6061

61-
if (installAllPrompt.publishAllPackages && currentSelectedSourceType === 'root') {
62-
// parallel publish root
62+
if (installAllPrompt.publishAllPackages) {
63+
// async publish
6364
await publishPackageAndItsMonoPackage(
6465
currentSelectedSourceType,
6566
options,
@@ -68,11 +69,8 @@ export const publish = async (options: PublishOption) => {
6869
isDevelopBranch,
6970
currentBranchName,
7071
);
71-
} else if (installAllPrompt.publishAllPackages && currentSelectedSourceType !== 'root') {
72-
// Serial publish non root situation TODO: change to parallel
73-
for (const eachPackage of depMonoPackages) {
74-
await publishByPackageName(eachPackage.name, options, depMap, isDevelopBranch, currentBranchName);
75-
}
72+
} else {
73+
await publishByPackageName(currentSelectedSourceType, options, depMap, isDevelopBranch, currentBranchName);
7674
}
7775
} else {
7876
await buildDeclaration();
@@ -103,42 +101,16 @@ async function publishByPackageName(
103101
) {
104102
logInfo(`Start publish ${sourceType}.`);
105103

106-
const targetPackageJson =
107-
sourceType === 'root'
108-
? pri.projectPackageJson || {}
109-
: pri.packages.find(eachPackage => eachPackage.name === sourceType).packageJson || {};
110-
111-
const targetConfig =
112-
sourceType === 'root'
113-
? pri.projectConfig
114-
: pri.packages.find(eachPackage => eachPackage.name === sourceType).config;
115-
116-
const targetRoot =
117-
sourceType === 'root'
118-
? pri.projectRootPath
119-
: pri.packages.find(eachPackage => eachPackage.name === sourceType).rootPath;
120-
121-
await fs.remove(path.join(pri.projectRootPath, pri.sourceConfig.distDir));
104+
const { targetPackageJson, targetConfig, targetRoot, targetPackageInfo } = prePareParamsBeforePublish(sourceType);
122105

123106
// Change source config here
124107
pri.sourceConfig = targetConfig;
125108
pri.sourceRoot = targetRoot;
126109
pri.selectedSourceType = sourceType;
127110

128-
if (!targetPackageJson.name) {
129-
logFatal(`No name found in ${sourceType} package.json`);
130-
}
131-
132-
if (!targetPackageJson.version) {
133-
logFatal(`No version found in ${sourceType} package.json`);
134-
}
135-
136-
// clean workingTree before publish
137-
if (!(await isWorkingTreeClean())) {
138-
await cleanWorkingTree();
139-
}
111+
await fs.remove(path.join(pri.projectRootPath, pri.sourceConfig.distDir));
140112

141-
logInfo('Check if npm package exist');
113+
await checkEnvBeforePublish(targetPackageJson, sourceType);
142114

143115
targetPackageJson.version = await generateVersion(
144116
options,
@@ -162,7 +134,7 @@ async function publishByPackageName(
162134
});
163135
}
164136

165-
await buildComponent();
137+
await buildComponent(targetPackageInfo);
166138

167139
if (options.bundle) {
168140
await commandBundle({ skipLint: true });
@@ -188,102 +160,79 @@ async function publishPackageAndItsMonoPackage(
188160
) {
189161
logInfo(`Start publish ${sourceType}.`);
190162

191-
const currentPackageJson =
192-
sourceType === 'root'
193-
? pri.projectPackageJson || {}
194-
: pri.packages.find(eachPackage => eachPackage.name === sourceType).packageJson || {};
195-
196-
const currentConfig =
197-
sourceType === 'root'
198-
? pri.projectConfig
199-
: pri.packages.find(eachPackage => eachPackage.name === sourceType).config;
200-
201-
const currentRoot =
202-
sourceType === 'root'
203-
? pri.projectRootPath
204-
: pri.packages.find(eachPackage => eachPackage.name === sourceType).rootPath;
205-
206-
await fs.remove(path.join(pri.projectRootPath, pri.sourceConfig.distDir));
163+
const { targetPackageJson, targetConfig, targetRoot, targetPackageInfo } = prePareParamsBeforePublish(sourceType);
207164

208165
// Change source config here
209-
pri.sourceConfig = currentConfig;
210-
pri.sourceRoot = currentRoot;
166+
pri.sourceConfig = targetConfig;
167+
pri.sourceRoot = targetRoot;
211168
pri.selectedSourceType = sourceType;
212169

213-
if (!currentPackageJson.name) {
214-
logFatal(`No name found in ${sourceType} package.json`);
215-
}
170+
await fs.remove(path.join(pri.projectRootPath, pri.sourceConfig.distDir));
216171

217-
if (!currentPackageJson.version) {
218-
logFatal(`No version found in ${sourceType} package.json`);
219-
}
172+
await checkEnvBeforePublish(targetPackageJson, sourceType);
220173

221174
const monoPackageVersion: {
222175
[key in string]: string;
223176
} = {};
224177

225-
if (sourceType === 'root') {
226-
// Generate all package version and upgrade
227-
await depMonoPackages.forEach(async item => {
228-
const version = await generateVersion(options, isDevelopBranch, item.packageJson, item.config, currentBranchName);
178+
// Generate all package version and upgrade
229179

230-
monoPackageVersion[item.name as string] = version;
180+
await depMonoPackages.forEach(async item => {
181+
const version = await generateVersion(options, isDevelopBranch, item.packageJson, item.config, currentBranchName);
231182

232-
item.packageJson.version = version;
183+
monoPackageVersion[item.name as string] = version;
233184

234-
const rootPath = depMonoPackages.find(eachPackage => eachPackage.name === item.name).rootPath;
185+
item.packageJson.version = version;
235186

236-
await fs.outputFile(path.join(rootPath, 'package.json'), `${JSON.stringify(item.packageJson, null, 2)}\n`);
237-
});
187+
const rootPath = item.rootPath;
238188

239-
// Update depMonoPackages version
240-
if (depMap) {
241-
depMap.forEach(value => {
242-
value.depMonoPackages.forEach(eachPackage => {
243-
eachPackage.packageJson.version = monoPackageVersion[eachPackage.packageJson.name];
244-
});
245-
});
246-
}
247-
} else {
248-
// TODO: add non root situation, the packages dep relation
249-
}
189+
await fs.outputFile(path.join(rootPath, 'package.json'), `${JSON.stringify(item.packageJson, null, 2)}\n`);
190+
});
191+
192+
// Update depMonoPackages version
193+
194+
depMonoPackages.forEach(item => {
195+
depMap.get(item.name).depMonoPackages.forEach(eachPackage => {
196+
eachPackage.packageJson.version = monoPackageVersion[eachPackage.packageJson.name];
197+
});
198+
});
250199

251200
// current package version
252-
const projectVersion = await generateVersion(
201+
const currentVersion = await generateVersion(
253202
options,
254203
isDevelopBranch,
255-
currentPackageJson,
256-
currentConfig,
204+
targetPackageJson,
205+
targetConfig,
257206
currentBranchName,
258207
);
259208

260-
currentPackageJson.version = projectVersion;
209+
targetPackageJson.version = currentVersion;
261210

262-
await fs.outputFile(path.join(currentRoot, 'package.json'), `${JSON.stringify(currentPackageJson, null, 2)}\n`);
211+
await fs.outputFile(path.join(targetRoot, 'package.json'), `${JSON.stringify(targetPackageJson, null, 2)}\n`);
263212

264213
if (!(await isWorkingTreeClean())) {
265-
const commitMessage = `upgrade ${sourceType} version to ${currentPackageJson.version} \n\n${depMonoPackages
214+
const commitMessage = `upgrade ${sourceType} version to ${targetPackageJson.version} \n\n${depMonoPackages
266215
.map(i => `upgrade ${i.name} version to ${i.packageJson.version} \n\n`)
267216
.join('')}`;
268217
await exec(`git add -A; git commit -m "${commitMessage}" -n`, {
269218
cwd: pri.projectRootPath,
270219
});
271220
}
272221

273-
await buildComponent();
274-
275-
if (options.bundle) {
276-
await commandBundle({ skipLint: true });
277-
}
278-
279-
/** parallel publish queue & tag with push */
222+
// async publish queue & add tag and push
280223
const publishQueue = depMonoPackages.map(item => {
281224
return new Promise(async resolve => {
225+
await buildComponent(item);
226+
227+
if (options.bundle) {
228+
await commandBundle({ skipLint: true });
229+
}
230+
282231
await moveSourceFilesToTempFolderAndPublish(
283232
item.name,
284233
options,
285234
item.config,
286-
pri.packages.find(eachPackage => eachPackage.name === item.name).rootPath,
235+
item.rootPath,
287236
depMap,
288237
isDevelopBranch,
289238
);
@@ -296,16 +245,22 @@ async function publishPackageAndItsMonoPackage(
296245
// push current package into publishQueue
297246
publishQueue.push(
298247
new Promise(async resolve => {
248+
await buildComponent(targetPackageInfo);
249+
250+
if (options.bundle) {
251+
await commandBundle({ skipLint: true });
252+
}
253+
299254
await moveSourceFilesToTempFolderAndPublish(
300255
sourceType,
301256
options,
302-
currentConfig,
303-
currentRoot,
257+
targetConfig,
258+
targetRoot,
304259
depMap,
305260
isDevelopBranch,
306261
);
307262

308-
await addTagAndPush(generateTag(sourceType, currentPackageJson), currentPackageJson);
263+
await addTagAndPush(generateTag(sourceType, targetPackageJson), targetPackageJson);
309264

310265
resolve();
311266
}),

src/built-in-plugins/command-publish/plugin/utils.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,54 @@ import { PublishOption } from './interface';
1111
import { exec } from '../../../utils/exec';
1212
import { pri, tempPath, declarationPath, srcPath } from '../../../node';
1313
import { isWorkingTreeClean } from '../../../utils/git-operate';
14-
import { logFatal, spinner, logText } from '../../../utils/log';
14+
import { logFatal, spinner, logText, logInfo } from '../../../utils/log';
1515
import { DepMap } from '../../../utils/packages';
16-
import { ProjectConfig, PackageJson } from '../../../utils/define';
16+
import { ProjectConfig, PackageJson, PackageInfo } from '../../../utils/define';
17+
18+
// prePare the info before publish
19+
export function prePareParamsBeforePublish(sourceType: string) {
20+
const targetPackageJson =
21+
sourceType === 'root'
22+
? pri.projectPackageJson || {}
23+
: pri.packages.find(eachPackage => eachPackage.name === sourceType).packageJson || {};
24+
25+
const targetConfig =
26+
sourceType === 'root'
27+
? pri.projectConfig
28+
: pri.packages.find(eachPackage => eachPackage.name === sourceType).config;
29+
30+
const targetRoot =
31+
sourceType === 'root'
32+
? pri.projectRootPath
33+
: pri.packages.find(eachPackage => eachPackage.name === sourceType).rootPath;
34+
35+
const targetPackageInfo: PackageInfo = {
36+
name: sourceType,
37+
rootPath: targetRoot,
38+
packageJson: targetPackageJson as PackageJson,
39+
config: targetConfig,
40+
};
41+
42+
return { targetPackageJson, targetConfig, targetRoot, targetPackageInfo };
43+
}
44+
45+
// check package.json and env etc.
46+
export async function checkEnvBeforePublish(targetPackageJson: Partial<PackageJson>, sourceType: string) {
47+
if (!targetPackageJson.name) {
48+
logFatal(`No name found in ${sourceType} package.json`);
49+
}
50+
51+
if (!targetPackageJson.version) {
52+
logFatal(`No version found in ${sourceType} package.json`);
53+
}
54+
55+
// clean workingTree before publish
56+
if (!(await isWorkingTreeClean())) {
57+
await cleanWorkingTree();
58+
}
59+
60+
logInfo('Check if npm package exist');
61+
}
1762

1863
export async function addTagAndPush(tagName: string, targetPackageJson: Partial<PackageJson>) {
1964
await spinner(`Add tag`, async () => {
@@ -170,7 +215,10 @@ export async function moveSourceFilesToTempFolderAndPublish(
170215

171216
await fs.remove(tempRoot);
172217

173-
await fs.copy(path.join(pri.projectRootPath, targetConfig.distDir), path.join(tempRoot, targetConfig.distDir));
218+
await fs.copy(
219+
path.join(pri.projectRootPath, targetConfig.distDir, sourceType),
220+
path.join(tempRoot, targetConfig.distDir),
221+
);
174222
await copyDeclaration(sourceType, publishTempName);
175223
await fs.copy(path.join(targetRoot, 'package.json'), path.join(tempRoot, 'package.json'));
176224

@@ -314,7 +362,7 @@ export async function copyDeclaration(sourceType: string, publishTempName: strin
314362
const targetPath = path.relative(path.join(declarationRoot, 'packages', sourceType, srcPathExtra), eachFile);
315363
fs.copySync(
316364
eachFile,
317-
path.join(pri.projectRootPath, tempPath.dir, publishTempName, declarationPath.dir, targetPath),
365+
path.join(pri.projectRootPath, tempPath.dir, publishTempName, sourceType, declarationPath.dir, targetPath),
318366
);
319367
});
320368
} else {
@@ -325,7 +373,7 @@ export async function copyDeclaration(sourceType: string, publishTempName: strin
325373
const targetPath = path.relative(path.join(declarationRoot, srcPathExtra), eachFile);
326374
fs.copySync(
327375
eachFile,
328-
path.join(pri.projectRootPath, tempPath.dir, publishTempName, declarationPath.dir, targetPath),
376+
path.join(pri.projectRootPath, tempPath.dir, publishTempName, sourceType, declarationPath.dir, targetPath),
329377
);
330378
});
331379
}

0 commit comments

Comments
 (0)