Files
HMDesign/examples/org-chart/verify-package.mjs
T

44 lines
3.5 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { execFileSync } from 'node:child_process';
import path from 'node:path';
const root = fileURLToPath(new URL('../../', import.meta.url));
const consumer = path.join(root, 'test-results/org-chart-consumer');
const packageRoot = path.join(consumer, 'node_modules/@hm-design/org-chart');
await mkdir(packageRoot, { recursive: true });
execFileSync('tar.exe', ['-xzf', path.join(root, 'test-results/hm-design-org-chart-0.1.0.tgz'), '-C', packageRoot, '--strip-components=1']);
await writeFile(path.join(consumer, 'package.json'), JSON.stringify({name:'org-chart-isolated-consumer',private:true,type:'module'}));
const manifest = JSON.parse(await readFile(path.join(packageRoot, 'package.json'), 'utf8'));
assert.deepEqual(manifest.dependencies, {orgchart:'6.0.0'});
assert.equal(manifest.peerDependencies, undefined);
const consumerRequire = createRequire(path.join(consumer, 'package.json'));
for (const subpath of ['', '/style.css', '/engine.css', '/browser']) await readFile(consumerRequire.resolve(`@hm-design/org-chart${subpath}`));
const module = await import(pathToFileURL(consumerRequire.resolve('@hm-design/org-chart')).href);
assert.equal(typeof module.createOrgChart, 'function');
assert.equal(typeof module.JsonOrgChartDataSource, 'function');
const fixture = JSON.parse(await readFile(new URL('data-v1.json', import.meta.url), 'utf8'));
assert.equal(module.validateOrgChartDataset(fixture).valid, true);
assert.equal(module.calculateSummary(fixture).raw.total, 4);
for (const file of ['index.js','orgchart.browser.js']) {
const bundle = await readFile(path.join(packageRoot, 'dist', file), 'utf8');
assert.ok(!/\b(?:import|export)\s+[^;\n]*?from\s*['"](?!\.)/.test(bundle));
assert.ok(!/\bimport\s*\(\s*['"](?!\.)/.test(bundle));
assert.ok(!/from\s*['"]react(?:-dom)?['"]/.test(bundle));
}
await writeFile(path.join(consumer, 'consumer.ts'), `import { createOrgChart, JsonOrgChartDataSource, type OrgChartDataset, type OrgChartDataSource } from '@hm-design/org-chart';
declare const data: OrgChartDataset;
const source: OrgChartDataSource = new JsonOrgChartDataSource(data);
const handle = createOrgChart({dataSource:source,stylesheetUrls:{engine:'engine.css',core:'style.css'}});
const stop = handle.subscribe(state=>{if(state.status==='ready') console.log(state.summary.raw.total);});
stop(); handle.destroy();\n`);
await writeFile(path.join(consumer, 'tsconfig.json'), JSON.stringify({compilerOptions:{target:'ES2022',module:'NodeNext',moduleResolution:'NodeNext',lib:['ES2022','DOM'],strict:true,noEmit:true,types:[],skipLibCheck:false},include:['consumer.ts']}));
const require = createRequire(import.meta.url);
const tscManifest = require.resolve('typescript/package.json');
const typescript = JSON.parse(await readFile(tscManifest, 'utf8'));
execFileSync(process.execPath, [path.resolve(path.dirname(tscManifest), typescript.bin.tsc), '-p', path.join(consumer,'tsconfig.json')], {stdio:'inherit'});
const report = {status:'PASS',checks:['Tarball public ESM import without DOM','Public CSS/browser entries exist','Strict NodeNext public declarations without React types','No unresolved bare imports in browser/ESM bundles','Tarball validated fixture and summary','No React peer/runtime dependency'],consumer};
await writeFile(path.join(consumer,'report.json'), JSON.stringify(report,null,2));
console.log(JSON.stringify(report,null,2));