Skip to content

Commit

Permalink
export to jsr
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed May 4, 2024
1 parent 0d3081d commit 3e48ea3
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 25 deletions.
78 changes: 53 additions & 25 deletions generate-exports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,64 @@
require 'json'
require 'pp'

here = Pathname.new(__dir__)
@here = Pathname.new(__dir__)

src_dir = here / 'src'
def build_exports
src_dir = @here / 'src'

tsx_files = src_dir
.find
.select{|fn| fn.file? and fn.to_s =~ /\.tsx?$/i }
.reject{|fn| fn.to_s =~ /\.spec\./ }
ts_files = src_dir
.find
.select{|fn| fn.file? and fn.to_s =~ /\.tsx?$/i }
.reject{|fn| fn.to_s =~ /\.spec\./ }

exports = Hash[
tsx_files
.sort
.map do |e|
relative_path = e.relative_path_from(src_dir)
relative_path_wo_ext = relative_path.sub(/\.[tj]sx?$/i, '').to_s
js_ext = if relative_path.fnmatch? '*.tsx' then 'jsx' else 'js' end
Hash[
ts_files
.sort
.map do |e|
relative_path = e.relative_path_from(src_dir)
relative_path_wo_ext = relative_path.sub(/\.[tj]sx?$/i, '').to_s
orig_ext = relative_path.extname.slice(1, 1e9)
js_ext = if relative_path.fnmatch? '*.tsx' then 'jsx' else 'js' end

[
"./lib/#{relative_path_wo_ext}",
{
import: "./lib/__esm/#{relative_path_wo_ext}.#{js_ext}",
require: "./lib/#{relative_path_wo_ext}.#{js_ext}",
type: "./lib/#{relative_path_wo_ext}.d.ts"
}
import_entry, import_spec = yield relative_path_wo_ext, js_ext, orig_ext
[import_entry, import_spec]
end
]
end

def rewrite_json json_path
v = JSON.parse(json_path.read, symbolize_names: true)
json_path.write JSON.pretty_generate(yield v)
end

def rewrite_pkg_json
pkg_json = @here / 'package.json'
exports = build_exports do |relative_path_wo_ext, js_ext|
["./lib/#{relative_path_wo_ext}",
{
import: "./lib/__esm/#{relative_path_wo_ext}.#{js_ext}",
require: "./lib/#{relative_path_wo_ext}.#{js_ext}",
type: "./lib/#{relative_path_wo_ext}.d.ts"
}
]
end
]

pkg_json = here / 'package.json'
rewrite_json pkg_json do |v|
{**v, exports: exports}
end
end

v = JSON.parse(pkg_json.read, symbolize_names: true)
def rewrite_jsr_json
jsr_json = @here / 'jsr.json'
exports = build_exports do |relative_path_wo_ext, js_ext, orig_ext|
[
"./#{relative_path_wo_ext}",
"./src/#{relative_path_wo_ext}.#{orig_ext}",
]
end
rewrite_json jsr_json do |v|
{**v, exports: exports}
end
end

pkg_json.write JSON.pretty_generate(**v, exports: exports)
rewrite_pkg_json
rewrite_jsr_json
71 changes: 71 additions & 0 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@ihate-work/ts-commonutil",
"version": "0.1.0",
"exports": {
"./algebra/monoid": "./src/algebra/monoid.ts",
"./algebra/topological-sort": "./src/algebra/topological-sort.ts",
"./algebra/total-ordered": "./src/algebra/total-ordered.ts",
"./binary-conversion/array-buffer": "./src/binary-conversion/array-buffer.ts",
"./binary-conversion/string": "./src/binary-conversion/string.ts",
"./binary-conversion/web": "./src/binary-conversion/web.ts",
"./collection/btree": "./src/collection/btree.ts",
"./collection/default-map": "./src/collection/default-map.ts",
"./collection/iterables": "./src/collection/iterables.ts",
"./collection/maps": "./src/collection/maps.ts",
"./collection/min-heap": "./src/collection/min-heap.ts",
"./collection/multiset": "./src/collection/multiset.ts",
"./collection/segment-tree": "./src/collection/segment-tree.ts",
"./collection/sets": "./src/collection/sets.ts",
"./concurrency/async-throttle": "./src/concurrency/async-throttle.ts",
"./concurrency/deferred": "./src/concurrency/deferred.ts",
"./concurrency/lazy-thenable": "./src/concurrency/lazy-thenable.ts",
"./concurrency/lease": "./src/concurrency/lease.ts",
"./concurrency/lockable": "./src/concurrency/lockable.ts",
"./concurrency/promise-container": "./src/concurrency/promise-container.ts",
"./concurrency/resource-pool": "./src/concurrency/resource-pool.ts",
"./concurrency/tic-toc": "./src/concurrency/tic-toc.ts",
"./concurrency/timing": "./src/concurrency/timing.ts",
"./event-emitter/typed-event-emitter": "./src/event-emitter/typed-event-emitter.ts",
"./fp-ts/monoid": "./src/fp-ts/monoid.ts",
"./frontend/dummy-img": "./src/frontend/dummy-img.ts",
"./frontend/fps-observable": "./src/frontend/fps-observable.ts",
"./frontend/measure-img": "./src/frontend/measure-img.ts",
"./frontend/observe-dom-mutation": "./src/frontend/observe-dom-mutation.ts",
"./index": "./src/index.ts",
"./logging/console-logger": "./src/logging/console-logger.ts",
"./logging/get-debug-namespace": "./src/logging/get-debug-namespace.ts",
"./logging/loglevel-logger": "./src/logging/loglevel-logger.ts",
"./logging/winston-logger": "./src/logging/winston-logger.ts",
"./node/fsp": "./src/node/fsp.ts",
"./node/index": "./src/node/index.ts",
"./node/subprocess": "./src/node/subprocess.ts",
"./react/component/aspect-ratio/aspect-ratio": "./src/react/component/aspect-ratio/aspect-ratio.ts",
"./react/component/font-awesome": "./src/react/component/font-awesome.tsx",
"./react/component/inspect-render-count": "./src/react/component/inspect-render-count.tsx",
"./react/component/pre-json": "./src/react/component/pre-json.tsx",
"./react/hook/use-async-effect": "./src/react/hook/use-async-effect.ts",
"./react/hook/use-concurrency-control": "./src/react/hook/use-concurrency-control.ts",
"./react/hook/use-depending-state": "./src/react/hook/use-depending-state.ts",
"./react/hook/use-fps": "./src/react/hook/use-fps.ts",
"./react/hook/use-in-server": "./src/react/hook/use-in-server.ts",
"./react/hook/use-life-cycle": "./src/react/hook/use-life-cycle.ts",
"./react/hook/use-mounted": "./src/react/hook/use-mounted.ts",
"./react/hook/use-observable-store": "./src/react/hook/use-observable-store.ts",
"./react/hook/use-prev": "./src/react/hook/use-prev.ts",
"./react/hook/use-promised": "./src/react/hook/use-promised.ts",
"./react/hook/use-versioned-memo": "./src/react/hook/use-versioned-memo.ts",
"./react/mobx/use-derived": "./src/react/mobx/use-derived.ts",
"./react/util/prop-of": "./src/react/util/prop-of.ts",
"./rxjs/distributions": "./src/rxjs/distributions.ts",
"./rxjs/stochastic-processes": "./src/rxjs/stochastic-processes.ts",
"./text/chunk-to-lines": "./src/text/chunk-to-lines.ts",
"./text/random-string": "./src/text/random-string.ts",
"./type/freeze": "./src/type/freeze.ts",
"./type/index": "./src/type/index.ts",
"./type/omit": "./src/type/omit.ts",
"./util/lru": "./src/util/lru.ts",
"./util/proxys": "./src/util/proxys.ts",
"./util/randoms": "./src/util/randoms.ts",
"./util/with-retry": "./src/util/with-retry.ts"
}
}

0 comments on commit 3e48ea3

Please sign in to comment.