Skip to content

Commit

Permalink
fix(builder): relation need pre-build when run build alone
Browse files Browse the repository at this point in the history
当只打包一个模块时,它关联的需要打包(只检测当前包下是否含'dist'),否则会存在异常
  • Loading branch information
deot committed Aug 17, 2023
1 parent 93d983a commit 891b6af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
31 changes: 29 additions & 2 deletions packages/builder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as path from 'node:path';
import * as fs from 'node:fs';
import type { Options } from '@deot/dev-shared';
import { Utils, Logger, Shell, Locals } from '@deot/dev-shared';
import { build } from './build';
Expand All @@ -12,16 +14,41 @@ export const run = (options: Options) => Utils.autoCatch(async () => {
options.dryRun = process.env.NODE_ENV === 'UNIT';
}

const { normalizePackageFolderNames } = Locals.impl();
let packageFolderName = Locals.getPackageFolderName(options.packageName || '*');

let inputs: string[] = [];
if (locals.workspace && packageFolderName === '*') {
inputs = normalizePackageFolderNames;
inputs = locals.normalizePackageFolderNames;
} else {
inputs = [packageFolderName];
}

// 当仅打包一个时,需要寻找关联需要提前打包的模块
if (options.packageName && options.packageName !== '*') {
let relations: string[] = [];
const walk = (packageNames: string[]) => {
relations = packageNames.concat(relations);
packageNames.forEach(i => {
if (locals.packageRelation[i].length) {
walk(locals.packageRelation[i]);
}
});
};
walk(locals.packageRelation[options.packageName]);

relations = relations
.filter((i, index, source) => source.indexOf(i) === index)
.map(i => Locals.getPackageFolderName(i))
.filter(i => {
try {
return options.dryRun || !fs.existsSync(path.resolve(locals.packageDir, i, 'dist'));
} catch (e) {
return false;
}
});
inputs = relations.concat(inputs);
}

if (options.dryRun) return Shell.spawn(`echo ${inputs.join(' ')}`);
await inputs
.reduce(
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe('build.ts', () => {
await instance.stop();

expect(instance.code).toBe(0);
expect(instance.stdout).toMatch(/^cli/);
// 打包cli时会关联当前包的模块 `shared adder builder linker tester releaser updater test cli`
expect(instance.stdout).toMatch(/^shared[\s\S]+cli/);
expect(instance.stderr).toBe('');
}, 60000);
});

0 comments on commit 891b6af

Please sign in to comment.