Skip to content

Commit

Permalink
chore: replace build script (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanai10a authored Mar 26, 2024
1 parent ab064a1 commit 9f079c5
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 26 deletions.
70 changes: 70 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import esbuild from 'esbuild';
import * as glob from 'glob';
import ignore from 'ignore';
import kleur from 'kleur';
import fs from 'node:fs/promises';

const ctrl = {
error: (msg) => {
console.log(`${kleur.bold(`${kleur.red('Error')}:`)} ${msg}`);
process.exit(1);
},

built: (msg) => {
console.log(`${kleur.bold(`${kleur.green('Built')}:`)} ${msg}`);
},

fail: (msg) => {
console.log(`${kleur.bold(`${kleur.red('Fail ')}:`)} ${msg}`);
},
};

const { include, exclude } = await fs
.readFile('tsconfig.json')
.then((b) => JSON.parse(b.toString()))
.then((o) => {
if (typeof o !== 'object') {
ctrl.error("root of tsconfig.json isn't object");
}

if (
!o.include ||
!Array.isArray(o.include) ||
!o.include.every((e) => typeof e === 'string')
) {
ctrl.error(".include of tsconfig.json isn't satisfy `string[]");
}

if (
!o.exclude ||
!Array.isArray(o.exclude) ||
!o.exclude.every((e) => typeof e === 'string')
) {
ctrl.error(".exclude of tsconfig.json isn't satisfy `string[]`");
}

return {
include: o.include,
exclude: o.exclude,
};
});

const ignorer = ignore().add(exclude);

glob.stream(include).on('data', async (p) => {
if (ignorer.test(p).ignored) {
return;
}

const r = await esbuild
.build({
entryPoints: [p],
platform: 'node',
outbase: '.',
outdir: 'build',
sourcemap: 'external',
})
.catch(() => ctrl.fail(p));

!r || ctrl.built(p);
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"node": ">=20.11.0"
},
"scripts": {
"build": "tsc -p .",
"build": "node build.js",
"check": "tsc -p .",
"build:prisma": "prisma generate",
"clean": "rm -r ./build",
"start": "node ./build/main.js",
Expand Down Expand Up @@ -48,10 +49,14 @@
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"@vitest/coverage-v8": "^1.2.2",
"esbuild": "^0.20.2",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-vitest": "^0.3.21",
"globals": "^15.0.0",
"glob": "^10.3.10",
"ignore": "^5.3.1",
"kleur": "^4.1.5",
"lefthook": "^1.6.1",
"prettier": "^3.2.5",
"vitest": "^1.2.2"
Expand Down
Loading

0 comments on commit 9f079c5

Please sign in to comment.