Skip to content

Commit

Permalink
create copyAssetsConcurrently and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrm007 committed May 5, 2023
1 parent a8143e1 commit 93cc624
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 40 deletions.
114 changes: 82 additions & 32 deletions src/cli/build/assets.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,107 @@
import memfs, { vol } from 'memfs';

import { copyAssets } from './assets';
import { copyAssets, copyAssetsConcurrently } from './assets';

jest.mock('fs', () => memfs);
jest.mock('fs-extra', () => memfs);

jest
.spyOn(console, 'log')
.mockImplementation((...args) => stdoutMock(`${args.join(' ')}\n`));

const stdoutMock = jest.fn().mockName('[stdout]');
const getStdOut = () => `${stdoutMock.name}${stdoutMock.mock.calls.join('')}`;

// a snapshot serializer to remove quotes around stdout
expect.addSnapshotSerializer({
test: (val) => typeof val === 'string' && val.startsWith(stdoutMock.name),
print: (val) => (val as string).trim().replace(stdoutMock.name, ''),
});

// the glob in package.json breaks syntax highlighting in VS Code
const justOutDirs = (fs: typeof vol) =>
Object.fromEntries(
Object.entries(fs.toJSON(['.'], {}, true)).filter(
([key]) => !(key.includes('package.json') || key.includes('src/')),
),
);

beforeEach(() => {
vol.reset();
vol.fromJSON({
'package.json': JSON.stringify({
skuba: {
assets: ['**/*.vocab/*trans.json'],
assets: ['**/*.vocab/*translations.json'],
},
}),
'src/app.ts': '',
'src/.vocab/index.ts': '',
'src/.vocab/trans.json': '',
'src/.vocab/id.trans.json': '',
'src/.vocab/th.trans.json': '',
'src/.vocab/translations.json': '',
'src/.vocab/th.translations.json': '',
'src/other.vocab/index.ts': '',
'src/other.vocab/trans.json': '',
'src/other.vocab/id.trans.json': '',
'src/other.vocab/th.trans.json': '',
'src/other.vocab/translations.json': '',
'src/other.vocab/th.translations.json': '',
});
jest.clearAllMocks();
});

describe('copyAssets', () => {
it('should copy the specified assets to the destination directory', async () => {
const { log } = jest.createMockFromModule<
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
typeof import('../../utils/logging')
>('../../utils/logging');
await copyAssets('lib', log);

expect(vol.toJSON(['.'], {}, true)).toMatchInlineSnapshot(`
it('should copy the assets specified in skuba config to the destination directory', async () => {
await copyAssets('lib');

expect(justOutDirs(vol)).toMatchInlineSnapshot(`
{
"lib/.vocab/id.trans.json": "",
"lib/.vocab/th.trans.json": "",
"lib/.vocab/trans.json": "",
"lib/other.vocab/id.trans.json": "",
"lib/other.vocab/th.trans.json": "",
"lib/other.vocab/trans.json": "",
"package.json": "{"skuba":{"assets":["**/*.vocab/*trans.json"]}}",
"src/.vocab/id.trans.json": "",
"src/.vocab/index.ts": "",
"src/.vocab/th.trans.json": "",
"src/.vocab/trans.json": "",
"src/app.ts": "",
"src/other.vocab/id.trans.json": "",
"src/other.vocab/index.ts": "",
"src/other.vocab/th.trans.json": "",
"src/other.vocab/trans.json": "",
"lib/.vocab/th.translations.json": "",
"lib/.vocab/translations.json": "",
"lib/other.vocab/th.translations.json": "",
"lib/other.vocab/translations.json": "",
}
`);
expect(getStdOut()).toMatchInlineSnapshot(`
Copying .vocab/th.translations.json
Copying .vocab/translations.json
Copying other.vocab/th.translations.json
Copying other.vocab/translations.json
`);
});
});

describe('copyAssetsConcurrently', () => {
it('should copy the assets specified in skuba config to the destination directories', async () => {
await copyAssetsConcurrently([
{
outDir: 'lib-commonjs',
name: 'commonjs',
prefixColor: 'green',
},
{
outDir: 'lib-es2015',
name: 'es2015',
prefixColor: 'yellow',
},
]);

expect(justOutDirs(vol)).toMatchInlineSnapshot(`
{
"lib-commonjs/.vocab/th.translations.json": "",
"lib-commonjs/.vocab/translations.json": "",
"lib-commonjs/other.vocab/th.translations.json": "",
"lib-commonjs/other.vocab/translations.json": "",
"lib-es2015/.vocab/th.translations.json": "",
"lib-es2015/.vocab/translations.json": "",
"lib-es2015/other.vocab/th.translations.json": "",
"lib-es2015/other.vocab/translations.json": "",
}
`);
expect(getStdOut()).toMatchInlineSnapshot(`
commonjs │ Copying .vocab/th.translations.json
commonjs │ Copying .vocab/translations.json
commonjs │ Copying other.vocab/th.translations.json
commonjs │ Copying other.vocab/translations.json
es2015 │ Copying .vocab/th.translations.json
es2015 │ Copying .vocab/translations.json
es2015 │ Copying other.vocab/th.translations.json
es2015 │ Copying other.vocab/translations.json
`);
});
});
29 changes: 28 additions & 1 deletion src/cli/build/assets.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import path from 'path';

import type { Color } from 'chalk';
import chalk from 'chalk';
import fs from 'fs-extra';

import { copyFile } from '../../utils/copy';
import { buildPatternToFilepathMap, crawlDirectory } from '../../utils/dir';
import { type Logger, log } from '../../utils/logging';
import { type Logger, createLogger, log } from '../../utils/logging';
import {
getConsumerManifest,
getPropFromConsumerManifest,
Expand Down Expand Up @@ -50,3 +52,28 @@ export const copyAssets = async (
);
}
};

interface CopyAssetsConfig {
outDir: string;
name: string;
prefixColor: typeof Color;
}

export const copyAssetsConcurrently = async (configs: CopyAssetsConfig[]) => {
const maxNameLength = configs.reduce(
(length, command) => Math.max(length, command.name.length),
0,
);

await Promise.all(
configs.map(({ outDir, name, prefixColor }) =>
copyAssets(
outDir,
createLogger(
false,
chalk[prefixColor](`${name.padEnd(maxNameLength)} │`),
),
),
),
);
};
19 changes: 12 additions & 7 deletions src/cli/buildPackage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import chalk from 'chalk';

import { hasSerialFlag } from '../utils/args';
import { execConcurrently } from '../utils/exec';
import { createLogger } from '../utils/logging';

import { copyAssets } from './build/assets';
import { copyAssetsConcurrently } from './build/assets';
import { tryAddEmptyExports } from './configure/addEmptyExports';

export const buildPackage = async (args = process.argv.slice(2)) => {
Expand Down Expand Up @@ -36,8 +33,16 @@ export const buildPackage = async (args = process.argv.slice(2)) => {
},
);

await Promise.all([
copyAssets('lib-commonjs', createLogger(false, chalk.green('commonjs │'))),
copyAssets('lib-es2015', createLogger(false, chalk.yellow('es2015 │'))),
await copyAssetsConcurrently([
{
outDir: 'lib-commonjs',
name: 'commonjs',
prefixColor: 'green',
},
{
outDir: 'lib-es2015',
name: 'es2015',
prefixColor: 'yellow',
},
]);
};

0 comments on commit 93cc624

Please sign in to comment.