Skip to content

Commit

Permalink
fix: Keep existing wireit commands
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Jul 29, 2022
1 parent 01ccbc8 commit 7ffb449
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
42 changes: 42 additions & 0 deletions packages/wireit-package/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,45 @@ describe('sequence', () => {
});
});
});

describe('existing wireit', () => {
before(() => {
const volume = {
'/root/package.json': JSON.stringify({
workspaces: ['packages/*'],
}),
'/root/packages/a/package.json': JSON.stringify({
name: 'a',
}),
'/root/packages/b/package.json': JSON.stringify({
name: 'b',
dependencies: { a: '*' },
wireit: {
clean: {
command: 'rm *',
},
},
}),
};
mockFs(volume);
});

after(() => {
mockFs.restore();
});

it('update', async () => {
await update({
command: 'npm run build',
name: 'build',
cwd: '/root',
});

const b = fs.readFileSync('/root/packages/b/package.json').toString();
const { wireit } = JSON.parse(b);

expect(wireit.clean).toEqual({
command: 'rm *',
});
});
});
20 changes: 9 additions & 11 deletions packages/wireit-package/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,33 @@ export async function update(args: UpdateArgs) {

if (!packageData?.manifest.dependencies) continue;

const { manifest, absPath, manifestPath } = packageData;

if (!manifest.dependencies) continue;

const dependencies: string[] = [];
const wireit = {
const wireit = (manifest['wireit'] as Record<string, unknown> | undefined) || {
[name]: {
command,
dependencies,
},
};

for (const [dependencyName] of Object.entries(packageData.manifest.dependencies)) {
for (const [dependencyName] of Object.entries(manifest.dependencies)) {
const dependencyPackage = result.packages[dependencyName];

if (!dependencyPackage) continue;

const relative = path.posix.relative(
packageData.absPath,
dependencyPackage.absPath,
);
const relative = path.posix.relative(absPath, dependencyPackage.absPath);

dependencies.push(`${relative}:${name}`);
}

if (dependencies.length > 0) {
packageData.manifest['wireit'] = wireit;
manifest['wireit'] = wireit;
}

await fs.writeFile(
packageData.manifestPath,
JSON.stringify(packageData.manifest, undefined, 2),
);
await fs.writeFile(manifestPath, JSON.stringify(manifest, undefined, 2));
}

return result;
Expand Down

0 comments on commit 7ffb449

Please sign in to comment.