Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
mkobayashime committed Jan 15, 2025
1 parent 016395b commit 9a232ec
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 274 deletions.
2 changes: 1 addition & 1 deletion commitlint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default {
extends: ["@commitlint/config-conventional"],
extends: ["@commitlint/config-conventional"],
};
84 changes: 42 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
{
"name": "bookmarklets-cli",
"version": "1.1.1",
"author": "Masaki Kobayashi",
"license": "MIT",
"private": false,
"repository": {
"type": "git",
"url": "https://github.com/mkobayashime/bookmarklets-cli.git"
},
"bin": "dist/index.mjs",
"type": "module",
"dependencies": {
"arg": "5.0.2",
"chalk": "5.4.0",
"chokidar": "3.6.0",
"clipboardy": "4.0.0",
"esbuild": "0.24.2",
"glob": "11.0.0",
"terser": "5.37.0"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@commitlint/cli": "19.6.1",
"@commitlint/config-conventional": "19.6.0",
"@mkobayashime/shared-config": "1.0.0",
"@rollup/plugin-typescript": "12.1.2",
"@swc/core": "1.10.1",
"@types/glob": "8.1.0",
"@types/node": "22.10.2",
"depcheck": "1.4.7",
"lefthook": "1.9.2",
"rollup": "4.29.0",
"ts-node": "^10.9.1",
"tslib": "2.8.1",
"typescript": "5.7.2",
"vitest": "2.1.8"
},
"scripts": {
"prepare": "lefthook install",
"prepublishOnly": "make build"
},
"keywords": ["bookmarklets", "bookmarklet"]
"name": "bookmarklets-cli",
"version": "1.1.1",
"author": "Masaki Kobayashi",
"license": "MIT",
"private": false,
"repository": {
"type": "git",
"url": "https://github.com/mkobayashime/bookmarklets-cli.git"
},
"bin": "dist/index.mjs",
"type": "module",
"dependencies": {
"arg": "5.0.2",
"chalk": "5.4.0",
"chokidar": "3.6.0",
"clipboardy": "4.0.0",
"esbuild": "0.24.2",
"glob": "11.0.0",
"terser": "5.37.0"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@commitlint/cli": "19.6.1",
"@commitlint/config-conventional": "19.6.0",
"@mkobayashime/shared-config": "1.0.0",
"@rollup/plugin-typescript": "12.1.2",
"@swc/core": "1.10.1",
"@types/glob": "8.1.0",
"@types/node": "22.10.2",
"depcheck": "1.4.7",
"lefthook": "1.9.2",
"rollup": "4.29.0",
"ts-node": "^10.9.1",
"tslib": "2.8.1",
"typescript": "5.7.2",
"vitest": "2.1.8"
},
"scripts": {
"prepare": "lefthook install",
"prepublishOnly": "make build"
},
"keywords": ["bookmarklets", "bookmarklet"]
}
2 changes: 1 addition & 1 deletion renovate.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": ["github>mkobayashime/renovate-config"]
"extends": ["github>mkobayashime/renovate-config"]
}
34 changes: 17 additions & 17 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import typescript from "@rollup/plugin-typescript";
import type { RollupOptions } from "rollup";

const config: RollupOptions = {
input: "src/index.ts",
output: {
dir: "dist",
entryFileNames: "[name].mjs",
},
plugins: [typescript()],
external: [
"arg",
"chalk",
"chokidar",
"clipboardy",
"esbuild",
"fs/promises",
"glob",
"path",
"terser",
],
input: "src/index.ts",
output: {
dir: "dist",
entryFileNames: "[name].mjs",
},
plugins: [typescript()],
external: [
"arg",
"chalk",
"chokidar",
"clipboardy",
"esbuild",
"fs/promises",
"glob",
"path",
"terser",
],
};

export default config;
34 changes: 17 additions & 17 deletions src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import * as esbuild from "esbuild";
import { minify } from "terser";

export const compile = async (filename: string) => {
const esbuildOutput = await esbuild.build({
entryPoints: [filename],
bundle: true,
minify: true,
format: "esm",
write: false,
});
const esbuildOutput = await esbuild.build({
entryPoints: [filename],
bundle: true,
minify: true,
format: "esm",
write: false,
});

if (!esbuildOutput.outputFiles[0]) {
throw new Error("esbuild outputFiles is empty");
}
const code = esbuildOutput.outputFiles[0].text;
if (!esbuildOutput.outputFiles[0]) {
throw new Error("esbuild outputFiles is empty");
}
const code = esbuildOutput.outputFiles[0].text;

const prod = `javascript:(()=>{${encodeURIComponent((await minify(code)).code ?? "")}})()`;
const dev = prod.slice(1);
const prod = `javascript:(()=>{${encodeURIComponent((await minify(code)).code ?? "")}})()`;
const dev = prod.slice(1);

return {
prod,
dev,
};
return {
prod,
dev,
};
};
196 changes: 98 additions & 98 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,81 +11,81 @@ import { glob } from "glob";
import { compile } from "./compile";

type BuildProps = {
inputsGlob: string;
distDir: string;
inputsGlob: string;
distDir: string;
};

const dev = ({ inputsGlob, distDir }: BuildProps) => {
try {
const watcher = chokidar.watch(inputsGlob).on("change", (filename) => {
(async () => {
try {
const { dev, prod } = await compile(filename);

console.log(chalk.green(`\nCompiled ${path.basename(filename)}`));

console.log(prod);
clipboard.writeSync(dev);

await writeFile(
path.resolve(
distDir,
path.basename(filename).replace(/.ts$/, ".js"),
),
prod,
);
} catch (err) {
console.error(err);
console.log("");
}
})().catch((err) => {
console.error(err);
});
});

watcher.on("ready", () =>
console.log(chalk.green("\nDev mode started: watching for file changes")),
);
} catch (err) {
console.error(err);
}
try {
const watcher = chokidar.watch(inputsGlob).on("change", (filename) => {
(async () => {
try {
const { dev, prod } = await compile(filename);

console.log(chalk.green(`\nCompiled ${path.basename(filename)}`));

console.log(prod);
clipboard.writeSync(dev);

await writeFile(
path.resolve(
distDir,
path.basename(filename).replace(/.ts$/, ".js"),
),
prod,
);
} catch (err) {
console.error(err);
console.log("");
}
})().catch((err) => {
console.error(err);
});
});

watcher.on("ready", () =>
console.log(chalk.green("\nDev mode started: watching for file changes")),
);
} catch (err) {
console.error(err);
}
};

const build = async ({ inputsGlob, distDir }: BuildProps) => {
try {
const files = await glob(inputsGlob);
for (const filepath of files) {
try {
const { prod } = await compile(filepath);
await writeFile(
path.resolve(distDir, path.basename(filepath).replace(/.ts$/, ".js")),
prod,
);
console.log(`Compiled ${path.basename(filepath)}`);
} catch (err) {
console.error(err);
console.log("");
}
}
} catch (err) {
console.error(err);
}
try {
const files = await glob(inputsGlob);
for (const filepath of files) {
try {
const { prod } = await compile(filepath);
await writeFile(
path.resolve(distDir, path.basename(filepath).replace(/.ts$/, ".js")),
prod,
);
console.log(`Compiled ${path.basename(filepath)}`);
} catch (err) {
console.error(err);
console.log("");
}
}
} catch (err) {
console.error(err);
}
};

//
(async () => {
const args = arg({
"--watch": Boolean,
"-W": Boolean,
"--dist-dir": String,
"-D": String,
"--help": Boolean,
"-H": Boolean,
});

if (args["--help"] ?? args["-H"]) {
console.log(
`
const args = arg({
"--watch": Boolean,
"-W": Boolean,
"--dist-dir": String,
"-D": String,
"--help": Boolean,
"-H": Boolean,
});

if (args["--help"] ?? args["-H"]) {
console.log(
`
bookmarklets-cli
https://www.npmjs.com/package/bookmarklets-cli
Expand All @@ -100,38 +100,38 @@ EXAMPLE
npx bookmarklets-cli --dist-dir 'out'
npx bookmarklets-cli --watch 'src/*.ts'
`.trim(),
);

process.exit(0);
}

const watch = args["--watch"] ?? args["-W"];

const dist = args["--dist-dir"] ?? args["-D"] ?? "dist";
await mkdir(dist, { recursive: true });

if (args._.length === 0) {
console.error(chalk.red("Fatal: Input files not passed"));
process.exit(1);
}
if (args._.length > 1) {
console.warn(
chalk.yellow(
"Caution:\nbookmarklets-cli currently doesn't support multiple input arguments.\nPass one glob expression instead.",
),
);
}

const buildProps = {
inputsGlob: args._[0],
distDir: dist,
};

if (watch) {
dev(buildProps);
} else {
await build(buildProps);
}
);

process.exit(0);
}

const watch = args["--watch"] ?? args["-W"];

const dist = args["--dist-dir"] ?? args["-D"] ?? "dist";
await mkdir(dist, { recursive: true });

if (args._.length === 0) {
console.error(chalk.red("Fatal: Input files not passed"));
process.exit(1);
}
if (args._.length > 1) {
console.warn(
chalk.yellow(
"Caution:\nbookmarklets-cli currently doesn't support multiple input arguments.\nPass one glob expression instead.",
),
);
}

const buildProps = {
inputsGlob: args._[0],
distDir: dist,
};

if (watch) {
dev(buildProps);
} else {
await build(buildProps);
}
})().catch((err) => {
console.error(err);
console.error(err);
});
Loading

0 comments on commit 9a232ec

Please sign in to comment.