Skip to content

Commit

Permalink
Modernize the dev env
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp committed Sep 10, 2024
1 parent 0a3e2a3 commit 058efcc
Show file tree
Hide file tree
Showing 14 changed files with 2,929 additions and 847 deletions.
47 changes: 27 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
test:
@env TZ=Asia/Kolkata npx vitest run
@env TZ=Asia/Kolkata pnpm exec vitest run

test-watch:
@env TZ=Asia/Kolkata npx vitest
@env TZ=Asia/Kolkata pnpm exec vitest

test-types: install-attw build
@cd lib && attw --pack
types:
@pnpm exec tsc --noEmit

.PHONY: build
build:
@rm -rf lib
@env npx babel src --source-root src --out-dir lib --extensions .mjs --out-file-extension .js --ignore "src/**/test.ts" --quiet
@rsync --archive --prune-empty-dirs --exclude 'test.ts' --relative src/./ lib
@make build-mts
@cp package.json lib
@cp *.md lib
types-watch:
@pnpm exec tsc --noEmit --watch

test-types: build
@pnpm exec attw --pack lib

build-mts:
build: prepare-build
@pnpm exec tsc -p tsconfig.lib.json
@env BABEL_ENV=esm pnpm exec babel src --config-file ./babel.config.json --source-root src --out-dir lib --extensions .js,.ts --out-file-extension .js --quiet
@env BABEL_ENV=cjs pnpm exec babel src --config-file ./babel.config.json --source-root src --out-dir lib --extensions .js,.ts --out-file-extension .cjs --quiet
@node copy.mjs
# @rm -rf lib/types.*js
@make build-cts

build-cts:
@find lib -name '*.d.ts' | while read file; do \
new_file=$${file%.d.ts}.d.mts; \
new_file=$${file%.d.ts}.d.cts; \
cp $$file $$new_file; \
done

prepare-build:
@rm -rf lib
@mkdir -p lib

publish: build
@cd lib && npm publish --access public
cd lib && npm publish --access public

publish-next: build
@cd lib && npm publish --access public --tag next
cd lib && npm publish --access public --tag next

install-attw:
@if ! command -v attw >/dev/null 2>&1; then \
npm i -g @arethetypeswrong/cli; \
fi
link:
@cd lib && npm link
53 changes: 53 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"presets": ["@babel/preset-typescript"],

"env": {
"cjs": {
"presets": [
[
"@babel/preset-env",
{
"targets": { "node": "current" },
"modules": "commonjs",
"loose": true
}
]
],

"plugins": [
[
"@babel/plugin-transform-modules-commonjs",
{ "strict": true, "noInterop": true }
],
[
"babel-plugin-replace-import-extension",
{ "extMapping": { ".js": ".cjs", ".ts": ".cjs" } }
]
]
},

"esm": {
"presets": [
[
"@babel/preset-env",
{ "targets": { "node": "current" }, "modules": false }
]
],

"plugins": [
[
"babel-plugin-replace-import-extension",
{ "extMapping": { ".ts": ".js" } }
]
]
}
},

"ignore": [
"src/**/*.d.ts",
"src/**/tests.ts",
"src/tests/**/*",
"src/**/tysts.ts",
"src/tysts/**/*"
]
}
55 changes: 55 additions & 0 deletions copy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import watcher from "@parcel/watcher";
import { copyFile, mkdir } from "fs/promises";
import { glob } from "glob";
import { minimatch } from "minimatch";
import { dirname, join, relative } from "path";

const watch = !!process.argv.find((arg) => arg === "--watch");

const srcRegExp = /^src\//;
const patterns = ["src/**/*.d.ts", "package.json", "*.md"];

if (watch) {
const debouncedCopy = debounceByArgs(copy, 100);

watcher.subscribe(process.cwd(), (error, events) => {
if (error) {
console.error("The filesystem watcher encountered an error:");
console.error(error);
process.exit(1);
}

events.forEach((event) => {
if (event.type !== "create" && event.type !== "update") return;
const path = relative(process.cwd(), event.path);
if (!patterns.some((pattern) => minimatch(path, pattern))) return;
debouncedCopy(path);
});
});
} else {
glob(patterns).then((paths) => Promise.all(paths.map(copy)));
}

async function copy(path) {
const libPath = srcRegExp.test(path)
? path.replace(/^src/, "lib")
: join("lib", path);
const dir = dirname(libPath);
await mkdir(dir, { recursive: true });
await copyFile(path, libPath);
console.log(`Copied ${path} to ${libPath}`);
}

export function debounceByArgs(func, waitFor) {
const timeouts = {};

return (...args) => {
const argsKey = JSON.stringify(args);
const later = () => {
delete timeouts[argsKey];
func(...args);
};
clearTimeout(timeouts[argsKey]);
timeouts[argsKey] = setTimeout(later, waitFor);
};
}
50 changes: 27 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,38 @@
"test": "env TZ=Asia/Kolkata vitest",
"build": "make build"
},
"sideEffects": false,
"browser": "./index",
"main": "./index.js",
"module": "./index.mjs",
"type": "module",
"main": "index.js",
"module": "index.js",
"exports": {
"./package.json": "./package.json",
".": {
"require": {
"types": "./index.d.ts",
"default": "./index.js"
"types": "./index.d.cts",
"default": "./index.cjs"
},
"import": {
"types": "./index.d.mts",
"default": "./index.mjs"
"types": "./index.d.ts",
"default": "./index.js"
}
},
"./date": {
"require": {
"types": "./date/index.d.ts",
"default": "./date/index.js"
"types": "./date/index.d.cts",
"default": "./date/index.cjs"
},
"import": {
"types": "./date/index.d.mts",
"default": "./date/index.mjs"
"default": "./date/index.js"
}
},
"./date/mini": {
"require": {
"types": "./date/mini.d.ts",
"default": "./date/mini.js"
"types": "./date/mini.d.cts",
"default": "./date/mini.cjs"
},
"import": {
"types": "./date/mini.d.mts",
"default": "./date/mini.mjs"
"types": "./date/mini.d.ts",
"default": "./date/mini.js"
}
}
},
Expand All @@ -60,14 +58,20 @@
},
"homepage": "https://github.com/date-fns/utc#readme",
"devDependencies": {
"@babel/cli": "^7.18.6",
"@babel/core": "^7.18.6",
"@babel/plugin-transform-modules-commonjs": "^7.18.6",
"@sinonjs/fake-timers": "^11.2.2",
"@arethetypeswrong/cli": "^0.16.2",
"@babel/cli": "^7.24.1",
"@babel/core": "^7.24.4",
"@babel/plugin-transform-modules-commonjs": "^7.24.1",
"@babel/preset-env": "^7.24.4",
"@babel/preset-typescript": "^7.24.1",
"@parcel/watcher": "^2.4.1",
"@sinonjs/fake-timers": "^13.0.1",
"@types/sinonjs__fake-timers": "^8.1.5",
"babel-plugin-add-import-extension": "^1.6.0",
"typescript": "^5.3.3",
"vitest": "^1.3.1"
"babel-plugin-replace-import-extension": "^1.1.4",
"glob": "^11.0.0",
"minimatch": "^10.0.1",
"typescript": "^5.6.2",
"vitest": "^2.0.5"
},
"packageManager": "[email protected]+sha512.73a29afa36a0d092ece5271de5177ecbf8318d454ecd701343131b8ebc0c1a91c487da46ab77c8e596d6acf1461e3594ced4becedf8921b074fbd8653ed7051c"
}
Loading

0 comments on commit 058efcc

Please sign in to comment.