// 파일 1
import { globSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
export function getWorkspacePJSONs(path = './workspaces/*/package.json') {
return Promise.all(
globSync(
fileURLToPath(import.meta.resolve(path))
).map(path => import(path, { with: { type: 'json' } }))
);
}
//파일 2
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { getWorkspacePJSONs } from './getWorkspacePJSONs.mjs';
const requiredKeywords = ['node.js', 'sliced bread'];
test('Check package.jsons', { concurrency: true }, async t => {
const pjsons = await getWorkspacePJSONs();
const cases = pjsons.map(pjson =>
// ⚠️ `t.test`, NOT `test`
t.test(`Ensure fields are properly set: ${pjson.name}`, () => {
assert.partialDeepStrictEqual(pjson.keywords, requiredKeywords);
})
);
// Allow the cases to run concurrently.
await Promise.allSettled(cases);
});
설명
./workspaces/*/package.json path 에 해당하는 package.json 파일을 받아서path => import 로 동적 import 하는데, 전부 Promise 로 받으니까 map 으로 변환globSync
↓
파일 경로 배열 반환
[
'api/package.json',
'web/package.json',
'mobile/package.json'
]
↓ map
[
import('api/package.json'),
import('web/package.json'),
import('mobile/package.json')
]
↓
[
Promise,
Promise,
Promise
]
↓ Promise.all
[
apiJson,
webJson,
mobileJson
]