Skip to content

Commit

Permalink
feat: read config from .blurhash-maprc.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
softberry committed Apr 3, 2024
1 parent 6054771 commit b4a9204
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 182 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ dist

# Compiled code
lib/
bin/

# Generated files
blurhash_encoder
**/blurhash_encoder
src/hashmap.json
test/**/*.hash
hashmap.json
**/hashmap.json
test/**/*.hash
.blurhash-maprc.js
100 changes: 100 additions & 0 deletions bin/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env node

const nodemon = require('nodemon');
import { program } from 'commander';
import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
import { BlurHashMap, BlurHashMapConfig } from '../src';
import {
ComponentRange,
HashMapJsonSaveToTarget,
zBlurHashMapConfig,
} from '../src/blur-hash-map';

const RC_CONFIG = resolve('.blurhash-maprc.js');

interface CommanderActionOptions {
assets: string;
extensions: string;
componentX?: ComponentRange;
componentY?: ComponentRange;
targetJson?: HashMapJsonSaveToTarget;
}
const packageJson: Record<string, string> = JSON.parse(
readFileSync('./package.json', 'utf-8')
);

const watchChanges = async (options: BlurHashMapConfig): Promise<void> => {
const blurHashGenerator = new BlurHashMap(options);
return blurHashGenerator.init().then(() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
nodemon({
watch: options.assetsRoot,
ext: [...(options.imageExtensions || []), 'hash'].join(''),
})
.on('quit', function () {
throw 'nodemon exited';
})
.on('restart', function (changedFiles = []) {
for (const file of changedFiles) {
blurHashGenerator.generateOrDelete(file);
}
});
});
};

const readConfig = (): Promise<BlurHashMapConfig> => {
return new Promise((res, rej) => {
if (existsSync(RC_CONFIG)) {
void import(RC_CONFIG).then((config: Record<string, unknown>) => {
if ('default' in config) {
res(zBlurHashMapConfig.parse(config.default));
} else {
rej('`blurhash-maprc.js` must have default export');
}
});
} else {
rej();
}
});
};

const readCommandParams = () => {
program
.version(packageJson.version)
.option('-a, --assets <assets>', 'Required. Set path to your assets folder')
.option(
'-e, --extensions <extensions>',
'Optional. Define image file extensions. Default: jpg,jpeg,png,bmp,webp'
)
.option('-x, --componentX <componentX>', 'Optional. Default : 4')
.option('-y, --componentY <componentY>', 'Optional. Default : 3')
.option('-t, --target <targetJson>', 'Optional. Default : project root')
.action((options: CommanderActionOptions) => {
if (typeof options.assets !== 'string') {
throw Error(
"Please define assets paths to be watched with the '-a' key"
);
}
if (typeof options.assets !== 'string') {
throw Error(
"Please define assets paths to be watched with the '-a' key"
);
}

const config: BlurHashMapConfig = {
assetsRoot: options.assets,
imageExtensions: BlurHashMap.toAllowedImageTypeList(options.extensions),
targetJson: options.targetJson,
components: { x: options.componentX, y: options.componentY },
};
void watchChanges(config);
});
program.parse(process.argv);
};

readConfig()
.then(watchChanges)
.catch(() => {
readCommandParams();
});
54 changes: 0 additions & 54 deletions cli/cli.ts

This file was deleted.

20 changes: 18 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
"name": "blurhash-map",
"version": "1.0.0",
"description": "A template for creating npm packages using TypeScript and VSCode",
"main": "./lib/index.js",
"main": "./lib/src/index.js",
"bin": {
"blurhash-map": "bin/cli.js"
"blurhash-map": "./lib/bin/cli.js"
},
"files": [
"lib/**/*",
"bin/**/*"
"lib/**/*"
],
"scripts": {
"build": "tsc --project tsconfig.build.json",
"cli": "tsc --project tsconfig.cli.json",
"build": "npm run clean && tsc --project tsconfig.build.json && npm run copy:c",
"clean": "rm -rf ./lib/",
"cm": "cz",
"copy:c": "cp -r src/C lib/src/C",
"lint": "eslint ./src/ --fix",
"prepare": "husky install",
"semantic-release": "semantic-release",
Expand Down Expand Up @@ -72,7 +71,8 @@
"semantic-release": "^21.0.1",
"ts-jest": "^29.1.2",
"ts-node": "^10.2.1",
"typescript": "^4.2.4"
"typescript": "^4.2.4",
"zod": "^3.22.4"
},
"config": {
"commitizen": {
Expand Down
Loading

0 comments on commit b4a9204

Please sign in to comment.