Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tsmpkg

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsmpkg - npm Package Compare versions

Comparing version 0.1.5 to 0.1.6

8

dist/bin.js
#!/usr/bin/env node --enable-source-maps
import{parseArgs as ut}from"node:util";import U from"node:fs/promises";import F from"node:path";import{ensureDir as z,pathExists as H}from"fs-extra/esm";var b=async t=>{let o=F.join(t,"./src");await z(o);let r=F.join(o,"index.ts");await H(r)||await U.writeFile(r,`export const hi = "hi";
`)};var x=t=>k(t).includes("cjs"),k=t=>{let o=t.tsup?.format??"esm";return(Array.isArray(o)?o:[o]).filter(r=>r!=="iife")},p=(t="commonjs")=>{let r=(typeof t=="string"?t:t.type??"commonjs")==="module";return e=>{switch(e){case"cjs":return r?".cjs":".js";case"esm":return r?".js":".mjs"}}},O=t=>k(t).map(p(t)),$=t=>!!t.manifest.devDependencies?.tsmpkg;var f=(t,o)=>`./dist/${t}${o}`,D=(t,{formats:o,pkgType:r})=>{let e=p(r);return Object.fromEntries(Object.entries(t).map(([n])=>[n==="index"?".":`./${n}`,o.length===1?f(n,e(o[0])):{import:f(n,e("esm")),require:f(n,e("cjs"))}]))};import C from"chalk";var d=(t,o)=>{if(o.length===0)return o;console.warn(C.red(`Errors were found in ${t}.`)),console.info(C.dim("Try running fix to resolve them."));for(let r of o)console.warn(C.red(r));return o};import K from"@npmcli/package-json";var v=async t=>await K.load(t),u=async t=>{let{content:o}=await v(t);return o};import Q from"node:path";import X from"node:fs/promises";import{load as Y}from"tsconfig";import{parseTSConfigJSON as Z}from"types-tsconfig";var _=async t=>{try{let o=await Y(t);return o||void 0}catch(o){throw new g(o instanceof Error?o:void 0)}},J=async t=>{let o=await _(t);if(!o)return;let r=Z(o.config);if(!r)throw new g;return r},A=async(t,o)=>{let r=Q.join(t,"tsconfig.json");return X.writeFile(r,JSON.stringify(o,void 0," "),"utf-8")},g=class t extends Error{constructor(o){super(`Invalid tsconfig.json. ${o?.message??""}`.trimEnd()),t.prototype.name="InvalidTsConfigError"}};import{findWorkspaceDir as tt}from"@pnpm/find-workspace-dir";import ot from"chalk";var w=async t=>{let o=await tt(t);return o||(console.error(ot.red`🚨Workspace dir could not be found from ${t}.\n'dev' only needed in monorepos.\nOnly pnpm monorepos currently supported.`),process.exit(1)),o};var rt={compilerOptions:{target:"ES2022",module:"ESNext",rootDir:"./src",moduleResolution:"nodenext",noEmit:!0,isolatedModules:!0,esModuleInterop:!0,forceConsistentCasingInFileNames:!0,strict:!0,skipLibCheck:!0,lib:["es2020"]},include:["./src"]},q=async t=>{await A(t,rt)};import N from"chalk";var h=async t=>{let o=await v(t),r=o.content,e=r.tsup?.entry??{index:"./src/index.ts"},n=k(r),s=x(r);if(!(typeof e=="object"&&!Array.isArray(e)))throw new Error("Entry points in tsup config must be an object.");console.info(N.dim`🛠️Fixing package.json`);let m=p(r);o.update({scripts:{...r.scripts,clean:"rm -rf dist",prepack:"tsup"},main:e.index?s?f("index",m("cjs")):f("index",m("esm")):void 0,exports:D(e,{formats:n,pkgType:r.type??"commonjs"}),tsup:{...r.tsup,entry:e,clean:!0,format:n,dts:!0}}),await o.save(),console.info(N.dim`✅ Done.`)};var R=async t=>{await h(t)};var S=async t=>{await b(t),await h(t),await q(t)};import l from"node:path";import T from"node:fs/promises";import{ensureDir as at,remove as ct}from"fs-extra/esm";import{findWorkspacePackagesNoCheck as pt}from"@pnpm/find-workspace-packages";import j from"node:path";var W=async t=>{let o=await u(t),r=[...et(o)];return d("package.json",r)};function*et(t){let o=x(t),r=p(t),e=r("cjs");if(t.main?o&&j.extname(t.main)!=""&&j.extname(t.main)!==e&&(yield`"main" field should have "${e}" extension when commonjs supported on package of type ${t.type??"commonjs"}.`):t.tsup?.entry&&"index"in t.tsup.entry&&(yield"`main` field must be provided when index entry point."),t.exports){let n=r("esm"),s=r("cjs");for(let[m,i]of Object.entries(t.exports)){if(!(i&&typeof i=="object"))continue;let a="import"in i&&i.import&&typeof i.import=="string"&&j.extname(i.import)!==n,c="require"in i&&i.require&&typeof i.require=="string"&&j.extname(i.require)!==s;(a||c)&&(yield`"exports" entry "${m}": should have "require" extension "${s}" and "import" extension "${n}" on packages of type "${t.type??"commonjs"}".`)}}t.files&&!t.files.find(n=>["dist","./dist","dist/"].includes(n))&&(yield"`files` must include `dist`")}var V=t=>d("tsconfig.json",t),B=async t=>{try{let o=await J(t),r=[...nt(o)];return V(r)}catch(o){if(o instanceof g)return V([o.message]);throw o}};function*nt(t){if(!t){yield"tsconfig.json: not found.";return}let{compilerOptions:o}=t;if(!o){yield"no compilerOptions set.";return}}var I=async t=>{let o=await u(t),r=[...st(o.tsup)];return d("tsup config",r)};function*st(t){t||(yield"Not found. Currently only `tsup` section within `package.json` supported."),t?.entry?Array.isArray(t?.entry)&&(yield"Array `entry` not yet supported."):yield"No `entry` defined."}import M from"chalk";import it from"node:path";var E=async(t,o)=>(console.log(M.dim`🔎 Validating %s`,M.bgBlackBright(it.relative(o.workspaceDir,t))),[...await W(t),...await B(t),...await I(t)].length===0),G=async t=>{await E(t,{workspaceDir:await w(t)})||process.exit(1)};import P from"chalk";var mt={index:"./src/index.ts"},ft=t=>{if(!t?.entry)return mt;if(Array.isArray(t.entry))throw new Error("Array entrypoints not yet supported. use object syntax");return t.entry},L=async t=>{let o=await w(t),r=(await pt(o)).filter(e=>e.dir!==o&&$(e));for(let e of r)await dt(e.dir,{workspaceDir:o});console.info(P.dim`✅ Done.`)},dt=async(t,o)=>{await E(t,o)||process.exit(1);let e=await u(t),n=O(e),s=l.join(t,"dist");await ct(s),await at(s),console.info(P.dim`🛠️Generating dev mode symlinks for %s in %s`,P.bgBlackBright(e.name),P.bgGray(l.relative(o.workspaceDir,s)));let m=ft(e.tsup),i=async(a,c,y)=>{await T.symlink(l.join(t,c),l.join(s,`${a}${y}`)),y===".js"&&(await T.writeFile(l.join(s,`${a}.d.ts`),`export * from ".${c.replace(".ts",".js")}";
//# sourceMappingURL=${a}.d.ts.map`,"utf-8"),await T.writeFile(l.join(s,`${a}.d.ts.map`),`{"version":3,"file":"${a}.d.ts","sourceRoot":"","sources":[".${c}"],"names":[],"mappings":"AAAA"}
`))};for(let[a,c]of Object.entries(m)){if(!c.startsWith("./"))throw new Error("Entry points must start with ./");for(let y of n)await i(a,c,y)}};import gt from"chalk";var lt=ut({tokens:!0,allowPositionals:!0});switch(lt.positionals[0]){case"init":{await S(process.cwd());break}case"dev":{await L(process.cwd());break}case"fix":{await R(process.cwd());break}case"check":{await G(process.cwd());break}default:{console.info("Usage: "+gt.bgGray`tsmpkg init|dev|fix|check`);break}}
import{parseArgs as lt}from"node:util";import H from"node:fs/promises";import b from"node:path";import{ensureDir as K,pathExists as Q}from"fs-extra/esm";var $=async t=>{let o=b.join(t,"./src");await K(o);let r=b.join(o,"index.ts");await Q(r)||await H.writeFile(r,`export const hi = "hi";
`)};var k=t=>w(t).includes("cjs"),w=t=>{let o=t.tsup?.format??"esm";return(Array.isArray(o)?o:[o]).filter(r=>r!=="iife")},p=(t="commonjs")=>{let r=(typeof t=="string"?t:t.type??"commonjs")==="module";return e=>{switch(e){case"cjs":return r?".cjs":".js";case"esm":return r?".js":".mjs"}}},O=t=>w(t).map(p(t)),D=t=>{switch(t){case".js":return".d.ts";case".mjs":return".d.mts";case".cjs":return".d.cts"}},J=t=>!!t.manifest.devDependencies?.tsmpkg;var f=(t,o)=>`./dist/${t}${o}`,A=(t,{formats:o,pkgType:r})=>{let e=p(r);return Object.fromEntries(Object.entries(t).map(([n])=>[n==="index"?".":`./${n}`,o.length===1?f(n,e(o[0])):{import:f(n,e("esm")),require:f(n,e("cjs"))}]))};import E from"chalk";var d=(t,o)=>{if(o.length===0)return o;console.warn(E.red(`Errors were found in ${t}.`)),console.info(E.dim("Try running fix to resolve them."));for(let r of o)console.warn(E.red(r));return o};import X from"@npmcli/package-json";var C=async t=>await X.load(t),u=async t=>{let{content:o}=await C(t);return o};import Y from"node:path";import Z from"node:fs/promises";import{load as _}from"tsconfig";import{parseTSConfigJSON as tt}from"types-tsconfig";var ot=async t=>{try{let o=await _(t);return o||void 0}catch(o){throw new g(o instanceof Error?o:void 0)}},q=async t=>{let o=await ot(t);if(!o)return;let r=tt(o.config);if(!r)throw new g;return r},N=async(t,o)=>{let r=Y.join(t,"tsconfig.json");return Z.writeFile(r,JSON.stringify(o,void 0," "),"utf-8")},g=class t extends Error{constructor(o){super(`Invalid tsconfig.json. ${o?.message??""}`.trimEnd()),t.prototype.name="InvalidTsConfigError"}};import{findWorkspaceDir as rt}from"@pnpm/find-workspace-dir";import et from"chalk";var j=async t=>{let o=await rt(t);return o||(console.error(et.red`🚨Workspace dir could not be found from ${t}.\n'dev' only needed in monorepos.\nOnly pnpm monorepos currently supported.`),process.exit(1)),o};var nt={compilerOptions:{target:"ES2022",module:"ESNext",rootDir:"./src",moduleResolution:"nodenext",noEmit:!0,isolatedModules:!0,esModuleInterop:!0,forceConsistentCasingInFileNames:!0,strict:!0,skipLibCheck:!0,lib:["es2020"]},include:["./src"]},R=async t=>{await N(t,nt)};import S from"chalk";var h=async t=>{let o=await C(t),r=o.content,e=r.tsup?.entry??{index:"./src/index.ts"},n=w(r),s=k(r);if(!(typeof e=="object"&&!Array.isArray(e)))throw new Error("Entry points in tsup config must be an object.");console.info(S.dim`🛠️Fixing package.json`);let m=p(r);o.update({scripts:{...r.scripts,clean:"rm -rf dist",prepack:"tsup"},main:e.index?s?f("index",m("cjs")):f("index",m("esm")):void 0,exports:A(e,{formats:n,pkgType:r.type??"commonjs"}),tsup:{...r.tsup,entry:e,clean:!0,format:n,dts:!0}}),await o.save(),console.info(S.dim`✅ Done.`)};var W=async t=>{await h(t)};var V=async t=>{await $(t),await h(t),await R(t)};import l from"node:path";import F from"node:fs/promises";import{ensureDir as pt,remove as mt}from"fs-extra/esm";import{findWorkspacePackagesNoCheck as ft}from"@pnpm/find-workspace-packages";import v from"node:path";var B=async t=>{let o=await u(t),r=[...st(o)];return d("package.json",r)};function*st(t){let o=k(t),r=p(t),e=r("cjs");if(t.main?o&&v.extname(t.main)!==e&&(yield`"main" field should have "${e}" extension when commonjs supported on package of type ${t.type??"commonjs"}.`):t.tsup?.entry&&"index"in t.tsup.entry&&(yield"`main` field must be provided when index entry point."),t.exports){let n=r("esm"),s=r("cjs");for(let[m,i]of Object.entries(t.exports)){if(!(i&&typeof i=="object"))continue;let a="import"in i&&i.import&&typeof i.import=="string"&&v.extname(i.import)!==n,c="require"in i&&i.require&&typeof i.require=="string"&&v.extname(i.require)!==s;(a||c)&&(yield`"exports" entry "${m}": should have "require" extension "${s}" and "import" extension "${n}" on packages of type "${t.type??"commonjs"}".`)}}t.files&&!t.files.find(n=>["dist","./dist","dist/"].includes(n))&&(yield"`files` must include `dist`")}var I=t=>d("tsconfig.json",t),M=async t=>{try{let o=await q(t),r=[...it(o)];return I(r)}catch(o){if(o instanceof g)return I([o.message]);throw o}};function*it(t){if(!t){yield"tsconfig.json: not found.";return}let{compilerOptions:o}=t;if(!o){yield"no compilerOptions set.";return}}var G=async t=>{let o=await u(t),r=[...at(o.tsup)];return d("tsup config",r)};function*at(t){t||(yield"Not found. Currently only `tsup` section within `package.json` supported."),t?.entry?Array.isArray(t?.entry)&&(yield"Array `entry` not yet supported."):yield"No `entry` defined."}import L from"chalk";import ct from"node:path";var T=async(t,o)=>(console.log(L.dim`🔎 Validating %s`,L.bgBlackBright(ct.relative(o.workspaceDir,t))),[...await B(t),...await M(t),...await G(t)].length===0),U=async t=>{await T(t,{workspaceDir:await j(t)})||process.exit(1)};import P from"chalk";var dt={index:"./src/index.ts"},ut=t=>{if(!t?.entry)return dt;if(Array.isArray(t.entry))throw new Error("Array entrypoints not yet supported. use object syntax");return t.entry},z=async t=>{let o=await j(t),r=(await ft(o)).filter(e=>e.dir!==o&&J(e));for(let e of r)await gt(e.dir,{workspaceDir:o});console.info(P.dim`✅ Done.`)},gt=async(t,o)=>{await T(t,o)||process.exit(1);let e=await u(t),n=O(e),s=l.join(t,"dist");await mt(s),await pt(s),console.info(P.dim`🛠️Generating dev mode symlinks for %s in %s`,P.bgBlackBright(e.name),P.bgGray(l.relative(o.workspaceDir,s)));let m=ut(e.tsup),i=async(a,c,y)=>{await F.symlink(l.join(t,c),l.join(s,`${a}${y}`));let x=D(y);await F.writeFile(l.join(s,`${a}${x}`),`export * from ".${c.replace(".ts",".js")}";
//# sourceMappingURL=${a}${x}.map`,"utf-8"),await F.writeFile(l.join(s,`${a}${x}.map`),`{"version":3,"file":"${a}${x}","sourceRoot":"","sources":[".${c}"],"names":[],"mappings":"AAAA"}
`)};for(let[a,c]of Object.entries(m)){if(!c.startsWith("./"))throw new Error("Entry points must start with ./");for(let y of n)await i(a,c,y)}};import yt from"chalk";var xt=lt({tokens:!0,allowPositionals:!0});switch(xt.positionals[0]){case"init":{await V(process.cwd());break}case"dev":{await z(process.cwd());break}case"fix":{await W(process.cwd());break}case"check":{await U(process.cwd());break}default:{console.info("Usage: "+yt.bgGray`tsmpkg init|dev|fix|check`);break}}
{
"name": "tsmpkg",
"version": "0.1.5",
"version": "0.1.6",
"description": "Tool for setting up and maintaining pure esm module typescript packages",

@@ -5,0 +5,0 @@ "type": "module",

@@ -43,2 +43,5 @@ import { beforeEach, describe, expect, it, test } from "vitest";

["dist/index.mjs", "module", ".cjs"],
["dist/index", "module", ".cjs"],
["dist/", "module", ".cjs"],
["dist", "module", ".cjs"],
])(

@@ -45,0 +48,0 @@ "main pointing to %s on module of type %s should have file extension %s",

@@ -26,7 +26,3 @@ import {

}
} else if (
cjsSupported &&
path.extname(pkg.main) != "" &&
path.extname(pkg.main) !== extForCjs
) {
} else if (cjsSupported && path.extname(pkg.main) !== extForCjs) {
yield `"main" field should have "${extForCjs}" extension when commonjs supported on package of type ${

@@ -33,0 +29,0 @@ pkg.type ?? "commonjs"

@@ -7,2 +7,3 @@ import path from "node:path";

import {
dtsExtensionFromExtension,
getPackageJsonContent,

@@ -70,16 +71,15 @@ getWorkspaceDir,

if (ext === ".js") {
await fs.writeFile(
path.join(distPath, `${name}.d.ts`),
`export * from ".${target.replace(
".ts",
".js",
)}";\n//# sourceMappingURL=${name}.d.ts.map`,
"utf-8",
);
await fs.writeFile(
path.join(distPath, `${name}.d.ts.map`),
`{"version":3,"file":"${name}.d.ts","sourceRoot":"","sources":[".${target}"],"names":[],"mappings":"AAAA"}\n`,
);
}
const dtsExt = dtsExtensionFromExtension(ext);
await fs.writeFile(
path.join(distPath, `${name}${dtsExt}`),
`export * from ".${target.replace(
".ts",
".js",
)}";\n//# sourceMappingURL=${name}${dtsExt}.map`,
"utf-8",
);
await fs.writeFile(
path.join(distPath, `${name}${dtsExt}.map`),
`{"version":3,"file":"${name}${dtsExt}","sourceRoot":"","sources":[".${target}"],"names":[],"mappings":"AAAA"}\n`,
);
};

@@ -86,0 +86,0 @@

@@ -32,2 +32,13 @@ import { Extension, Format, PackageContent, PkgType } from "./types.js";

export const dtsExtensionFromExtension = (ext: Extension) => {
switch (ext) {
case ".js":
return ".d.ts";
case ".mjs":
return ".d.mts";
case ".cjs":
return ".d.cts";
}
};
// TODO: need some better criteria but don't want to exclude private packages

@@ -34,0 +45,0 @@ // but some may have it as a workspace package dep rather than dev dep on each package

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc