Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hashmap.json file moved to src folder #11

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ jobs:
release:
name: Release
runs-on: ubuntu-latest
permissions:
actions: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ lib/

# Generated files
blurhash_encoder
**/blurhash_encoder
**/blurhash_encoder
src/hashmap.json
Binary file removed src/.DS_Store
Binary file not shown.
27 changes: 11 additions & 16 deletions src/blur-hash-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ const DEFAULT_COMPONENT_RATIO: DefaultComponentRatio = { x: 4, y: 3 };
const C_ROOT = 'src/C';
const EXEC_INITIAL = 'src/C/blurhash_encoder';
const EXEC_MAIN = 'blurhash_encoder';

const HASHMAP_JSON_FILE_NAME = './hashmap.json';
export interface DefaultComponentRatio {
x: ComponentRange;
y: ComponentRange;
}

export interface BlurHashMapConfig {
assetsRoot: string;
hashMapJsonPath: string;
imageExtensions?: AllowedImageTypeList;
components?: { x: ComponentRange; y: ComponentRange };
}
Expand All @@ -55,20 +54,19 @@ export class BlurHashMap {
execMain: resolve(EXEC_MAIN),
execInitial: resolve(EXEC_INITIAL),
cRoot: resolve(C_ROOT),
hashMapJsonPath: resolve(config.hashMapJsonPath),
components: config.components || DEFAULT_COMPONENT_RATIO,
makeCmd: 'make blurhash_encoder',
imageExtensions: config.imageExtensions || ALLOWED_IMAGE_TYPES,
};
}

async init(): Promise<void> {
get hashMapJsonPath(): string {
return resolve(__dirname, HASHMAP_JSON_FILE_NAME);
}
async init(): Promise<string> {
const imageFiles = this.getAllImageFiles();
this.createExecutableIfNotFound();
if (!this.checkAllowedFileExtensions()) {
console.log('***');
return;
}
this.checkAllowedFileExtensions();
imageFiles.forEach(imageFilePath => {
this.generateOrDelete(imageFilePath, true);
});
Expand Down Expand Up @@ -104,7 +102,7 @@ export class BlurHashMap {
return file.replace(this.config.assetsRoot, '');
}

async createJson(): Promise<void> {
async createJson(): Promise<string> {
return this.cleanUp()
.then(() => this.getAllImageFiles())
.then(imageFiles => {
Expand All @@ -124,13 +122,10 @@ export class BlurHashMap {
);
return json;
})
.then(json => {
return writeFileSync(
this.config.hashMapJsonPath,
JSON.stringify(json),
'utf-8'
);
});
.then(json =>
writeFileSync(this.hashMapJsonPath, JSON.stringify(json), 'utf-8')
)
.then(() => this.hashMapJsonPath);
}

private checkAllowedFileExtensions() {
Expand Down
12 changes: 4 additions & 8 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import path from 'path';
import { AllowedImageTypes } from '../src/blur-hash-map';
const config: BlurHashMapConfig = {
assetsRoot: 'test/fixtures/assets/',
hashMapJsonPath: 'test/fixtures/lib/hashmap.json',
imageExtensions: ['jpg', 'jpeg', 'png'],
};

Expand All @@ -25,7 +24,6 @@ const cleanUpRestOver = () => {
try {
fs.unlinkSync(blurHashMap.config.execInitial);
fs.unlinkSync(blurHashMap.config.execMain);
fs.unlinkSync(blurHashMap.config.hashMapJsonPath);
} catch (e) {
//
}
Expand All @@ -49,15 +47,13 @@ describe('index', () => {
});

it('should create hashmap and json', async () => {
await blurHashMap.init();
const hashMapJsonPath = await blurHashMap.init();

expect(fs.existsSync(blurHashMap.config.execMain)).toBe(true);
expect(fs.existsSync(blurHashMap.config.execInitial)).toBe(false);
expect(
JSON.parse(
fs.readFileSync(blurHashMap.config.hashMapJsonPath).toString()
)
).toEqual(hashmapJSON);
expect(JSON.parse(fs.readFileSync(hashMapJsonPath).toString())).toEqual(
hashmapJSON
);
});

it('should throw if not allowed file extension configured', async () => {
Expand Down
Loading