-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathindex.mjs
More file actions
1 lines (1 loc) · 2.8 KB
/
index.mjs
File metadata and controls
1 lines (1 loc) · 2.8 KB
1
import e from"node:url";import n from"node:fs/promises";import t from"node:path";import o from"node:os";import{platform as i}from"node:process";import{spawn as a}from"node:child_process";const r="package";async function testPack(a){if(i.startsWith("win"))return void console.log("Skipping test on Windows");if(!("resolve"in import.meta))return void console.log("Skipping test on platform without `import.meta.resolve` support");const r=await n.mkdtemp(t.join(o.tmpdir(),"csstools-pack-test-"));try{const n=new URL(import.meta.resolve(a));console.log(`Testing module: ${a}`);const o=e.fileURLToPath(n),i=await findPackageJsonFromDir(t.dirname(o)),s=t.dirname(i),c=await pack(s,r),p=await unpack(c,r),d=await getPackageInfo(t.join(p,"package.json"));await eraseDevDependenciesInfo(t.join(p,"package.json")),await createRootPackage(r,d),await runNPMInstall(r),await runTest(r)}finally{await n.rm(r,{recursive:!0})}}async function findPackageJsonFromDir(e,o=10){const i=t.join(e,"package.json");try{return await n.access(i),i}catch{if("/"===e||o<=0)throw new Error("Could not find package.json")}return findPackageJsonFromDir(t.dirname(e),o-1)}async function pack(e,o){const r=await n.mkdir(t.join(o,"pack"),{recursive:!0}),s=a("npm",["pack","--pack-destination",r],{cwd:e,shell:"win32"===i}),c=await new Promise((e,n)=>{let t="",o="";s.stdout.on("data",e=>{t+=e.toString()}),s.stderr.on("data",e=>{o+=e.toString()}),s.on("close",i=>{0===i?e(t.trim()):(console.error(o),n(new Error(`npm pack exited with code ${i}`)))})});return t.join(r,c)}async function unpack(e,o){const i=t.join(o,r);await n.mkdir(i,{recursive:!0});const s=a("tar",["-xf",e,"-C",r,"--strip-components","1"],{cwd:o});return await new Promise((e,n)=>{s.on("close",t=>{0===t?e():n(new Error(`tar exited with code ${t}`))})}),i}async function eraseDevDependenciesInfo(e){const t=JSON.parse(await n.readFile(e,"utf8"));delete t.devDependencies,await n.writeFile(e,JSON.stringify(t,null,"\t"))}async function getPackageInfo(e){return JSON.parse(await n.readFile(e,"utf8"))}async function createRootPackage(e,o){await n.writeFile(t.join(e,"package.json"),JSON.stringify({name:"@csstools/pack-test--root",private:!0,type:"module",version:"1.0.0",description:"",workspaces:[r],dependencies:o.peerDependencies??{},scripts:{test:"node --test"}},null,"\t")),await n.writeFile(t.join(e,"index.mjs"),`import '${o.name}';`)}async function runNPMInstall(e){const n=a("npm",["install","--ignore-scripts","--omit","dev","--engine-strict","false"],{cwd:e,stdio:"inherit",shell:"win32"===i});await new Promise((e,t)=>{n.on("close",n=>{0===n?e():t(new Error(`npm install exited with code ${n}`))})})}async function runTest(e){const n=a("node",["index.mjs"],{cwd:e,stdio:"inherit",shell:"win32"===i});await new Promise((e,t)=>{n.on("close",n=>{0===n?e():t(new Error(`npm install exited with code ${n}`))})})}export{testPack};