Skip to content

Commit

Permalink
feat: 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
simonkovtyk committed Sep 9, 2024
1 parent 825053b commit c28b4c2
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 1 deletion.
39 changes: 39 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Deploy
on:
push:
branches:
- release

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install
uses: pnpm/action-setup@v4
with:
version: latest
run_install: true

- name: Build
run: tsc

- name: Assets
run: |
cp package.json dist/package.json
cp README.md dist/README.md
- name: Config
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
cd dist
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
echo "registry=https://registry.npmjs.org/" >> ~/.npmrc
- name: Publish
run: |
cd dist
pnpm publish --access public --no-git-checks
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.idea
package-lock.json
pnpm-lock.yaml
dist
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# esbuild-plugin-copy
# esbuild-plugin-copy-it
The plugin copies assets to the esbuild out folder, ensuring that all required files are included alongside the built code
61 changes: 61 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "esbuild-plugin-file-copy",
"description": "The plugin copies assets to the esbuild out folder, ensuring that all required files are included alongside the built code.",
"keywords": [
"frontend",
"backend",
"packages",
"esbuild",
"build",
"esbuild plugin",
"esbuild tool",
"build plugin",
"build tool",
"package.json",
"developer tools",
"build process",
"plugin",
"esbuild dependency",
"dependency management",
"optimize package json",
"validate package json",
"esbuild copy package json",
"esbuild copy",
"package json plugin",
"package config plugin"
],
"license": "MIT",
"version": "1.0.1",
"bugs": "https://github.com/simonkovtyk/esbuild-plugin-file-copy/issues",
"homepage": "https://github.com/simonkovtyk/esbuild-plugin-file-copy",
"repository": {
"type": "git",
"url": "git+https://github.com/simonkovtyk/esbuild-plugin-file-copy.git"
},
"exports": {
".": {
"default": "./public-api.js",
"types": "./public-api.d.ts"
}
},
"author": {
"name": "Simon Kovtyk",
"url": "https://github.com/simonkovtyk",
"email": "privat@kovtyk.com"
},
"maintainers": [
{
"name": "Simon Kovtyk",
"url": "https://github.com/simonkovtyk",
"email": "privat@kovtyk.com"
}
],
"dependencies": {
"esbuild": ">=0.20.0",
"fast-glob": "^3.3.2"
},
"devDependencies": {
"typescript": "^5.5.4",
"@types/node": "^22.5.4"
}
}
32 changes: 32 additions & 0 deletions src/core/helpers/out.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from "node:path";
import process from "node:process";
import { ResolvePathOptions } from "../types/options.type";

// Prefer out dir before out file
const resolveOutDir = (options: ResolvePathOptions): string => {
const explicitOutBase: string | undefined = options.overrideOutBase ?? options.outBase;

const outBase = explicitOutBase
? path.join(process.cwd(), explicitOutBase)
: process.cwd();

const explicitOutDir: string | undefined = options.overrideOutDir ?? options.outDir;

if (explicitOutDir !== undefined) {
return path.join(outBase, explicitOutDir);
}

const explicitOutFile: string | undefined = options.overrideOutFile ?? options.outFile;

if (explicitOutFile !== undefined) {
const dirOfOutFile = path.parse(explicitOutFile).dir;

return path.join(outBase, dirOfOutFile);
}

return path.join(outBase, "dist")
}

export {
resolveOutDir
}
82 changes: 82 additions & 0 deletions src/core/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import path from "node:path";
import { resolveOutDir } from "./helpers/out.helper";
import { Input, Lifecycle, Options, ResolvePathOptions } from "./types/options.type";
import { PluginBuild, Plugin } from "esbuild";
import fastGlob from "fast-glob"
import fs from "node:fs";

const handler = (inputs: string[] | Input[], options: ResolvePathOptions) => {
return async () => {
const resolvedOutDir: string = resolveOutDir(options);

inputs.forEach((input: string | Input): void => {
if (typeof input === "string") {
const globs: string[] = fastGlob.sync(input);

if (globs.length === 0)
return;

if (! fs.existsSync(resolvedOutDir))
fs.mkdirSync(resolvedOutDir, { recursive: true });

globs.forEach((glob: string): void => {
const fileName: string = path.parse(glob).base;

fs.copyFileSync(glob, `${ resolvedOutDir }/${ fileName }`);
});

return;
}

const globs: string[] = fastGlob.sync(input.glob);

if (globs.length === 0)
return;

globs.forEach((glob: string): void => {
const fileName: string = path.parse(glob).base;

const outDir: string = input.output ?? resolvedOutDir;

if (! fs.existsSync(outDir))
fs.mkdirSync(outDir, { recursive: true });

fs.copyFileSync(glob, `${ outDir }/${ fileName }`);
})
});
}
}

const copyPlugin = (options: Options): Plugin => ({
name: "esbuild-plugin-package-json",
setup: (build: PluginBuild) => {
const lifecycle: Lifecycle = options.lifecycle ?? "onEnd";

const resolvePathOptions: ResolvePathOptions = {
outBase: build.initialOptions.outbase,
outDir: build.initialOptions.outdir,
outFile: build.initialOptions.outfile,
overrideOutBase: options.overrideOutBase,
overrideOutDir: options.overrideOutDir,
overrideOutFile: options.overrideOutFile
}

const handlerRef = handler(options.inputs, resolvePathOptions);

switch (lifecycle) {
case "onStart":
build.onStart(handlerRef);
break;
case "onEnd":
build.onEnd(handlerRef);
break;
case "onDispose":
build.onDispose(handlerRef);
break;
}
}
})

export {
copyPlugin
}
30 changes: 30 additions & 0 deletions src/core/types/options.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
type Lifecycle = "onStart" | "onEnd" | "onDispose";

type Input = {
glob: string,
output?: string | undefined
}

type PathOverrides = {
overrideOutBase?: string | undefined,
overrideOutDir?: string | undefined,
overrideOutFile?: string | undefined
}

type Options = {
lifecycle?: Lifecycle | undefined,
inputs: string[] | Input[]
} & PathOverrides

type ResolvePathOptions = {
outDir?: string | undefined,
outFile?: string | undefined,
outBase?: string | undefined
} & PathOverrides

export type {
ResolvePathOptions,
Lifecycle,
Options,
Input
}
2 changes: 2 additions & 0 deletions src/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./core/plugin";
export * from "./core/types/options.type";
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"outDir": "dist",
"module": "NodeNext",
"target": "ESNext",
"moduleResolution": "NodeNext",
"allowSyntheticDefaultImports": true,
"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"noImplicitOverride": true,
"noUncheckedIndexedAccess": true,
"declaration": true,
"alwaysStrict": true,
"isolatedModules": true
}
}

0 comments on commit c28b4c2

Please sign in to comment.